sourcegit/src/Models/TextMateHelper.cs

54 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.IO;
2024-07-14 09:30:31 -07:00
using Avalonia;
using Avalonia.Styling;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;
namespace SourceGit.Models
{
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)
return editor.InstallTextMate(new RegistryOptions(ThemeName.DarkPlus));
2024-07-14 09:30:31 -07:00
return editor.InstallTextMate(new RegistryOptions(ThemeName.LightPlus));
}
public static void SetThemeByApp(TextMate.Installation installation)
{
if (installation == null)
return;
2024-07-14 09:30:31 -07:00
if (installation.RegistryOptions is RegistryOptions reg)
{
2024-07-14 09:30:31 -07:00
if (Application.Current?.ActualThemeVariant == ThemeVariant.Dark)
installation.SetTheme(reg.LoadTheme(ThemeName.DarkPlus));
else
installation.SetTheme(reg.LoadTheme(ThemeName.LightPlus));
}
}
public static void SetGrammarByFileName(TextMate.Installation installation, string filePath)
{
2024-07-14 09:30:31 -07:00
if (installation is { RegistryOptions: RegistryOptions reg })
{
2024-07-14 09:30:31 -07:00
var ext = Path.GetExtension(filePath);
if (ext == ".h")
ext = ".cpp";
else if (ext == ".resx" || ext == ".plist")
ext = ".xml";
installation.SetGrammar(reg.GetScopeByExtension(ext));
GC.Collect();
}
}
}
}