2024-03-20 04:49:01 -07:00
|
|
|
|
using System;
|
2024-08-17 09:18:18 -07:00
|
|
|
|
using System.Collections.Generic;
|
2024-03-20 04:49:01 -07:00
|
|
|
|
using System.IO;
|
2024-03-20 03:27:48 -07:00
|
|
|
|
|
2024-07-14 09:30:31 -07:00
|
|
|
|
using Avalonia;
|
2024-08-17 09:18:18 -07:00
|
|
|
|
using Avalonia.Platform;
|
2024-03-20 03:27:48 -07:00
|
|
|
|
using Avalonia.Styling;
|
|
|
|
|
|
|
|
|
|
using AvaloniaEdit;
|
|
|
|
|
using AvaloniaEdit.TextMate;
|
|
|
|
|
|
|
|
|
|
using TextMateSharp.Grammars;
|
2024-08-17 09:18:18 -07:00
|
|
|
|
using TextMateSharp.Internal.Grammars.Reader;
|
|
|
|
|
using TextMateSharp.Internal.Types;
|
|
|
|
|
using TextMateSharp.Registry;
|
|
|
|
|
using TextMateSharp.Themes;
|
2024-03-20 03:27:48 -07:00
|
|
|
|
|
|
|
|
|
namespace SourceGit.Models
|
|
|
|
|
{
|
2024-09-21 01:33:34 -07:00
|
|
|
|
public static class GrammarUtility
|
2024-08-17 09:18:18 -07:00
|
|
|
|
{
|
2024-09-21 07:04:50 -07:00
|
|
|
|
private static readonly ExtraGrammar[] s_extraGrammars =
|
2024-09-21 01:33:34 -07:00
|
|
|
|
[
|
|
|
|
|
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"),
|
|
|
|
|
];
|
|
|
|
|
|
2024-09-21 07:02:50 -07:00
|
|
|
|
public static string GetScope(string file, RegistryOptions reg)
|
2024-08-17 09:18:18 -07:00
|
|
|
|
{
|
2024-09-21 01:33:34 -07:00
|
|
|
|
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";
|
2024-09-25 01:24:04 -07:00
|
|
|
|
|
2024-09-21 07:04:50 -07:00
|
|
|
|
foreach (var grammar in s_extraGrammars)
|
2024-08-17 09:18:18 -07:00
|
|
|
|
{
|
2024-09-21 01:33:34 -07:00
|
|
|
|
if (grammar.Extension.Equals(extension, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return grammar.Scope;
|
|
|
|
|
}
|
2024-08-17 09:18:18 -07:00
|
|
|
|
|
2024-09-21 07:02:50 -07:00
|
|
|
|
return reg.GetScopeByExtension(extension);
|
2024-09-21 01:33:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 07:02:50 -07:00
|
|
|
|
public static IRawGrammar GetGrammar(string scopeName, RegistryOptions reg)
|
2024-09-21 01:33:34 -07:00
|
|
|
|
{
|
2024-09-21 07:04:50 -07:00
|
|
|
|
foreach (var grammar in s_extraGrammars)
|
2024-09-21 01:33:34 -07:00
|
|
|
|
{
|
|
|
|
|
if (grammar.Scope.Equals(scopeName, StringComparison.OrdinalIgnoreCase))
|
2024-08-17 09:18:18 -07:00
|
|
|
|
{
|
2024-09-21 01:33:34 -07:00
|
|
|
|
var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/Grammars/{grammar.File}",
|
|
|
|
|
UriKind.RelativeOrAbsolute));
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return GrammarReader.ReadGrammarSync(new StreamReader(asset));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-08-17 09:18:18 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-21 01:33:34 -07:00
|
|
|
|
|
2024-09-21 07:02:50 -07:00
|
|
|
|
return reg.GetGrammar(scopeName);
|
2024-08-17 09:18:18 -07:00
|
|
|
|
}
|
2024-08-18 08:14:44 -07:00
|
|
|
|
|
2024-09-21 01:33:34 -07:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-09-21 07:02:50 -07:00
|
|
|
|
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);
|
2024-09-25 01:24:04 -07:00
|
|
|
|
|
2024-09-21 01:33:34 -07:00
|
|
|
|
private readonly RegistryOptions _backend = new(defaultTheme);
|
2024-08-17 09:18:18 -07:00
|
|
|
|
}
|
2024-08-18 08:14:44 -07:00
|
|
|
|
|
2024-03-20 03:27:48 -07:00
|
|
|
|
public static class TextMateHelper
|
|
|
|
|
{
|
|
|
|
|
public static TextMate.Installation CreateForEditor(TextEditor editor)
|
|
|
|
|
{
|
2024-09-25 01:24:04 -07:00
|
|
|
|
return editor.InstallTextMate(Application.Current?.ActualThemeVariant == ThemeVariant.Dark ?
|
|
|
|
|
new RegistryOptionsWrapper(ThemeName.DarkPlus) :
|
2024-09-21 07:02:50 -07:00
|
|
|
|
new RegistryOptionsWrapper(ThemeName.LightPlus));
|
2024-03-20 03:27:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetThemeByApp(TextMate.Installation installation)
|
|
|
|
|
{
|
2024-09-21 07:02:50 -07:00
|
|
|
|
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
|
2024-03-20 03:27:48 -07:00
|
|
|
|
{
|
2024-08-17 18:33:40 -07:00
|
|
|
|
var isDark = Application.Current?.ActualThemeVariant == ThemeVariant.Dark;
|
|
|
|
|
installation.SetTheme(reg.LoadTheme(isDark ? ThemeName.DarkPlus : ThemeName.LightPlus));
|
2024-03-20 03:27:48 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetGrammarByFileName(TextMate.Installation installation, string filePath)
|
|
|
|
|
{
|
2024-08-17 09:18:18 -07:00
|
|
|
|
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
|
2024-03-20 03:27:48 -07:00
|
|
|
|
{
|
2024-09-21 07:02:50 -07:00
|
|
|
|
installation.SetGrammar(reg.GetScope(filePath));
|
2024-07-14 09:30:31 -07:00
|
|
|
|
GC.Collect();
|
2024-03-20 03:27:48 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|