-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customizable syntax highlighting for AvaloniaEdit
- Loading branch information
Showing
4 changed files
with
63 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
ShowMeTheXaml.Avalonia.AvaloniaEdit/XamlDisplayAvaloniaEditThemeBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using Avalonia; | ||
using Avalonia.Xaml.Interactivity; | ||
using AvaloniaEdit; | ||
using AvaloniaEdit.TextMate; | ||
using TextMateSharp.Grammars; | ||
|
||
namespace ShowMeTheXaml.Avalonia.AvaloniaEdit; | ||
|
||
public class XamlDisplayAvaloniaEditThemeBehavior : Behavior<TextEditor> { | ||
private IDisposable? _disposable; | ||
public static readonly AttachedProperty<ThemeName> CodeHighlightThemeNameProperty = | ||
XamlDisplayAvaloniaEdit.CodeHighlightThemeNameProperty.AddOwner<XamlDisplayAvaloniaEditThemeBehavior>(); | ||
private RegistryOptions? _registryOptions; | ||
private TextMate.Installation? _textMateInstallation; | ||
|
||
public ThemeName CodeHighlightThemeName { | ||
get => GetValue(CodeHighlightThemeNameProperty); | ||
set => SetValue(CodeHighlightThemeNameProperty, value); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void OnAttachedToVisualTree() { | ||
base.OnAttachedToVisualTree(); | ||
|
||
_registryOptions = new RegistryOptions(CodeHighlightThemeName); | ||
_textMateInstallation = AssociatedObject!.InstallTextMate(_registryOptions); | ||
_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId("xml")); | ||
|
||
_disposable = this.GetObservable(CodeHighlightThemeNameProperty) | ||
.Subscribe(name => { | ||
_textMateInstallation.SetTheme(_registryOptions.LoadTheme(name)); | ||
}); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void OnDetachedFromVisualTree() { | ||
base.OnDetachedFromVisualTree(); | ||
_disposable?.Dispose(); | ||
_textMateInstallation?.Dispose(); | ||
} | ||
} | ||
|
||
public static class XamlDisplayAvaloniaEdit { | ||
public static readonly AttachedProperty<ThemeName> CodeHighlightThemeNameProperty = | ||
AvaloniaProperty.RegisterAttached<XamlDisplay, ThemeName>("CodeHighlightThemeName", typeof(XamlDisplayAvaloniaEdit)); | ||
|
||
public static ThemeName GetCodeHighlightThemeName(XamlDisplay element) { | ||
return element.GetValue(CodeHighlightThemeNameProperty); | ||
} | ||
|
||
public static void SetCodeHighlightThemeName(XamlDisplay element, ThemeName value) { | ||
element.SetValue(CodeHighlightThemeNameProperty, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters