1 package net.sourceforge.phpdt.internal.ui.text.java;
4 * (c) Copyright IBM Corp. 2000, 2001.
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Hashtable;
13 import java.util.List;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.jface.dialogs.ErrorDialog;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.contentassist.ICompletionProposal;
23 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
24 import org.eclipse.jface.text.contentassist.IContextInformation;
25 import org.eclipse.jface.text.contentassist.IContextInformationExtension;
26 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
28 import org.eclipse.ui.IEditorPart;
30 //import org.eclipse.jdt.core.ICompilationUnit;
31 //import org.eclipse.jdt.core.JavaCore;
32 //import org.eclipse.jdt.core.JavaModelException;
34 //import org.eclipse.jdt.ui.IWorkingCopyManager;
35 //import org.eclipse.jdt.ui.PreferenceConstants;
37 import net.sourceforge.phpdt.internal.corext.template.ContextType;
38 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
39 //import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import net.sourceforge.phpdt.internal.ui.JavaUIMessages;
41 //import org.eclipse.jdt.internal.ui.text.JavaCodeReader;
42 import net.sourceforge.phpdt.internal.ui.text.template.TemplateEngine;
46 * Java completion processor.
48 public class JavaCompletionProcessor_NotInUseVersion implements IContentAssistProcessor {
50 private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
52 private final IContextInformation fContextInformation;
53 private int fPosition;
55 public ContextInformationWrapper(IContextInformation contextInformation) {
56 fContextInformation= contextInformation;
60 * @see IContextInformation#getContextDisplayString()
62 public String getContextDisplayString() {
63 return fContextInformation.getContextDisplayString();
67 * @see IContextInformation#getImage()
69 public Image getImage() {
70 return fContextInformation.getImage();
74 * @see IContextInformation#getInformationDisplayString()
76 public String getInformationDisplayString() {
77 return fContextInformation.getInformationDisplayString();
81 * @see IContextInformationExtension#getContextInformationPosition()
83 public int getContextInformationPosition() {
87 public void setContextInformationPosition(int position) {
93 // private final static String VISIBILITY= JavaCore.CODEASSIST_VISIBILITY_CHECK;
94 private final static String ENABLED= "enabled"; //$NON-NLS-1$
95 private final static String DISABLED= "disabled"; //$NON-NLS-1$
99 private IEditorPart fEditor;
100 // private ResultCollector fCollector;
101 // private IWorkingCopyManager fManager;
102 private IContextInformationValidator fValidator;
104 private char[] fProposalAutoActivationSet;
105 private JavaCompletionProposalComparator fComparator;
106 private boolean fAllowAddImports;
108 private TemplateEngine fTemplateEngine;
109 // private ExperimentalResultCollector fExperimentalCollector;
111 private int fNumberOfComputedResults= 0;
114 public JavaCompletionProcessor_NotInUseVersion(IEditorPart editor) {
116 // fCollector= new ResultCollector();
117 // fManager= JavaPlugin.getDefault().getWorkingCopyManager();
118 ContextType contextType= ContextTypeRegistry.getInstance().getContextType("php"); //$NON-NLS-1$
119 if (contextType != null)
120 fTemplateEngine= new TemplateEngine(contextType);
121 // fExperimentalCollector= new ExperimentalResultCollector();
122 fAllowAddImports= true;
124 fComparator= new JavaCompletionProposalComparator();
128 * Sets this processor's set of characters triggering the activation of the
129 * completion proposal computation.
131 * @param activationSet the activation set
133 public void setCompletionProposalAutoActivationCharacters(char[] activationSet) {
134 fProposalAutoActivationSet= activationSet;
138 * Tells this processor to restrict its proposal to those element
139 * visible in the actual invocation context.
141 * @param restrict <code>true</code> if proposals should be restricted
143 public void restrictProposalsToVisibility(boolean restrict) {
144 // Hashtable options= JavaCore.getOptions();
145 // Object value= options.get(VISIBILITY);
146 // if (value instanceof String) {
147 // String newValue= restrict ? ENABLED : DISABLED;
148 // if ( !newValue.equals((String) value)) {
149 // options.put(VISIBILITY, newValue);
150 // JavaCore.setOptions(options);
156 * Tells this processor to order the proposals alphabetically.
158 * @param order <code>true</code> if proposals should be ordered.
160 public void orderProposalsAlphabetically(boolean order) {
161 fComparator.setOrderAlphabetically(order);
165 * Tells this processor to restrict is proposals to those
166 * starting with matching cases.
168 * @param restrict <code>true</code> if proposals should be restricted
170 public void restrictProposalsToMatchingCases(boolean restrict) {
175 * Tells this processor to add import statement for proposals that have
176 * a fully qualified type name
178 * @param restrict <code>true</code> if import can be added
180 public void allowAddingImports(boolean allowAddingImports) {
181 fAllowAddImports= allowAddingImports;
185 * @see IContentAssistProcessor#getErrorMessage()
187 public String getErrorMessage() {
188 // if (fNumberOfComputedResults == 0)
189 return JavaUIMessages.getString("JavaEditor.codeassist.noCompletions"); //$NON-NLS-1$
190 // return fCollector.getErrorMessage();
194 * @see IContentAssistProcessor#getContextInformationValidator()
196 public IContextInformationValidator getContextInformationValidator() {
197 // if (fValidator == null)
198 // fValidator= new JavaParameterListValidator();
203 * @see IContentAssistProcessor#getContextInformationAutoActivationCharacters()
205 public char[] getContextInformationAutoActivationCharacters() {
210 * @see IContentAssistProcessor#getCompletionProposalAutoActivationCharacters()
212 public char[] getCompletionProposalAutoActivationCharacters() {
213 return fProposalAutoActivationSet;
216 // private boolean looksLikeMethod(JavaCodeReader reader) throws IOException {
217 // int curr= reader.read();
218 // while (curr != JavaCodeReader.EOF && Character.isWhitespace((char) curr))
219 // curr= reader.read();
221 // if (curr == JavaCodeReader.EOF)
224 // return Character.isJavaIdentifierPart((char) curr) || Character.isJavaIdentifierStart((char) curr);
227 private int guessContextInformationPosition(ITextViewer viewer, int offset) {
228 int contextPosition= offset;
230 IDocument document= viewer.getDocument();
234 // JavaCodeReader reader= new JavaCodeReader();
235 // reader.configureBackwardReader(document, offset, true, true);
237 // int nestingLevel= 0;
239 // int curr= reader.read();
240 // while (curr != JavaCodeReader.EOF) {
242 // if (')' == (char) curr)
245 // else if ('(' == (char) curr) {
248 // if (nestingLevel < 0) {
249 // int start= reader.getOffset();
250 // if (looksLikeMethod(reader))
255 // curr= reader.read();
257 // } catch (IOException e) {
260 return contextPosition;
263 private List addContextInformations(ITextViewer viewer, int offset) {
264 ICompletionProposal[] proposals= internalComputeCompletionProposals(viewer, offset, -1);
266 List result= new ArrayList();
267 for (int i= 0; i < proposals.length; i++) {
268 IContextInformation contextInformation= proposals[i].getContextInformation();
269 if (contextInformation != null) {
270 ContextInformationWrapper wrapper= new ContextInformationWrapper(contextInformation);
271 wrapper.setContextInformationPosition(offset);
279 * @see IContentAssistProcessor#computeContextInformation(ITextViewer, int)
281 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
282 int contextInformationPosition= guessContextInformationPosition(viewer, offset);
283 List result= addContextInformations(viewer, contextInformationPosition);
284 return (IContextInformation[]) result.toArray(new IContextInformation[result.size()]);
288 * Order the given proposals.
290 private ICompletionProposal[] order(ICompletionProposal[] proposals) {
291 Arrays.sort(proposals, fComparator);
296 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
298 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
299 int contextInformationPosition= guessContextInformationPosition(viewer, offset);
300 return internalComputeCompletionProposals(viewer, offset, contextInformationPosition);
303 private ICompletionProposal[] internalComputeCompletionProposals(ITextViewer viewer, int offset, int contextOffset) {
305 // ICompilationUnit unit= fManager.getWorkingCopy(fEditor.getEditorInput());
306 // IJavaCompletionProposal[] results;
308 // if (JavaPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES)) {
311 // if (unit != null) {
313 // fExperimentalCollector.reset(offset, contextOffset, unit.getJavaProject(), fAllowAddImports ? unit : null);
314 // fExperimentalCollector.setViewer(viewer);
316 // Point selection= viewer.getSelectedRange();
317 // if (selection.y > 0)
318 // fExperimentalCollector.setReplacementLength(selection.y);
320 // unit.codeComplete(offset, fExperimentalCollector);
322 // } catch (JavaModelException x) {
323 // Shell shell= viewer.getTextWidget().getShell();
324 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
327 // results= fExperimentalCollector.getResults();
332 // if (unit != null) {
334 // fCollector.reset(offset, contextOffset, unit.getJavaProject(), fAllowAddImports ? unit : null);
335 // Point selection= viewer.getSelectedRange();
336 // if (selection.y > 0)
337 // fCollector.setReplacementLength(selection.y);
339 // unit.codeComplete(offset, fCollector);
341 // } catch (JavaModelException x) {
342 // Shell shell= viewer.getTextWidget().getShell();
343 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
346 // results= fCollector.getResults();
349 if (fTemplateEngine != null) {
350 IJavaCompletionProposal[] results;
352 fTemplateEngine.reset();
353 fTemplateEngine.complete(viewer, offset); //, unit);
354 // } catch (JavaModelException x) {
355 // Shell shell= viewer.getTextWidget().getShell();
356 // ErrorDialog.openError(shell, JavaTextMessages.getString("CompletionProcessor.error.accessing.title"), JavaTextMessages.getString("CompletionProcessor.error.accessing.message"), x.getStatus()); //$NON-NLS-2$ //$NON-NLS-1$
359 IJavaCompletionProposal[] templateResults= fTemplateEngine.getResults();
361 // concatenate arrays
362 IJavaCompletionProposal[] total= new IJavaCompletionProposal[templateResults.length]; // +results.length ];
363 System.arraycopy(templateResults, 0, total, 0, templateResults.length);
364 // System.arraycopy(results, 0, total, templateResults.length, results.length);
368 fNumberOfComputedResults= (results == null ? 0 : results.length);
371 * Order here and not in result collector to make sure that the order
372 * applies to all proposals and not just those of the compilation unit.
374 return order(results);
376 return new IJavaCompletionProposal[0];