Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » help - Don't get how to extend Syntax coloring
help - Don't get how to extend Syntax coloring [message #494840] Mon, 02 November 2009 21:42 Go to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Hi,
I have a hard time figuring out how to add more syntax coloring. I
understand the difference between lexical and model based highlighting,
and I understand the code that is in the documentation (well, enough to
write something that would perform the highlighting I want).

But how to "register" what needs to be registered baffles me. The
documentation does not mention this at all - simply that something
should be registered... but not where or how this is done.

I am happy to describe how it is done if I am given some pointers in the
right direction...

I am currently looking at the generated MyLangUiModule - but I am not
sure which of the bind methods to override... or, am I in the wrong place...

confused, but otherwise an happy xtext user... :)
Regards
- henrik
Re: help - Don't get how to extend Syntax coloring [message #494859 is a reply to message #494840] Mon, 02 November 2009 23:02 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
By experimenting, I think I can answer my own question.

Basically, I wrote this:
public class BeeLangTokenToAttributeIdMapper extends
DefaultAntlrTokenToAttributeIdMapper {
@Override
protected String calculateId(String tokenName, int tokenType) {

if("RULE_DOCUMENTATION".equals(tokenName)) {
return DefaultLexicalHighlightingConfiguration.STRING_ID;
}

return super.calculateId(tokenName, tokenType);
}
}

and registered this class like this:

public class BeeLangUiModule extends
org.eclipse.b3.AbstractBeeLangUiModule {

public Class<? extends AbstractAntlrTokenToAttributeIdMapper>
bindTokenToAttributeIdMapper() {
return BeeLangTokenToAttributeIdMapper.class;
}

}

What I found confusing is that there is no method called
"bindTokenToAttributeIdMapper" on the
org.eclipse.b3.AbstractBeeLangUiModule.


Are there many other things not found as bindXXX methods on the
generated superclass that could be registered like this? How can one
find out what they are?

Regards
- henrik


On 11/2/09 10:42 PM, Henrik Lindberg wrote:
> Hi,
> I have a hard time figuring out how to add more syntax coloring. I
> understand the difference between lexical and model based highlighting,
> and I understand the code that is in the documentation (well, enough to
> write something that would perform the highlighting I want).
>
> But how to "register" what needs to be registered baffles me. The
> documentation does not mention this at all - simply that something
> should be registered... but not where or how this is done.
>
> I am happy to describe how it is done if I am given some pointers in the
> right direction...
>
> I am currently looking at the generated MyLangUiModule - but I am not
> sure which of the bind methods to override... or, am I in the wrong
> place...
>
> confused, but otherwise an happy xtext user... :)
> Regards
> - henrik
Re: help - Don't get how to extend Syntax coloring [message #494881 is a reply to message #494840] Tue, 03 November 2009 04:11 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
I also managed to create a semantic highlighter. I do however not
understand how to work with the tree and nodes. Is there some
description of Composite and Leaf nodes, navigating between Ecore to
grammar elements etc.

Any pointer how I can learn this stuff appreciated :)

- henrik

On 11/2/09 10:42 PM, Henrik Lindberg wrote:
> Hi,
> I have a hard time figuring out how to add more syntax coloring. I
> understand the difference between lexical and model based highlighting,
> and I understand the code that is in the documentation (well, enough to
> write something that would perform the highlighting I want).
>
> But how to "register" what needs to be registered baffles me. The
> documentation does not mention this at all - simply that something
> should be registered... but not where or how this is done.
>
> I am happy to describe how it is done if I am given some pointers in the
> right direction...
>
> I am currently looking at the generated MyLangUiModule - but I am not
> sure which of the bind methods to override... or, am I in the wrong
> place...
>
> confused, but otherwise an happy xtext user... :)
> Regards
> - henrik
Re: help - Don't get how to extend Syntax coloring [message #494898 is a reply to message #494881] Tue, 03 November 2009 07:28 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi Henrik,

regarding semantic highlighting you may want to have a look at this blog
post which contains some code snippets that may serve as starter (it
misses some null-checks though).

http://blogs.itemis.de/stundzig/archives/467

The NodeUtil-class is your friend when it comes to navigating between
semantic model and parse tree.

Binding: The name of the bind-Methods does not matter (it has to start
with bind). Sometimes you want to override existing bind-methods
(exchanging an implementation bound there) in other cases you introduce
new ones (e.g. if you want to modify the XtextEditor itself, you can
bind an implementation extending it).

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: help - Don't get how to extend Syntax coloring [message #494990 is a reply to message #494898] Tue, 03 November 2009 13:35 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Thanks Alex,

On 11/3/09 8:28 AM, Alexander Nittka wrote:
> regarding semantic highlighting you may want to have a look at this blog
> post which contains some code snippets that may serve as starter (it
> misses some null-checks though).
>
> http://blogs.itemis.de/stundzig/archives/467

the blog post was very helpful. I managed to get the highlighting
working by traversing the ecore tree, and then finding the corresponding
node. I implemented a method similar to highlightFirstFeature (shown in
the blog post), but that colors an entire composite node (as that was I
needed in this case).

> Binding: The name of the bind-Methods does not matter (it has to start
> with bind). Sometimes you want to override existing bind-methods
> (exchanging an implementation bound there) in other cases you introduce
> new ones (e.g. if you want to modify the XtextEditor itself, you can
> bind an implementation extending it).

ok, that makes sense, but how do I know what meaningful things I can
extend the XtextEditor with? I mean, if I bind an implementation of
IHamburgerBar there is nothing that would know how to use it to order a
burger :).

- henrik
Re: help - Don't get how to extend Syntax coloring [message #494993 is a reply to message #494990] Tue, 03 November 2009 13:47 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
This is what I implemented:

private void highlightObject(EObject semantic, String highlightID,
IHighlightedPositionAcceptor acceptor) {
NodeAdapter adapter = NodeUtil.getNodeAdapter(semantic);
if(adapter == null) {
// TODO: WARNING - Could not find node
return;
}
CompositeNode node = adapter.getParserNode();
if(node == null) {
// TODO: WARNING - Could not find node
return;
}
acceptor.addPosition(node.getOffset(), node.getLength(), highlightID);
}

This works fine, except that recoloring is a bit too lazy :). If an
element is inserted into the highlighted element it gets its coloring
from the lexical coloring, the semantic coloring is not re-triggered. I
have to delete/insert the element, or reopen the editor to trigger it.

Is this a bug, or am I required to find all the leafs under the object
and color them individually?

Regards
- henrik

On 11/3/09 2:35 PM, Henrik Lindberg wrote:
> Thanks Alex,
>
> On 11/3/09 8:28 AM, Alexander Nittka wrote:
> > regarding semantic highlighting you may want to have a look at this blog
> > post which contains some code snippets that may serve as starter (it
> > misses some null-checks though).
> >
> > http://blogs.itemis.de/stundzig/archives/467
>
> the blog post was very helpful. I managed to get the highlighting
> working by traversing the ecore tree, and then finding the corresponding
> node. I implemented a method similar to highlightFirstFeature (shown in
> the blog post), but that colors an entire composite node (as that was I
> needed in this case).
>
>> Binding: The name of the bind-Methods does not matter (it has to start
>> with bind). Sometimes you want to override existing bind-methods
>> (exchanging an implementation bound there) in other cases you introduce
>> new ones (e.g. if you want to modify the XtextEditor itself, you can
>> bind an implementation extending it).
>
> ok, that makes sense, but how do I know what meaningful things I can
> extend the XtextEditor with? I mean, if I bind an implementation of
> IHamburgerBar there is nothing that would know how to use it to order a
> burger :).
>
> - henrik
Re: help - Don't get how to extend Syntax coloring [message #495000 is a reply to message #494840] Tue, 03 November 2009 14:01 Go to previous messageGo to next message
Tim  is currently offline Tim Friend
Messages: 21
Registered: August 2009
Junior Member
Hi Hendrik,

I'm also trying to get the syntax coloring to work. My thought was easy, implement a copy of the default hyghlightingconfiguration and change some colors.

Original file: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.tmf/org .eclipse.xtext/plugins/org.eclipse.xtext.ui.common/src/org/e clipse/xtext/ui/common/editor/syntaxcoloring/DefaultLexicalH ighlightingConfiguration.java?root=Modeling_Project&view =co

My implementation below, but it seems like it is not implemented at all???? No colors have changed after I generate everything with the MWE Workflow. How did you manage to get it to work?

../ide/v07/APAPLUiModule.java
package nl.uu.cs.apapl.ide.v07;
public class APAPLUiModule extends nl.uu.cs.apapl.ide.v07.AbstractAPAPLUiModule {
		  public Class bindSemanticConfig() {
		    return MySemanticHighlightingConfiguration.class;
		  }
}

../ide/v07/MySemanticHighlightingConfiguration.java
package nl.uu.cs.apapl.ide.v07;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.xtext.ui.common.editor.syntaxcoloring.*;
import org.eclipse.xtext.ui.core.editor.utils.TextStyle;

public class MySemanticHighlightingConfiguration implements ILexicalHighlightingConfiguration {

	public static final String KEYWORD_ID = "keyword";
	public static final String PUNCTUATION_ID = "punctuation";
	public static final String COMMENT_ID = "comment";
	public static final String STRING_ID = "string";
	public static final String NUMBER_ID = "number";
	public static final String DEFAULT_ID = "default";
	public static final String INVALID_TOKEN_ID = "error";

	public void configure(IHighlightingConfigurationAcceptor acceptor) {
		acceptor.acceptDefaultHighlighting(KEYWORD_ID, "Keyword", keywordTextStyle());
		acceptor.acceptDefaultHighlighting(PUNCTUATION_ID, "Punctuation character", punctuationTextStyle());
		acceptor.acceptDefaultHighlighting(COMMENT_ID, "Comment", commentTextStyle());
		acceptor.acceptDefaultHighlighting(STRING_ID, "String", stringTextStyle());
		acceptor.acceptDefaultHighlighting(NUMBER_ID, "Number", numberTextStyle());
		acceptor.acceptDefaultHighlighting(DEFAULT_ID, "Default", defaultTextStyle());
		acceptor.acceptDefaultHighlighting(INVALID_TOKEN_ID, "Invalid Symbol", errorTextStyle());
	}
	
	public TextStyle defaultTextStyle() {
		TextStyle textStyle = new TextStyle();
		textStyle.setBackgroundColor(new RGB(255, 255, 255));
		textStyle.setColor(new RGB(100, 100, 50));
		return textStyle;
	}

	public TextStyle keywordTextStyle() {
		TextStyle textStyle = defaultTextStyle().copy();
		textStyle.setColor(new RGB(127, 0, 85));
		textStyle.setStyle(SWT.BOLD);
		return textStyle;
	}	

	// all the text styles here..............

}

[Updated on: Tue, 03 November 2009 14:02]

Report message to a moderator

Re: help - Don't get how to extend Syntax coloring [message #495045 is a reply to message #495000] Tue, 03 November 2009 16:01 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
You need to register things in the UI "module".
Here are the things I have registered:

public class BeeLangUiModule extends
org.eclipse.b3.AbstractBeeLangUiModule {

public Class<? extends AbstractAntlrTokenToAttributeIdMapper>
bindTokenToAttributeIdMapper() {
return BeeLangTokenToAttributeIdMapper.class;
}
public Class<? extends ILexicalHighlightingConfiguration>
bindILexicalHighlightingConfiguration() {
return BeeLangLexicalHighlightConfiguration.class;
}
public Class<? extends ISemanticHighlightingConfiguration>
bindISemanticHighlightingConfiguration() {
return BeeLangSemanticHighligtConfiuration.class;
}
public Class<? extends ISemanticHighlightingCalculator>
bindISemanticHighlightingCalculator() {
return BeeLangSemanticHighlightingCalculator.class;
}
}

Do something comparable...

Regards
- henrik

On 11/3/09 3:01 PM, Tim wrote:
> Hi Hendrik,
>
> I'm also trying to get the syntax coloring to work. My thought was easy,
> implement a copy of the default hyghlightingconfiguration and change
> some colors.
>
> Original file:
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.tmf/org .eclipse.xtext/plugins/org.eclipse.xtext.ui.common/src/org/e clipse/xtext/ui/common/editor/syntaxcoloring/DefaultLexicalH ighlightingConfiguration.java?root=Modeling_Project&view =co
>
>
> My implementation below, but it seems like it is not implemented at
> all???? No colors have changed after I generate everything with the MWE
> Workflow. How did you manage to get it to work?
>
> ./ide/v07/APAPLUiModule.java
> public class APAPLUiModule extends
> nl.uu.cs.apapl.ide.v07.AbstractAPAPLUiModule {
> public Class bindSemanticConfig() {
> return MySemanticHighlightingConfiguration.class;
> }
> }
> ./ide/v07/MySemanticHighlightingConfiguration.java
> package nl.uu.cs.apapl.ide.v07;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.RGB;
> import org.eclipse.xtext.ui.common.editor.syntaxcoloring.*;
> import org.eclipse.xtext.ui.core.editor.utils.TextStyle;
>
> public class MySemanticHighlightingConfiguration implements
> ILexicalHighlightingConfiguration {
>
> public static final String KEYWORD_ID = "keyword";
> public static final String PUNCTUATION_ID = "punctuation";
> public static final String COMMENT_ID = "comment";
> public static final String STRING_ID = "string";
> public static final String NUMBER_ID = "number";
> public static final String DEFAULT_ID = "default";
> public static final String INVALID_TOKEN_ID = "error";
>
> public void configure(IHighlightingConfigurationAcceptor acceptor) {
> acceptor.acceptDefaultHighlighting(KEYWORD_ID, "Keyword",
> keywordTextStyle());
> acceptor.acceptDefaultHighlighting(PUNCTUATION_ID, "Punctuation
> character", punctuationTextStyle());
> acceptor.acceptDefaultHighlighting(COMMENT_ID, "Comment",
> commentTextStyle());
> acceptor.acceptDefaultHighlighting(STRING_ID, "String", stringTextStyle());
> acceptor.acceptDefaultHighlighting(NUMBER_ID, "Number", numberTextStyle());
> acceptor.acceptDefaultHighlighting(DEFAULT_ID, "Default",
> defaultTextStyle());
> acceptor.acceptDefaultHighlighting(INVALID_TOKEN_ID, "Invalid Symbol",
> errorTextStyle());
> }
>
> public TextStyle defaultTextStyle() {
> TextStyle textStyle = new TextStyle();
> textStyle.setBackgroundColor(new RGB(255, 255, 255));
> textStyle.setColor(new RGB(100, 100, 50));
> return textStyle;
> }
>
> public TextStyle keywordTextStyle() {
> TextStyle textStyle = defaultTextStyle().copy();
> textStyle.setColor(new RGB(127, 0, 85));
> textStyle.setStyle(SWT.BOLD);
> return textStyle;
> }
>
> // all the text styles here..............
>
> }
Previous Topic:What does this mean: attribute is not a token, parameter, or return value: current
Next Topic:[Xtext] some other code completion issues
Goto Forum:
  


Current Time: Fri Apr 26 19:18:07 GMT 2024

Powered by FUDForum. Page generated in 0.03300 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top