Search code examples
c#wpfcolorssyntax-highlightingavalonedit

How to change syntax highlighting colors of Avalon edit (WPF)?


I'm using the AvalonEdit text editor for a project in WPF and I need to change the syntax colors of a language.

I installed the Nuget package and wrote xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit" in the XAML of the MainWindow.

<avalonEdit:TextEditor Foreground="White" SyntaxHighlighting="PHP" ShowLineNumbers="True" FontSize="16">

How can I change the colors of a language? I saw some files in the AvalonEdit Sample demo folder "ICSharpCode.AvalonEdit\Highlighting\Resources", but I dont Know how to use them.


Solution

  • If you don't want to change the syntax rules but just change the colors it is quite easy. I changed them to match Visual Studio for C# like this (editor refers to my ICSharpCode.AvalonEdit.TextEditor):

    var highlighting = editor.SyntaxHighlighting;
    highlighting.GetNamedColor("StringInterpolation").Foreground = new SimpleHighlightingBrush(Colors.Black);
    highlighting.GetNamedColor("Punctuation").Foreground = new SimpleHighlightingBrush(Colors.Black);
    highlighting.GetNamedColor("NumberLiteral").Foreground = new SimpleHighlightingBrush(Colors.Black);
    highlighting.GetNamedColor("Comment").Foreground = new SimpleHighlightingBrush(Colors.ForestGreen);
    highlighting.GetNamedColor("MethodCall").Foreground = new SimpleHighlightingBrush(Colors.DarkGoldenrod);
    highlighting.GetNamedColor("GetSetAddRemove").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("Visibility").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("ParameterModifiers").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("Modifiers").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("String").Foreground = new SimpleHighlightingBrush(Colors.Brown);
    highlighting.GetNamedColor("Char").Foreground = new SimpleHighlightingBrush(Colors.Red);
    highlighting.GetNamedColor("Preprocessor").Foreground = new SimpleHighlightingBrush(Colors.DarkGray);
    highlighting.GetNamedColor("TrueFalse").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("Keywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("ValueTypeKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("SemanticKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("NamespaceKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("ReferenceTypeKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("ThisOrBaseReference").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("NullOrValueKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("GotoKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("ContextKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("ExceptionKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("CheckedKeyword").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("UnsafeKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("OperatorKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    highlighting.GetNamedColor("SemanticKeywords").Foreground = new SimpleHighlightingBrush(Colors.Blue);
    
    foreach (var color in highlighting.NamedHighlightingColors) {
        color.FontWeight = null;
    }
    editor.SyntaxHighlighting = null;
    editor.SyntaxHighlighting = highlighting;