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 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 = {
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue