Posted 12 years ago by Brian
Version: 4.0.0271
Avatar
How can I make a subset of the tags in my schema be colored differently? For example, if I were using the XHTML schema and wanted <div> and <p> to be bold and blue text.

Thanks,
Brian

Comments (9)

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Since the XML language is implemented as a dynamic language, its tokens are of type DynamicToken and have a CustomHighlightingStyle property on them. You can set this immediately after the lexical parsing phase how you wish. Maybe override PerformLexicalParse in your language class and implement this code after calling the base method but only update tokens in the range that was parsed.


Actipro Software Support

Posted 12 years ago by Brian
Avatar
How do I get the tokens from the range that was parsed while in that method?
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
The return value of PerformLexicalParse is the TextRange that was updated. When you use a TextStream to scan tokens, only go over tokens in that TextRange.


Actipro Software Support

Posted 12 years ago by Brian
Avatar
Is this loop correct?


       public override TextRange PerformLexicalParse(Document document, TextRange parseTextRange, ILexicalParseTarget parseTarget)
        {
            TextRange range = base.PerformLexicalParse(document, parseTextRange, parseTarget);

            TextStream stream = document.GetTextStream(range.StartOffset);
            while (stream.Offset <= range.EndOffset)
            {
                DynamicToken token = (DynamicToken) stream.ReadToken();

                // Apply styling here
            }
            return range;
        }


If so, how do I determine:
1) That the token is a start tag that is in a given XML namespace.
2) The local name of the XML element in that namespace.

That is the information I will need to provide the correct highlighting.

Thanks,
Brian

[Modified at 02/14/2008 11:23 AM]
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Yes that looks like the right sort of code.

Your other questions are where things get tricky since the tokens don't store that info. You'd have to get an XmlContext at each tag start, which may be expensive timewise if you do it a ton. You'd have to try it to see how much it impacts perf. But also, you'd not want to run that until you had the semantic parsing completed. So instead, you'd want to override OnDocumentSemanticParseDataChanged and cache the text range somehow since it doesn't know the range at that point.


Actipro Software Support

Posted 12 years ago by Brian
Avatar
When I had the language defined as XML, this was pretty easy to do by adding a couple extra State's and the custom styles tied to those states. Could I just programatically add states to the language instead of going through all of this?
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
The semantic parser will look for specific token ID's. So if you made your extra states and tokens match the existing corresponding XmlLexicalStateID and XmlTokenID values, that could possibly work. You'd want to run a simple test before diving too deeply into it to verify. Please let us know the results.


Actipro Software Support

Posted 12 years ago by Brian
Avatar
Before I do that, do you have any plans for adding more direct support for this type of thing? It just seems like I should be able to do something like this out-of-the-box:


xmlSyntaxLangage.ElementStyleRules.Add(
     "http://www.w3.org/1999/xhtml",   
     "div", 
     new HighlightingStyle(....));

Thanks,
Brian
Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Possibly however it can't really be done with the current setup because the lexical parser is completely separate from the semantic parser so the lexer doesn't know or care what element is what. This is a good idea for future major versions though if we can find a way to do it.


Actipro Software Support

The latest build of this product (v2018.1 build 0341) was released 10 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.