From 46807db5b9101064b73f220b1c1d693d345add2a Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Wed, 6 Jul 2005 19:52:16 +0000 Subject: [PATCH] Bug 98881 - Added lots of goodies to the search completion to make it more usable. Put the void back in the DOM indexer/no params case. Really trying to remove duplicates more than anything which is 98765. --- .../tests/FunctionMethodPatternTests.java | 2 +- .../index/ctagsindexer/CTagsIndexAll.java | 10 +-- .../domsourceindexer/IndexVisitorUtil.java | 2 + .../contentassist/CCompletionProcessor2.java | 28 ++++--- .../contentassist/CCompletionProposal.java | 80 +++++++++++++------ .../CCompletionProposalComparator.java | 7 +- .../DOMCompletionContributor.java | 68 ++++++++++------ 7 files changed, 129 insertions(+), 68 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/search/org/eclipse/cdt/core/search/tests/FunctionMethodPatternTests.java b/core/org.eclipse.cdt.core.tests/search/org/eclipse/cdt/core/search/tests/FunctionMethodPatternTests.java index 160b078b23b..a2c683d8664 100644 --- a/core/org.eclipse.cdt.core.tests/search/org/eclipse/cdt/core/search/tests/FunctionMethodPatternTests.java +++ b/core/org.eclipse.cdt.core.tests/search/org/eclipse/cdt/core/search/tests/FunctionMethodPatternTests.java @@ -118,7 +118,7 @@ public class FunctionMethodPatternTests extends BaseSearchTest { search( workspace, pattern, scope, resultCollector ); matches = resultCollector.getSearchResults(); - assertEquals( 0, matches.size()); // The pattern must match the use of the void in the code + assertEquals( 2, matches.size()); pattern = SearchEngine.createSearchPattern( "turnAgain()", METHOD, DECLARATIONS, true ); //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ctagsindexer/CTagsIndexAll.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ctagsindexer/CTagsIndexAll.java index 87777f87e34..1252cf38d87 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ctagsindexer/CTagsIndexAll.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ctagsindexer/CTagsIndexAll.java @@ -237,16 +237,16 @@ class CTagsIndexAll extends CTagsIndexRequest { } Process p = launcher.execute(ctagsExecutable, args, null, directoryToRunFrom); //$NON-NLS-1$ + if (p == null) { + //CTags not installed + indexer.createProblemMarker(CCorePlugin.getResourceString("CTagsIndexMarker.CTagsMissing"), project); //$NON-NLS-1$ + return false; + } p.waitFor(); } catch (InterruptedException e) { return false; } - catch (NullPointerException e){ - //CTags not installed - indexer.createProblemMarker(CCorePlugin.getResourceString("CTagsIndexMarker.CTagsMissing"), project); //$NON-NLS-1$ - return false; - } return true; } diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java index 1e5948d1668..6d6dabc5cbc 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java @@ -221,6 +221,8 @@ public class IndexVisitorUtil { } if (function.takesVarArgs()) parameterList.add("...".toCharArray()); //$NON-NLS-1$ + else if (parameters.length == 0) + parameterList.add("void".toCharArray()); //$NON-NLS-1$ } catch (DOMException e) { } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java index 4de0ee555e6..009f87f491f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor2.java @@ -22,6 +22,7 @@ import org.eclipse.cdt.internal.ui.CUIMessages; import org.eclipse.cdt.internal.ui.text.CParameterListValidator; import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.text.ICCompletionProposal; import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -117,30 +118,39 @@ public class CCompletionProcessor2 implements IContentAssistProcessor { } } - ICompletionProposal[] propsArray = null; + ICCompletionProposal[] propsArray = null; if (!proposals.isEmpty()) { errorMessage = null; - propsArray = (ICompletionProposal[])proposals.toArray(new ICompletionProposal[proposals.size()]); + propsArray = (ICCompletionProposal[])proposals.toArray(new ICCompletionProposal[proposals.size()]); CCompletionProposalComparator propsComp = new CCompletionProposalComparator(); propsComp.setOrderAlphabetically(true); Arrays.sort(propsArray, propsComp); - // remove duplicates - ICompletionProposal last = propsArray[0]; + // remove duplicates but leave the ones with return types + + int last = 0; int removed = 0; for (int i = 1; i < propsArray.length; ++i) { - if (propsComp.compare(last, propsArray[i]) == 0) { - // Remove the duplicate - ++removed; + if (propsComp.compare(propsArray[last], propsArray[i]) == 0) { + // We want to leave the one that has the return string if any + boolean lastReturn = propsArray[last].getIdString() != propsArray[last].getDisplayString(); + boolean iReturn = propsArray[i].getIdString() != propsArray[i].getDisplayString(); + + if (!lastReturn && iReturn) + // flip i down to last + propsArray[last] = propsArray[i]; + + // Remove the duplicate propsArray[i] = null; + ++removed; } else // update last - last = propsArray[i]; + last = i; } if (removed > 0) { // Strip out the null entries - ICompletionProposal[] newArray = new ICompletionProposal[propsArray.length - removed]; + ICCompletionProposal[] newArray = new ICCompletionProposal[propsArray.length - removed]; int j = 0; for (int i = 0; i < propsArray.length; ++i) if (propsArray[i] != null) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposal.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposal.java index bd080669f09..09f3796792a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposal.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposal.java @@ -55,6 +55,7 @@ import org.eclipse.ui.texteditor.link.EditorLinkedModeUI; public class CCompletionProposal implements ICCompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3 { private String fDisplayString; + private String fIdString; private String fReplacementString; private int fReplacementOffset; private int fReplacementLength; @@ -81,39 +82,57 @@ public class CCompletionProposal implements ICCompletionProposal, ICompletionPro * If set to null, the replacement string will be taken as display string. */ public CCompletionProposal(String replacementString, int replacementOffset, int replacementLength, Image image, String displayString, int relevance) { - this(replacementString, replacementOffset, replacementLength, image, displayString, relevance, null); + this(replacementString, replacementOffset, replacementLength, image, displayString, null, relevance, null); } - /** - * Creates a new completion proposal. All fields are initialized based on the provided information. - * - * @param replacementString the actual string to be inserted into the document - * @param replacementOffset the offset of the text to be replaced - * @param replacementLength the length of the text to be replaced - * @param image the image to display for this proposal - * @param displayString the string to be displayed for the proposal - * @param viewer the text viewer for which this proposal is computed, may be null - * If set to null, the replacement string will be taken as display string. - */ - public CCompletionProposal(String replacementString, int replacementOffset, int replacementLength, Image image, String displayString, int relevance, ITextViewer viewer) { - Assert.isNotNull(replacementString); + /** + * Creates a new completion proposal. All fields are initialized based on the provided information. + * + * @param replacementString the actual string to be inserted into the document + * @param replacementOffset the offset of the text to be replaced + * @param replacementLength the length of the text to be replaced + * @param image the image to display for this proposal + * @param displayString the string to be displayed for the proposal + * @param viewer the text viewer for which this proposal is computed, may be null + * If set to null, the replacement string will be taken as display string. + */ + public CCompletionProposal(String replacementString, int replacementOffset, int replacementLength, Image image, String displayString, int relevance, ITextViewer viewer) { + this(replacementString, replacementOffset, replacementLength, image, displayString, null, relevance, viewer); + } + + /** + * Creates a new completion proposal. All fields are initialized based on the provided information. + * + * @param replacementString the actual string to be inserted into the document + * @param replacementOffset the offset of the text to be replaced + * @param replacementLength the length of the text to be replaced + * @param image the image to display for this proposal + * @param displayString the string to be displayed for the proposal + * @param idString the string to be uniquely identify this proposal + * @param viewer the text viewer for which this proposal is computed, may be null + * If set to null, the replacement string will be taken as display string. + */ + public CCompletionProposal(String replacementString, int replacementOffset, int replacementLength, Image image, String displayString, String idString, int relevance, ITextViewer viewer) { + Assert.isNotNull(replacementString); Assert.isTrue(replacementOffset >= 0); Assert.isTrue(replacementLength >= 0); - fReplacementString= replacementString; - fReplacementOffset= replacementOffset; - fReplacementLength= replacementLength; - fImage= image; - fDisplayString= displayString != null ? displayString : replacementString; - fRelevance= relevance; - fTextViewer= viewer; + fReplacementString = replacementString; + fReplacementOffset = replacementOffset; + fReplacementLength = replacementLength; + fImage = image; + fRelevance = relevance; + fTextViewer = viewer; + + fDisplayString = displayString != null ? displayString : replacementString; + fIdString = idString != null ? idString : displayString; - fCursorPosition= replacementString.length(); + fCursorPosition = replacementString.length(); - fContextInformation= null; - fContextInformationPosition= -1; - fTriggerCharacters= null; - fProposalInfo= null; + fContextInformation = null; + fContextInformationPosition = -1; + fTriggerCharacters = null; + fProposalInfo = null; } /** @@ -339,6 +358,15 @@ public class CCompletionProposal implements ICCompletionProposal, ICompletionPro return fDisplayString; } + /** + * This method is used by the comparator to compare proposals. It ignores the return type of a function. + * + * @return the string representing the display name without the return type (if any). + */ + public String getIdString() { + return fIdString; + } + /* * @see ICompletionProposal#getAdditionalProposalInfo() */ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposalComparator.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposalComparator.java index 2048cc17bdb..be7e69bbc83 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposalComparator.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProposalComparator.java @@ -36,13 +36,18 @@ public class CCompletionProposalComparator implements Comparator { public int compare(Object o1, Object o2) { ICCompletionProposal c1= (ICCompletionProposal) o1; ICCompletionProposal c2= (ICCompletionProposal) o2; + if (!fOrderAlphabetically) { int relevanceDif= c2.getRelevance() - c1.getRelevance(); if (relevanceDif != 0) { return relevanceDif; } } - return c1.getDisplayString().compareToIgnoreCase(c2.getDisplayString()); + + String id1 = c1.getIdString(); + String id2 = c2.getIdString(); + + return id1.compareToIgnoreCase(id2); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java index c0ab071579c..5ff561cf44f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/DOMCompletionContributor.java @@ -113,28 +113,35 @@ public class DOMCompletionContributor implements ICompletionContributor { repStringBuff.append(function.getName()); repStringBuff.append('('); - StringBuffer args = new StringBuffer(); + StringBuffer dispargs = new StringBuffer(); // for the displayString + StringBuffer idargs = new StringBuffer(); // for the idString String returnTypeStr = null; try { IParameter[] params = function.getParameters(); if (params != null) for (int i = 0; i < params.length; ++i) { IType paramType = params[i].getType(); - if (i > 0) - args.append(','); - - args.append(ASTTypeUtil.getType(paramType)); + if (i > 0) { + dispargs.append(','); + idargs.append(','); + } + + dispargs.append(ASTTypeUtil.getType(paramType)); + idargs.append(ASTTypeUtil.getType(paramType)); String paramName = params[i].getName(); if (paramName != null && paramName.length() > 0) { - args.append(' '); - args.append(paramName); + dispargs.append(' '); + dispargs.append(paramName); } } if (function.takesVarArgs()) { - if (args.length() > 0) - args.append(','); - args.append(" ..."); //$NON-NLS-1$ + if (params.length > 0) { + dispargs.append(','); + idargs.append(','); + } + dispargs.append(" ..."); //$NON-NLS-1$ + idargs.append(" ..."); //$NON-NLS-1$ } IType returnType = function.getType().getReturnType(); @@ -142,26 +149,35 @@ public class DOMCompletionContributor implements ICompletionContributor { returnTypeStr = ASTTypeUtil.getType(returnType); } catch (DOMException e) { } - String argString = args.toString(); + + String dispargString = dispargs.toString(); + String idargString = idargs.toString(); - StringBuffer descStringBuff = new StringBuffer(repStringBuff.toString()); - descStringBuff.append(argString); - descStringBuff.append(')'); + StringBuffer dispStringBuff = new StringBuffer(repStringBuff.toString()); + dispStringBuff.append(dispargString); + dispStringBuff.append(')'); + if (returnTypeStr != null) { + dispStringBuff.append(' '); + dispStringBuff.append(returnTypeStr); + } + String dispString = dispStringBuff.toString(); + + StringBuffer idStringBuff = new StringBuffer(repStringBuff.toString()); + idStringBuff.append(idargString); + idStringBuff.append(')'); + String idString = idStringBuff.toString(); - if (returnTypeStr != null) { - descStringBuff.append(' '); - descStringBuff.append(returnTypeStr); - } - - repStringBuff.append(')'); - String repString = repStringBuff.toString(); - String descString = descStringBuff.toString(); - - CCompletionProposal proposal = createProposal(repString, descString, image, completionNode, offset, viewer); + repStringBuff.append(')'); + String repString = repStringBuff.toString(); + + int repLength = completionNode.getLength(); + int repOffset = offset - repLength; + CCompletionProposal proposal = new CCompletionProposal(repString, repOffset, repLength, image, dispString, idString, 1, viewer); + proposal.setCursorPosition(repString.length() - 1); - if (argString.length() > 0) { - CProposalContextInformation info = new CProposalContextInformation(repString, argString); + if (dispargString.length() > 0) { + CProposalContextInformation info = new CProposalContextInformation(repString, dispargString); info.setContextInformationPosition(offset); proposal.setContextInformation(info); }