1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import org.eclipse.jface.preference.IPreferenceStore; 14 15 import org.eclipse.jface.text.IDocument; 16 import org.eclipse.jface.text.contentassist.IContextInformation; 17 18 import org.eclipse.jdt.core.CompletionProposal; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.Signature; 21 22 import org.eclipse.jdt.ui.PreferenceConstants; 23 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext; 24 25 import org.eclipse.jdt.internal.ui.JavaPlugin; 26 27 28 public class JavaMethodCompletionProposal extends LazyJavaCompletionProposal { 29 30 protected final static char[] METHOD_TRIGGERS= new char[] { ';', ',', '.', '\t', '[' }; 31 32 protected final static char[] METHOD_WITH_ARGUMENTS_TRIGGERS= new char[] { '(', '-', ' ' }; 33 34 protected final static char[] METHOD_NAME_TRIGGERS= new char[] { ';' }; 35 36 private boolean fHasParameters; 37 private boolean fHasParametersComputed= false; 38 private FormatterPrefs fFormatterPrefs; 39 40 public JavaMethodCompletionProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context) { 41 super(proposal, context); 42 } 43 44 public void apply(IDocument document, char trigger, int offset) { 45 if (trigger == ' ' || trigger == '(') 46 trigger= '\0'; 47 super.apply(document, trigger, offset); 48 if (needsLinkedMode()) { 49 setUpLinkedMode(document, ')'); 50 } 51 } 52 53 protected boolean needsLinkedMode() { 54 return hasArgumentList() && hasParameters(); 55 } 56 57 public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { 58 if (hasArgumentList()) { 59 String completion= String.valueOf(fProposal.getName()); 60 if (isCamelCaseMatching()) { 61 String prefix= getPrefix(document, completionOffset); 62 return getCamelCaseCompound(prefix, completion); 63 } 64 65 return completion; 66 } 67 return super.getPrefixCompletionText(document, completionOffset); 68 } 69 70 protected IContextInformation computeContextInformation() { 71 if (fProposal.getKind() == CompletionProposal.METHOD_REF && hasParameters() && (getReplacementString().endsWith(RPAREN) || getReplacementString().length() == 0)) { 74 ProposalContextInformation contextInformation= new ProposalContextInformation(fProposal); 75 if (fContextInformationPosition != 0 && fProposal.getCompletion().length == 0) 76 contextInformation.setContextInformationPosition(fContextInformationPosition); 77 return contextInformation; 78 } 79 return super.computeContextInformation(); 80 } 81 82 protected char[] computeTriggerCharacters() { 83 if (fProposal.getKind() == CompletionProposal.METHOD_NAME_REFERENCE) 84 return METHOD_NAME_TRIGGERS; 85 if (hasParameters()) 86 return METHOD_WITH_ARGUMENTS_TRIGGERS; 87 return METHOD_TRIGGERS; 88 } 89 90 99 protected final boolean hasParameters() { 100 if (!fHasParametersComputed) { 101 fHasParametersComputed= true; 102 fHasParameters= computeHasParameters(); 103 } 104 return fHasParameters; 105 } 106 107 private boolean computeHasParameters() throws IllegalArgumentException { 108 return Signature.getParameterCount(fProposal.getSignature()) > 0; 109 } 110 111 118 protected boolean hasArgumentList() { 119 if (CompletionProposal.METHOD_NAME_REFERENCE == fProposal.getKind()) 120 return false; 121 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 122 boolean noOverwrite= preferenceStore.getBoolean(PreferenceConstants.CODEASSIST_INSERT_COMPLETION) ^ isToggleEating(); 123 char[] completion= fProposal.getCompletion(); 124 return !isInJavadoc() && completion.length > 0 && (noOverwrite || completion[completion.length - 1] == ')'); 125 } 126 127 132 protected final FormatterPrefs getFormatterPrefs() { 133 if (fFormatterPrefs == null) 134 fFormatterPrefs= new FormatterPrefs(fInvocationContext.getProject()); 135 return fFormatterPrefs; 136 } 137 138 141 protected String computeReplacementString() { 142 if (!hasArgumentList()) 143 return super.computeReplacementString(); 144 145 StringBuffer buffer= new StringBuffer (); 147 buffer.append(fProposal.getName()); 148 149 FormatterPrefs prefs= getFormatterPrefs(); 150 if (prefs.beforeOpeningParen) 151 buffer.append(SPACE); 152 buffer.append(LPAREN); 153 154 if (hasParameters()) { 155 setCursorPosition(buffer.length()); 156 157 if (prefs.afterOpeningParen) 158 buffer.append(SPACE); 159 160 161 } else { 165 if (prefs.inEmptyList) 166 buffer.append(SPACE); 167 } 168 169 buffer.append(RPAREN); 170 171 return buffer.toString(); 172 173 } 174 175 protected ProposalInfo computeProposalInfo() { 176 IJavaProject project= fInvocationContext.getProject(); 177 if (project != null) 178 return new MethodProposalInfo(project, fProposal); 179 return super.computeProposalInfo(); 180 } 181 182 185 protected String computeSortString() { 186 193 char[] name= fProposal.getName(); 194 char[] parameterList= Signature.toCharArray(fProposal.getSignature(), null, null, false, false); 195 int parameterCount= Signature.getParameterCount(fProposal.getSignature()) % 10; StringBuffer buf= new StringBuffer (name.length + 2 + parameterList.length); 197 198 buf.append(name); 199 buf.append('\0'); buf.append(parameterCount); 201 buf.append(parameterList); 202 return buf.toString(); 203 } 204 205 208 protected boolean isValidPrefix(String prefix) { 209 if (super.isValidPrefix(prefix)) 210 return true; 211 212 String word= getDisplayString(); 213 if (isInJavadoc()) { 214 int idx = word.indexOf("{@link "); if (idx==0) { 216 word = word.substring(7); 217 } else { 218 idx = word.indexOf("{@value "); if (idx==0) { 220 word = word.substring(8); 221 } 222 } 223 } 224 return isPrefix(prefix, word); 225 } 226 } 227 | Popular Tags |