1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Set ; 17 18 import org.eclipse.core.runtime.CoreException; 19 20 import org.eclipse.swt.graphics.Image; 21 22 import org.eclipse.jface.resource.ImageDescriptor; 23 import org.eclipse.jface.text.ITextViewer; 24 25 import org.eclipse.jdt.core.CompletionRequestorAdapter; 26 import org.eclipse.jdt.core.Flags; 27 import org.eclipse.jdt.core.ICompilationUnit; 28 import org.eclipse.jdt.core.IJavaElement; 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.IType; 31 import org.eclipse.jdt.core.Signature; 32 import org.eclipse.jdt.core.compiler.IProblem; 33 34 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 35 36 import org.eclipse.jdt.internal.codeassist.IExtendedCompletionRequestor; 37 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 38 import org.eclipse.jdt.internal.corext.util.TypeFilter; 39 import org.eclipse.jdt.internal.ui.JavaPlugin; 40 import org.eclipse.jdt.internal.ui.JavaPluginImages; 41 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry; 42 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; 43 44 47 public class ResultCollector extends CompletionRequestorAdapter implements IExtendedCompletionRequestor { 48 49 private final static char[] METHOD_WITH_ARGUMENTS_TRIGGERS= new char[] { '(', '-', ' ' }; 50 private final static char[] METHOD_TRIGGERS= new char[] { ';', ',', '.', '\t', '[', ' ' }; 51 private final static char[] TYPE_TRIGGERS= new char[] { '.', '\t', '[', '(', ' ' }; 52 private final static char[] VAR_TRIGGER= new char[] { '\t', ' ', '=', ';', '.' }; 53 54 protected IJavaProject fJavaProject; 55 protected ICompilationUnit fCompilationUnit; protected int fCodeAssistOffset; 57 protected int fContextOffset; 58 protected ITextViewer fTextViewer; 59 60 private ArrayList fFields= 61 new ArrayList (), 62 fKeywords= new ArrayList (10), 63 fLabels= new ArrayList (10), 64 fMethods= new ArrayList (), 65 fModifiers= new ArrayList (10), 66 fPackages= new ArrayList (), 67 fTypes= new ArrayList (), 68 fVariables= new ArrayList (); 69 70 private IProblem fLastProblem; 71 private ImageDescriptorRegistry fRegistry= JavaPlugin.getImageDescriptorRegistry(); 72 73 private ArrayList [] fResults = new ArrayList [] { 74 fPackages, fLabels, fModifiers, fKeywords, fTypes, fMethods, fFields, fVariables 75 }; 76 77 private Set fSuggestedMethodNames= new HashSet (); 78 79 private int fUserReplacementLength; 80 81 85 private boolean fPreventEating= true; 86 87 90 public void acceptClass(char[] packageName, char[] typeName, char[] completionName, int modifiers, int start, int end, int relevance) { 91 if (TypeFilter.isFiltered(packageName)) { 92 return; 93 } 94 95 ImageDescriptor descriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, false, modifiers); 96 if (Flags.isDeprecated(modifiers)) 97 descriptor= getDeprecatedDescriptor(descriptor); 98 99 ProposalInfo info= new ProposalInfo(fJavaProject, packageName, typeName); 100 fTypes.add(createTypeCompletion(start, end, new String (completionName), descriptor, new String (typeName), new String (packageName), info, relevance)); 101 } 102 103 106 public void acceptError(IProblem error) { 107 fLastProblem= error; 108 } 109 110 113 public void acceptField( 114 char[] declaringTypePackageName, char[] declaringTypeName, char[] name, 115 char[] typePackageName, char[] typeName, char[] completionName, 116 int modifiers, int start, int end, int relevance) { 117 118 if (TypeFilter.isFiltered(declaringTypePackageName)) { 119 return; 120 } 121 122 123 ImageDescriptor descriptor= getFieldDescriptor(modifiers); 124 125 StringBuffer nameBuffer= new StringBuffer (); 126 nameBuffer.append(name); 127 if (typeName.length > 0) { 128 nameBuffer.append(" "); nameBuffer.append(typeName); 130 } 131 if (declaringTypeName != null && declaringTypeName.length > 0) { 132 nameBuffer.append(" - "); nameBuffer.append(declaringTypeName); 134 } 135 136 JavaCompletionProposal proposal= createCompletion(start, end, new String (completionName), descriptor, nameBuffer.toString(), relevance); 137 proposal.setProposalInfo(new ProposalInfo(fJavaProject, declaringTypePackageName, declaringTypeName, name)); 138 proposal.setTriggerCharacters(VAR_TRIGGER); 139 140 fFields.add(proposal); 141 } 142 143 146 public void acceptInterface(char[] packageName, char[] typeName, char[] completionName, int modifiers, int start, int end, int relevance) { 147 if (TypeFilter.isFiltered(packageName)) { 148 return; 149 } 150 151 ImageDescriptor descriptor= JavaElementImageProvider.getTypeImageDescriptor(true, false, false, modifiers); 152 if (Flags.isDeprecated(modifiers)) 153 descriptor= getDeprecatedDescriptor(descriptor); 154 155 ProposalInfo info= new ProposalInfo(fJavaProject, packageName, typeName); 156 fTypes.add(createTypeCompletion(start, end, new String (completionName), descriptor, new String (typeName), new String (packageName), info, relevance)); 157 } 158 159 162 public void acceptAnonymousType(char[] superTypePackageName, char[] superTypeName, char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames, 163 char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) { 164 165 if (TypeFilter.isFiltered(superTypePackageName)) { 166 return; 167 } 168 169 JavaCompletionProposal proposal= createAnonymousTypeCompletion(superTypePackageName, superTypeName, parameterTypeNames, parameterNames, completionName, completionStart, completionEnd, relevance); 170 proposal.setProposalInfo(new ProposalInfo(fJavaProject, superTypePackageName, superTypeName)); 171 fTypes.add(proposal); 172 } 173 174 177 public void acceptKeyword(char[] keyword, int start, int end, int relevance) { 178 String kw= new String (keyword); 179 fKeywords.add(createCompletion(start, end, kw, null, kw, relevance)); 180 } 181 182 185 public void acceptLabel(char[] labelName, int start, int end, int relevance) { 186 String ln= new String (labelName); 187 fLabels.add(createCompletion(start, end, ln, null, ln, relevance)); 188 } 189 190 193 public void acceptLocalVariable(char[] name, char[] typePackageName, char[] typeName, int modifiers, int start, int end, int relevance) { 194 StringBuffer buf= new StringBuffer (); 195 buf.append(name); 196 if (typeName != null) { 197 buf.append(" "); buf.append(typeName); 199 } 200 JavaCompletionProposal proposal= createCompletion(start, end, new String (name), JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE, buf.toString(), relevance); 201 proposal.setTriggerCharacters(VAR_TRIGGER); 202 fVariables.add(proposal); 203 } 204 205 protected String getParameterSignature(char[][] parameterTypeNames, char[][] parameterNames) { 206 StringBuffer buf = new StringBuffer (); 207 if (parameterTypeNames != null) { 208 for (int i = 0; i < parameterTypeNames.length; i++) { 209 if (i > 0) { 210 buf.append(','); 211 buf.append(' '); 212 } 213 buf.append(parameterTypeNames[i]); 214 if (parameterNames != null && parameterNames[i] != null) { 215 buf.append(' '); 216 buf.append(parameterNames[i]); 217 } 218 } 219 } 220 return buf.toString(); 221 } 222 223 226 public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, 227 char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames, 228 char[] returnTypePackageName, char[] returnTypeName, char[] completionName, int modifiers, 229 int start, int end, int relevance) { 230 231 if (completionName == null) 232 return; 233 234 if (TypeFilter.isFiltered(declaringTypePackageName)) { 235 return; 236 } 237 238 JavaCompletionProposal proposal= createMethodCallCompletion(declaringTypeName, name, parameterPackageNames, parameterTypeNames, parameterNames, returnTypeName, completionName, modifiers, start, end, relevance); 239 boolean isConstructor= returnTypeName == null ? true : returnTypeName.length == 0; 240 proposal.setProposalInfo(new ProposalInfo(fJavaProject, declaringTypePackageName, declaringTypeName, name, parameterPackageNames, parameterTypeNames, isConstructor)); 241 242 boolean hasOpeningBracket= completionName.length == 0 || (completionName.length > 0 && completionName[completionName.length - 1] == ')'); 243 244 ProposalContextInformation contextInformation= null; 245 if (hasOpeningBracket && parameterTypeNames.length > 0) { 246 contextInformation= new ProposalContextInformation(); 247 contextInformation.setInformationDisplayString(getParameterSignature(parameterTypeNames, parameterNames)); 248 contextInformation.setContextDisplayString(proposal.getDisplayString()); 249 contextInformation.setImage(proposal.getImage()); 250 int position= (completionName.length == 0) ? fContextOffset : -1; 251 contextInformation.setContextInformationPosition(position); 252 proposal.setContextInformation(contextInformation); 253 } 254 255 boolean userMustCompleteParameters= (contextInformation != null && completionName.length > 0); 256 char[] triggers= userMustCompleteParameters ? METHOD_WITH_ARGUMENTS_TRIGGERS : METHOD_TRIGGERS; 257 proposal.setTriggerCharacters(triggers); 258 259 if (userMustCompleteParameters) { 260 proposal.setCursorPosition(completionName.length - 1); 262 } 263 264 fMethods.add(proposal); 265 } 266 267 268 271 public void acceptModifier(char[] modifier, int start, int end, int relevance) { 272 String mod= new String (modifier); 273 fModifiers.add(createCompletion(start, end, mod, null, mod, relevance)); 274 } 275 276 279 public void acceptPackage(char[] packageName, char[] completionName, int start, int end, int relevance) { 280 if (TypeFilter.isFiltered(packageName)) { 281 return; 282 } 283 284 fPackages.add(createCompletion(start, end, new String (completionName), JavaPluginImages.DESC_OBJS_PACKAGE, new String (packageName), relevance)); 285 } 286 287 290 public void acceptType(char[] packageName, char[] typeName, char[] completionName, int start, int end, int relevance) { 291 if (TypeFilter.isFiltered(packageName)) { 292 return; 293 } 294 295 ProposalInfo info= new ProposalInfo(fJavaProject, packageName, typeName); 296 fTypes.add(createTypeCompletion(start, end, new String (completionName), JavaPluginImages.DESC_OBJS_CLASS, new String (typeName), new String (packageName), info, relevance)); 297 } 298 299 302 public void acceptMethodDeclaration(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, char[][] parameterPackageNames, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypePackageName, char[] returnTypeName, char[] completionName, int modifiers, int start, int end, int relevance) { 303 StringBuffer displayString= getMethodDisplayString(null, name, parameterTypeNames, parameterNames, returnTypeName); 304 displayString.append(" - "); displayString.append(JavaTextMessages.getFormattedString("ResultCollector.overridingmethod", new String (declaringTypeName))); 307 StringBuffer typeName= new StringBuffer (); 308 if (declaringTypePackageName.length > 0) { 309 typeName.append(declaringTypePackageName); 310 typeName.append('.'); 311 } 312 typeName.append(declaringTypeName); 313 314 String [] paramTypes= new String [parameterTypeNames.length]; 315 for (int i= 0; i < parameterTypeNames.length; i++) { 316 paramTypes[i]= Signature.createTypeSignature(parameterTypeNames[i], true); 317 } 318 319 JavaCompletionProposal proposal= new OverrideCompletionProposal(fJavaProject, fCompilationUnit, typeName.toString(), new String (name), paramTypes, start, getLength(start, end), displayString.toString(), new String (completionName)); 320 proposal.setImage(getImage(getMemberDescriptor(modifiers))); 321 proposal.setProposalInfo(new ProposalInfo(fJavaProject, declaringTypePackageName, declaringTypeName, name, parameterPackageNames, parameterTypeNames, returnTypeName.length == 0)); 322 proposal.setRelevance(relevance + 100); 323 fMethods.add(proposal); 324 325 fSuggestedMethodNames.add(new String (name)); 326 327 } 328 329 332 public void acceptPotentialMethodDeclaration(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, int completionStart, int completionEnd, int relevance) { 333 if (fCompilationUnit == null) { 334 return; 335 } 336 String prefix= new String (selector); 337 338 try { 339 IJavaElement element= fCompilationUnit.getElementAt(fCodeAssistOffset); 340 if (element != null) { 341 IType type= (IType) element.getAncestor(IJavaElement.TYPE); 342 if (type != null) { 343 GetterSetterCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 100, fSuggestedMethodNames, fMethods); 344 MethodCompletionProposal.evaluateProposals(type, prefix, completionStart, completionEnd - completionStart, relevance + 99, fSuggestedMethodNames, fMethods); 345 } 346 } 347 } catch (CoreException e) { 348 JavaPlugin.log(e); 349 } 350 } 351 352 355 public void acceptVariableName(char[] typePackageName, char[] typeName, char[] name, char[] completionName, int start, int end, int relevance) { 356 StringBuffer buf= new StringBuffer (); 358 buf.append(name); 359 if (typeName != null && typeName.length > 0) { 360 buf.append(" - "); buf.append(typeName); 362 } 363 JavaCompletionProposal proposal= createCompletion(start, end, new String (completionName), null, buf.toString(), relevance); 364 proposal.setTriggerCharacters(VAR_TRIGGER); 365 fVariables.add(proposal); 366 } 367 368 public String getErrorMessage() { 369 if (fLastProblem != null) 370 return fLastProblem.getMessage(); 371 return ""; } 373 374 public JavaCompletionProposal[] getResults() { 375 int totLen= 0; 377 for (int i= 0; i < fResults.length; i++) { 378 totLen += fResults[i].size(); 379 } 380 JavaCompletionProposal[] result= new JavaCompletionProposal[totLen]; 381 int k= 0; 382 for (int i= 0; i < fResults.length; i++) { 383 ArrayList curr= fResults[i]; 384 int currLen= curr.size(); 385 for (int j= 0; j < currLen; j++) { 386 JavaCompletionProposal proposal= (JavaCompletionProposal) curr.get(j); 387 proposal.setRelevance(proposal.getRelevance() * 16 + i); 389 result[k++]= proposal; 390 } 391 } 392 return result; 393 } 394 395 public JavaCompletionProposal[] getKeywordCompletions() { 396 return (JavaCompletionProposal[]) fKeywords.toArray(new JavaCompletionProposal[fKeywords.size()]); 397 } 398 399 private StringBuffer getMethodDisplayString(char[] declaringTypeName, char[] name, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypeName) { 400 StringBuffer nameBuffer= new StringBuffer (); 401 nameBuffer.append(name); 402 nameBuffer.append('('); 403 if (parameterTypeNames != null && parameterTypeNames.length > 0) { 404 nameBuffer.append(getParameterSignature(parameterTypeNames, parameterNames)); 405 } 406 nameBuffer.append(')'); 407 if (returnTypeName != null && returnTypeName.length > 0) { 408 nameBuffer.append(" "); nameBuffer.append(returnTypeName); 410 } 411 if (declaringTypeName != null && declaringTypeName.length > 0) { 412 nameBuffer.append(" - "); nameBuffer.append(declaringTypeName); 414 } 415 return nameBuffer; 416 } 417 418 protected JavaCompletionProposal createMethodCallCompletion(char[] declaringTypeName, char[] name, char[][] parameterTypePackageNames, char[][] parameterTypeNames, char[][] parameterNames, char[] returnTypeName, char[] completionName, int modifiers, int start, int end, int relevance) { 419 ImageDescriptor descriptor= getMemberDescriptor(modifiers); 420 StringBuffer nameBuffer= getMethodDisplayString(declaringTypeName, name, parameterTypeNames, parameterNames, returnTypeName); 421 return createCompletion(start, end, new String (completionName), descriptor, nameBuffer.toString(), relevance); 422 } 423 424 425 protected JavaCompletionProposal createAnonymousTypeCompletion(char[] declaringTypePackageName, char[] declaringTypeName, char[][] parameterTypeNames, char[][] parameterNames, char[] completionName, int start, int end, int relevance) { 426 StringBuffer declTypeBuf= new StringBuffer (); 427 if (declaringTypePackageName.length > 0) { 428 declTypeBuf.append(declaringTypePackageName); 429 declTypeBuf.append('.'); 430 } 431 declTypeBuf.append(declaringTypeName); 432 433 StringBuffer nameBuffer= new StringBuffer (); 434 nameBuffer.append(declaringTypeName); 435 nameBuffer.append('('); 436 if (parameterTypeNames.length > 0) { 437 nameBuffer.append(getParameterSignature(parameterTypeNames, parameterNames)); 438 } 439 nameBuffer.append(')'); 440 nameBuffer.append(" "); nameBuffer.append(JavaTextMessages.getString("ResultCollector.anonymous_type")); 443 int length= end - start; 444 445 return new AnonymousTypeCompletionProposal(fJavaProject, fCompilationUnit, start, length, new String (completionName), nameBuffer.toString(), declTypeBuf.toString(), relevance); 446 } 447 448 449 protected JavaCompletionProposal createTypeCompletion(int start, int end, String completion, ImageDescriptor descriptor, String typeName, String containerName, ProposalInfo proposalInfo, int relevance) { 450 451 String fullName= JavaModelUtil.concatenateName(containerName, typeName); 453 StringBuffer buf= new StringBuffer (Signature.getSimpleName(fullName)); 454 String typeQualifier= Signature.getQualifier(fullName); 455 if (typeQualifier.length() > 0) { 456 buf.append(" - "); buf.append(typeQualifier); 458 } else if (containerName != null) { 459 buf.append(JavaTextMessages.getString("ResultCollector.default_package")); } 461 String name= buf.toString(); 462 463 ICompilationUnit cu= null; 464 if (containerName != null && fCompilationUnit != null) { 465 if (completion.equals(fullName)) { 466 cu= fCompilationUnit; 467 } 468 } 469 470 JavaCompletionProposal proposal= new JavaTypeCompletionProposal(completion, cu, start, getLength(start, end), getImage(descriptor), name, relevance, typeName, containerName); 471 proposal.setProposalInfo(proposalInfo); 472 proposal.setTriggerCharacters(TYPE_TRIGGERS); 473 return proposal; 474 } 475 476 protected ImageDescriptor getMemberDescriptor(int modifiers) { 477 ImageDescriptor desc= JavaElementImageProvider.getMethodImageDescriptor(false, modifiers); 478 479 if (Flags.isDeprecated(modifiers)) 480 desc= getDeprecatedDescriptor(desc); 481 482 if (Flags.isStatic(modifiers)) 483 desc= getStaticDescriptor(desc); 484 485 return desc; 486 } 487 488 protected ImageDescriptor getFieldDescriptor(int modifiers) { 489 ImageDescriptor desc= JavaElementImageProvider.getFieldImageDescriptor(false, modifiers); 490 491 if (Flags.isDeprecated(modifiers)) 492 desc= getDeprecatedDescriptor(desc); 493 494 if (Flags.isStatic(modifiers)) 495 desc= getStaticDescriptor(desc); 496 497 return desc; 498 } 499 500 protected ImageDescriptor getDeprecatedDescriptor(ImageDescriptor descriptor) { 501 return new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.DEPRECATED, JavaElementImageProvider.SMALL_SIZE); 502 } 503 504 protected ImageDescriptor getStaticDescriptor(ImageDescriptor descriptor) { 505 return new JavaElementImageDescriptor(descriptor, JavaElementImageDescriptor.STATIC, JavaElementImageProvider.SMALL_SIZE); 506 } 507 508 protected JavaCompletionProposal createCompletion(int start, int end, String completion, ImageDescriptor descriptor, String name, int relevance) { 509 return new JavaCompletionProposal(completion, start, getLength(start, end), getImage(descriptor), name, relevance, fTextViewer); 510 } 511 512 private int getLength(int start, int end) { 513 int length; 514 if (fUserReplacementLength == -1) { 515 length= fPreventEating ? fCodeAssistOffset - start : end - start; 516 } else { 517 length= fUserReplacementLength; 518 if (start < fCodeAssistOffset) { 520 length+= fCodeAssistOffset - start; 521 } 522 } 523 return length; 524 } 525 526 private Image getImage(ImageDescriptor descriptor) { 527 return (descriptor == null) ? null : fRegistry.get(descriptor); 528 } 529 530 540 public void reset(int codeAssistOffset, int contextOffset, IJavaProject jproject, ICompilationUnit cu) { 541 fJavaProject= jproject; 542 fCompilationUnit= cu; 543 fCodeAssistOffset= codeAssistOffset; 544 fContextOffset= contextOffset; 545 546 fUserReplacementLength= -1; 547 548 fLastProblem= null; 549 550 for (int i= 0; i < fResults.length; i++) 551 fResults[i].clear(); 552 553 fSuggestedMethodNames.clear(); 554 } 555 556 565 public void reset(int codeAssistOffset, IJavaProject jproject, ICompilationUnit cu) { 566 reset(codeAssistOffset, -1, jproject, cu); 567 } 568 569 572 public void setViewer(ITextViewer viewer) { 573 fTextViewer= viewer; 574 } 575 576 581 public void setReplacementLength(int length) { 582 fUserReplacementLength= length; 583 } 584 585 589 public void setPreventEating(boolean preventEating) { 590 fPreventEating= preventEating; 591 } 592 593 } 594 | Popular Tags |