1 11 package org.eclipse.jdt.ui.text.java; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.resource.ImageDescriptor; 16 17 import org.eclipse.jdt.core.CompletionContext; 18 import org.eclipse.jdt.core.CompletionProposal; 19 import org.eclipse.jdt.core.Flags; 20 import org.eclipse.jdt.core.Signature; 21 22 import org.eclipse.jdt.internal.corext.template.java.SignatureUtil; 23 import org.eclipse.jdt.internal.corext.util.Messages; 24 25 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 26 import org.eclipse.jdt.ui.JavaElementLabels; 27 28 import org.eclipse.jdt.internal.ui.JavaPluginImages; 29 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; 30 31 32 40 public class CompletionProposalLabelProvider { 41 42 47 private CompletionContext fContext; 48 49 52 public CompletionProposalLabelProvider() { 53 } 54 55 72 public String createParameterList(CompletionProposal proposal) { 73 int kind= proposal.getKind(); 74 switch (kind) { 75 case CompletionProposal.METHOD_REF: 76 return appendUnboundedParameterList(new StringBuffer (), proposal).toString(); 77 case CompletionProposal.TYPE_REF: 78 return appendTypeParameterList(new StringBuffer (), proposal).toString(); 79 default: 80 Assert.isLegal(false); 81 return null; } 83 } 84 85 92 private StringBuffer appendUnboundedParameterList(StringBuffer buffer, CompletionProposal methodProposal) { 93 char[] signature= SignatureUtil.fix83600(methodProposal.getSignature()); 96 char[][] parameterNames= methodProposal.findParameterNames(null); 97 char[][] parameterTypes= Signature.getParameterTypes(signature); 98 99 for (int i= 0; i < parameterTypes.length; i++) 100 parameterTypes[i]= createTypeDisplayName(SignatureUtil.getLowerBound(parameterTypes[i])); 101 102 if (Flags.isVarargs(methodProposal.getFlags())) { 103 int index= parameterTypes.length - 1; 104 parameterTypes[index]= convertToVararg(parameterTypes[index]); 105 } 106 return appendParameterSignature(buffer, parameterTypes, parameterNames); 107 } 108 109 117 private StringBuffer appendTypeParameterList(StringBuffer buffer, CompletionProposal typeProposal) { 118 char[] signature= SignatureUtil.fix83600(typeProposal.getSignature()); 121 char[][] typeParameters= Signature.getTypeArguments(signature); 122 for (int i= 0; i < typeParameters.length; i++) { 123 char[] param= typeParameters[i]; 124 typeParameters[i]= Signature.toCharArray(param); 125 } 126 return appendParameterSignature(buffer, typeParameters, null); 127 } 128 129 147 private char[] convertToVararg(char[] typeName) { 148 if (typeName == null) 149 return typeName; 150 final int len= typeName.length; 151 if (len < 2) 152 return typeName; 153 154 if (typeName[len - 1] != ']') 155 return typeName; 156 if (typeName[len - 2] != '[') 157 return typeName; 158 159 char[] vararg= new char[len + 1]; 160 System.arraycopy(typeName, 0, vararg, 0, len - 2); 161 vararg[len - 2]= '.'; 162 vararg[len - 1]= '.'; 163 vararg[len]= '.'; 164 return vararg; 165 } 166 167 177 private char[] createTypeDisplayName(char[] typeSignature) throws IllegalArgumentException { 178 char[] displayName= Signature.getSimpleName(Signature.toCharArray(typeSignature)); 179 180 boolean useShortGenerics= false; 182 if (useShortGenerics) { 183 StringBuffer buf= new StringBuffer (); 184 buf.append(displayName); 185 int pos; 186 do { 187 pos= buf.indexOf("? extends "); if (pos >= 0) { 189 buf.replace(pos, pos + 10, "+"); } else { 191 pos= buf.indexOf("? super "); if (pos >= 0) 193 buf.replace(pos, pos + 8, "-"); } 195 } while (pos >= 0); 196 return buf.toString().toCharArray(); 197 } 198 return displayName; 199 } 200 201 210 private final StringBuffer appendParameterSignature(StringBuffer buffer, char[][] parameterTypes, char[][] parameterNames) { 211 if (parameterTypes != null) { 212 for (int i = 0; i < parameterTypes.length; i++) { 213 if (i > 0) { 214 buffer.append(','); 215 buffer.append(' '); 216 } 217 buffer.append(parameterTypes[i]); 218 if (parameterNames != null && parameterNames[i] != null) { 219 buffer.append(' '); 220 buffer.append(parameterNames[i]); 221 } 222 } 223 } 224 return buffer; 225 } 226 227 247 String createMethodProposalLabel(CompletionProposal methodProposal) { 248 StringBuffer nameBuffer= new StringBuffer (); 249 250 nameBuffer.append(methodProposal.getName()); 252 253 nameBuffer.append('('); 255 appendUnboundedParameterList(nameBuffer, methodProposal); 256 nameBuffer.append(')'); 257 258 if (!methodProposal.isConstructor()) { 260 char[] returnType= createTypeDisplayName(SignatureUtil.getUpperBound(Signature.getReturnType(SignatureUtil.fix83600(methodProposal.getSignature())))); 262 nameBuffer.append(" "); nameBuffer.append(returnType); 264 } 265 266 nameBuffer.append(" - "); String declaringType= extractDeclaringTypeFQN(methodProposal); 269 declaringType= Signature.getSimpleName(declaringType); 270 nameBuffer.append(declaringType); 271 272 return nameBuffer.toString(); 273 } 274 275 293 String createJavadocMethodProposalLabel(CompletionProposal methodProposal) { 294 StringBuffer nameBuffer= new StringBuffer (); 295 296 nameBuffer.append(methodProposal.getCompletion()); 298 299 nameBuffer.append(" - "); String declaringType= extractDeclaringTypeFQN(methodProposal); 302 declaringType= Signature.getSimpleName(declaringType); 303 nameBuffer.append(declaringType); 304 305 return nameBuffer.toString(); 306 } 307 308 String createOverrideMethodProposalLabel(CompletionProposal methodProposal) { 309 StringBuffer nameBuffer= new StringBuffer (); 310 311 nameBuffer.append(methodProposal.getName()); 313 314 nameBuffer.append('('); 316 appendUnboundedParameterList(nameBuffer, methodProposal); 317 nameBuffer.append(") "); 319 char[] returnType= createTypeDisplayName(SignatureUtil.getUpperBound(Signature.getReturnType(SignatureUtil.fix83600(methodProposal.getSignature())))); 322 nameBuffer.append(returnType); 323 324 nameBuffer.append(" - "); 327 String declaringType= extractDeclaringTypeFQN(methodProposal); 328 declaringType= Signature.getSimpleName(declaringType); 329 nameBuffer.append(Messages.format(JavaTextMessages.ResultCollector_overridingmethod, new String (declaringType))); 330 331 return nameBuffer.toString(); 332 } 333 334 341 private String extractDeclaringTypeFQN(CompletionProposal methodProposal) { 342 char[] declaringTypeSignature= methodProposal.getDeclarationSignature(); 343 if (declaringTypeSignature == null) 346 return "java.lang.Object"; return SignatureUtil.stripSignatureToFQN(String.valueOf(declaringTypeSignature)); 348 } 349 350 366 String createTypeProposalLabel(CompletionProposal typeProposal) { 367 char[] signature; 368 if (fContext != null && fContext.isInJavadoc()) 369 signature= Signature.getTypeErasure(typeProposal.getSignature()); 370 else 371 signature= typeProposal.getSignature(); 372 char[] fullName= Signature.toCharArray(signature); 373 return createTypeProposalLabel(fullName); 374 } 375 376 String createJavadocTypeProposalLabel(CompletionProposal typeProposal) { 377 char[] fullName= Signature.toCharArray(typeProposal.getSignature()); 378 return createJavadocTypeProposalLabel(fullName); 379 } 380 381 String createJavadocSimpleProposalLabel(CompletionProposal proposal) { 382 return createSimpleLabel(proposal); 384 } 385 386 String createTypeProposalLabel(char[] fullName) { 387 int qIndex= findSimpleNameStart(fullName); 390 391 StringBuffer buf= new StringBuffer (); 392 buf.append(fullName, qIndex, fullName.length - qIndex); 393 if (qIndex > 0) { 394 buf.append(JavaElementLabels.CONCAT_STRING); 395 buf.append(fullName, 0, qIndex - 1); 396 } 397 return buf.toString(); 398 } 399 400 String createJavadocTypeProposalLabel(char[] fullName) { 401 int qIndex= findSimpleNameStart(fullName); 404 405 StringBuffer buf= new StringBuffer ("{@link "); buf.append(fullName, qIndex, fullName.length - qIndex); 407 buf.append('}'); 408 if (qIndex > 0) { 409 buf.append(JavaElementLabels.CONCAT_STRING); 410 buf.append(fullName, 0, qIndex - 1); 411 } 412 return buf.toString(); 413 } 414 415 private int findSimpleNameStart(char[] array) { 416 int lastDot= 0; 417 for (int i= 0, len= array.length; i < len; i++) { 418 char ch= array[i]; 419 if (ch == '<') { 420 return lastDot; 421 } else if (ch == '.') { 422 lastDot= i + 1; 423 } 424 } 425 return lastDot; 426 } 427 428 String createSimpleLabelWithType(CompletionProposal proposal) { 429 StringBuffer buf= new StringBuffer (); 430 buf.append(proposal.getCompletion()); 431 char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature()); 432 if (typeName.length > 0) { 433 buf.append(" "); buf.append(typeName); 435 } 436 return buf.toString(); 437 } 438 439 446 private boolean isThisPrefix(char[] string) { 447 if (string == null || string.length < 5) 448 return false; 449 return string[0] == 't' && string[1] == 'h' && string[2] == 'i' && string[3] == 's' && string[4] == '.'; 450 } 451 452 String createLabelWithTypeAndDeclaration(CompletionProposal proposal) { 453 char[] name= proposal.getCompletion(); 454 if (!isThisPrefix(name)) 455 name= proposal.getName(); 456 457 StringBuffer buf= new StringBuffer (); 458 buf.append(name); 459 char[] typeName= Signature.getSignatureSimpleName(proposal.getSignature()); 460 if (typeName.length > 0) { 461 buf.append(" "); buf.append(typeName); 463 } 464 char[] declaration= proposal.getDeclarationSignature(); 465 if (declaration != null) { 466 declaration= Signature.getSignatureSimpleName(declaration); 467 if (declaration.length > 0) { 468 buf.append(" - "); buf.append(declaration); 470 } 471 } 472 473 return buf.toString(); 474 } 475 476 String createPackageProposalLabel(CompletionProposal proposal) { 477 Assert.isTrue(proposal.getKind() == CompletionProposal.PACKAGE_REF); 478 return String.valueOf(proposal.getDeclarationSignature()); 479 } 480 481 String createSimpleLabel(CompletionProposal proposal) { 482 return String.valueOf(proposal.getCompletion()); 483 } 484 485 String createAnonymousTypeLabel(CompletionProposal proposal) { 486 char[] declaringTypeSignature= proposal.getDeclarationSignature(); 487 488 StringBuffer buffer= new StringBuffer (); 489 buffer.append(Signature.getSignatureSimpleName(declaringTypeSignature)); 490 buffer.append('('); 491 appendUnboundedParameterList(buffer, proposal); 492 buffer.append(')'); 493 buffer.append(" "); buffer.append(JavaTextMessages.ResultCollector_anonymous_type); 495 496 return buffer.toString(); 497 } 498 499 505 public String createLabel(CompletionProposal proposal) { 506 switch (proposal.getKind()) { 507 case CompletionProposal.METHOD_NAME_REFERENCE: 508 case CompletionProposal.METHOD_REF: 509 case CompletionProposal.POTENTIAL_METHOD_DECLARATION: 510 if (fContext != null && fContext.isInJavadoc()) 511 return createJavadocMethodProposalLabel(proposal); 512 return createMethodProposalLabel(proposal); 513 case CompletionProposal.METHOD_DECLARATION: 514 return createOverrideMethodProposalLabel(proposal); 515 case CompletionProposal.ANONYMOUS_CLASS_DECLARATION: 516 return createAnonymousTypeLabel(proposal); 517 case CompletionProposal.TYPE_REF: 518 return createTypeProposalLabel(proposal); 519 case CompletionProposal.JAVADOC_TYPE_REF: 520 return createJavadocTypeProposalLabel(proposal); 521 case CompletionProposal.JAVADOC_FIELD_REF: 522 case CompletionProposal.JAVADOC_VALUE_REF: 523 case CompletionProposal.JAVADOC_BLOCK_TAG: 524 case CompletionProposal.JAVADOC_INLINE_TAG: 525 case CompletionProposal.JAVADOC_PARAM_REF: 526 return createJavadocSimpleProposalLabel(proposal); 527 case CompletionProposal.JAVADOC_METHOD_REF: 528 return createJavadocMethodProposalLabel(proposal); 529 case CompletionProposal.PACKAGE_REF: 530 return createPackageProposalLabel(proposal); 531 case CompletionProposal.ANNOTATION_ATTRIBUTE_REF: 532 case CompletionProposal.FIELD_REF: 533 return createLabelWithTypeAndDeclaration(proposal); 534 case CompletionProposal.LOCAL_VARIABLE_REF: 535 case CompletionProposal.VARIABLE_DECLARATION: 536 return createSimpleLabelWithType(proposal); 537 case CompletionProposal.KEYWORD: 538 case CompletionProposal.LABEL_REF: 539 return createSimpleLabel(proposal); 540 default: 541 Assert.isTrue(false); 542 return null; 543 } 544 } 545 546 552 public ImageDescriptor createImageDescriptor(CompletionProposal proposal) { 553 final int flags= proposal.getFlags(); 554 555 ImageDescriptor descriptor; 556 switch (proposal.getKind()) { 557 case CompletionProposal.METHOD_DECLARATION: 558 case CompletionProposal.METHOD_NAME_REFERENCE: 559 case CompletionProposal.METHOD_REF: 560 case CompletionProposal.ANNOTATION_ATTRIBUTE_REF: 561 case CompletionProposal.POTENTIAL_METHOD_DECLARATION: 562 descriptor= JavaElementImageProvider.getMethodImageDescriptor(false, flags); 563 break; 564 case CompletionProposal.ANONYMOUS_CLASS_DECLARATION: 565 case CompletionProposal.TYPE_REF: 566 switch (Signature.getTypeSignatureKind(proposal.getSignature())) { 567 case Signature.CLASS_TYPE_SIGNATURE: 568 descriptor= JavaElementImageProvider.getTypeImageDescriptor(false, false, flags, false); 569 break; 570 case Signature.TYPE_VARIABLE_SIGNATURE: 571 descriptor= JavaPluginImages.DESC_OBJS_TYPEVARIABLE; 572 break; 573 default: 574 descriptor= null; 575 } 576 break; 577 case CompletionProposal.FIELD_REF: 578 descriptor= JavaElementImageProvider.getFieldImageDescriptor(false, flags); 579 break; 580 case CompletionProposal.LOCAL_VARIABLE_REF: 581 case CompletionProposal.VARIABLE_DECLARATION: 582 descriptor= JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE; 583 break; 584 case CompletionProposal.PACKAGE_REF: 585 descriptor= JavaPluginImages.DESC_OBJS_PACKAGE; 586 break; 587 case CompletionProposal.KEYWORD: 588 case CompletionProposal.LABEL_REF: 589 descriptor= null; 590 break; 591 case CompletionProposal.JAVADOC_METHOD_REF: 592 case CompletionProposal.JAVADOC_TYPE_REF: 593 case CompletionProposal.JAVADOC_FIELD_REF: 594 case CompletionProposal.JAVADOC_VALUE_REF: 595 case CompletionProposal.JAVADOC_BLOCK_TAG: 596 case CompletionProposal.JAVADOC_INLINE_TAG: 597 case CompletionProposal.JAVADOC_PARAM_REF: 598 descriptor = JavaPluginImages.DESC_OBJS_JAVADOCTAG; 599 break; 600 default: 601 descriptor= null; 602 Assert.isTrue(false); 603 } 604 605 if (descriptor == null) 606 return null; 607 return decorateImageDescriptor(descriptor, proposal); 608 } 609 610 ImageDescriptor createMethodImageDescriptor(CompletionProposal proposal) { 611 final int flags= proposal.getFlags(); 612 return decorateImageDescriptor(JavaElementImageProvider.getMethodImageDescriptor(false, flags), proposal); 613 } 614 615 ImageDescriptor createTypeImageDescriptor(CompletionProposal proposal) { 616 final int flags= proposal.getFlags(); 617 boolean isInterfaceOrAnnotation= Flags.isInterface(flags) || Flags.isAnnotation(flags); 618 return decorateImageDescriptor(JavaElementImageProvider.getTypeImageDescriptor(true , isInterfaceOrAnnotation, flags, false), proposal); 619 } 620 621 ImageDescriptor createFieldImageDescriptor(CompletionProposal proposal) { 622 final int flags= proposal.getFlags(); 623 return decorateImageDescriptor(JavaElementImageProvider.getFieldImageDescriptor(false, flags), proposal); 624 } 625 626 ImageDescriptor createLocalImageDescriptor(CompletionProposal proposal) { 627 return decorateImageDescriptor(JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE, proposal); 628 } 629 630 ImageDescriptor createPackageImageDescriptor(CompletionProposal proposal) { 631 return decorateImageDescriptor(JavaPluginImages.DESC_OBJS_PACKAGE, proposal); 632 } 633 634 643 private ImageDescriptor decorateImageDescriptor(ImageDescriptor descriptor, CompletionProposal proposal) { 644 int adornments= 0; 645 int flags= proposal.getFlags(); 646 int kind= proposal.getKind(); 647 648 if (Flags.isDeprecated(flags)) 649 adornments |= JavaElementImageDescriptor.DEPRECATED; 650 651 if (kind == CompletionProposal.FIELD_REF || kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_NAME_REFERENCE || kind == CompletionProposal.METHOD_REF) 652 if (Flags.isStatic(flags)) 653 adornments |= JavaElementImageDescriptor.STATIC; 654 655 if (kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_DECLARATION || kind == CompletionProposal.METHOD_NAME_REFERENCE || kind == CompletionProposal.METHOD_REF) 656 if (Flags.isSynchronized(flags)) 657 adornments |= JavaElementImageDescriptor.SYNCHRONIZED; 658 659 if (kind == CompletionProposal.TYPE_REF && Flags.isAbstract(flags) && !Flags.isInterface(flags)) 660 adornments |= JavaElementImageDescriptor.ABSTRACT; 661 662 if (kind == CompletionProposal.FIELD_REF) { 663 if (Flags.isTransient(flags)) 664 adornments |= JavaElementImageDescriptor.TRANSIENT; 665 if (Flags.isVolatile(flags)) 666 adornments |= JavaElementImageDescriptor.VOLATILE; 667 } 668 669 return new JavaElementImageDescriptor(descriptor, adornments, JavaElementImageProvider.SMALL_SIZE); 670 } 671 672 678 void setContext(CompletionContext context) { 679 fContext= context; 680 } 681 682 } 683 | Popular Tags |