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-08-17 09:18:18 -07:00
|
|
|
|
public class RegistryOptionsWrapper : IRegistryOptions
|
|
|
|
|
{
|
|
|
|
|
public RegistryOptionsWrapper(ThemeName defaultTheme)
|
|
|
|
|
{
|
|
|
|
|
_backend = new RegistryOptions(defaultTheme);
|
2024-08-17 18:33:40 -07:00
|
|
|
|
_extraGrammars = new List<IRawGrammar>();
|
2024-08-17 09:18:18 -07:00
|
|
|
|
|
2024-09-20 07:30:40 -07:00
|
|
|
|
string[] extraGrammarFiles = ["toml.json", "kotlin.json", "haxe.json", "hxml.json"];
|
2024-08-17 09:18:18 -07:00
|
|
|
|
foreach (var file in extraGrammarFiles)
|
|
|
|
|
{
|
|
|
|
|
var asset = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/Grammars/{file}",
|
|
|
|
|
UriKind.RelativeOrAbsolute));
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var grammar = GrammarReader.ReadGrammarSync(new StreamReader(asset));
|
2024-08-17 18:33:40 -07:00
|
|
|
|
_extraGrammars.Add(grammar);
|
2024-08-17 09:18:18 -07:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-18 08:14:44 -07:00
|
|
|
|
|
2024-08-17 09:18:18 -07:00
|
|
|
|
public IRawTheme GetTheme(string scopeName)
|
|
|
|
|
{
|
|
|
|
|
return _backend.GetTheme(scopeName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IRawGrammar GetGrammar(string scopeName)
|
|
|
|
|
{
|
2024-08-17 18:33:40 -07:00
|
|
|
|
var grammar = _extraGrammars.Find(x => x.GetScopeName().Equals(scopeName, StringComparison.Ordinal));
|
|
|
|
|
return grammar ?? _backend.GetGrammar(scopeName);
|
2024-08-17 09:18:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ICollection<string> GetInjections(string scopeName)
|
|
|
|
|
{
|
|
|
|
|
return _backend.GetInjections(scopeName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IRawTheme GetDefaultTheme()
|
|
|
|
|
{
|
|
|
|
|
return _backend.GetDefaultTheme();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IRawTheme LoadTheme(ThemeName name)
|
|
|
|
|
{
|
|
|
|
|
return _backend.LoadTheme(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetScopeByFileName(string filename)
|
|
|
|
|
{
|
|
|
|
|
var extension = Path.GetExtension(filename);
|
|
|
|
|
if (extension == ".h")
|
|
|
|
|
extension = ".cpp";
|
2024-08-17 18:33:40 -07:00
|
|
|
|
else if (extension == ".resx" || extension == ".plist" || extension == ".manifest")
|
2024-08-17 09:18:18 -07:00
|
|
|
|
extension = ".xml";
|
|
|
|
|
else if (extension == ".command")
|
|
|
|
|
extension = ".sh";
|
2024-09-20 04:51:05 -07:00
|
|
|
|
else if (extension == ".kt" || extension == ".kts")
|
|
|
|
|
extension = ".kotlin";
|
|
|
|
|
|
|
|
|
|
var grammar = _extraGrammars.Find(x => x.GetScopeName().EndsWith(extension, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
if (grammar != null)
|
|
|
|
|
return grammar.GetScopeName();
|
2024-08-17 09:18:18 -07:00
|
|
|
|
|
|
|
|
|
return _backend.GetScopeByExtension(extension);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 18:33:40 -07:00
|
|
|
|
private readonly RegistryOptions _backend;
|
|
|
|
|
private readonly List<IRawGrammar> _extraGrammars;
|
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-07-14 09:30:31 -07:00
|
|
|
|
if (Application.Current?.ActualThemeVariant == ThemeVariant.Dark)
|
2024-08-17 09:18:18 -07:00
|
|
|
|
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.DarkPlus));
|
2024-07-14 09:30:31 -07:00
|
|
|
|
|
2024-08-17 09:18:18 -07:00
|
|
|
|
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.LightPlus));
|
2024-03-20 03:27:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetThemeByApp(TextMate.Installation installation)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (installation == null)
|
|
|
|
|
return;
|
2024-03-20 03:27:48 -07:00
|
|
|
|
|
2024-08-17 09:18:18 -07:00
|
|
|
|
if (installation.RegistryOptions is 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-08-17 09:18:18 -07:00
|
|
|
|
installation.SetGrammar(reg.GetScopeByFileName(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
|
|
|
|
}
|