| Home » Modeling » TMF (Xtext) » [xText] syntax highlighting
 Goto Forum:| 
| [xText] syntax highlighting [message #55525] | Fri, 03 July 2009 10:58  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: anders.hessellund.gmail.com 
 Hi,
 
 my first little grammar is progressing and the editor-features that
 xText allows with minimal code are amazing! I am, however, a bit stuck
 on the syntax highlighting part. The documentation is a bit too
 abstract. Specifically, in the grammar below, I'd like to do thing like
 making XML elements bold. What are the steps, I need to go through?
 
 Thanks,
 
 Anders
 
 grammar c.m.XmlGrammar with org.eclipse.xtext.common.Terminals
 
 generate xmlGrammar "http://www.example.com/XmlGrammar"
 
 Model :
 (productions+=Production)*
 ;
 
 Production :
 name=ID '=' expression=OrExpression ';'
 ;
 
 OrExpression :
 Expression ( '|' right=OrExpression )?
 ;
 
 Expression :
 XmlElement |
 expressions=QuantifiedExpression+
 ;
 
 XmlElement :
 XmlNonTerminal |
 XmlTerminal
 ;
 
 XmlNonTerminal :
 '<'  begin=ID '>'
 expression=Expression
 '</' end=ID '>'
 ;
 
 XmlTerminal :
 '<'  name=ID '/>'
 ;
 
 QuantifiedExpression :
 TerminalExpression ( '?' | '*' | '+' )?
 ;
 
 TerminalExpression :
 name=[Production] |
 '(' expression=OrExpression ')'
 ;
 |  |  |  |  | 
| Re: [xText] syntax highlighting [message #55552 is a reply to message #55525] | Fri, 03 July 2009 11:40   |  | 
| Eclipse User  |  |  |  |  | Hi Anders, 
 in your case, you'll need semantic highlighting.
 
 First of all, please register a highlighting configuration. It should
 look like this:
 
 public class SemanticHighlightingConfiguration
 implements ISemanticHighlightingConfiguration {
 
 public final static String XML = "XML";
 
 public void configure(
 IHighlightingConfigurationAcceptor acceptor) {
 acceptor.acceptDefaultHighlighting(XML, "XML  Element",
 xmlStyle());
 }
 
 public TextStyle defaultTextStyle() {
 TextStyle textStyle = new TextStyle();
 textStyle.setBackgroundColor(new RGB(255, 255, 255));
 textStyle.setColor(new RGB(0, 0, 0));
 return textStyle;
 }
 
 public TextStyle xmlStyle() {
 TextStyle textStyle = defaultTextStyle().copy();
 textStyle.setStyle(SWT.BOLD);
 return textStyle;
 }
 }
 
 The second part is to implement the algorithm, that computes the
 highlighted ranges in your file:
 
 public class SemanticHighlightingCalculator implements
 ISemanticHighlightingCalculator {
 
 public void provideHighlightingFor(XtextResource resource,
 IHighlightedPositionAcceptor acceptor) {
 if (resource == null)
 return;
 
 Iterator<EObject> iter =
 EcoreUtil.getAllContents(resource, true);
 while(iter.hasNext()) {
 EObject current = iter.next();
 if (current instanceof XmlNonTerminal) {
 AbstractNode begin =
 getFirstFeatureNode(current,
 XmlGrammar.Literals.XML_NON_TERMINAL__BEGIN.getName());
 highlightNode(begin, SemanticHighlightingConfiguration.XML, acceptor);
 AbstractNode end =
 getFirstFeatureNode(current,
 XmlGrammar.Literals.XML_NON_TERMINAL__END.getName());
 highlightNode(end, SemanticHighlightingConfiguration.XML, acceptor);
 } else if (current instanceof XmlTerminal) {
 AbstractNode name = getFirstFeatureNode(current,
 XmlGrammar.Literals.XML_TERMINAL__NAME.getName());
 highlightNode(node, SemanticHighlightingConfiguration.XML, acceptor);
 }
 }
 }
 
 private void highlightNode(AbstractNode node, String id,
 IHighlightedPositionAcceptor acceptor) {
 if (node == null)
 return;
 if (node instanceof LeafNode) {
 acceptor.addPosition(node.getOffset(), node.getLength(), id);
 } else {
 for (LeafNode leaf: node.getLeafNodes()) {
 if (!leaf.isHidden()) {
 acceptor.addPosition(leaf.getOffset(), leaf.getLength(), id);
 }
 }
 }
 }
 
 public AbstractNode getFirstFeatureNode(EObject semantic, String feature) {
 NodeAdapter adapter = NodeUtil.getNodeAdapter(semantic);
 if (adapter != null) {
 CompositeNode node = adapter.getParserNode();
 if (node != null) {
 for (AbstractNode child: node.getChildren()) {
 if (child instanceof LeafNode) {
 if (feature.equals(((LeafNode) child).getFeature())) {
 return child;
 }
 }
 }
 }
 }
 return null;
 }
 
 }
 
 To enable the bold style for the '<' and '>' -signs as well, you should
 go with the token to id mapper as it is responsible for the highlighting
 configuration of keywords. Please try something like this:
 
 public class TokenToIdMapper extends DefaultAntlrTokenToAttributeIdMapper {
 
 @Override
 protected String calculateId(String tokenName, int tokenType) {
 if ("'<'".equals(tokenName) || "'/>'".equals(tokenName) ||
 "'>'".equals(tokenName)) {
 return SemanticHighlightingConfiguration.XML;
 }
 }
 }
 
 Finally don't forget to bind your implementations in your ui module:
 
 public class XmlGrammarUiModule extends AbstractXtextUiModule {
 
 public Class<? extends ISemanticHighlightingCalculator>
 bindISemanticHighlightingCalculator() {
 return SemanticHighlightingCalculator.class;
 }
 
 public Class<? extends ISemanticHighlightingConfiguration>
 bindISemanticHighlightingConfiguration() {
 return SemanticHighlightingConfiguration.class;
 }
 
 public Class<? extends AbstractAntlrTokenToAttributeIdMapper>
 bindTokenToAttributeIdMapper() {
 return DefaultAntlrTokenToAttributeIdMapper.class;
 }
 }
 
 Can you please elaborate on what you mean by "to abstract" in the
 appropriate section of the documentation? I'ld be happy to improve it :-)
 
 Hope that helps,
 Sebastian
 
 btw: the code snippets come without any guarantee ;-)
 
 Am 03.07.2009 16:58 Uhr, schrieb Anders Hessellund:
 > Hi,
 >
 > my first little grammar is progressing and the editor-features that
 > xText allows with minimal code are amazing! I am, however, a bit stuck
 > on the syntax highlighting part. The documentation is a bit too
 > abstract. Specifically, in the grammar below, I'd like to do thing like
 > making XML elements bold. What are the steps, I need to go through?
 >
 > Thanks,
 >
 > Anders
 >
 > grammar c.m.XmlGrammar with org.eclipse.xtext.common.Terminals
 >
 > generate xmlGrammar "http://www.example.com/XmlGrammar"
 >
 > Model :
 > (productions+=Production)*
 > ;
 >
 > Production :
 > name=ID '=' expression=OrExpression ';'
 > ;
 >
 > OrExpression :
 > Expression ( '|' right=OrExpression )?
 > ;
 >
 > Expression :
 > XmlElement |
 > expressions=QuantifiedExpression+
 > ;
 >
 > XmlElement :
 > XmlNonTerminal |
 > XmlTerminal
 > ;
 >
 > XmlNonTerminal :
 > '<' begin=ID '>'
 > expression=Expression
 > '</' end=ID '>'
 > ;
 >
 > XmlTerminal :
 > '<' name=ID '/>'
 > ;
 >
 > QuantifiedExpression :
 > TerminalExpression ( '?' | '*' | '+' )?
 > ;
 >
 > TerminalExpression :
 > name=[Production] |
 > '(' expression=OrExpression ')'
 > ;
 |  |  |  |  | 
| Re: [xText] syntax highlighting [message #55634 is a reply to message #55552] | Fri, 03 July 2009 15:43   |  | 
| Eclipse User  |  |  |  |  | Originally posted by: anders.hessellund.gmail.com 
 Sebastian Zarnekow wrote:
 > Hi Anders,
 >
 > in your case, you'll need semantic highlighting.
 >
 > First of all, please register a highlighting configuration. It should
 > look like this:
 >
 > public class SemanticHighlightingConfiguration
 >     implements ISemanticHighlightingConfiguration {
 >
 >     public final static String XML = "XML";
 >
 >     public void configure(
 >         IHighlightingConfigurationAcceptor acceptor) {
 >         acceptor.acceptDefaultHighlighting(XML, "XML  Element",
 >             xmlStyle());
 >     }
 >
 >     public TextStyle defaultTextStyle() {
 >         TextStyle textStyle = new TextStyle();
 >         textStyle.setBackgroundColor(new RGB(255, 255, 255));
 >         textStyle.setColor(new RGB(0, 0, 0));
 >         return textStyle;
 >     }
 >
 >     public TextStyle xmlStyle() {
 >         TextStyle textStyle = defaultTextStyle().copy();
 >         textStyle.setStyle(SWT.BOLD);
 >         return textStyle;
 >     }
 > }
 >
 > The second part is to implement the algorithm, that computes the
 > highlighted ranges in your file:
 >
 > public class SemanticHighlightingCalculator implements
 > ISemanticHighlightingCalculator {
 >
 >     public void provideHighlightingFor(XtextResource resource,
 >             IHighlightedPositionAcceptor acceptor) {
 >         if (resource == null)
 >             return;
 >
 >         Iterator<EObject> iter =
 >             EcoreUtil.getAllContents(resource, true);
 >         while(iter.hasNext()) {
 >             EObject current = iter.next();
 >             if (current instanceof XmlNonTerminal) {
 >                 AbstractNode begin =
 >                     getFirstFeatureNode(current,
 > XmlGrammar.Literals.XML_NON_TERMINAL__BEGIN.getName());
 >                 highlightNode(begin,
 > SemanticHighlightingConfiguration.XML, acceptor);
 >                 AbstractNode end =
 >                     getFirstFeatureNode(current,
 > XmlGrammar.Literals.XML_NON_TERMINAL__END.getName());
 >                 highlightNode(end,
 > SemanticHighlightingConfiguration.XML, acceptor);
 >             } else if (current instanceof XmlTerminal) {
 >                 AbstractNode name = getFirstFeatureNode(current,
 > XmlGrammar.Literals.XML_TERMINAL__NAME.getName());
 >                 highlightNode(node,
 > SemanticHighlightingConfiguration.XML, acceptor);
 >             }
 >         }
 >     }
 >
 >     private void highlightNode(AbstractNode node, String id,
 > IHighlightedPositionAcceptor acceptor) {
 >         if (node == null)
 >             return;
 >         if (node instanceof LeafNode) {
 >             acceptor.addPosition(node.getOffset(), node.getLength(), id);
 >         } else {
 >             for (LeafNode leaf: node.getLeafNodes()) {
 >                 if (!leaf.isHidden()) {
 >                     acceptor.addPosition(leaf.getOffset(),
 > leaf.getLength(), id);
 >                 }
 >             }
 >         }
 >     }
 >
 >     public AbstractNode getFirstFeatureNode(EObject semantic, String
 > feature) {
 >         NodeAdapter adapter = NodeUtil.getNodeAdapter(semantic);
 >         if (adapter != null) {
 >             CompositeNode node = adapter.getParserNode();
 >             if (node != null) {
 >                 for (AbstractNode child: node.getChildren()) {
 >                     if (child instanceof LeafNode) {
 >                         if (feature.equals(((LeafNode)
 > child).getFeature())) {
 >                             return child;
 >                         }
 >                     }
 >                 }
 >             }
 >         }
 >         return null;
 >     }
 >
 > }
 >
 > To enable the bold style for the '<' and '>' -signs as well, you should
 > go with the token to id mapper as it is responsible for the highlighting
 > configuration of keywords. Please try something like this:
 >
 > public class TokenToIdMapper extends DefaultAntlrTokenToAttributeIdMapper {
 >
 >     @Override
 >     protected String calculateId(String tokenName, int tokenType) {
 >         if ("'<'".equals(tokenName) || "'/>'".equals(tokenName) ||
 > "'>'".equals(tokenName)) {
 >             return SemanticHighlightingConfiguration.XML;
 >         }
 >     }
 > }
 >
 > Finally don't forget to bind your implementations in your ui module:
 >
 > public class XmlGrammarUiModule extends AbstractXtextUiModule {
 >
 >     public Class<? extends ISemanticHighlightingCalculator>
 > bindISemanticHighlightingCalculator() {
 >         return SemanticHighlightingCalculator.class;
 >     }
 >
 >     public Class<? extends ISemanticHighlightingConfiguration>
 > bindISemanticHighlightingConfiguration() {
 >         return SemanticHighlightingConfiguration.class;
 >     }
 >
 >     public Class<? extends AbstractAntlrTokenToAttributeIdMapper>
 > bindTokenToAttributeIdMapper() {
 >         return DefaultAntlrTokenToAttributeIdMapper.class;
 >     }
 > }
 >
 > Can you please elaborate on what you mean by "to abstract" in the
 > appropriate section of the documentation? I'ld be happy to improve it :-)
 >
 > Hope that helps,
 > Sebastian
 >
 > btw: the code snippets come without any guarantee ;-)
 >
 > Am 03.07.2009 16:58 Uhr, schrieb Anders Hessellund:
 >> Hi,
 >>
 >> my first little grammar is progressing and the editor-features that
 >> xText allows with minimal code are amazing! I am, however, a bit stuck
 >> on the syntax highlighting part. The documentation is a bit too
 >> abstract. Specifically, in the grammar below, I'd like to do thing like
 >> making XML elements bold. What are the steps, I need to go through?
 >>
 >> Thanks,
 >>
 >> Anders
 >>
 >> grammar c.m.XmlGrammar with org.eclipse.xtext.common.Terminals
 >>
 >> generate xmlGrammar "http://www.example.com/XmlGrammar"
 >>
 >> Model :
 >> (productions+=Production)*
 >> ;
 >>
 >> Production :
 >> name=ID '=' expression=OrExpression ';'
 >> ;
 >>
 >> OrExpression :
 >> Expression ( '|' right=OrExpression )?
 >> ;
 >>
 >> Expression :
 >> XmlElement |
 >> expressions=QuantifiedExpression+
 >> ;
 >>
 >> XmlElement :
 >> XmlNonTerminal |
 >> XmlTerminal
 >> ;
 >>
 >> XmlNonTerminal :
 >> '<' begin=ID '>'
 >> expression=Expression
 >> '</' end=ID '>'
 >> ;
 >>
 >> XmlTerminal :
 >> '<' name=ID '/>'
 >> ;
 >>
 >> QuantifiedExpression :
 >> TerminalExpression ( '?' | '*' | '+' )?
 >> ;
 >>
 >> TerminalExpression :
 >> name=[Production] |
 >> '(' expression=OrExpression ')'
 >> ;
 >
 Thanks a lot!!
 
 This was extremely helpful!
 
 My comment about the documentation being "too abstract" is, I think,
 mainly due to the lack of a proper recipe. Since syntax coloring is
 probably the first thing most people will turn too when they succeed in
 generating an editor, it would make sense to have a step-by-step recipe
 (like the one you gave me above). I could probably have figured some of
 these things out but it would have taken a lot more time since javadoc
 appears to be missing.
 
 Anyway, I don't mean to mock xText. I am actually extremely impressed
 with your product and very pleased with the rapid community feedback.
 
 Thanks,
 
 Anders
 |  |  |  |  | 
| Re: [xText] syntax highlighting [message #55661 is a reply to message #55634] | Fri, 03 July 2009 16:03   |  | 
| Eclipse User  |  |  |  |  | Hi Anders, 
 see below.
 
 Am 03.07.2009 21:43 Uhr, schrieb Anders Hessellund:
 
 [snip]
 > Thanks a lot!!
 >
 > This was extremely helpful!
 
 :-)
 
 >
 > My comment about the documentation being "too abstract" is, I think,
 > mainly due to the lack of a proper recipe. Since syntax coloring is
 > probably the first thing most people will turn too when they succeed in
 > generating an editor, it would make sense to have a step-by-step recipe
 > (like the one you gave me above). I could probably have figured some of
 > these things out but it would have taken a lot more time since javadoc
 > appears to be missing.
 >
 
 Thanks for the hint, I think I can add something similar to the docs.
 Btw: Have you tried the version that can be downloaded from the itemis
 website http://Xtext.itemis.com ?
 It is more up to date (slightly) and contains some javadocs for the
 syntax highlighter. Nevertheless we are aware of the fact, that some
 large pieces of the Xtext framework lack comprehensive javadocs and we
 try to improve it steadily.
 
 > Anyway, I don't mean to mock xText. I am actually extremely impressed
 > with your product and very pleased with the rapid community feedback.
 >
 
 Thanks for the nice words.
 
 Regards,
 Sebastian
 
 > Thanks,
 >
 > Anders
 |  |  |  |  | 
| Re: [xText] syntax highlighting [message #55816 is a reply to message #55661] | Mon, 06 July 2009 04:45  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: anders.hessellund.gmail.com 
 > Thanks for the hint, I think I can add something similar to the docs.
 > Btw: Have you tried the version that can be downloaded from the itemis
 > website http://Xtext.itemis.com ?
 > It is more up to date (slightly) and contains some javadocs for the
 > syntax highlighter. Nevertheless we are aware of the fact, that some
 > large pieces of the Xtext framework lack comprehensive javadocs and we
 > try to improve it steadily.
 
 No, I haven't tried that yet.
 
 Btw, when you're updating the documentation, you might want to consider
 adding a note about the deployment process. Specifically, which features
 (antlr, xtext etc) do you need when deploying a generated xText editor
 as a plugin or a feature.
 
 Cheers,
 
 Anders
 |  |  |  | 
 
 
 Current Time: Fri Oct 31 05:31:50 EDT 2025 
 Powered by FUDForum . Page generated in 0.03886 seconds |