mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
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.
This commit is contained in:
parent
6de6572a5a
commit
028d21cfd8
2 changed files with 67 additions and 19 deletions
|
@ -10,6 +10,8 @@ package org.eclipse.cdt.internal.ui.text.contentassist;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
|
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.model.IWorkingCopy;
|
||||||
import org.eclipse.cdt.core.parser.Keywords;
|
import org.eclipse.cdt.core.parser.Keywords;
|
||||||
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
|
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
|
||||||
|
@ -25,29 +27,56 @@ public class KeywordCompletionContributor implements ICompletionContributor {
|
||||||
IWorkingCopy workingCopy, ASTCompletionNode completionNode,
|
IWorkingCopy workingCopy, ASTCompletionNode completionNode,
|
||||||
String prefix, List proposals) {
|
String prefix, List proposals) {
|
||||||
|
|
||||||
|
// No prefix, no completions
|
||||||
|
if (prefix.length() == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!validContext(completionNode))
|
||||||
|
return;
|
||||||
|
|
||||||
String[] keywords = cppkeywords; // default to C++
|
String[] keywords = cppkeywords; // default to C++
|
||||||
if (workingCopy != null && workingCopy.isCLanguage())
|
if (workingCopy != null && workingCopy.isCLanguage())
|
||||||
keywords = ckeywords;
|
keywords = ckeywords;
|
||||||
|
|
||||||
if (prefix.length() > 0)
|
if (prefix.length() > 0)
|
||||||
for (int i = 0; i < keywords.length; ++i)
|
for (int i = 0; i < keywords.length; ++i)
|
||||||
if (keywords[i].startsWith(prefix))
|
if (keywords[i].startsWith(prefix)) {
|
||||||
handleKeyword(keywords[i], completionNode, offset, viewer, proposals);
|
ImageDescriptor imagedesc = CElementImageProvider.getKeywordImageDescriptor();
|
||||||
}
|
Image image = imagedesc != null ? CUIPlugin.getImageDescriptorRegistry().get(imagedesc) : null;
|
||||||
|
int repLength = prefix.length();
|
||||||
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;
|
int repOffset = offset - repLength;
|
||||||
return new CCompletionProposal(repString, repOffset, repLength, image, dispString, 1, viewer);
|
proposals.add(new CCompletionProposal(keywords[i], repOffset, repLength, image, keywords[i], 1, viewer));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Image getImage(ImageDescriptor desc) {
|
// TODO This is copied from the search completion contributor
|
||||||
return desc != null ? CUIPlugin.getImageDescriptorRegistry().get(desc) : null;
|
// 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
|
// These are the keywords we complete
|
||||||
|
|
|
@ -21,10 +21,11 @@ import org.eclipse.cdt.core.model.ICElement;
|
||||||
import org.eclipse.cdt.core.model.IWorkingCopy;
|
import org.eclipse.cdt.core.model.IWorkingCopy;
|
||||||
import org.eclipse.cdt.core.search.BasicSearchResultCollector;
|
import org.eclipse.cdt.core.search.BasicSearchResultCollector;
|
||||||
import org.eclipse.cdt.core.search.ICSearchConstants;
|
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.ICSearchScope;
|
||||||
import org.eclipse.cdt.core.search.IMatch;
|
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.SearchEngine;
|
||||||
|
import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor;
|
||||||
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
|
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor;
|
import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor;
|
||||||
|
@ -33,6 +34,20 @@ import org.eclipse.swt.graphics.Image;
|
||||||
|
|
||||||
public class SearchCompletionContributor implements ICompletionContributor {
|
public class SearchCompletionContributor implements ICompletionContributor {
|
||||||
|
|
||||||
|
// 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,
|
public void contributeCompletionProposals(ITextViewer viewer, int offset,
|
||||||
IWorkingCopy workingCopy, ASTCompletionNode completionNode, String prefix,
|
IWorkingCopy workingCopy, ASTCompletionNode completionNode, String prefix,
|
||||||
List proposals)
|
List proposals)
|
||||||
|
@ -56,7 +71,11 @@ public class SearchCompletionContributor implements ICompletionContributor {
|
||||||
scope = SearchEngine.createWorkspaceScope();
|
scope = SearchEngine.createWorkspaceScope();
|
||||||
|
|
||||||
// Create the pattern
|
// 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
|
// Run the search
|
||||||
BasicSearchResultCollector collector = new BasicSearchResultCollector();
|
BasicSearchResultCollector collector = new BasicSearchResultCollector();
|
||||||
|
|
Loading…
Add table
Reference in a new issue