1 package net.sourceforge.phpdt.internal.corext.template.php;
3 import org.eclipse.jface.text.BadLocationException;
4 import org.eclipse.jface.text.IDocument;
5 import org.eclipse.jface.text.templates.TemplateContextType;
6 import org.eclipse.jface.text.templates.Template;
7 import org.eclipse.jface.text.templates.TemplateBuffer;
8 import org.eclipse.jface.text.templates.TemplateException;
9 import org.eclipse.jface.text.templates.TemplateTranslator;
11 import net.sourceforge.phpdt.core.ICompilationUnit;
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 of HTML tags '#' for the start of colour attributes
24 * '{' for the start of smarty partitions inside HTML code
26 private static final String specialChars = "&<#{";
29 * Creates a javadoc template context.
35 * @param completionOffset
36 * the completion offset within the document.
37 * @param completionLength
38 * the completion length within the document.
39 * @param compilationUnit
40 * the compilation unit (may be <code>null</code>).
42 public HTMLUnitContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength,
43 ICompilationUnit compilationUnit) {
44 super(type, document, completionOffset, completionLength, compilationUnit);
48 * @see TemplateContext#canEvaluate(Template templates)
50 public boolean canEvaluate(Template template) {
51 String key = getKey();
56 return template.matches(key, getContextType().getId()) && (key.length() != 0)
57 && template.getName().toLowerCase().startsWith(key.toLowerCase());
61 * @see DocumentTemplateContext#getCompletionPosition();
63 public int getStart() {
64 IDocument document = getDocument();
66 int start = getCompletionOffset();
69 ch = document.getChar(start - 1);
70 if (specialChars.indexOf(ch) != (-1)) {
73 if (Character.isUnicodeIdentifierPart(ch)) {
79 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1))) {
81 if ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1)) {
86 // while (((start != 0)
87 // && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
89 // && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
94 // && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
96 // && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
102 } catch (BadLocationException e) {
103 return getCompletionOffset();
108 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getEnd()
110 public int getEnd() {
112 if (getCompletionLength() == 0)
113 return super.getEnd();
116 IDocument document = getDocument();
118 int start = getCompletionOffset();
119 int end = getCompletionOffset() + getCompletionLength();
121 while (start != end && Character.isWhitespace(document.getChar(end - 1)))
126 } catch (BadLocationException e) {
127 return super.getEnd();
132 * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getKey()
134 public String getKey() {
136 if (getCompletionLength() == 0)
137 return super.getKey();
140 IDocument document = getDocument();
142 int start = getStart();
143 int end = getCompletionOffset();
144 return start <= end ? document.get(start, end - start) : ""; //$NON-NLS-1$
146 } catch (BadLocationException e) {
147 return super.getKey();
152 * @see TemplateContext#evaluate(Template)
154 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
155 TemplateTranslator translator = new TemplateTranslator();
156 TemplateBuffer buffer = translator.translate(template);
158 getContextType().resolve(buffer, this);