Setting the language after applying the dark theme fails to color code correctly

SyntaxEditor for Windows Forms Forum

Posted 3 months ago by AF
Version: 20.1.0401
Platform: .NET 4.7
Environment: Windows 10 (64-bit)
Avatar

If you set the Dark theme before setting the language, the code fails to apply the correct C# colors:

public Form1()
{
    // Register display item classification types
    new DisplayItemClassificationTypeProvider().RegisterAll();

    // Load Dark Theme
    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApp4.Dark.vssettings"))
    {
        if (stream != null)
            AmbientHighlightingStyleRegistry.Instance.ImportHighlightingStyles(stream);
    }

    // Create syntax editor
    InitializeComponent();

    // Set Language to C#
    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApp4.CSharp.langdef"))
    {
        if (stream != null)
        {
            SyntaxLanguageDefinitionSerializer serializer = new SyntaxLanguageDefinitionSerializer();
            syntaxEditor1.Document.Language = serializer.LoadFromStream(stream);
        }
    }
}

If you change it so the Dark theme is applied after setting the language it works OK.

Do I need to call something else after setting the langauge to make the above work?

I think this is a common scenario as setting the Dark theme at application startup makes more sense than after you have created a new view containing the syntax editor.

Comments (3)

Posted 3 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

I'm trying to reproduce the same thing in our main demo, but it appears to be working correctly.  Please see what the result is for you with these steps:

1) Open "SDI Code Editor" sample.

2) Choose the "File / Import Visual Studio Settings" menu item and pick the Dark.vssettings file we ship in the "SampleBrowser\ProductSamples\SyntaxEditorSamples\Languages\Themes" folder.  You should now see the editor in dark theme.

3) Choose the "Language / C#" menu item, which will load the CSharp.langdef file.  The editor remains in dark theme with C# keywords colored in appropriate dark colors, which is as intended.

Do you see anything different?


Actipro Software Support

Posted 3 months ago by AF
Avatar

Hi,

The sequence in the sample is different, i.e. you have already created a syntaxeditor and set the language in the MainControl() constructor BEFORE you then apply the Dark Theme.

Typically an MDI application would set the theme BEFORE creating your first view which contains the syntax editor.

To replicate this in your sample, simply change the first 2 lines in the constructor to be...

public MainControl() {

	// ADD THIS LINE HERE OR DARK THEME IS NOT APPLIED
	new DisplayItemClassificationTypeProvider().RegisterAll();

	// APPLY DARK THEME
	OpenVSSettingsFile();

	// NOW CREATE SYNTAX EDITOR
	InitializeComponent();

	// etc etc
}

...and now the language colors don't work.

Answer - Posted 3 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Thanks for the information, I see the problem.  The registry.ImportHighlightingStyles call only updates the IHighlightingStyles for classification types that have already been registered with the registry.  In the order you did things, the basic C# classification types (Keyword, etc.) weren't yet registered with the registry, so there was nothing for the ImportHighlightingStyles call to override.

If you preload your language (like this in our sample) before the ImportHighlightingStyles call, then it works fine:

SyntaxEditorHelper.LoadLanguageDefinitionFromResourceStream("CSharp.langdef");


Actipro Software Support

The latest build of this product (v2020.1 build 0402) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.