]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core extension/org/eclipse/jdt/internal/corext/template/java/JavaDocContext.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core extension / org / eclipse / jdt / internal / corext / template / java / JavaDocContext.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.template.java;
12
13 import org.eclipse.jface.preference.IPreferenceStore;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.Position;
19 import org.eclipse.jface.text.TextUtilities;
20 import org.eclipse.jface.text.templates.Template;
21 import org.eclipse.jface.text.templates.TemplateBuffer;
22 import org.eclipse.jface.text.templates.TemplateContextType;
23 import org.eclipse.jface.text.templates.TemplateException;
24 import org.eclipse.jface.text.templates.TemplateTranslator;
25
26 import org.eclipse.jdt.core.ICompilationUnit;
27 import org.eclipse.jdt.core.IJavaProject;
28
29 import org.eclipse.jdt.internal.corext.util.Strings;
30
31 import org.eclipse.jdt.ui.PreferenceConstants;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34
35
36 /**
37  * A context for javadoc.
38  */
39 public class JavaDocContext extends CompilationUnitContext {
40
41         // tags
42         private static final char HTML_TAG_BEGIN= '<';
43         private static final char HTML_TAG_END= '>';
44         private static final char JAVADOC_TAG_BEGIN= '@';
45
46         /**
47          * Creates a javadoc template context.
48          *
49          * @param type the context type.
50          * @param document the document.
51          * @param completionOffset the completion offset within the document.
52          * @param completionLength the completion length within the document.
53          * @param compilationUnit the compilation unit (may be <code>null</code>).
54          */
55         public JavaDocContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) {
56                 super(type, document, completionOffset, completionLength, compilationUnit);
57         }
58
59         /**
60          * Creates a javadoc template context.
61          *
62          * @param type the context type.
63          * @param document the document.
64          * @param completionPosition the position defining the completion offset and length
65          * @param compilationUnit the compilation unit (may be <code>null</code>).
66          * @since 3.2
67          */
68         public JavaDocContext(TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) {
69                 super(type, document, completionPosition, compilationUnit);
70         }
71
72         /*
73          * @see TemplateContext#canEvaluate(Template templates)
74          */
75         @Override
76         public boolean canEvaluate(Template template) {
77                 String key= getKey();
78
79                 if (fForceEvaluation)
80                         return true;
81
82                 return
83                         template.matches(key, getContextType().getId()) &&
84                         (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase());
85         }
86
87         /*
88          * @see DocumentTemplateContext#getStart()
89          */
90         @Override
91         public int getStart() {
92                 if (fIsManaged && getCompletionLength() > 0)
93                         return super.getStart();
94
95                 try {
96                         IDocument document= getDocument();
97
98                         if (getCompletionLength() == 0) {
99                                 int start= getCompletionOffset();
100
101                                 if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END))
102                                         start--;
103
104                                 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
105                                         start--;
106
107                                 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
108                                         start--;
109
110                                 // include html and javadoc tags
111                                 if ((start != 0) && (
112                                         (document.getChar(start - 1) == HTML_TAG_BEGIN) ||
113                                         (document.getChar(start - 1) == JAVADOC_TAG_BEGIN)))
114                                 {
115                                         start--;
116                                 }
117
118                                 return start;
119
120                         }
121
122                         int start= getCompletionOffset();
123                         int end= getCompletionOffset() + getCompletionLength();
124
125                         while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
126                                 start--;
127
128                         while (start != end && Character.isWhitespace(document.getChar(start)))
129                                 start++;
130
131                         if (start == end)
132                                 start= getCompletionOffset();
133
134                         return start;
135
136
137                 } catch (BadLocationException e) {
138                         return getCompletionOffset();
139                 }
140         }
141
142         /*
143          * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getEnd()
144          */
145         @Override
146         public int getEnd() {
147
148                 if (fIsManaged || getCompletionLength() == 0)
149                         return super.getEnd();
150
151                 try {
152                         IDocument document= getDocument();
153
154                         int start= getCompletionOffset();
155                         int end= getCompletionOffset() + getCompletionLength();
156
157                         while (start != end && Character.isWhitespace(document.getChar(end - 1)))
158                                 end--;
159
160                         return end;
161
162                 } catch (BadLocationException e) {
163                         return super.getEnd();
164                 }
165         }
166
167         /*
168          * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getKey()
169          */
170         @Override
171         public String getKey() {
172
173                 if (getCompletionLength() == 0)
174                         return super.getKey();
175
176                 try {
177                         IDocument document= getDocument();
178
179                         int start= getStart();
180                         int end= getCompletionOffset();
181                         return start <= end
182                                 ? document.get(start, end - start)
183                                 : ""; //$NON-NLS-1$
184
185                 } catch (BadLocationException e) {
186                         return super.getKey();
187                 }
188         }
189
190         /*
191          * @see TemplateContext#evaluate(Template)
192          */
193         @Override
194         public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
195                 TemplateTranslator translator= new TemplateTranslator();
196                 TemplateBuffer buffer= translator.translate(template);
197
198                 getContextType().resolve(buffer, this);
199
200                 IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore();
201                 boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
202
203                 IJavaProject project= getJavaProject();
204                 JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
205                 formatter.format(buffer, this);
206
207                 return buffer;
208         }
209
210         /**
211          * Returns the indentation level at the position of code completion.
212          *
213          * @return the indentation level at the position of the code completion
214          */
215         private int getIndentation() {
216                 int start= getStart();
217                 IDocument document= getDocument();
218                 try {
219                         IRegion region= document.getLineInformationOfOffset(start);
220                         String lineContent= document.get(region.getOffset(), region.getLength());
221                         IJavaProject project= getJavaProject();
222                         return Strings.computeIndentUnits(lineContent, project);
223                 } catch (BadLocationException e) {
224                         return 0;
225                 }
226         }
227 }
228