Skip to content

Commit

Permalink
feat: Add SyntaxHighlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
furesoft committed Oct 11, 2024
1 parent 2396ad3 commit 736fea9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Grid ColumnDefinitions="AUTO,*,AUTO" RowDefinitions="35,*,35">
<c:ExecutionBar Grid.Column="0" Grid.Row="0" Margin="5,3,0,0" />

<avaloniaEdit:TextEditor MinWidth="150" MaxHeight="450" MinHeight="150" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3"
<avaloniaEdit:TextEditor x:Name="editor" MinWidth="150" MaxHeight="450" MinHeight="150" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3"
FontFamily="Cascadia Code,Consolas,Menlo,Monospace" Margin="0,5,0,5"
ShowLineNumbers="True">
<i:Interaction.Behaviors>
Expand Down
19 changes: 18 additions & 1 deletion src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,5 +22,18 @@ public ProgramEditorControl()
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);

IHighlightingDefinition customHighlighting;
using (Stream s = GetType().Assembly.GetManifestResourceStream("MimaSim.Resources.CustomHighlighting.xshd"))

Check warning on line 27 in src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
{
using (XmlReader reader = new XmlTextReader(s))

Check warning on line 29 in src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'input' in 'XmlTextReader.XmlTextReader(Stream input)'.
{
customHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
}
}

HighlightingManager.Instance.RegisterHighlighting("Custom Highlighting", new string[] { ".cool" }, customHighlighting);

editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(".cool");
}
}
40 changes: 40 additions & 0 deletions src/MimaSim/MimaSim/Resources/CustomHighligting.xshd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<SyntaxDefinition name="Custom Highlighting" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" />
<Color name="String" foreground="Blue" />

<!-- This is the main ruleset. -->
<RuleSet>
<Span color="Comment" begin="//" />
<Span color="Comment" multiline="true" begin="/\*" end="\*/" />

<Span color="String">
<Begin>"</Begin>
<End>"</End>
<RuleSet>
<!-- nested span for escape sequences -->
<Span begin="\\" end="." />
</RuleSet>
</Span>

<Keywords fontWeight="bold" foreground="Blue">
<Word>if</Word>
<Word>else</Word>
<Word>var</Word>
</Keywords>

<Keywords fontWeight="bold" fontStyle="italic" foreground="Red">
<Word>AvalonEdit</Word>
</Keywords>

<!-- Digits -->
<Rule foreground="DarkBlue">
\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
</Rule>
</RuleSet>
</SyntaxDefinition>

0 comments on commit 736fea9

Please sign in to comment.