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