KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > JavaMethodCompletionProposal


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     /** Triggers for method proposals without parameters. Do not modify. */
30     protected final static char[] METHOD_TRIGGERS= new char[] { ';', ',', '.', '\t', '[' };
31     /** Triggers for method proposals. Do not modify. */
32     protected final static char[] METHOD_WITH_ARGUMENTS_TRIGGERS= new char[] { '(', '-', ' ' };
33     /** Triggers for method name proposals (static imports). Do not modify. */
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 JavaDoc getPrefixCompletionText(IDocument document, int completionOffset) {
58         if (hasArgumentList()) {
59             String JavaDoc completion= String.valueOf(fProposal.getName());
60             if (isCamelCaseMatching()) {
61                 String JavaDoc 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         // no context information for METHOD_NAME_REF proposals (e.g. for static imports)
72
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=94654
73
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     /**
91      * Returns <code>true</code> if the method being inserted has at least one parameter. Note
92      * that this does not say anything about whether the argument list should be inserted. This
93      * depends on the position in the document and the kind of proposal; see
94      * {@link #hasArgumentList() }.
95      *
96      * @return <code>true</code> if the method has any parameters, <code>false</code> if it has
97      * no parameters
98      */

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 JavaDoc {
108         return Signature.getParameterCount(fProposal.getSignature()) > 0;
109     }
110
111     /**
112      * Returns <code>true</code> if the argument list should be inserted by the proposal,
113      * <code>false</code> if not.
114      *
115      * @return <code>true</code> when the proposal is not in javadoc nor within an import and comprises the
116      * parameter list
117      */

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     /**
128      * Returns the method formatter preferences.
129      *
130      * @return the formatter settings
131      */

132     protected final FormatterPrefs getFormatterPrefs() {
133         if (fFormatterPrefs == null)
134             fFormatterPrefs= new FormatterPrefs(fInvocationContext.getProject());
135         return fFormatterPrefs;
136     }
137     
138     /*
139      * @see org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal#computeReplacementString()
140      */

141     protected String JavaDoc computeReplacementString() {
142         if (!hasArgumentList())
143             return super.computeReplacementString();
144         
145         // we're inserting a method plus the argument list - respect formatter preferences
146
StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
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             // don't add the trailing space, but let the user type it in himself - typing the closing paren will exit
162
// if (prefs.beforeClosingParen)
163
// buffer.append(SPACE);
164
} 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     /*
183      * @see org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal#computeSortString()
184      */

185     protected String JavaDoc computeSortString() {
186         /*
187          * Lexicographical sort order:
188          * 1) by relevance (done by the proposal sorter)
189          * 2) by method name
190          * 3) by parameter count
191          * 4) by parameter type names
192          */

193         char[] name= fProposal.getName();
194         char[] parameterList= Signature.toCharArray(fProposal.getSignature(), null, null, false, false);
195         int parameterCount= Signature.getParameterCount(fProposal.getSignature()) % 10; // we don't care about insane methods with >9 parameters
196
StringBuffer JavaDoc buf= new StringBuffer JavaDoc(name.length + 2 + parameterList.length);
197         
198         buf.append(name);
199         buf.append('\0'); // separator
200
buf.append(parameterCount);
201         buf.append(parameterList);
202         return buf.toString();
203     }
204     
205     /*
206      * @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#isValidPrefix(java.lang.String)
207      */

208     protected boolean isValidPrefix(String JavaDoc prefix) {
209         if (super.isValidPrefix(prefix))
210             return true;
211         
212         String JavaDoc word= getDisplayString();
213         if (isInJavadoc()) {
214             int idx = word.indexOf("{@link "); //$NON-NLS-1$
215
if (idx==0) {
216                 word = word.substring(7);
217             } else {
218                 idx = word.indexOf("{@value "); //$NON-NLS-1$
219
if (idx==0) {
220                     word = word.substring(8);
221                 }
222             }
223         }
224         return isPrefix(prefix, word);
225     }
226 }
227
Popular Tags