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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.template.java;
13 import org.eclipse.jface.preference.IPreferenceStore;
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;
26 import org.eclipse.jdt.core.ICompilationUnit;
27 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.internal.corext.util.Strings;
31 import org.eclipse.jdt.ui.PreferenceConstants;
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 * A context for javadoc.
39 public class JavaDocContext extends CompilationUnitContext {
42 private static final char HTML_TAG_BEGIN= '<';
43 private static final char HTML_TAG_END= '>';
44 private static final char JAVADOC_TAG_BEGIN= '@';
47 * Creates a javadoc template context.
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>).
55 public JavaDocContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) {
56 super(type, document, completionOffset, completionLength, compilationUnit);
60 * Creates a javadoc template context.
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>).
68 public JavaDocContext(TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) {
69 super(type, document, completionPosition, compilationUnit);
73 * @see TemplateContext#canEvaluate(Template templates)
76 public boolean canEvaluate(Template template) {
83 template.matches(key, getContextType().getId()) &&
84 (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase());
88 * @see DocumentTemplateContext#getStart()
91 public int getStart() {
92 if (fIsManaged && getCompletionLength() > 0)
93 return super.getStart();
96 IDocument document= getDocument();
98 if (getCompletionLength() == 0) {
99 int start= getCompletionOffset();
101 if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END))
104 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
107 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1)))
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)))
122 int start= getCompletionOffset();
123 int end= getCompletionOffset() + getCompletionLength();
125 while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1)))
128 while (start != end && Character.isWhitespace(document.getChar(start)))
132 start= getCompletionOffset();
137 } catch (BadLocationException e) {
138 return getCompletionOffset();
143 * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getEnd()
146 public int getEnd() {
148 if (fIsManaged || getCompletionLength() == 0)
149 return super.getEnd();
152 IDocument document= getDocument();
154 int start= getCompletionOffset();
155 int end= getCompletionOffset() + getCompletionLength();
157 while (start != end && Character.isWhitespace(document.getChar(end - 1)))
162 } catch (BadLocationException e) {
163 return super.getEnd();
168 * @see org.eclipse.jdt.internal.corext.template.DocumentTemplateContext#getKey()
171 public String getKey() {
173 if (getCompletionLength() == 0)
174 return super.getKey();
177 IDocument document= getDocument();
179 int start= getStart();
180 int end= getCompletionOffset();
182 ? document.get(start, end - start)
185 } catch (BadLocationException e) {
186 return super.getKey();
191 * @see TemplateContext#evaluate(Template)
194 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
195 TemplateTranslator translator= new TemplateTranslator();
196 TemplateBuffer buffer= translator.translate(template);
198 getContextType().resolve(buffer, this);
200 IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore();
201 boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
203 IJavaProject project= getJavaProject();
204 JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
205 formatter.format(buffer, this);
211 * Returns the indentation level at the position of code completion.
213 * @return the indentation level at the position of the code completion
215 private int getIndentation() {
216 int start= getStart();
217 IDocument document= getDocument();
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) {