From 736fea966b6a92e80abefd059484df2eb24de982 Mon Sep 17 00:00:00 2001 From: Chris Anders Date: Fri, 11 Oct 2024 06:53:28 +0000 Subject: [PATCH] feat: Add SyntaxHighlighting --- .../Controls/ProgramEditorControl.axaml | 2 +- .../Controls/ProgramEditorControl.axaml.cs | 19 ++++++++- .../MimaSim/Resources/CustomHighligting.xshd | 40 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/MimaSim/MimaSim/Resources/CustomHighligting.xshd diff --git a/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml b/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml index afda654e..d33f874e 100644 --- a/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml +++ b/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml @@ -12,7 +12,7 @@ - diff --git a/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs b/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs index fac5a5f9..17fb861e 100644 --- a/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs +++ b/src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs @@ -1,6 +1,10 @@ -using Avalonia.Controls; +using System.IO; +using System.Xml; +using Avalonia.Controls; using Avalonia.Markup.Xaml; using Avalonia.ReactiveUI; +using AvaloniaEdit.Highlighting; +using AvaloniaEdit.Highlighting.Xshd; using MimaSim.ViewModels; using ReactiveUI; @@ -18,5 +22,18 @@ public ProgramEditorControl() private void InitializeComponent() { AvaloniaXamlLoader.Load(this); + + IHighlightingDefinition customHighlighting; + using (Stream s = GetType().Assembly.GetManifestResourceStream("MimaSim.Resources.CustomHighlighting.xshd")) + { + using (XmlReader reader = new XmlTextReader(s)) + { + customHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance); + } + } + + HighlightingManager.Instance.RegisterHighlighting("Custom Highlighting", new string[] { ".cool" }, customHighlighting); + + editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(".cool"); } } \ No newline at end of file diff --git a/src/MimaSim/MimaSim/Resources/CustomHighligting.xshd b/src/MimaSim/MimaSim/Resources/CustomHighligting.xshd new file mode 100644 index 00000000..73fdb6b5 --- /dev/null +++ b/src/MimaSim/MimaSim/Resources/CustomHighligting.xshd @@ -0,0 +1,40 @@ + + + + + + + + + + + + " + " + + + + + + + + if + else + var + + + + AvalonEdit + + + + + \b0[xX][0-9a-fA-F]+ # hex number + | \b + ( \d+(\.[0-9]+)? #number with optional floating point + | \.[0-9]+ #or just starting with floating point + ) + ([eE][+-]?[0-9]+)? # optional exponent + + + \ No newline at end of file