From 93706449be68704b3a88f8bf8f934aab7bbc0230 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 10 Oct 2024 15:42:26 +0800 Subject: [PATCH] enhance: only update grammar if it is necessary --- src/Models/TextMateHelper.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Models/TextMateHelper.cs b/src/Models/TextMateHelper.cs index 2e38e4dd..0eb5e489 100644 --- a/src/Models/TextMateHelper.cs +++ b/src/Models/TextMateHelper.cs @@ -81,6 +81,8 @@ namespace SourceGit.Models 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); @@ -111,10 +113,15 @@ namespace SourceGit.Models public static void SetGrammarByFileName(TextMate.Installation installation, string filePath) { - if (installation is { RegistryOptions: RegistryOptionsWrapper reg }) + if (installation is { RegistryOptions: RegistryOptionsWrapper reg } && !string.IsNullOrEmpty(filePath)) { - installation.SetGrammar(reg.GetScope(filePath)); - GC.Collect(); + var scope = reg.GetScope(filePath); + if (reg.LastScope != scope) + { + reg.LastScope = scope; + installation.SetGrammar(reg.GetScope(filePath)); + GC.Collect(); + } } } }