Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/Models/TextMateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ private record ExtraGrammar(string Scope, List<string> Extensions, string File)

public class RegistryOptionsWrapper(ThemeName defaultTheme) : IRegistryOptions
{
public string LastScope { get; set; } = string.Empty;

public IRawTheme GetTheme(string scopeName) => _backend.GetTheme(scopeName);
public IRawTheme GetDefaultTheme() => _backend.GetDefaultTheme();
public IRawTheme LoadTheme(ThemeName name) => _backend.LoadTheme(name);
Expand All @@ -98,11 +96,20 @@ public class RegistryOptionsWrapper(ThemeName defaultTheme) : IRegistryOptions

public static class TextMateHelper
{
private static RegistryOptionsWrapper s_darkRegistry;
private static RegistryOptionsWrapper s_lightRegistry;

private static RegistryOptionsWrapper GetRegistry(bool isDark)
{
if (isDark)
return s_darkRegistry ??= new RegistryOptionsWrapper(ThemeName.DarkPlus);
return s_lightRegistry ??= new RegistryOptionsWrapper(ThemeName.LightPlus);
}

public static TextMate.Installation CreateForEditor(TextEditor editor)
{
return editor.InstallTextMate(Application.Current?.ActualThemeVariant == ThemeVariant.Dark ?
new RegistryOptionsWrapper(ThemeName.DarkPlus) :
new RegistryOptionsWrapper(ThemeName.LightPlus));
var isDark = Application.Current?.ActualThemeVariant == ThemeVariant.Dark;
return editor.InstallTextMate(GetRegistry(isDark == true));
}

public static void SetThemeByApp(TextMate.Installation installation)
Expand All @@ -114,18 +121,29 @@ public static void SetThemeByApp(TextMate.Installation installation)
}
}

public static void SetGrammarByFileName(TextMate.Installation installation, string filePath)
public static void SetGrammarByFileName(TextMate.Installation installation, string filePath, ref string lastScope)
{
if (installation is { RegistryOptions: RegistryOptionsWrapper reg } && !string.IsNullOrEmpty(filePath))
{
// Registry options are shared across editors, so the last grammar scope must stay editor-local.
var scope = reg.GetScope(filePath);
if (reg.LastScope != scope)
if (lastScope != scope)
{
reg.LastScope = scope;
installation.SetGrammar(reg.GetScope(filePath));
lastScope = scope;
installation.SetGrammar(scope);
GC.Collect();
}
}
}

public static void DisposeInstallation(ref TextMate.Installation installation, ref string lastScope, bool collectGarbage = false)
{
installation?.Dispose();
installation = null;
lastScope = string.Empty;

if (collectGarbage)
GC.Collect();
}
}
}
8 changes: 3 additions & 5 deletions src/Views/AIAssistant.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override void OnLoaded(RoutedEventArgs e)
if (_textMate == null)
{
_textMate = Models.TextMateHelper.CreateForEditor(this);
Models.TextMateHelper.SetGrammarByFileName(_textMate, "README.md");
Models.TextMateHelper.SetGrammarByFileName(_textMate, "README.md", ref _lastGrammarScope);
TextArea.TextView.LineTransformers.Add(new LineStyleTransformer());
}
}
Expand All @@ -75,10 +75,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;

if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope);

GC.Collect();
}
Expand Down Expand Up @@ -125,6 +122,7 @@ private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs
}

private TextMate.Installation _textMate = null;
private string _lastGrammarScope = string.Empty;
}

public partial class AIAssistant : ChromelessWindow
Expand Down
8 changes: 3 additions & 5 deletions src/Views/Blame.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
TextArea.TextView.VisualLinesChanged -= OnTextViewVisualLinesChanged;

if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
Expand All @@ -388,7 +385,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
if (change.Property == FileProperty)
{
if (File is { Length: > 0 })
Models.TextMateHelper.SetGrammarByFileName(_textMate, File);
Models.TextMateHelper.SetGrammarByFileName(_textMate, File, ref _lastGrammarScope);
}
if (change.Property == BlameDataProperty)
{
Expand Down Expand Up @@ -454,6 +451,7 @@ private void OnTextViewVisualLinesChanged(object sender, EventArgs e)
}

private TextMate.Installation _textMate = null;
private string _lastGrammarScope = string.Empty;
private string _highlight = string.Empty;
}

Expand Down
8 changes: 3 additions & 5 deletions src/Views/CommandLogContentPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected override void OnLoaded(RoutedEventArgs e)
if (_textMate == null)
{
_textMate = Models.TextMateHelper.CreateForEditor(this);
Models.TextMateHelper.SetGrammarByFileName(_textMate, "Log.log");
Models.TextMateHelper.SetGrammarByFileName(_textMate, "Log.log", ref _lastGrammarScope);
TextArea.TextView.LineTransformers.Add(new LineStyleTransformer());
}
}
Expand All @@ -117,10 +117,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
base.OnUnloaded(e);

if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope);

GC.Collect();
}
Expand Down Expand Up @@ -152,5 +149,6 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
}

private TextMate.Installation _textMate = null;
private string _lastGrammarScope = string.Empty;
}
}
10 changes: 4 additions & 6 deletions src/Views/MergeConflictEditor.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ protected override void OnLoaded(RoutedEventArgs e)

_textMate = Models.TextMateHelper.CreateForEditor(this);
if (!string.IsNullOrEmpty(FileName))
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName);
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName, ref _lastGrammarScope);

TextArea.TextView.ContextRequested += OnTextViewContextRequested;
TextArea.TextView.PointerEntered += OnTextViewPointerChanged;
Expand All @@ -331,10 +331,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
TextArea.TextView.VisualLinesChanged -= OnTextViewVisualLinesChanged;

if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope);

base.OnUnloaded(e);
}
Expand All @@ -346,7 +343,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
if (change.Property == LinesProperty)
UpdateContent();
else if (change.Property == FileNameProperty)
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName);
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName, ref _lastGrammarScope);
else if (change.Property.Name == nameof(ActualThemeVariant) && change.NewValue != null)
Models.TextMateHelper.SetThemeByApp(_textMate);
else if (change.Property == SelectedChunkProperty)
Expand Down Expand Up @@ -539,6 +536,7 @@ private void UpdateSelectedChunkPosition(ViewModels.MergeConflictEditor vm, doub
}

private TextMate.Installation _textMate;
private string _lastGrammarScope = string.Empty;
private ScrollViewer _scrollViewer;
}

Expand Down
14 changes: 5 additions & 9 deletions src/Views/RevisionFileContentViewer.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;

if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope);

GC.Collect();
}
Expand All @@ -82,7 +79,7 @@ protected override void OnDataContextChanged(EventArgs e)
if (DataContext is Models.RevisionTextFile source)
{
Text = source.Content;
Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName);
Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName, ref _lastGrammarScope);
ScrollToHome();
}
else
Expand Down Expand Up @@ -139,19 +136,18 @@ private void UpdateTextMate()
_textMate ??= Models.TextMateHelper.CreateForEditor(this);

if (DataContext is Models.RevisionTextFile file)
Models.TextMateHelper.SetGrammarByFileName(_textMate, file.FileName);
Models.TextMateHelper.SetGrammarByFileName(_textMate, file.FileName, ref _lastGrammarScope);
}
else if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
GC.Collect();
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope, true);

TextArea.TextView.Redraw();
}
}

private TextMate.Installation _textMate = null;
private string _lastGrammarScope = string.Empty;
}

public partial class RevisionFileContentViewer : UserControl
Expand Down
8 changes: 3 additions & 5 deletions src/Views/SelfUpdate.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void OnLoaded(RoutedEventArgs e)
if (_textMate == null)
{
_textMate = Models.TextMateHelper.CreateForEditor(this);
Models.TextMateHelper.SetGrammarByFileName(_textMate, "README.md");
Models.TextMateHelper.SetGrammarByFileName(_textMate, "README.md", ref _lastGrammarScope);
}
}

Expand All @@ -43,10 +43,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
base.OnUnloaded(e);

if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope);

GC.Collect();
}
Expand All @@ -60,6 +57,7 @@ protected override void OnDataContextChanged(EventArgs e)
}

private TextMate.Installation _textMate = null;
private string _lastGrammarScope = string.Empty;
}

public partial class SelfUpdate : ChromelessWindow
Expand Down
14 changes: 5 additions & 9 deletions src/Views/TextDiffView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,7 @@ protected override void OnUnloaded(RoutedEventArgs e)
TextArea.TextView.VisualLinesChanged -= OnTextViewVisualLinesChanged;

if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
Expand All @@ -576,7 +573,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
}
else if (change.Property == FileNameProperty)
{
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName);
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName, ref _lastGrammarScope);
}
else if (change.Property.Name == nameof(ActualThemeVariant) && change.NewValue != null)
{
Expand Down Expand Up @@ -773,16 +770,14 @@ private void UpdateTextMate()
TextArea.TextView.LineTransformers.Remove(_lineStyleTransformer);
_textMate = Models.TextMateHelper.CreateForEditor(this);
TextArea.TextView.LineTransformers.Add(_lineStyleTransformer);
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName);
Models.TextMateHelper.SetGrammarByFileName(_textMate, FileName, ref _lastGrammarScope);
}
}
else
{
if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
GC.Collect();
Models.TextMateHelper.DisposeInstallation(ref _textMate, ref _lastGrammarScope, true);

TextArea.TextView.Redraw();
}
Expand Down Expand Up @@ -885,6 +880,7 @@ private async Task CopyWithoutIndicatorsAsync()

private bool _execSizeChanged;
private TextMate.Installation _textMate;
private string _lastGrammarScope = string.Empty;
private TextLocation _lastSelectStart = TextLocation.Empty;
private TextLocation _lastSelectEnd = TextLocation.Empty;
private LineStyleTransformer _lineStyleTransformer;
Expand Down