1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.template.php;
13 import net.sourceforge.phpdt.core.ICompilationUnit;
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.templates.Template;
18 import org.eclipse.jface.text.templates.TemplateBuffer;
19 import org.eclipse.jface.text.templates.TemplateContextType;
20 import org.eclipse.jface.text.templates.TemplateException;
21 import org.eclipse.jface.text.templates.TemplateTranslator;
24 * A context for javadoc.
26 public class JavaDocContext extends CompilationUnitContext {
29 private static final char HTML_TAG_BEGIN = '<';
31 private static final char HTML_TAG_END = '>';
33 private static final char JAVADOC_TAG_BEGIN = '@';
36 * Creates a javadoc template context.
42 * @param completionOffset
43 * the completion offset within the document.
44 * @param completionLength
45 * the completion length within the document.
46 * @param compilationUnit
47 * the compilation unit (may be <code>null</code>).
49 public JavaDocContext(TemplateContextType type, IDocument document,
50 int completionOffset, int completionLength,
51 ICompilationUnit compilationUnit) {
52 super(type, document, completionOffset, completionLength,
57 * @see TemplateContext#canEvaluate(Template templates)
59 public boolean canEvaluate(Template template) {
60 String key = getKey();
65 return template.matches(key, getContextType().getId())
66 && (key.length() != 0)
67 && template.getName().toLowerCase().startsWith(
72 * @see DocumentTemplateContext#getStart()
74 public int getStart() {
76 IDocument document = getDocument();
78 if (getCompletionLength() == 0) {
79 int start = getCompletionOffset();
82 && (document.getChar(start - 1) == HTML_TAG_END))
86 && Character.isUnicodeIdentifierPart(document
91 && Character.isUnicodeIdentifierStart(document
95 // include html and javadoc tags
97 && ((document.getChar(start - 1) == HTML_TAG_BEGIN) || (document
98 .getChar(start - 1) == JAVADOC_TAG_BEGIN))) {
106 int start = getCompletionOffset();
107 int end = getCompletionOffset() + getCompletionLength();
110 && Character.isUnicodeIdentifierPart(document
111 .getChar(start - 1)))
115 && Character.isWhitespace(document.getChar(start)))
119 start = getCompletionOffset();
124 } catch (BadLocationException e) {
125 return getCompletionOffset();
130 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getEnd()
132 public int getEnd() {
134 if (getCompletionLength() == 0)
135 return super.getEnd();
138 IDocument document = getDocument();
140 int start = getCompletionOffset();
141 int end = getCompletionOffset() + getCompletionLength();
144 && Character.isWhitespace(document.getChar(end - 1)))
149 } catch (BadLocationException e) {
150 return super.getEnd();
155 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getKey()
157 public String getKey() {
159 if (getCompletionLength() == 0)
160 return super.getKey();
163 IDocument document = getDocument();
165 int start = getStart();
166 int end = getCompletionOffset();
167 return start <= end ? document.get(start, end - start) : ""; //$NON-NLS-1$
169 } catch (BadLocationException e) {
170 return super.getKey();
175 * @see TemplateContext#evaluate(Template)
177 public TemplateBuffer evaluate(Template template)
178 throws BadLocationException, TemplateException {
179 TemplateTranslator translator = new TemplateTranslator();
180 TemplateBuffer buffer = translator.translate(template);
182 getContextType().resolve(buffer, this);