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