Skip to content

Commit 9370644

Browse files
committed
enhance: only update grammar if it is necessary
1 parent 68519c7 commit 9370644

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/Models/TextMateHelper.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ private record ExtraGrammar(string Scope, string Extension, string File)
8181

8282
public class RegistryOptionsWrapper(ThemeName defaultTheme) : IRegistryOptions
8383
{
84+
public string LastScope { get; set; } = string.Empty;
85+
8486
public IRawTheme GetTheme(string scopeName) => _backend.GetTheme(scopeName);
8587
public IRawTheme GetDefaultTheme() => _backend.GetDefaultTheme();
8688
public IRawTheme LoadTheme(ThemeName name) => _backend.LoadTheme(name);
@@ -111,10 +113,15 @@ public static void SetThemeByApp(TextMate.Installation installation)
111113

112114
public static void SetGrammarByFileName(TextMate.Installation installation, string filePath)
113115
{
114-
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
116+
if (installation is { RegistryOptions: RegistryOptionsWrapper reg } && !string.IsNullOrEmpty(filePath))
115117
{
116-
installation.SetGrammar(reg.GetScope(filePath));
117-
GC.Collect();
118+
var scope = reg.GetScope(filePath);
119+
if (reg.LastScope != scope)
120+
{
121+
reg.LastScope = scope;
122+
installation.SetGrammar(reg.GetScope(filePath));
123+
GC.Collect();
124+
}
118125
}
119126
}
120127
}

0 commit comments

Comments
 (0)