1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 88796 - Refactored keyword completion into separate contributor. Added some of the missing keywords and made the keywords language specific for c versus c++.

This commit is contained in:
Doug Schaefer 2005-07-19 15:40:28 +00:00
parent dc22806259
commit 6de6572a5a
3 changed files with 136 additions and 52 deletions

View file

@ -1378,6 +1378,10 @@
class="org.eclipse.cdt.internal.ui.text.contentassist.SearchCompletionContributor"
id="Search"
priority="3"/>
<contributor
class="org.eclipse.cdt.internal.ui.text.contentassist.KeywordCompletionContributor"
id="Keywords"
priority="10"/>
<contributor
class="org.eclipse.cdt.internal.ui.text.contentassist.HelpCompletionContributor"
id="Help"

View file

@ -90,13 +90,7 @@ public class DOMCompletionContributor implements ICompletionContributor {
if (macros[i].getName().toString().startsWith(prefix))
handleMacro(macros[i], completionNode, offset, viewer, proposals);
}
// Check for the keywords
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 handleBinding(IBinding binding, ASTCompletionNode completionNode, int offset, ITextViewer viewer, List proposals) {
@ -232,13 +226,6 @@ public class DOMCompletionContributor implements ICompletionContributor {
proposals.add(createProposal(macroName, macroName, image, completionNode, offset, viewer));
}
private void handleKeyword(String keyword, ASTCompletionNode completionNode, int offset, ITextViewer viewer, List proposals) {
// TODO we should really check the context to make sure
// it is valid for the keyword to appear here
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;
@ -297,42 +284,4 @@ public class DOMCompletionContributor implements ICompletionContributor {
: null;
}
// These are the keywords we complete
// We only do the ones that are > 5 characters long
private static String [] keywords = {
"const_cast", //$NON-NLS-1$
"continue", //$NON-NLS-1$
"default", //$NON-NLS-1$
"delete", //$NON-NLS-1$
"double", //$NON-NLS-1$
"dynamic_cast", //$NON-NLS-1$
"explicit", //$NON-NLS-1$
"export", //$NON-NLS-1$
"extern", //$NON-NLS-1$
"friend", //$NON-NLS-1$
"inline", //$NON-NLS-1$
"mutable", //$NON-NLS-1$
"namespace", //$NON-NLS-1$
"operator", //$NON-NLS-1$
"private", //$NON-NLS-1$
"protected", //$NON-NLS-1$
"register", //$NON-NLS-1$
"reinterpret_cast", //$NON-NLS-1$
"return", //$NON-NLS-1$
"signed", //$NON-NLS-1$
"sizeof", //$NON-NLS-1$
"static", //$NON-NLS-1$
"static_cast", //$NON-NLS-1$
"struct", //$NON-NLS-1$
"switch", //$NON-NLS-1$
"template", //$NON-NLS-1$
"typedef", //$NON-NLS-1$
"typeid", //$NON-NLS-1$
"typename", //$NON-NLS-1$
"unsigned", //$NON-NLS-1$
"virtual", //$NON-NLS-1$
"volatile", //$NON-NLS-1$
"wchar_t" //$NON-NLS-1$
};
}

View file

@ -0,0 +1,131 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
**********************************************************************/
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.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.swt.graphics.Image;
public class KeywordCompletionContributor implements ICompletionContributor {
public void contributeCompletionProposals(ITextViewer viewer, int offset,
IWorkingCopy workingCopy, ASTCompletionNode completionNode,
String prefix, List proposals) {
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);
}
private Image getImage(ImageDescriptor desc) {
return desc != null ? CUIPlugin.getImageDescriptorRegistry().get(desc) : null;
}
// These are the keywords we complete
// We only do the ones that are >= 5 characters long
private static String [] ckeywords = {
Keywords.BREAK,
Keywords.CONST,
Keywords.CONTINUE,
Keywords.DEFAULT,
Keywords.DOUBLE,
Keywords.EXTERN,
Keywords.FLOAT,
Keywords.INLINE,
Keywords.REGISTER,
Keywords.RESTRICT,
Keywords.RETURN,
Keywords.SHORT,
Keywords.SIGNED,
Keywords.SIZEOF,
Keywords.STATIC,
Keywords.STRUCT,
Keywords.SWITCH,
Keywords.TYPEDEF,
Keywords.UNION,
Keywords.UNSIGNED,
Keywords.VOLATILE,
Keywords.WHILE,
Keywords._BOOL,
Keywords._COMPLEX,
Keywords._IMAGINARY
};
private static String [] cppkeywords = {
Keywords.BREAK,
Keywords.CATCH,
Keywords.CLASS,
Keywords.CONST,
Keywords.CONST_CAST,
Keywords.CONTINUE,
Keywords.DEFAULT,
Keywords.DELETE,
Keywords.DOUBLE,
Keywords.DYNAMIC_CAST,
Keywords.EXPLICIT,
Keywords.EXPORT,
Keywords.EXTERN,
Keywords.FALSE,
Keywords.FLOAT,
Keywords.FRIEND,
Keywords.INLINE,
Keywords.MUTABLE,
Keywords.NAMESPACE,
Keywords.OPERATOR,
Keywords.PRIVATE,
Keywords.PROTECTED,
Keywords.PUBLIC,
Keywords.REGISTER,
Keywords.REINTERPRET_CAST,
Keywords.RETURN,
Keywords.SHORT,
Keywords.SIGNED,
Keywords.SIZEOF,
Keywords.STATIC,
Keywords.STATIC_CAST,
Keywords.STRUCT,
Keywords.SWITCH,
Keywords.TEMPLATE,
Keywords.THROW,
Keywords.TYPEDEF,
Keywords.TYPEID,
Keywords.TYPENAME,
Keywords.UNION,
Keywords.UNSIGNED,
Keywords.USING,
Keywords.VIRTUAL,
Keywords.VOLATILE,
Keywords.WCHAR_T,
Keywords.WHILE
};
}