1 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.TemplateContextType; 21 import org.eclipse.jface.text.templates.Template; 22 import org.eclipse.jface.text.templates.TemplateBuffer; 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 39 public class JavaDocContext extends CompilationUnitContext { 40 41 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 55 public JavaDocContext(TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) { 56 super(type, document, completionOffset, completionLength, compilationUnit); 57 } 58 59 68 public JavaDocContext(TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) { 69 super(type, document, completionPosition, compilationUnit); 70 } 71 72 75 public boolean canEvaluate(Template template) { 76 String key= getKey(); 77 78 if (fForceEvaluation) 79 return true; 80 81 return 82 template.matches(key, getContextType().getId()) && 83 (key.length() != 0) && template.getName().toLowerCase().startsWith(key.toLowerCase()); 84 } 85 86 89 public int getStart() { 90 if (fIsManaged && getCompletionLength() > 0) 91 return super.getStart(); 92 93 try { 94 IDocument document= getDocument(); 95 96 if (getCompletionLength() == 0) { 97 int start= getCompletionOffset(); 98 99 if ((start != 0) && (document.getChar(start - 1) == HTML_TAG_END)) 100 start--; 101 102 while ((start != 0) && Character.isUnicodeIdentifierPart(document.getChar(start - 1))) 103 start--; 104 105 if ((start != 0) && Character.isUnicodeIdentifierStart(document.getChar(start - 1))) 106 start--; 107 108 if ((start != 0) && ( 110 (document.getChar(start - 1) == HTML_TAG_BEGIN) || 111 (document.getChar(start - 1) == JAVADOC_TAG_BEGIN))) 112 { 113 start--; 114 } 115 116 return start; 117 118 } 119 120 int start= getCompletionOffset(); 121 int end= getCompletionOffset() + getCompletionLength(); 122 123 while (start != 0 && Character.isUnicodeIdentifierPart(document.getChar(start - 1))) 124 start--; 125 126 while (start != end && Character.isWhitespace(document.getChar(start))) 127 start++; 128 129 if (start == end) 130 start= getCompletionOffset(); 131 132 return start; 133 134 135 } catch (BadLocationException e) { 136 return getCompletionOffset(); 137 } 138 } 139 140 143 public int getEnd() { 144 145 if (fIsManaged || getCompletionLength() == 0) 146 return super.getEnd(); 147 148 try { 149 IDocument document= getDocument(); 150 151 int start= getCompletionOffset(); 152 int end= getCompletionOffset() + getCompletionLength(); 153 154 while (start != end && Character.isWhitespace(document.getChar(end - 1))) 155 end--; 156 157 return end; 158 159 } catch (BadLocationException e) { 160 return super.getEnd(); 161 } 162 } 163 164 167 public String getKey() { 168 169 if (getCompletionLength() == 0) 170 return super.getKey(); 171 172 try { 173 IDocument document= getDocument(); 174 175 int start= getStart(); 176 int end= getCompletionOffset(); 177 return start <= end 178 ? document.get(start, end - start) 179 : ""; 181 } catch (BadLocationException e) { 182 return super.getKey(); 183 } 184 } 185 186 189 public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { 190 TemplateTranslator translator= new TemplateTranslator(); 191 TemplateBuffer buffer= translator.translate(template); 192 193 getContextType().resolve(buffer, this); 194 195 IPreferenceStore prefs= JavaPlugin.getDefault().getPreferenceStore(); 196 boolean useCodeFormatter= prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER); 197 198 IJavaProject project= getJavaProject(); 199 JavaFormatter formatter= new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project); 200 formatter.format(buffer, this); 201 202 return buffer; 203 } 204 205 210 private int getIndentation() { 211 int start= getStart(); 212 IDocument document= getDocument(); 213 try { 214 IRegion region= document.getLineInformationOfOffset(start); 215 String lineContent= document.get(region.getOffset(), region.getLength()); 216 IJavaProject project= getJavaProject(); 217 return Strings.computeIndentUnits(lineContent, project); 218 } catch (BadLocationException e) { 219 return 0; 220 } 221 } 222 } 223 224 | Popular Tags |