Fixed ?
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / template / php / PHPDocContext.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.template.php;
6
7 import net.sourceforge.phpdt.internal.corext.template.ContextType;
8 import net.sourceforge.phpdt.internal.corext.template.DocumentTemplateContext;
9 import net.sourceforge.phpdt.internal.corext.template.Template;
10 import net.sourceforge.phpdt.internal.corext.template.TemplateBuffer;
11 import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16
17 /**
18  * A context for phpdoc.
19  */
20 public class PHPDocContext extends DocumentTemplateContext  {
21
22         // tags
23         private static final char HTML_TAG_BEGIN= '<';
24         private static final char HTML_TAG_END= '>';
25         private static final char JAVADOC_TAG_BEGIN= '@';       
26
27         /**
28          * Creates a phpdoc template context.
29          * 
30          * @param type   the context type.
31          * @param document the document.
32          * @param completionPosition the completion position within the document.
33          * @param unit the compilation unit (may be <code>null</code>).
34          */
35         public PHPDocContext(ContextType type, IDocument document, int completionOffset, int completionLength)
36         //, int completionOffset, int completionLength, ICompilationUnit compilationUnit)
37         {
38     super(type, document, completionOffset, completionLength);
39 //              super(type, document, completionOffset, completionLength, compilationUnit);
40         }
41
42         /*
43          * @see TemplateContext#canEvaluate(Template templates)
44          */
45         public boolean canEvaluate(Template template) {
46                 String key= getKey();
47                 
48 //              if (fForceEvaluation)
49 //                      return true;
50     return template.matches(getKey(), getContextType().getName());
51 //              return
52 //                      template.matches(key, getContextType().getName()) &&
53 //                      (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase());
54         }
55
56         /*
57          * @see DocumentTemplateContext#getStart()
58          */ 
59         public int getStart() {
60                 try {
61                         IDocument document= getDocument();
62
63                         if (getCompletionLength() == 0) {
64                                 int start= getCompletionOffset();
65                 
66                                 if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END))
67                                         start--;
68                 
69                                 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
70                                         start--;
71                                 
72                                 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
73                                         start--;
74                 
75                                 // include html and phpdoc tags
76                                 if ((start != 0) && (
77                                         (document.getChar(start - 1) == HTML_TAG_BEGIN) ||
78                                         (document.getChar(start - 1) == JAVADOC_TAG_BEGIN)))
79                                 {
80                                         start--;
81                                 }       
82                 
83                                 return start;
84                                 
85                         } else {
86
87                                 int start= getCompletionOffset();
88                                 int end= getCompletionOffset() + getCompletionLength();
89
90                                 while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
91                                         start--;
92                                 
93                                 while (start != end && Character.isWhitespace(document.getChar(start)))
94                                         start++;
95                                 
96                                 if (start == end)
97                                         start= getCompletionOffset();   
98                                 
99                                 return start;                                   
100                         }
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
145                                 ? document.get(start, end - start)
146                                 : ""; //$NON-NLS-1$
147                         
148                 } catch (BadLocationException e) {
149                         return super.getKey();                  
150                 }
151         }
152
153         /*
154          * @see TemplateContext#evaluate(Template)
155          */
156         public TemplateBuffer evaluate(Template template) throws CoreException {
157                 TemplateTranslator translator= new TemplateTranslator();
158                 TemplateBuffer buffer= translator.translate(template.getPattern());
159
160                 getContextType().edit(buffer, this);
161                         
162                 return buffer;
163         }
164
165 }
166