1

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.

gNt
  • 79
  • 1
  • 9

2 Answers2

1

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;
henon
  • 2,022
  • 21
  • 23
0

You can change syntax highlighting by loading your own highlighting definition at run time. See this entry for more details: How do I create an AvalonEdit syntax file (.xshd) and embed it into my assembly?

user8276908
  • 1,051
  • 8
  • 20