Organized imports
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / HTMLUnitContext.java
1 package net.sourceforge.phpdt.internal.corext.template.php;
2
3 import net.sourceforge.phpdt.core.ICompilationUnit;
4
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;
12
13 /**
14  * A context for javadoc.
15  */
16 public class HTMLUnitContext extends CompilationUnitContext {
17
18   // tags
19   //    private static final char HTML_TAG_BEGIN= '<';
20   //    private static final char HTML_TAG_END= '>';
21   //    private static final char JAVADOC_TAG_BEGIN= '@';
22   /**
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
25    */
26   private static final String specialChars = "&<#{";
27
28   /**
29    * Creates a javadoc template context.
30    * 
31    * @param type
32    *          the context type.
33    * @param document
34    *          the document.
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>).
41    */
42   public HTMLUnitContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength,
43       ICompilationUnit compilationUnit) {
44     super(type, document, completionOffset, completionLength, compilationUnit);
45   }
46
47   /*
48    * @see TemplateContext#canEvaluate(Template templates)
49    */
50   public boolean canEvaluate(Template template) {
51     String key = getKey();
52
53     if (fForceEvaluation)
54       return true;
55
56     return template.matches(key, getContextType().getId()) && (key.length() != 0)
57         && template.getName().toLowerCase().startsWith(key.toLowerCase());
58   }
59
60   /*
61    * @see DocumentTemplateContext#getCompletionPosition();
62    */
63   public int getStart() {
64     IDocument document = getDocument();
65     try {
66       int start = getCompletionOffset();
67       char ch = ' ';
68       while (start != 0) {
69         ch = document.getChar(start - 1);
70         if (specialChars.indexOf(ch) != (-1)) {
71           return --start;
72         }
73         if (Character.isUnicodeIdentifierPart(ch)) {
74           start--;
75         } else {
76           break;
77         }
78       }
79       if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1))) {
80         start--;
81         if ((start != 0) && specialChars.indexOf(document.getChar(start - 1)) != (-1)) {
82           start--;
83         }
84       }
85
86       //      while (((start != 0)
87       //        && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
88       //        || ((start != 0)
89       //          && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
90       //        start--;
91       //      }
92       //
93       //if (((start != 0)
94       //        && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
95       //        || ((start != 0)
96       //                && specialChars.indexOf(document.getChar(start - 1)) != (-1))) {
97       //        start--;
98       //}
99
100       return start;
101
102     } catch (BadLocationException e) {
103       return getCompletionOffset();
104     }
105   }
106
107   /*
108    * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getEnd()
109    */
110   public int getEnd() {
111
112     if (getCompletionLength() == 0)
113       return super.getEnd();
114
115     try {
116       IDocument document = getDocument();
117
118       int start = getCompletionOffset();
119       int end = getCompletionOffset() + getCompletionLength();
120
121       while (start != end && Character.isWhitespace(document.getChar(end - 1)))
122         end--;
123
124       return end;
125
126     } catch (BadLocationException e) {
127       return super.getEnd();
128     }
129   }
130
131   /*
132    * @see net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext#getKey()
133    */
134   public String getKey() {
135
136     if (getCompletionLength() == 0)
137       return super.getKey();
138
139     try {
140       IDocument document = getDocument();
141
142       int start = getStart();
143       int end = getCompletionOffset();
144       return start <= end ? document.get(start, end - start) : ""; //$NON-NLS-1$
145
146     } catch (BadLocationException e) {
147       return super.getKey();
148     }
149   }
150
151   /*
152    * @see TemplateContext#evaluate(Template)
153    */
154   public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
155     TemplateTranslator translator = new TemplateTranslator();
156     TemplateBuffer buffer = translator.translate(template);
157
158     getContextType().resolve(buffer, this);
159
160     return buffer;
161   }
162
163 }
164