1 package net.sourceforge.phpdt.internal.corext.template.php;
3 import net.sourceforge.phpdt.core.ICompilationUnit;
5 import org.eclipse.jface.text.BadLocationException;
6 import org.eclipse.jface.text.IDocument;
7 import org.eclipse.jface.text.templates.Template;
8 import org.eclipse.jface.text.templates.TemplateBuffer;
9 import org.eclipse.jface.text.templates.TemplateContextType;
10 import org.eclipse.jface.text.templates.TemplateException;
11 import org.eclipse.jface.text.templates.TemplateTranslator;
14 * A context for javadoc.
16 public class HTMLUnitContext extends CompilationUnitContext {
19 // private static final char HTML_TAG_BEGIN= '<';
20 // private static final char HTML_TAG_END= '>';
21 // private static final char JAVADOC_TAG_BEGIN= '@';
23 * special characters '&' for the start of HTML entities '<' for the start
24 * of HTML tags '#' for the start of colour attributes '{' for the start of
25 * smarty partitions inside HTML code
27 private static final String specialChars = "&<#{";
30 * Creates a javadoc template context.
36 * @param completionOffset
37 * the completion offset within the document.
38 * @param completionLength
39 * the completion length within the document.
40 * @param compilationUnit
41 * the compilation unit (may be <code>null</code>).
43 public HTMLUnitContext(TemplateContextType type, IDocument document,
44 int completionOffset, int completionLength,
45 ICompilationUnit compilationUnit) {
46 super(type, document, completionOffset, completionLength,
51 * @see TemplateContext#canEvaluate(Template templates)
53 public boolean canEvaluate(Template template) {
54 String key = getKey();
59 return template.matches(key, getContextType().getId())
60 && (key.length() != 0)
61 && template.getName().toLowerCase().startsWith(
66 * @see DocumentTemplateContext#getCompletionPosition();
68 public int getStart() {
69 IDocument document = getDocument();
71 int start = getCompletionOffset();
74 ch = document.getChar(start - 1);
75 if (specialChars.indexOf(ch) != (-1)) {
78 if (Character.isUnicodeIdentifierPart(ch)) {
85 && Character.isUnicodeIdentifierStart(document
86 .getChar(start - 1))) {
89 && specialChars.indexOf(document.getChar(start - 1)) != (-1)) {
94 // while (((start != 0)
95 // && Character.isUnicodeIdentifierPart(document.getChar(start -
98 // && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
103 // && Character.isUnicodeIdentifierStart(document.getChar(start -
106 // && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
112 } catch (BadLocationException e) {
113 return getCompletionOffset();
118 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getEnd()
120 public int getEnd() {
122 if (getCompletionLength() == 0)
123 return super.getEnd();
126 IDocument document = getDocument();
128 int start = getCompletionOffset();
129 int end = getCompletionOffset() + getCompletionLength();
132 && Character.isWhitespace(document.getChar(end - 1)))
137 } catch (BadLocationException e) {
138 return super.getEnd();
143 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getKey()
145 public String getKey() {
147 if (getCompletionLength() == 0)
148 return super.getKey();
151 IDocument document = getDocument();
153 int start = getStart();
154 int end = getCompletionOffset();
155 return start <= end ? document.get(start, end - start) : ""; //$NON-NLS-1$
157 } catch (BadLocationException e) {
158 return super.getKey();
163 * @see TemplateContext#evaluate(Template)
165 public TemplateBuffer evaluate(Template template)
166 throws BadLocationException, TemplateException {
167 TemplateTranslator translator = new TemplateTranslator();
168 TemplateBuffer buffer = translator.translate(template);
170 getContextType().resolve(buffer, this);