Trouble with semantic highlighting [message #763371] |
Fri, 09 December 2011 16:48 |
Romain Aïssat Messages: 16 Registered: June 2011 |
Junior Member |
|
|
Hi all,
I've got some troubles with a particular case of semantic highlighting. Here is the grammar :
grammar org.xtext.Tsh with org.eclipse.xtext.common.Terminals
generate tsh "http://www.xtext.org/Tsh"
Model:
(definitions += Definition)*
(expressions += ExpressionRule ';')* ;
Definition :
'def' alias = Alias;
Alias :
name = ID;
ExpressionRule returns Expression :
'common' 'Prefix' TerminalExpression
( {TypeA.left = current} 'suffixA' right = TerminalExpression
| {TypeB.left = current} 'suffixB' right = TerminalExpression
);
Constant :
value = INT;
TerminalExpression returns Expression :
Constant | '(' ExpressionRule ')' | Reference;
Reference :
Reference = [Alias];
In my uiModule, i binded ISemanticHighlightingCalculator and IHighlightingConfiguration to my implementations of these interfaces. Here are the two implementations :
package org.xtext.ui;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.nodemodel.impl.CompositeNodeWithSemanticElement;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.ui.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.xtext.tsh.Constant;
import org.xtext.tsh.Reference;
import org.xtext.tsh.TypeA;
import org.xtext.tsh.TypeB;
public class TshSemanticHighlightingCalculator implements ISemanticHighlightingCalculator {
@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor) {
if (resource == null || resource.getParseResult() == null)
return;
INode root = resource.getParseResult().getRootNode();
for (INode node : root.getAsTreeIterable()) {
if(node instanceof CompositeNodeWithSemanticElement){
CompositeNodeWithSemanticElement compositeNode = (CompositeNodeWithSemanticElement) node;
if (node.getSemanticElement() instanceof Constant || node.getSemanticElement() instanceof Reference) {
acceptor.addPosition(node.getOffset(), node.getLength(), TshHighlightingConfiguration.CONSTANT_ID);
}
if(compositeNode.getSemanticElement() instanceof TypeA){
for(INode subNode : compositeNode.getChildren()){
System.out.println(subNode.getText());
}
System.out.println();
acceptor.addPosition(node.getOffset(), node.getLength(), TshHighlightingConfiguration.TYPE_A_ID);
}
if(compositeNode.getSemanticElement() instanceof TypeB){
for(INode subNode : compositeNode.getChildren()){
System.out.println(subNode.getText());
}
System.out.println();
acceptor.addPosition(node.getOffset(), node.getLength(), TshHighlightingConfiguration.TYPE_B_ID);
}
}
}
}
}
and
package org.xtext.ui;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.xtext.ui.editor.syntaxcoloring.DefaultHighlightingConfiguration;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfigurationAcceptor;
import org.eclipse.xtext.ui.editor.utils.TextStyle;
public class TshHighlightingConfiguration extends DefaultHighlightingConfiguration {
public static final String
CONSTANT_ID = "constant",
TYPE_A_ID = "typeA",
TYPE_B_ID = "typeB";
public void configure(IHighlightingConfigurationAcceptor acceptor){
super.configure(acceptor);
acceptor.acceptDefaultHighlighting(CONSTANT_ID, "Constants", constantTextStyle());
acceptor.acceptDefaultHighlighting(TYPE_A_ID, "Type A", aTextStyle());
acceptor.acceptDefaultHighlighting(TYPE_B_ID, "Type B", bTextStyle());
}
public TextStyle constantTextStyle(){
TextStyle style = new TextStyle();
style.setColor(new RGB(1,1,254));
style.setStyle(SWT.BOLD);
return style;
}
public TextStyle aTextStyle(){
TextStyle style = new TextStyle();
style.setColor(new RGB(254,1,1));
style.setStyle(SWT.BOLD);
return style;
}
public TextStyle bTextStyle(){
TextStyle style = new TextStyle();
style.setColor(new RGB(1,254,1));
style.setStyle(SWT.BOLD);
return style;
}
}
Here is the model :
common Prefix 1 suffixA 1;
common Prefix 2 suffixB 2;
The problem is : if I modify the keyword "Prefix" in one of the two expressions, like writing for example "Preofix", then it will raise of course a syntax error and the keyword Prefix will no more be highlighted as intended but as Reference. This is ok. But when i fix "Preofix" to "Prefix", "Prefix" is now highlighted as a default keyword (RGB.color = (127,0,85), style = SWT.BOLD == default eclipse keyword style) when it should be highlighted as TypeA or TypeB (depending on the line of the model I modified).
I've tried different ways to fix this, but in vain.
If somebody knows a way to fix it... Thanks in advance!
Romain
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03623 seconds