feature: use bold font for keyword prefix of commit subject

Regexes to detect `keyword`
* ^(\[[\w\s]+\])
* ^(\w+\s?:)
This commit is contained in:
leo 2024-10-16 14:48:22 +08:00
parent 7f8a1c384a
commit 8f9e2cefe9
No known key found for this signature in database

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Avalonia;
using Avalonia.Collections;
@ -155,7 +156,7 @@ namespace SourceGit.Views
private Status _status = Status.Normal;
}
public class CommitSubjectPresenter : TextBlock
public partial class CommitSubjectPresenter : TextBlock
{
public static readonly StyledProperty<string> SubjectProperty =
AvaloniaProperty.Register<CommitSubjectPresenter, string>(nameof(Subject));
@ -191,6 +192,21 @@ namespace SourceGit.Views
if (string.IsNullOrEmpty(subject))
return;
var offset = 0;
var keywordMatch = REG_KEYWORD_FORMAT1().Match(subject);
if (!keywordMatch.Success)
keywordMatch = REG_KEYWORD_FORMAT2().Match(subject);
if (keywordMatch.Success)
{
var keyword = new Run(subject.Substring(0, keywordMatch.Length));
keyword.FontWeight = FontWeight.Bold;
Inlines.Add(keyword);
offset = keywordMatch.Length;
subject = subject.Substring(offset);
}
var rules = IssueTrackerRules;
if (rules == null || rules.Count == 0)
{
@ -223,6 +239,7 @@ namespace SourceGit.Views
inlines.Add(link);
pos = match.Start + match.Length;
match.Start += offset; // Because we use this index of whole subject to detect mouse event.
}
if (pos < subject.Length)
@ -288,6 +305,12 @@ namespace SourceGit.Views
}
}
[GeneratedRegex(@"^(\[[\w\s]+\])")]
private static partial Regex REG_KEYWORD_FORMAT1();
[GeneratedRegex(@"^(\w+\s?:)")]
private static partial Regex REG_KEYWORD_FORMAT2();
private List<Models.Hyperlink> _matches = null;
private Models.Hyperlink _lastHover = null;
}