Xtext 2.0 Syntax Coloring [message #708869] |
Wed, 03 August 2011 08:43 |
Caner Messages: 98 Registered: July 2011 |
Member |
|
|
Hello
I faced with a problem while I was trying to add syntax coloring feature for a type.
I have created a OurDefaultHighlightingConfiguration class (in com.example.mydsl.ui same packet with DslUIModel)which implements ISemanticHighlightingCalculator and overridden
following method
However, when I want to call NodeUtil, there is not such a NodeUtil class?
If you can help, I will appreciate
BR
Caner
@Override
public void provideHighlightingFor(XtextResource resource,
IHighlightedPositionAcceptor acceptor) {
/*
if(resource==null)
return;
Iterable<AbstractNode> allNode= NodeUtil.getAllContents(resource.getParseResult().getRootNode());
*/
}
[Updated on: Wed, 03 August 2011 20:26] Report message to a moderator
|
|
|
|
|
|
|
|
Re: NodeUtil class not exists [message #708982 is a reply to message #708953] |
Wed, 03 August 2011 11:16 |
Caner Messages: 98 Registered: July 2011 |
Member |
|
|
Hi again
now I have the code as below, is there anything missing? I didnt understand why it not working
I have implemented these two classes
public class MyHighlightingConfiguration implements IHighlightingConfiguration {
public final static String my_REF = "first reference";
public void configure(IHighlightingConfigurationAcceptor acceptor) {
acceptor.acceptDefaultHighlighting(my_REF,
"first_reference", myReferenceTextStyle());
}
public TextStyle myReferenceTextStyle() {
TextStyle textStyle = new TextStyle();
textStyle.setStyle(SWT.ITALIC);
textStyle.setColor(new RGB(127,0,85));
return textStyle;
}
}
public class OurDefaultHighlightingConfiguration implements ISemanticHighlightingCalculator {
public void provideHighlightingFor(XtextResource resource,
IHighlightedPositionAcceptor acceptor) {
if (resource == null || resource.getParseResult() == null)
return;
INode root = resource.getParseResult().getRootNode();
//Writer is the target type for coloring
for (INode node : root.getAsTreeIterable()) {
if (node.getGrammarElement() instanceof Writer) {
acceptor.addPosition(node.getOffset(), node.getLength(),
MyHighlightingConfiguration.my_REF);
}
}
}
}
And I bind them in the DSLUIMODEL
public class DSLUiModule extends AbstractDSLModule {
......
public Class<? extends OurDefaultHighlightingConfiguration> bindISemanticHighlightingCalculator(){
return OurDefaultHighlightingConfiguration.class;
}
public Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration () {
return MyHighlightingConfiguration.class;
}
}
BR caner
[Updated on: Wed, 03 August 2011 11:16] Report message to a moderator
|
|
|
|
Re: NodeUtil class not exists [message #709045 is a reply to message #709015] |
Wed, 03 August 2011 12:49 |
Caner Messages: 98 Registered: July 2011 |
Member |
|
|
Hi
yes, you are right Alex, the Writer is a type which has defined like this
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
LibModel:
(types+=Type)*;
Type:
LibComment1|NonEmpityLine;
LibComment1:
text=HASH_COMMENT
;
NonEmpityLine hidden (WS) :
Writer=Writer ':' WritersBooks=WritersBooks':'
Writer:
Writer=Words
;
Words hidden (): ID ;
So How I need to call writer for coloring? can you give me an example how it supposed to be? Is there any link for a sample???
BR
Caner
[Updated on: Wed, 03 August 2011 13:22] Report message to a moderator
|
|
|
|
Re: NodeUtil class not exists [message #709280 is a reply to message #709176] |
Wed, 03 August 2011 18:27 |
Caner Messages: 98 Registered: July 2011 |
Member |
|
|
Hi
Thanx for explanation, The problem is with Xtext 2.0 , its new and therefore, there are not so much sample or tutorial and people didnt observe Xtext 2.0 in the blogs, thats why the documentation is weak
if there is something about xtext on the web 99% is xtext 1.x for instance there is not a link which used NodeModelUtils in a sample
BR
Caner
[Updated on: Wed, 03 August 2011 18:38] Report message to a moderator
|
|
|
Re: NodeUtil class not exists [message #709293 is a reply to message #709280] |
Wed, 03 August 2011 18:46 |
|
this is why i pointed you to the migration guide
Quote:Rewritten Node Model
To reduce memory consumption, the node model has been redesigned in Xtext 2.0. We no longer use EMF, but a chained list of compressed nodes instead.
The package org.eclipse.xtext.nodemodel now contains the relevant interfaces to program against. The new interfaces follow the naming convention of other types in the framework. They are called INode (src), ICompositeNode (src) and ILeafNode (src). That way, most of the migration will be done by prefixing the old names with an I and use the organize imports tool. Please make sure not to program against concrete or abstract classes.
If you used the node model a lot, you should have a closer look at the new APIs. The EObject API is no longer available on the nodes. Instead, you we offer a couple of Iterables for traversing the tree. Where appropriate, helper methods of the former ParseTreeUtil and NodeUtil have become members of the nodes, e.g. NodeUtil.getAllContents(AbstractNode) has become INode.getAsTreeIterable() (src) The remaining methods have been converted and moved to the new NodeModelUtils (src).
so you won't need any 2.0 specific code since you should be able to translate the 1.0 code to 2.0 code
Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: NodeUtil class not exists [message #709304 is a reply to message #709293] |
Wed, 03 August 2011 19:09 |
|
So with proper naming and binding the following might work
package org.xtext.example.mydsl.ui;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.nodemodel.INode;
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.example.mydsl.services.MyDslGrammarAccess;
import com.google.inject.Inject;
public class OurDefaultHighlightingCalculator implements ISemanticHighlightingCalculator {
@Inject
private MyDslGrammarAccess grammarAccess;
public void provideHighlightingFor(XtextResource resource,
IHighlightedPositionAcceptor acceptor) {
if (resource == null || resource.getParseResult() == null)
return;
INode root = resource.getParseResult().getRootNode();
//Writer is the target type for coloring
for (INode node : root.getAsTreeIterable()) {
if (node.getGrammarElement() instanceof RuleCall && ((RuleCall)node.getGrammarElement()).getRule().equals(grammarAccess.getWriterRule())) {
acceptor.addPosition(node.getOffset(), node.getLength(),
MyHighlightingConfiguration.my_REF);
}
}
}
}
public Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator(){
return OurDefaultHighlightingCalculator.class;
}
public Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration () {
return MyHighlightingConfiguration.class;
}
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
Re: NodeUtil class not exists [message #713482 is a reply to message #709342] |
Mon, 08 August 2011 14:23 |
Caner Messages: 98 Registered: July 2011 |
Member |
|
|
Hi Christian
Regarding syntax coloring;
If a type has a keyword/symbol , I am not able to implement syntax coloring for that type, for example i have a type which has "keyword" as a keyword
and try to create syntax coloring feature just like below, but it doesnt work. Do you have any advice for me please?
BR
caner
Grammar
LibModel:
(types+=Type)*;
Type:
typeWithKey|NonEmpityLine;
typeWithKey: 'keyword' type=Words ;
Words hidden (): ID ;
public class OurDefaultHighlightingConfiguration implements
ISemanticHighlightingCalculator {
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.getGrammarElement() instanceof
RuleCall&& (RuleCall)node.getGrammarElement()).getRule().equals(grammarAccess.gettypeWithKeyRule())) {
acceptor.addPosition(node.getOffset(), node.getLength(),
MyHighlightingConfiguration.typeWithKey_Ref);
}
public class MyHighlightingConfiguration implements IHighlightingConfiguration {
public static String typeWithKey_Ref = "keyword";
--etc---
public void configure(IHighlightingConfigurationAcceptor acceptor) {
acceptor.acceptDefaultHighlighting(typeWithKey_Ref ,
"With Key", mytypeWithKeyTextStyle());
private TextStyle mytypeWithKeyTextStyle[code][/code]() {
TextStyle textStyle = new TextStyle();
textStyle.setStyle(SWT.ITALIC);
textStyle.setStyle(SWT.BOLD);
textStyle.setColor(new RGB(42, 0, 255));
return textStyle;
}
and of course these classes have been binned in UIModule.java
[Updated on: Mon, 08 August 2011 14:25] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06761 seconds