Repository navigation
Expand file tree
/
Copy pathTextMateHelper.cs
More file actions
121 lines (105 loc) · 4.37 KB
/
Copy pathTextMateHelper.cs
File metadata and controls
121 lines (105 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.IO;
using Avalonia;
using Avalonia.Platform;
using Avalonia.Styling;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;
using TextMateSharp.Internal.Grammars.Reader;
using TextMateSharp.Internal.Types;
using TextMateSharp.Registry;
using TextMateSharp.Themes;
namespace SourceGit.Models
{
public static class GrammarUtility
{
private static readonly ExtraGrammar[] s_extraGrammars =
[
new ExtraGrammar("source.toml", ".toml", "toml.json"),
new ExtraGrammar("source.kotlin", ".kotlin", "kotlin.json"),
new ExtraGrammar("source.hx", ".hx", "haxe.json"),
new ExtraGrammar("source.hxml", ".hxml", "hxml.json"),
];
public static string GetScope(string file, RegistryOptions reg)
{
var extension = Path.GetExtension(file);
if (extension == ".h")
extension = ".cpp";
else if (extension == ".resx" || extension == ".plist" || extension == ".manifest")
extension = ".xml";
else if (extension == ".command")
extension = ".sh";
else if (extension == ".kt" || extension == ".kts")
extension = ".kotlin";
foreach (var grammar in s_extraGrammars)
{
if (grammar.Extension.Equals(extension, StringComparison.OrdinalIgnoreCase))
return grammar.Scope;
}
return reg.GetScopeByExtension(extension);
}
public static IRawGrammar GetGrammar(string scopeName, RegistryOptions reg)
{
foreach (var grammar in s_extraGrammars)
{
if (grammar.Scope.Equals(scopeName, StringComparison.OrdinalIgnoreCase))
{
var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/Grammars/{grammar.File}",
UriKind.RelativeOrAbsolute));
try
{
return GrammarReader.ReadGrammarSync(new StreamReader(asset));
}
catch
{
break;
}
}
}
return reg.GetGrammar(scopeName);
}
private record ExtraGrammar(string Scope, string Extension, string File)
{
public readonly string Scope = Scope;
public readonly string Extension = Extension;
public readonly string File = File;
}
}
public class RegistryOptionsWrapper(ThemeName defaultTheme) : IRegistryOptions
{
public IRawTheme GetTheme(string scopeName) => _backend.GetTheme(scopeName);
public IRawTheme GetDefaultTheme() => _backend.GetDefaultTheme();
public IRawTheme LoadTheme(ThemeName name) => _backend.LoadTheme(name);
public ICollection<string> GetInjections(string scopeName) => _backend.GetInjections(scopeName);
public IRawGrammar GetGrammar(string scopeName) => GrammarUtility.GetGrammar(scopeName, _backend);
public string GetScope(string filename) => GrammarUtility.GetScope(filename, _backend);
private readonly RegistryOptions _backend = new(defaultTheme);
}
public static class TextMateHelper
{
public static TextMate.Installation CreateForEditor(TextEditor editor)
{
return editor.InstallTextMate(Application.Current?.ActualThemeVariant == ThemeVariant.Dark ?
new RegistryOptionsWrapper(ThemeName.DarkPlus) :
new RegistryOptionsWrapper(ThemeName.LightPlus));
}
public static void SetThemeByApp(TextMate.Installation installation)
{
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
{
var isDark = Application.Current?.ActualThemeVariant == ThemeVariant.Dark;
installation.SetTheme(reg.LoadTheme(isDark ? ThemeName.DarkPlus : ThemeName.LightPlus));
}
}
public static void SetGrammarByFileName(TextMate.Installation installation, string filePath)
{
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
{
installation.SetGrammar(reg.GetScope(filePath));
GC.Collect();
}
}
}
}