feature: enable syntax highlighting in revision files view (#333)

This commit is contained in:
leo 2024-08-08 10:12:39 +08:00
parent 1ab0ea27df
commit 6bd0920d71
No known key found for this signature in database
3 changed files with 27 additions and 1 deletions

View file

@ -14,6 +14,7 @@ namespace SourceGit.Models
public class RevisionTextFile
{
public string FileName { get; set; }
public string Content { get; set; }
}

View file

@ -183,7 +183,7 @@ namespace SourceGit.ViewModels
}
else
{
var txt = new Models.RevisionTextFile() { Content = content };
var txt = new Models.RevisionTextFile() { FileName = file.Path, Content = content };
Dispatcher.UIThread.Invoke(() => ViewRevisionFileContent = txt);
}
});

View file

@ -9,6 +9,7 @@ using Avalonia.Media;
using AvaloniaEdit;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.TextMate;
namespace SourceGit.Views
{
@ -35,6 +36,7 @@ namespace SourceGit.Views
base.OnLoaded(e);
TextArea.TextView.ContextRequested += OnTextViewContextRequested;
UpdateTextMate();
}
protected override void OnUnloaded(RoutedEventArgs e)
@ -42,6 +44,13 @@ namespace SourceGit.Views
base.OnUnloaded(e);
TextArea.TextView.ContextRequested -= OnTextViewContextRequested;
if (_textMate != null)
{
_textMate.Dispose();
_textMate = null;
}
GC.Collect();
}
@ -50,10 +59,15 @@ namespace SourceGit.Views
base.OnDataContextChanged(e);
if (DataContext is Models.RevisionTextFile source)
{
UpdateTextMate();
Text = source.Content;
}
else
{
Text = string.Empty;
}
}
private void OnTextViewContextRequested(object sender, ContextRequestedEventArgs e)
{
@ -85,6 +99,17 @@ namespace SourceGit.Views
TextArea.TextView.OpenContextMenu(menu);
e.Handled = true;
}
private void UpdateTextMate()
{
if (_textMate == null)
_textMate = Models.TextMateHelper.CreateForEditor(this);
if (DataContext is Models.RevisionTextFile file)
Models.TextMateHelper.SetGrammarByFileName(_textMate, file.FileName);
}
private TextMate.Installation _textMate = null;
}
public partial class RevisionFiles : UserControl