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