Custom Syntax Colouring for ide [message #1800781] |
Tue, 08 January 2019 08:51 |
P J 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 #1800796 is a reply to message #1800792] |
Tue, 08 January 2019 12:57 |
|
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
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
|
Re: Custom Syntax Colouring for ide [message #1800847 is a reply to message #1800796] |
Wed, 09 January 2019 04:47 |
P J 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 #1800849 is a reply to message #1800848] |
Wed, 09 January 2019 05:44 |
P J 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 #1851893 is a reply to message #1800796] |
Thu, 21 April 2022 09:26 |
Marvin Dongus 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
;
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.06486 seconds