mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-22 04:34:59 +00:00
enhance: text editor (#365)
* support extra grammars. * avoid crashing on text editor detached from visual tree
This commit is contained in:
parent
a3496a9d2f
commit
39fba17648
6 changed files with 453 additions and 33 deletions
|
@ -1,24 +1,104 @@
|
|||
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 class RegistryOptionsWrapper : IRegistryOptions
|
||||
{
|
||||
public RegistryOptionsWrapper(ThemeName defaultTheme)
|
||||
{
|
||||
_backend = new RegistryOptions(defaultTheme);
|
||||
_extraGrammars = new Dictionary<string, IRawGrammar>();
|
||||
|
||||
string[] extraGrammarFiles = ["toml.json"];
|
||||
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));
|
||||
_extraGrammars.Add(grammar.GetScopeName(), grammar);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IRawTheme GetTheme(string scopeName)
|
||||
{
|
||||
return _backend.GetTheme(scopeName);
|
||||
}
|
||||
|
||||
public IRawGrammar GetGrammar(string scopeName)
|
||||
{
|
||||
if (_extraGrammars.TryGetValue(scopeName, out var grammar))
|
||||
return grammar;
|
||||
|
||||
return _backend.GetGrammar(scopeName);
|
||||
}
|
||||
|
||||
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);
|
||||
var scope = $"source{extension}";
|
||||
if (_extraGrammars.ContainsKey(scope))
|
||||
return scope;
|
||||
|
||||
if (extension == ".h")
|
||||
extension = ".cpp";
|
||||
else if (extension == ".resx" || extension == ".plist")
|
||||
extension = ".xml";
|
||||
else if (extension == ".command")
|
||||
extension = ".sh";
|
||||
|
||||
return _backend.GetScopeByExtension(extension);
|
||||
}
|
||||
|
||||
private RegistryOptions _backend = null;
|
||||
private Dictionary<string, IRawGrammar> _extraGrammars = null;
|
||||
}
|
||||
|
||||
public static class TextMateHelper
|
||||
{
|
||||
public static TextMate.Installation CreateForEditor(TextEditor editor)
|
||||
{
|
||||
if (Application.Current?.ActualThemeVariant == ThemeVariant.Dark)
|
||||
return editor.InstallTextMate(new RegistryOptions(ThemeName.DarkPlus));
|
||||
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.DarkPlus));
|
||||
|
||||
return editor.InstallTextMate(new RegistryOptions(ThemeName.LightPlus));
|
||||
return editor.InstallTextMate(new RegistryOptionsWrapper(ThemeName.LightPlus));
|
||||
}
|
||||
|
||||
public static void SetThemeByApp(TextMate.Installation installation)
|
||||
|
@ -26,7 +106,7 @@ namespace SourceGit.Models
|
|||
if (installation == null)
|
||||
return;
|
||||
|
||||
if (installation.RegistryOptions is RegistryOptions reg)
|
||||
if (installation.RegistryOptions is RegistryOptionsWrapper reg)
|
||||
{
|
||||
if (Application.Current?.ActualThemeVariant == ThemeVariant.Dark)
|
||||
installation.SetTheme(reg.LoadTheme(ThemeName.DarkPlus));
|
||||
|
@ -37,15 +117,9 @@ namespace SourceGit.Models
|
|||
|
||||
public static void SetGrammarByFileName(TextMate.Installation installation, string filePath)
|
||||
{
|
||||
if (installation is { RegistryOptions: RegistryOptions reg })
|
||||
if (installation is { RegistryOptions: RegistryOptionsWrapper reg })
|
||||
{
|
||||
var ext = Path.GetExtension(filePath);
|
||||
if (ext == ".h")
|
||||
ext = ".cpp";
|
||||
else if (ext == ".resx" || ext == ".plist")
|
||||
ext = ".xml";
|
||||
|
||||
installation.SetGrammar(reg.GetScopeByExtension(ext));
|
||||
installation.SetGrammar(reg.GetScopeByFileName(filePath));
|
||||
GC.Collect();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue