Reputation: 4419
To have custom keywords for I do the following:
plugin.xml
and extend GPPLanguage class.extend CLanguageKeywords and override getKeywords to return super.getKeywords plus custom keywords:
public class MyLanguageKeywords extends CLanguageKeywords {
public MyLanguageKeywords(ParserLanguage language, IScannerExtensionConfiguration config) {
super(language, config);
}
@Override
public String[] getKeywords() {
String[] cppKeywords = super.getKeywords();
String[] myKeywords = new String[] {
"input",
"output",
"input port",
"output port",
};
return (String[]) ArrayUtils.addAll(cppKeywords, myKeywords);
}
}
implement getAdapter() to return an instance of MyLanguageKeywords
This works for "input" and "output", but not for "input port" or "output port". How can I have keyword highlighting for "port" but only in the combinations "input port" or "output port"?
Upvotes: 1
Views: 61
Reputation: 52819
I can think of two options:
Modify CDT's lexer to support keywords containing whitespace. I don't think this is something you can do using the public API, you'd have to modify core CDT classes like org.eclipse.cdt.internal.core.parser.scanner.Lexer
. (Alternatively, you could consider upstreaming a patch to add such support to the lexer.)
Use semantic highlighting to highlight your whitespace-containing keywords. There's an org.eclipse.cdt.ui.semanticHighlighting
extension point that allows plugins to provide custom semantic highlighting.
The difference in the user experience would be that lexical highlighting is applied immediately after every keystroke, while semantic highlighting is applied after a short delay after typing stops (since it's more expensive to compute). You can see this effect if you compare how the override
and final
keywords are currently highlighted in C++, compared to other keywords: override
and final
use semantic highlighting because they're context-sensitive keywords.
I should also mention that whether the second approach works, depends on how your new keywords fit into the grammar. They need to be parsed as IASTName
in order for semantic highlighting to be applied to them.
Upvotes: 1