From 028d21cfd8913cbf703da509cac4dc10e1702440 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Tue, 19 Jul 2005 17:34:38 +0000 Subject: [PATCH] Bug 103857 - Limited the search to exclude fields and methods since they usually have a more limited scope (really only want global only). Also fixed up the keyword contributor to work when there is no completion node. --- .../KeywordCompletionContributor.java | 61 ++++++++++++++----- .../SearchCompletionContributor.java | 25 +++++++- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java index d8523cb03b9..4a11302299d 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/KeywordCompletionContributor.java @@ -10,6 +10,8 @@ package org.eclipse.cdt.internal.ui.text.contentassist; import java.util.List; import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; +import org.eclipse.cdt.core.dom.ast.IASTFieldReference; +import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.parser.Keywords; import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; @@ -25,31 +27,58 @@ public class KeywordCompletionContributor implements ICompletionContributor { IWorkingCopy workingCopy, ASTCompletionNode completionNode, String prefix, List proposals) { + // No prefix, no completions + if (prefix.length() == 0) + return; + + if (!validContext(completionNode)) + return; + String[] keywords = cppkeywords; // default to C++ if (workingCopy != null && workingCopy.isCLanguage()) keywords = ckeywords; if (prefix.length() > 0) for (int i = 0; i < keywords.length; ++i) - if (keywords[i].startsWith(prefix)) - handleKeyword(keywords[i], completionNode, offset, viewer, proposals); - } - - private void handleKeyword(String keyword, ASTCompletionNode completionNode, int offset, ITextViewer viewer, List proposals) { - Image image = getImage(CElementImageProvider.getKeywordImageDescriptor()); - proposals.add(createProposal(keyword, keyword, image, completionNode, offset, viewer)); - } - - private CCompletionProposal createProposal(String repString, String dispString, Image image, ASTCompletionNode completionNode, int offset, ITextViewer viewer) { - int repLength = completionNode.getLength(); - int repOffset = offset - repLength; - return new CCompletionProposal(repString, repOffset, repLength, image, dispString, 1, viewer); + if (keywords[i].startsWith(prefix)) { + ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor(); + Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null; + int repLength = prefix.length(); + int repOffset = offset - repLength; + proposals.add(new CCompletionProposal(keywords[i], repOffset, repLength, image, keywords[i], 1, viewer)); + } } - private Image getImage(ImageDescriptor desc) { - return desc != null ? CUIPlugin.getImageDescriptorRegistry().get(desc) : null; + // TODO This is copied from the search completion contributor + // We should make this common + private boolean validContext(ASTCompletionNode completionNode) { + if (completionNode == null) + // No completion node, assume true + return true; + + boolean valid = true; + IASTName[] names = completionNode.getNames(); + for (int i = 0; i < names.length; i++) { + IASTName name = names[i]; + + // not hooked up, not a valid name, ignore + if (name.getTranslationUnit() == null) + continue; + + // member access currently isn't valid + if (name.getParent() instanceof IASTFieldReference) { + valid = false; + continue; + } + + // found one that was valid + return true; + } + + // Couldn't find a valid context + return valid; } - + // These are the keywords we complete // We only do the ones that are >= 5 characters long private static String [] ckeywords = { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java index 03603c17b07..0274743e4ce 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java @@ -21,10 +21,11 @@ import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.search.BasicSearchResultCollector; import org.eclipse.cdt.core.search.ICSearchConstants; -import org.eclipse.cdt.core.search.ICSearchPattern; import org.eclipse.cdt.core.search.ICSearchScope; import org.eclipse.cdt.core.search.IMatch; +import org.eclipse.cdt.core.search.OrPattern; import org.eclipse.cdt.core.search.SearchEngine; +import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor; import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; @@ -33,7 +34,21 @@ import org.eclipse.swt.graphics.Image; public class SearchCompletionContributor implements ICompletionContributor { - public void contributeCompletionProposals(ITextViewer viewer, int offset, + // The completion search for list + // Kind of like the All Elements but excluding METHODS and FIELDS + private static SearchFor[] completionSearchFor = { + ICSearchConstants.CLASS_STRUCT, + ICSearchConstants.FUNCTION, + ICSearchConstants.VAR, + ICSearchConstants.UNION, + ICSearchConstants.ENUM, + ICSearchConstants.ENUMTOR, + ICSearchConstants.NAMESPACE, + ICSearchConstants.TYPEDEF, + ICSearchConstants.MACRO + }; + + public void contributeCompletionProposals(ITextViewer viewer, int offset, IWorkingCopy workingCopy, ASTCompletionNode completionNode, String prefix, List proposals) { @@ -56,7 +71,11 @@ public class SearchCompletionContributor implements ICompletionContributor { scope = SearchEngine.createWorkspaceScope(); // Create the pattern - ICSearchPattern pattern = SearchEngine.createSearchPattern(prefix + "*", ICSearchConstants.UNKNOWN_SEARCH_FOR, ICSearchConstants.ALL_OCCURRENCES, true); //$NON-NLS-1$ + String patternString = prefix + "*"; //$NON-NLS-1$ + OrPattern pattern = new OrPattern(); + for (int i = 0; i < completionSearchFor.length; i++) + pattern.addPattern( SearchEngine.createSearchPattern( patternString, + completionSearchFor[i], ICSearchConstants.ALL_OCCURRENCES, true)); // Run the search BasicSearchResultCollector collector = new BasicSearchResultCollector();