1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import java.util.Collection ; 14 import java.util.Set ; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IStatus; 18 19 import org.eclipse.jface.resource.ImageDescriptor; 20 21 import org.eclipse.jface.text.Assert; 22 import org.eclipse.jface.text.BadLocationException; 23 import org.eclipse.jface.text.IDocument; 24 import org.eclipse.jface.text.IRegion; 25 import org.eclipse.jface.text.TextUtilities; 26 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4; 27 28 import org.eclipse.jdt.core.IMethod; 29 import org.eclipse.jdt.core.IType; 30 import org.eclipse.jdt.core.JavaConventions; 31 import org.eclipse.jdt.core.Signature; 32 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 33 import org.eclipse.jdt.core.formatter.CodeFormatter; 34 35 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; 36 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; 37 import org.eclipse.jdt.internal.corext.util.Strings; 38 39 import org.eclipse.jdt.ui.CodeGeneration; 40 import org.eclipse.jdt.ui.JavaElementImageDescriptor; 41 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.JavaPluginImages; 44 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; 45 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; 46 47 public class MethodCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 { 48 49 50 public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set suggestedMethods, Collection result) throws CoreException { 51 IMethod[] methods= type.getMethods(); 52 if (!type.isInterface()) { 53 String constructorName= type.getElementName(); 54 if (constructorName.length() > 0 && constructorName.startsWith(prefix) && !hasMethod(methods, constructorName) && suggestedMethods.add(constructorName)) { 55 result.add(new MethodCompletionProposal(type, constructorName, null, offset, length, relevance + 500)); 56 } 57 } 58 59 if (prefix.length() > 0 && !"main".equals(prefix) && !hasMethod(methods, prefix) && suggestedMethods.add(prefix)) { if (!JavaConventions.validateMethodName(prefix).matches(IStatus.ERROR)) { 61 result.add(new MethodCompletionProposal(type, prefix, Signature.SIG_VOID, offset, length, relevance)); 62 } 63 } 64 } 65 66 private static boolean hasMethod(IMethod[] methods, String name) { 67 for (int i= 0; i < methods.length; i++) { 68 IMethod curr= methods[i]; 69 if (curr.getElementName().equals(name) && curr.getParameterTypes().length == 0) { 70 return true; 71 } 72 } 73 return false; 74 } 75 76 private final IType fType; 77 private final String fReturnTypeSig; 78 private final String fMethodName; 79 80 public MethodCompletionProposal(IType type, String methodName, String returnTypeSig, int start, int length, int relevance) { 81 super("", type.getCompilationUnit(), start, length, null, getDisplayName(methodName, returnTypeSig), relevance); Assert.isNotNull(type); 83 Assert.isNotNull(methodName); 84 85 fType= type; 86 fMethodName= methodName; 87 fReturnTypeSig= returnTypeSig; 88 89 if (returnTypeSig == null) { 90 setProposalInfo(new ProposalInfo(type)); 91 92 ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE); 93 setImage(JavaPlugin.getImageDescriptorRegistry().get(desc)); 94 } else { 95 setImage(JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE)); 96 } 97 } 98 99 private static String getDisplayName(String methodName, String returnTypeSig) { 100 StringBuffer buf= new StringBuffer (); 101 buf.append(methodName); 102 buf.append('('); 103 buf.append(')'); 104 if (returnTypeSig != null) { 105 buf.append(" "); buf.append(Signature.toString(returnTypeSig)); 107 buf.append(" - "); buf.append(JavaTextMessages.MethodCompletionProposal_method_label); 109 } else { 110 buf.append(" - "); buf.append(JavaTextMessages.MethodCompletionProposal_constructor_label); 112 } 113 return buf.toString(); 114 } 115 116 119 protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { 120 121 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject()); 122 boolean addComments= settings.createComments; 123 124 String [] empty= new String [0]; 125 String lineDelim= TextUtilities.getDefaultLineDelimiter(document); 126 String declTypeName= fType.getTypeQualifiedName('.'); 127 boolean isInterface= fType.isInterface(); 128 129 StringBuffer buf= new StringBuffer (); 130 if (addComments) { 131 String comment= CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim); 132 if (comment != null) { 133 buf.append(comment); 134 buf.append(lineDelim); 135 } 136 } 137 if (fReturnTypeSig != null) { 138 if (!isInterface) { 139 buf.append("private "); } 141 } else { 142 buf.append("public "); } 144 145 if (fReturnTypeSig != null) { 146 buf.append(Signature.toString(fReturnTypeSig)); 147 } 148 buf.append(' '); 149 buf.append(fMethodName); 150 if (isInterface) { 151 buf.append("();"); buf.append(lineDelim); 153 } else { 154 buf.append("() {"); buf.append(lineDelim); 156 157 String body= CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", lineDelim); if (body != null) { 159 buf.append(body); 160 buf.append(lineDelim); 161 } 162 buf.append("}"); buf.append(lineDelim); 164 } 165 String stub= buf.toString(); 166 167 IRegion region= document.getLineInformationOfOffset(getReplacementOffset()); 169 int lineStart= region.getOffset(); 170 int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth); 171 172 String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, null, lineDelim, fType.getJavaProject()); 173 174 if (replacement.endsWith(lineDelim)) { 175 replacement= replacement.substring(0, replacement.length() - lineDelim.length()); 176 } 177 178 setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement)); 179 return true; 180 } 181 182 public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { 183 return new String (); } 185 186 189 public boolean isAutoInsertable() { 190 return false; 191 } 192 } 193 | Popular Tags |