1 11 12 package org.eclipse.jdt.internal.ui.text.java; 13 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.swt.graphics.Image; 18 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.contentassist.IContextInformation; 21 22 import org.eclipse.jdt.core.CompletionProposal; 23 import org.eclipse.jdt.core.ICompilationUnit; 24 import org.eclipse.jdt.core.IJavaProject; 25 import org.eclipse.jdt.core.JavaCore; 26 import org.eclipse.jdt.core.Signature; 27 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 28 29 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext; 30 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 33 34 public class LazyJavaCompletionProposal extends AbstractJavaCompletionProposal { 35 36 protected static final String LPAREN= "("; protected static final String RPAREN= ")"; protected static final String COMMA= ","; protected static final String SPACE= " "; 41 protected static final class FormatterPrefs { 42 43 public final boolean beforeOpeningParen; 44 public final boolean afterOpeningParen; 45 public final boolean beforeComma; 46 public final boolean afterComma; 47 public final boolean beforeClosingParen; 48 public final boolean inEmptyList; 49 50 51 public final boolean beforeOpeningBracket; 52 public final boolean afterOpeningBracket; 53 public final boolean beforeTypeArgumentComma; 54 public final boolean afterTypeArgumentComma; 55 public final boolean beforeClosingBracket; 56 57 FormatterPrefs(IJavaProject project) { 58 beforeOpeningParen= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_PAREN_IN_METHOD_INVOCATION, false); 59 afterOpeningParen= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_OPENING_PAREN_IN_METHOD_INVOCATION, false); 60 beforeComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_METHOD_INVOCATION_ARGUMENTS, false); 61 afterComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_METHOD_INVOCATION_ARGUMENTS, true); 62 beforeClosingParen= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_PAREN_IN_METHOD_INVOCATION, false); 63 inEmptyList= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BETWEEN_EMPTY_PARENS_IN_METHOD_INVOCATION, false); 64 65 beforeOpeningBracket= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_ANGLE_BRACKET_IN_PARAMETERIZED_TYPE_REFERENCE, false); 66 afterOpeningBracket= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_OPENING_ANGLE_BRACKET_IN_PARAMETERIZED_TYPE_REFERENCE, false); 67 beforeTypeArgumentComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_COMMA_IN_PARAMETERIZED_TYPE_REFERENCE, false); 68 afterTypeArgumentComma= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_COMMA_IN_PARAMETERIZED_TYPE_REFERENCE, true); 69 beforeClosingBracket= getCoreOption(project, DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_CLOSING_ANGLE_BRACKET_IN_PARAMETERIZED_TYPE_REFERENCE, false); 70 } 71 72 protected final boolean getCoreOption(IJavaProject project, String key, boolean def) { 73 String option= getCoreOption(project, key); 74 if (JavaCore.INSERT.equals(option)) 75 return true; 76 if (JavaCore.DO_NOT_INSERT.equals(option)) 77 return false; 78 return def; 79 } 80 81 protected final String getCoreOption(IJavaProject project, String key) { 82 if (project == null) 83 return JavaCore.getOption(key); 84 return project.getOption(key, true); 85 } 86 } 87 88 private boolean fDisplayStringComputed; 89 private boolean fReplacementStringComputed; 90 private boolean fReplacementOffsetComputed; 91 private boolean fReplacementLengthComputed; 92 private boolean fCursorPositionComputed; 93 private boolean fImageComputed; 94 private boolean fContextInformationComputed; 95 private boolean fProposalInfoComputed; 96 private boolean fTriggerCharactersComputed; 97 private boolean fSortStringComputed; 98 private boolean fRelevanceComputed; 99 private FormatterPrefs fFormatterPrefs; 100 101 104 protected final CompletionProposal fProposal; 105 protected int fContextInformationPosition; 106 107 public LazyJavaCompletionProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context) { 108 super(context); 109 Assert.isNotNull(proposal); 110 Assert.isNotNull(context); 111 Assert.isNotNull(context.getCoreContext()); 112 fProposal= proposal; 113 } 114 115 118 public final char[] getTriggerCharacters() { 119 if (!fTriggerCharactersComputed) 120 setTriggerCharacters(computeTriggerCharacters()); 121 return super.getTriggerCharacters(); 122 } 123 124 protected char[] computeTriggerCharacters() { 125 return new char[0]; 126 } 127 128 132 public final void setTriggerCharacters(char[] triggerCharacters) { 133 fTriggerCharactersComputed= true; 134 super.setTriggerCharacters(triggerCharacters); 135 } 136 137 141 public final void setProposalInfo(ProposalInfo proposalInfo) { 142 fProposalInfoComputed= true; 143 super.setProposalInfo(proposalInfo); 144 } 145 146 153 protected final ProposalInfo getProposalInfo() { 154 if (!fProposalInfoComputed) 155 setProposalInfo(computeProposalInfo()); 156 return super.getProposalInfo(); 157 } 158 159 protected ProposalInfo computeProposalInfo() { 160 return null; 161 } 162 163 168 public final void setCursorPosition(int cursorPosition) { 169 fCursorPositionComputed= true; 170 super.setCursorPosition(cursorPosition); 171 } 172 173 protected final int getCursorPosition() { 174 if (!fCursorPositionComputed) 175 setCursorPosition(computeCursorPosition()); 176 return super.getCursorPosition(); 177 } 178 179 protected int computeCursorPosition() { 180 return getReplacementString().length(); 181 } 182 183 186 protected final boolean isInJavadoc() { 187 return fInvocationContext.getCoreContext().isInJavadoc(); 188 } 189 190 193 public final IContextInformation getContextInformation() { 194 if (!fContextInformationComputed) 195 setContextInformation(computeContextInformation()); 196 return super.getContextInformation(); 197 } 198 199 protected IContextInformation computeContextInformation() { 200 return null; 201 } 202 203 207 public final void setContextInformation(IContextInformation contextInformation) { 208 fContextInformationComputed= true; 209 super.setContextInformation(contextInformation); 210 } 211 212 215 public final String getDisplayString() { 216 if (!fDisplayStringComputed) 217 setDisplayString(computeDisplayString()); 218 return super.getDisplayString(); 219 } 220 221 protected final void setDisplayString(String string) { 222 fDisplayStringComputed= true; 223 super.setDisplayString(string); 224 } 225 226 protected String computeDisplayString() { 227 return fInvocationContext.getLabelProvider().createLabel(fProposal); 228 } 229 230 233 public final String getAdditionalProposalInfo() { 234 return super.getAdditionalProposalInfo(); 235 } 236 237 240 public final int getContextInformationPosition() { 241 if (getContextInformation() == null) 242 return getReplacementOffset() - 1; 243 return getReplacementOffset() + getCursorPosition(); 244 } 245 246 250 public final int getReplacementOffset() { 251 if (!fReplacementOffsetComputed) 252 setReplacementOffset(fProposal.getReplaceStart()); 253 return super.getReplacementOffset(); 254 } 255 256 260 public final void setReplacementOffset(int replacementOffset) { 261 fReplacementOffsetComputed= true; 262 super.setReplacementOffset(replacementOffset); 263 } 264 265 268 public final int getPrefixCompletionStart(IDocument document, int completionOffset) { 269 return getReplacementOffset(); 270 } 271 272 276 public final int getReplacementLength() { 277 if (!fReplacementLengthComputed) 278 setReplacementLength(fProposal.getReplaceEnd() - fProposal.getReplaceStart()); 279 return super.getReplacementLength(); 280 } 281 282 286 public final void setReplacementLength(int replacementLength) { 287 fReplacementLengthComputed= true; 288 super.setReplacementLength(replacementLength); 289 } 290 291 295 public final String getReplacementString() { 296 if (!fReplacementStringComputed) 297 setReplacementString(computeReplacementString()); 298 return super.getReplacementString(); 299 } 300 301 protected String computeReplacementString() { 302 return String.valueOf(fProposal.getCompletion()); 303 } 304 305 309 public final void setReplacementString(String replacementString) { 310 fReplacementStringComputed= true; 311 super.setReplacementString(replacementString); 312 } 313 314 317 public final Image getImage() { 318 if (!fImageComputed) 319 setImage(computeImage()); 320 return super.getImage(); 321 } 322 323 protected Image computeImage() { 324 return JavaPlugin.getImageDescriptorRegistry().get(fInvocationContext.getLabelProvider().createImageDescriptor(fProposal)); 325 } 326 327 331 public final void setImage(Image image) { 332 fImageComputed= true; 333 super.setImage(image); 334 } 335 336 339 protected boolean isValidPrefix(String prefix) { 340 if (super.isValidPrefix(prefix)) 341 return true; 342 343 if (fProposal.getKind() == CompletionProposal.METHOD_NAME_REFERENCE) { 344 StringBuffer buf= new StringBuffer (); 346 buf.append(Signature.toCharArray(fProposal.getDeclarationSignature())); 347 buf.append('.'); 348 buf.append(getDisplayString()); 349 return isPrefix(prefix, buf.toString()); 350 } 351 352 return false; 353 } 354 355 359 public final int getRelevance() { 360 if (!fRelevanceComputed) 361 setRelevance(computeRelevance()); 362 return super.getRelevance(); 363 } 364 365 369 public final void setRelevance(int relevance) { 370 fRelevanceComputed= true; 371 super.setRelevance(relevance); 372 } 373 374 protected int computeRelevance() { 375 final int baseRelevance= fProposal.getRelevance() * 16; 376 switch (fProposal.getKind()) { 377 case CompletionProposal.PACKAGE_REF: 378 return baseRelevance + 0; 379 case CompletionProposal.LABEL_REF: 380 return baseRelevance + 1; 381 case CompletionProposal.KEYWORD: 382 return baseRelevance + 2; 383 case CompletionProposal.TYPE_REF: 384 case CompletionProposal.ANONYMOUS_CLASS_DECLARATION: 385 return baseRelevance + 3; 386 case CompletionProposal.METHOD_REF: 387 case CompletionProposal.METHOD_NAME_REFERENCE: 388 case CompletionProposal.METHOD_DECLARATION: 389 case CompletionProposal.ANNOTATION_ATTRIBUTE_REF: 390 return baseRelevance + 4; 391 case CompletionProposal.POTENTIAL_METHOD_DECLARATION: 392 return baseRelevance + 4 ; 393 case CompletionProposal.FIELD_REF: 394 return baseRelevance + 5; 395 case CompletionProposal.LOCAL_VARIABLE_REF: 396 case CompletionProposal.VARIABLE_DECLARATION: 397 return baseRelevance + 6; 398 default: 399 return baseRelevance; 400 } 401 } 402 403 public final String getSortString() { 404 if (!fSortStringComputed) 405 setSortString(computeSortString()); 406 return super.getSortString(); 407 } 408 409 protected final void setSortString(String string) { 410 fSortStringComputed= true; 411 super.setSortString(string); 412 } 413 414 protected String computeSortString() { 415 return getDisplayString(); 416 } 417 418 protected FormatterPrefs getFormatterPrefs() { 419 if (fFormatterPrefs == null) { 420 ICompilationUnit cu= fInvocationContext.getCompilationUnit(); 421 fFormatterPrefs= new FormatterPrefs(cu == null ? null : cu.getJavaProject()); 422 } 423 return fFormatterPrefs; 424 } 425 426 431 public void setContextInformationPosition(int contextInformationPosition) { 432 fContextInformationPosition= contextInformationPosition; 433 } 434 } 435 | Popular Tags |