Question

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?

Was it helpful?

Solution

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top