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.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IStatus; 19 20 import org.eclipse.jface.resource.ImageDescriptor; 21 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 50 public class MethodDeclarationCompletionProposal extends JavaTypeCompletionProposal implements ICompletionProposalExtension4 { 51 52 53 public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set suggestedMethods, Collection result) throws CoreException { 54 IMethod[] methods= type.getMethods(); 55 if (!type.isInterface()) { 56 String constructorName= type.getElementName(); 57 if (constructorName.length() > 0 && constructorName.startsWith(prefix) && !hasMethod(methods, constructorName) && suggestedMethods.add(constructorName)) { 58 result.add(new MethodDeclarationCompletionProposal(type, constructorName, null, offset, length, relevance + 500)); 59 } 60 } 61 62 if (prefix.length() > 0 && !"main".equals(prefix) && !hasMethod(methods, prefix) && suggestedMethods.add(prefix)) { if (!JavaConventions.validateMethodName(prefix).matches(IStatus.ERROR)) { 64 result.add(new MethodDeclarationCompletionProposal(type, prefix, Signature.SIG_VOID, offset, length, relevance)); 65 } 66 } 67 } 68 69 private static boolean hasMethod(IMethod[] methods, String name) { 70 for (int i= 0; i < methods.length; i++) { 71 IMethod curr= methods[i]; 72 if (curr.getElementName().equals(name) && curr.getParameterTypes().length == 0) { 73 return true; 74 } 75 } 76 return false; 77 } 78 79 private final IType fType; 80 private final String fReturnTypeSig; 81 private final String fMethodName; 82 83 public MethodDeclarationCompletionProposal(IType type, String methodName, String returnTypeSig, int start, int length, int relevance) { 84 super("", type.getCompilationUnit(), start, length, null, getDisplayName(methodName, returnTypeSig), relevance); Assert.isNotNull(type); 86 Assert.isNotNull(methodName); 87 88 fType= type; 89 fMethodName= methodName; 90 fReturnTypeSig= returnTypeSig; 91 92 if (returnTypeSig == null) { 93 setProposalInfo(new ProposalInfo(type)); 94 95 ImageDescriptor desc= new JavaElementImageDescriptor(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE); 96 setImage(JavaPlugin.getImageDescriptorRegistry().get(desc)); 97 } else { 98 setImage(JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE)); 99 } 100 } 101 102 private static String getDisplayName(String methodName, String returnTypeSig) { 103 StringBuffer buf= new StringBuffer (); 104 buf.append(methodName); 105 buf.append('('); 106 buf.append(')'); 107 if (returnTypeSig != null) { 108 buf.append(" "); buf.append(Signature.toString(returnTypeSig)); 110 buf.append(" - "); buf.append(JavaTextMessages.MethodCompletionProposal_method_label); 112 } else { 113 buf.append(" - "); buf.append(JavaTextMessages.MethodCompletionProposal_constructor_label); 115 } 116 return buf.toString(); 117 } 118 119 122 protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException { 123 124 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject()); 125 boolean addComments= settings.createComments; 126 127 String [] empty= new String [0]; 128 String lineDelim= TextUtilities.getDefaultLineDelimiter(document); 129 String declTypeName= fType.getTypeQualifiedName('.'); 130 boolean isInterface= fType.isInterface(); 131 132 StringBuffer buf= new StringBuffer (); 133 if (addComments) { 134 String comment= CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim); 135 if (comment != null) { 136 buf.append(comment); 137 buf.append(lineDelim); 138 } 139 } 140 if (fReturnTypeSig != null) { 141 if (!isInterface) { 142 buf.append("private "); } 144 } else { 145 buf.append("public "); } 147 148 if (fReturnTypeSig != null) { 149 buf.append(Signature.toString(fReturnTypeSig)); 150 } 151 buf.append(' '); 152 buf.append(fMethodName); 153 if (isInterface) { 154 buf.append("();"); buf.append(lineDelim); 156 } else { 157 buf.append("() {"); buf.append(lineDelim); 159 160 String body= CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", lineDelim); if (body != null) { 162 buf.append(body); 163 buf.append(lineDelim); 164 } 165 buf.append("}"); buf.append(lineDelim); 167 } 168 String stub= buf.toString(); 169 170 IRegion region= document.getLineInformationOfOffset(getReplacementOffset()); 172 int lineStart= region.getOffset(); 173 int indent= Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth); 174 175 String replacement= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, null, lineDelim, fType.getJavaProject()); 176 177 if (replacement.endsWith(lineDelim)) { 178 replacement= replacement.substring(0, replacement.length() - lineDelim.length()); 179 } 180 181 setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement)); 182 return true; 183 } 184 185 public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { 186 return new String (); } 188 189 192 public boolean isAutoInsertable() { 193 return false; 194 } 195 } 196 | Popular Tags |