Adding DynamicLexicalPattern during runtime

SyntaxEditor for WPF Forum

Posted 8 years ago by Danny
Version: 11.1.0543
Avatar
I have integrated the WPF Syntax Editor into my solution and everything is great but when I add a new DynamicLexicalPattern to an existing DynamicLexicalPatternGroup that pattern is added and seems fine using the debugger, but the patten doesn't get highlighted in the editor.

Details:
1. I have an DynamicLexicalPatternGroup defined in the definitions file.
2. I initialize it via code, by clearing the DynamicLexicalPatternGroup and adding hundreds of patterns to it.
3. The patterns added to it work fine and get highlighted.
4. At a later stage (a user imports a library) and new patterns need to be highlighted, so I fetch the existing DynamicLexicalPatternGroup and add another pattern to it, either using Add or Insert.
5. When looking in the debugger the new pattern is indeed added to the group and seems just like all of the other patterns.
6. But the new pattern doesn't get highlighted.

Code:
// Code was cleared from irrelevant lines.
void InitPatterns()
{
DynamicLexer dynamicLexer = (DynamicLexer)this.GetLexer();
DynamicLexicalPatternGroup functionPatternGroup = dynamicLexer.LexicalStates["Default"].LexicalPatternGroups["Function"];
functionPatternGroup.Patterns.Clear();

// Gets my functions whose identifiers need to be added.
List<MyFunctions> funcs= IQLanguageDB.AllLibraries;
// Iterate over all of the functions.
for (int i = 0; i < funcs.Count; i++)
{
DynamicLexicalPattern pattern = new DynamicLexicalPattern(funcs[i].Identifier);
functionPatternGroup.Patterns.Add(pattern);
}
}

// Code was cleared from irrelevant lines.
void AddAnotherFunction(string functionIdentifier)
{
DynamicLexer dynamicLexer = (DynamicLexer)this.GetLexer();
DynamicLexicalPatternGroup functionPatternGroup = dynamicLexer.LexicalStates["Default"].LexicalPatternGroups["Function"];
DynamicLexicalPattern pattern = new DynamicLexicalPattern(functionIdentifier);
functionPatternGroup.Patterns.Add(pattern);
}

Summary:
InitPatterns works but only if I run it once, if I run it twice then the words are no longer highlighted. AddAnotherFunction sometimes works one time but doesn't work again, meaning words do get add to the pattern group but they don't get highlighted.

Please help!

Comments (2)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Yuval,

It is probably due to internal caching the lexer does for optimization. Please read the documentation topic called "Text/Parsing Framework - Lexing / Basic Concepts" and in that, the section "Changing Mergable Lexers". That using statement should always wrap any changes you make, and will likely fix the problem.


Actipro Software Support

Posted 8 years ago by Danny
Avatar
Thanks, the issue was resolved by adding the following code:

using (IDisposable batch = parentLexer.CreateChangeBatch()) {
// Update lexical states, patterns, etc. on parent lexer here
}
The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.