highlighting question

SyntaxEditor for Windows Forms Forum

Posted 12 years ago by Anzor
Version: 4.0.0261
Avatar
Hi, I have a question about highlighting.

I'm using a SyntaxEditor control to write log messages in the following example format:

error: File not found 'gunship.asd'
debug: Loading module '/lua/levels/currentlevel.lua'
info: Loaded file: ui/energy_icon.asd
-size: 23425 bytes
-scope: local
info: Initialization time: 0.84 seconds.

(Individual messages may span multiple lines).

The goal is to have each message (including prefix) be a different highlight. For example error messages are read, debug messages are blue, etc.

I have already achieved this in the following way.

// Construct a sample language that shows many highlighting style features
DynamicSyntaxLanguage language = new DynamicSyntaxLanguage("Sample", false);
language.IsUpdating = true;

string[] prefixes = new string[] { "Debug", "Info", "Warning", "Error" };
Dictionary<string, Color> colors = new Dictionary<string, Color>();
colors["Debug"] = Color.Turquoise;
colors["Info"] = Color.SaddleBrown;
colors["Warning"] = Color.Salmon;
colors["Error"] = Color.Olive;

string allPrefixes = "Debug:|Info:|Warning:|Error:";

foreach (string prefix in prefixes)
{
    HighlightingStyle contentStyle = new HighlightingStyle(prefix + "StyleC", null, colors[prefix], Color.Empty, DefaultableBoolean.False, DefaultableBoolean.Default, HighlightingStyleLineStyle.Default);
    HighlightingStyle titleStyle = new HighlightingStyle(prefix + "StyleT", null, colors[prefix], Color.Empty, DefaultableBoolean.True, DefaultableBoolean.Default, HighlightingStyleLineStyle.Default);
    language.HighlightingStyles.Add(contentStyle);
    language.HighlightingStyles.Add(titleStyle);

    // Create an message state
    DynamicLexicalState messageState = new DynamicLexicalState(0, prefix + "MessageState");
    messageState.DefaultTokenKey = prefix + "MessageDefaultToken";
    messageState.DefaultHighlightingStyle = contentStyle;
    language.LexicalStates.Add(messageState);
    language.DefaultLexicalState.ChildLexicalStates.Add(messageState);

    DynamicLexicalScope lexicalScope = new DynamicLexicalScope();
    lexicalScope.StartLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Regex, prefix + "MessageStartToken", titleStyle, string.Format("^{0}:", prefix));
    lexicalScope.EndLexicalPatternGroup = new LexicalPatternGroup(LexicalPatternType.Regex, prefix + "MessageEndToken", titleStyle, @"{LineTerminatorMacro}", allPrefixes);

    messageState.LexicalScopes.Add(lexicalScope);
}

language.IsUpdating = false;
this.syntaxEditor1.Document.Language = language;
However, I have recently received feedback from my users saying that the prefixes (error:, warning:, etc) being written over and over are annoying, and that they already know what type of message it is based on the color.

My question is, is there a way I could remove these prefixes from being shown, and still maintain color-codedness across the different message types?

Any help would be appreciated.
Thanks! :)

Comments (4)

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Anzor,

Unfortunately with controls like SyntaxEditor, the highlighting is based on the text in the document so you do need some sort of prefix for your line. You might be able to leverage the DocumentLine.BackColor feature that lets you provide a background color for a particular line. But you probably want to change text forecolor and not backcolor. Span indicators would work for foreground too however if you start adding a ton of them it will slow down performance.

Really in this scenario you might be better off using a RichTextBox.


Actipro Software Support

Posted 12 years ago by Anzor
Avatar
Yea...

The main reason I decided to use this control instead of a normal textbox is because a textbox can fill up. In my application, there is a lot of text that is logged (over time), and the textbox filling up surely happens.

Do you have any good ideas for how to handle this situation efficiently?
...Besides clearing the entire thing ;)
Posted 12 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Are there any non-printing characters available that could be used as markers in your text? Alternatively, you could maintain line-number information separately that tells you which lines are of which semantic types, and then use a post-parse handler to run through the tokens and update the highlighting styles on your tokens. Of course, I have no idea how / if you can do this with a dynamic language, but you definitely can with a syntaxlanguage derived language.

Kelly Leahy Software Architect Milliman, USA

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
That's an interesting idea... you could make your lexical parser parse each line into a single complete token. Then in the semantic parse phase, iterate through the new lines added, and update the token's CustomHighlightingStyle property for each token (line). Note that this requires the use of a dynamic language to use that property.

But other than doing something like that, you really do need some sort of text pattern to look for. I'm not sure if there are any zero width characters, you could try that as well if you do find some.


Actipro Software Support

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

Add Comment

Please log in to a validated account to post comments.