0

I add a new Keywords object to the main rule set at run time. But except those keywords, other rules are colored properly.

Can anyone explain why words loaded at runtime don't get highlighted?

using (Stream stream = typeof(Window1).Assembly.GetManifestResourceStream("testAvalonEdit.MyLang.xshd")) {
    using (XmlReader reader = new XmlTextReader(stream)) {
        xshd = HighlightingLoader.LoadXshd(reader);
        customHighlighting = HighlightingLoader.Load(xshd, HighlightingManager.Instance);
        updateStandardParametersList();
    }
}
HighlightingManager.Instance.RegisterHighlighting("MyLang Highlighting", new string[ { ".s" }, customHighlighting);


Where, the highlighted method is:

void updateStandardParametersList() {
    //find existing color. It exists for sure.
    XshdColor existingColor = xshd.Elements.OfType<XshdColor>().Where(xc => string.Equals(xc.Name, "StandardParametersColor", StringComparison.CurrentCultureIgnoreCase)).First();

    XshdKeywords newKeyWords = new XshdKeywords();
    newKeyWords.ColorReference = new XshdReference<XshdColor>(existingColor);

    //I add new words to the Keywords object
    for(int i = 1; i < 25; i++)
        newKeyWords.Words.Add("A000" + i.ToString("00"));
    for(int i = 1; i < 25; i++)
        newKeyWords.Words.Add("B000" + i.ToString("00"));
    for(int i = 1; i < 5; i++)
        newKeyWords.Words.Add("C0000" + i);

    XshdRuleSet mainRuleSet = xshd.Elements.OfType<XshdRuleSet>().Where(o => string.IsNullOrEmpty(o.Name)).First();
    mainRuleSet.Elements.Add(newKeyWords);
}

Thanks!


Update 1

After trying Daniel's suggestion,

xshd = HighlightingLoader.LoadXshd(reader);
updateStandardParametersList();
customHighlighting = HighlightingLoader.Load(xshd, HighlightingManager.Instance);

I get this exception:

enter image description here

So, why is this exception thrown? All I am trying to do is add Keywords object and setting its color to a predefined color in XSHD.

Or, is it not the right way?

Nayan
  • 3,092
  • 25
  • 34

1 Answers1

2

The call HighlightingLoader.Load(xshd) creates an IHighlightingDefinition from the information stored in the xshd object. If you change the xshd later, the IHighlightingDefinition won't know about those changes.

To fix this problem, call HighlightingLoader.Load() only after you are done updating the highlighting:

    xshd = HighlightingLoader.LoadXshd(reader);
    updateStandardParametersList();
    customHighlighting = HighlightingLoader.Load(xshd, HighlightingManager.Instance);

For the duplicate color exception: the expression new XshdReference<XshdColor>(existingColor) corresponds to an XSHD color that is defined inline on the keywords element (it is a color definition, not just a reference). Thus, you have multiple definitions for the color.

To create a reference to the existing named color, use:

    newKeyWords.ColorReference = new XshdReference<XshdColor>(null, "StandardParametersColor");
Daniel
  • 15,944
  • 2
  • 54
  • 60
  • Daniel, i tried that first, but observed an ecception. That's why this code was written this way. But i'll try again. But i was hoping that the changes in list were observable. Maybe i'll share such implementation with you. – Nayan Apr 23 '14 at 17:35
  • Daniel, can you please comment on the update I provided? – Nayan Apr 24 '14 at 16:36
  • Thanks Daniel, the new solution worked. Now, I will work on making dynamic addition not so hard. :) – Nayan Apr 24 '14 at 18:26
  • Daniel, since there are not many training materials on AvalonEdit, how can I get answers (to complex requirements) quickly? Here on stackoverflow? – Nayan Apr 24 '14 at 18:28