Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Custom Syntax Colouring for ide
Custom Syntax Colouring for ide [message #1800781] Tue, 08 January 2019 08:51 Go to next message
P J is currently offline P JFriend
Messages: 64
Registered: October 2018
Member
Hi,

I want to implement syntax highlighting support for my xtext DSL. Could you please show me an example of a simple synatax highlighting mplementation if I need to highlight "Hello" keyword in red in the below .xtext file? I need to implement this in the .ide project.

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID '!';



Since I am a beginner to implement different classes in Xtext, the document's examples did not help me much in understanding. So I would appreciate if anyone could support me.

Regards!

[Updated on: Tue, 08 January 2019 08:52]

Report message to a moderator

Re: Custom Syntax Colouring for ide [message #1800792 is a reply to message #1800781] Tue, 08 January 2019 12:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
In which IDE do you want to color? VSCode? Theia? Xtext Web? Eclipse?

do you want to change all keywords (lexical highlighting)
or do you want to implement semantic highlighting


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Syntax Colouring for ide [message #1800796 is a reply to message #1800792] Tue, 08 January 2019 12:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
for eclipse:

in ide

new class
package org.xtext.example.mydsl.ide;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.ide.editor.syntaxcoloring.DefaultSemanticHighlightingCalculator;
import org.eclipse.xtext.ide.editor.syntaxcoloring.HighlightingStyles;
import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.util.CancelIndicator;
import org.xtext.example.mydsl.myDsl.Model;
import org.xtext.example.mydsl.services.MyDslGrammarAccess;

import com.google.inject.Inject;

public class MyDslSemanticHighlightingCalculator extends DefaultSemanticHighlightingCalculator {
	
	public static class MyDslHighlightingStyles implements HighlightingStyles {
		public static String KEYWORD_RED_ID = KEYWORD_ID + "Red";
	}

	@Inject
	MyDslGrammarAccess ga;

	@Override
	protected boolean highlightElement(EObject object, IHighlightedPositionAcceptor acceptor,
			CancelIndicator cancelIndicator) {
		if (object instanceof Model) {
			Keyword keyword = ga.getGreetingAccess().getHelloKeyword_0();
			for (ILeafNode n : NodeModelUtils.findActualNodeFor(object).getLeafNodes()) {
				if (keyword == n.getGrammarElement()) {
					acceptor.addPosition(n.getOffset(), n.getLength(), MyDslHighlightingStyles.KEYWORD_RED_ID);
				}
			}
		}
		return super.highlightElement(object, acceptor, cancelIndicator);
	}

}


(+ add export to manifest)

in ui

new class
package org.xtext.example.mydsl.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;
import org.xtext.example.mydsl.ide.MyDslSemanticHighlightingCalculator.MyDslHighlightingStyles;

public class MyDslHighlightingConfiguration extends DefaultHighlightingConfiguration {
	
	@Override
	public void configure(IHighlightingConfigurationAcceptor acceptor) {
		acceptor.acceptDefaultHighlighting(MyDslHighlightingStyles.KEYWORD_RED_ID, "KeywordRed", keywordRedTextStyle());
		super.configure(acceptor);
	}
	
	public TextStyle keywordRedTextStyle() {
		TextStyle textStyle = defaultTextStyle().copy();
		textStyle.setColor(new RGB(255, 0, 0));
		textStyle.setStyle(SWT.BOLD);
		return textStyle;
	}

}



add bindings to existing ui module
	def Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration() {
		return MyDslHighlightingConfiguration
	}
	
	def Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() {
		MyDslSemanticHighlightingCalculator
	}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Syntax Colouring for ide [message #1800847 is a reply to message #1800796] Wed, 09 January 2019 04:47 Go to previous messageGo to next message
P J is currently offline P JFriend
Messages: 64
Registered: October 2018
Member
Hi Christian,

Thank you for the example. I'm using the Eclipse IDE and I need syntax highlighting to be implemented in a jar file.

I created a new java class with the first code sample that you've given (MyDslSemanticHighlightingCalculator) and I get errors for the following: -

import org.xtext.example.mydsl.myDsl.Model;   -  "The import org.xtext.example.mydsl.myDsl cannot be resolved"
import org.xtext.example.mydsl.services.MyDslGrammarAccess;  -  "The import org.xtext.example.mydsl.services cannot be resolved"
MyDslGrammarAccess ga;  -  "MyDslGrammarAccess cannot be resolved to a type"


Another thing to mention is, my project is not a plugin project, so it doesn't have a .ui directory. It only has org.xtext.example.mydsl, *.ide, and *.parent directories.

Could you please help me eliminate these errors?

Regards!

[Updated on: Wed, 09 January 2019 05:01]

Report message to a moderator

Re: Custom Syntax Colouring for ide [message #1800848 is a reply to message #1800847] Wed, 09 January 2019 05:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
Please answer my questi9n of what you actually want to achieve.
Which ide yourdsl runs in
The development ide does not count of course

Plus this one: How is your dsl called


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 09 January 2019 05:36]

Report message to a moderator

Re: Custom Syntax Colouring for ide [message #1800849 is a reply to message #1800848] Wed, 09 January 2019 05:44 Go to previous messageGo to next message
P J is currently offline P JFriend
Messages: 64
Registered: October 2018
Member
What I want to achieve: - I want to implement syntax highlighting for my DSL which is implemented as a LS-Sidecar in Eclipse Che. I have tried various approaches but nothing worked. Therefore, I'm trying whether my goal could be achieved through this approach.

My DSL is just the same as the one I mentioned at the beginning of this question.
It is called MyDsl.

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID '!';


Regards!

[Updated on: Wed, 09 January 2019 05:45]

Report message to a moderator

Re: Custom Syntax Colouring for ide [message #1800850 is a reply to message #1800849] Wed, 09 January 2019 05:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
Then you should ask Che if they support lsp highlighting at all. My guess answer is no.
Then they do client side highlighting only.
If so you need to find out how they do it.
If they do it with a Textmate grammar
You need to find out hometown do it with Textmate.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Syntax Colouring for ide [message #1800851 is a reply to message #1800850] Wed, 09 January 2019 05:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
And if Che does support servers use highlight

https://github.com/theia-ide/yang-lsp/blob/master/yang-lsp/io.typefox.yang.ide/src/main/java/io/typefox/yang/ide/editor/syntaxcoloring/YangSemanticHighlightingCalculator.xtend

Plus these bindings

def Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() {
return YangSemanticHighlightingCalculator;
}

def Class<? extends ISemanticHighlightingStyleToTokenMapper> bindISemanticHighlightingStyleToTokenMapper() {
return YangSemanticHighlightingCalculator;
}

Plus what needs to be done one Che client side
Again something this is the wrong pl,ace to ask



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Syntax Colouring for ide [message #1851893 is a reply to message #1800796] Thu, 21 April 2022 09:26 Go to previous messageGo to next message
Marvin Dongus is currently offline Marvin DongusFriend
Messages: 8
Registered: February 2022
Junior Member
Here in this old post is the exact thing described what i want to do in my own DSL.
I want to color a specific keyword in my own DSL.

I implemented:
> MyDslSemanticHighlightingCalculator in the ide/src + export in the Manifest
> MyDslHighlightingConfiguration in the ui/src
> and done the bindings in the ui Module

My problem ist that the implementation haven't any affect in my exportet application on my Xtext project. If I set a breakpoint in my UIModule binding it is never reached. Can annyone help me to find myProblem?

My whole code:

1. MyUIModule:
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration;
import de.dongus.myDsl.ide.MyDslSemanticHighlightingCalculator;

/**
* Use this class to register components to be used within the Eclipse IDE.
*/
@FinalFieldsConstructor

public class DslUiModule extends AbstractDslUiModule {

public DslUiModule(AbstractUIPlugin plugin) {
super(plugin);
}

Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration() {
return MyDslHighlightingConfiguration.class;
}

Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() {
return MyDslSemanticHighlightingCalculator.class;
}
}

2. My MyDslHighlightingConfiguration:
import org.eclipse.xtext.ui.editor.syntaxcoloring.DefaultHighlightingConfiguration;
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfigurationAcceptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.xtext.ui.editor.utils.TextStyle;
import de.dongus.myDsl.ide.MyDslSemanticHighlightingCalculator.MyDslHighlightingStyles;


public class MyDslHighlightingConfiguration extends DefaultHighlightingConfiguration {

@Override
public void configure(IHighlightingConfigurationAcceptor acceptor) {
acceptor.acceptDefaultHighlighting(MyDslHighlightingStyles.KEYWORD_RED_ID, "KeywordRed", keywordRedTextStyle());
super.configure(acceptor);
}

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

3. MyDslSemanticHighlightingCalculator:
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.ide.editor.syntaxcoloring.DefaultSemanticHighlightingCalculator;
import org.eclipse.xtext.ide.editor.syntaxcoloring.HighlightingStyles;
import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.util.CancelIndicator;
import com.google.inject.Inject;
import de.dongus.myDsl.dsl.Program;
import de.dongus.myDsl.impact.services.DslGrammarAccess;

public class MyDslSemanticHighlightingCalculator extends DefaultSemanticHighlightingCalculator {

public static class MyDslHighlightingStyles implements HighlightingStyles {
public static String KEYWORD_RED_ID = KEYWORD_ID + "Red";
}

@Inject
DslGrammarAccess ga;

@Override
protected boolean highlightElement(EObject object, IHighlightedPositionAcceptor acceptor,
CancelIndicator cancelIndicator) {
if (object instanceof Program) {
Keyword keyword = ga.getVariableDeclarationAccess().getDeclareKeyword_1();
for (ILeafNode n : NodeModelUtils.findActualNodeFor(object).getLeafNodes()) {
if (keyword == n.getGrammarElement()) {
acceptor.addPosition(n.getOffset(), n.getLength(), MyDslHighlightingStyles.KEYWORD_RED_ID);
System.out.println("Ich habe das Keyword markiert !");
}
}
}
return super.highlightElement(object, acceptor, cancelIndicator);
}
}

4. MyGrammar(shortened):

Program:
block+= Block;
;
Block:
variableDefinition += VariableDefinition;
;
VariableDefinition:
'define' 'int' name=ID '=' expr = Expression
;



Re: Custom Syntax Colouring for ide [message #1851895 is a reply to message #1851893] Thu, 21 April 2022 09:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
You need to implement the service for Lsp yourself and or contribute it back to Xtext

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Custom Syntax Colouring for ide [message #1851993 is a reply to message #1851895] Mon, 25 April 2022 10:01 Go to previous messageGo to next message
Marvin Dongus is currently offline Marvin DongusFriend
Messages: 8
Registered: February 2022
Junior Member
How can i contribute it back to the xtext language Server Protocoll?
Re: Custom Syntax Colouring for ide [message #1852008 is a reply to message #1851993] Mon, 25 April 2022 17:39 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14349
Registered: July 2009
Senior Member
Code,is at github.com/eclipse/Xtext-core

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Including Maven Dependencies on XText Eclipse Project
Next Topic:How to react on external changes of the AST
Goto Forum:
  


Current Time: Sun Sep 25 08:22:16 GMT 2022

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

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

Back to the top