refactor: commit sha link in message (#382)

* remove the built-in commit issue tracker rule
* hyperlink in commit message now supports commit sha
This commit is contained in:
leo 2024-08-21 12:46:36 +08:00
parent 6485a6f93a
commit 191763e1d8
No known key found for this signature in database
11 changed files with 113 additions and 90 deletions

31
src/Models/Hyperlink.cs Normal file
View file

@ -0,0 +1,31 @@
using Avalonia.Controls.Documents;
namespace SourceGit.Models
{
public class Hyperlink
{
public int Start { get; set; } = 0;
public int Length { get; set; } = 0;
public string Link { get; set; } = "";
public bool IsCommitSHA { get; set; } = false;
public Hyperlink(int start, int length, string link, bool isCommitSHA = false)
{
Start = start;
Length = length;
Link = link;
IsCommitSHA = isCommitSHA;
}
public bool Intersect(int start, int length)
{
if (start == Start)
return true;
if (start < Start)
return start + length > Start;
return start < Start + Length;
}
}
}

View file

@ -1,29 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Avalonia.Controls.Documents;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.Models namespace SourceGit.Models
{ {
public class IssueTrackerMatch
{
public int Start { get; set; } = 0;
public int Length { get; set; } = 0;
public string URL { get; set; } = "";
public Run Link { get; set; } = null;
public bool Intersect(int start, int length)
{
if (start == Start)
return true;
if (start < Start)
return start + length > Start;
return start < Start + Length;
}
}
public class IssueTrackerRule : ObservableObject public class IssueTrackerRule : ObservableObject
{ {
public string Name public string Name
@ -65,7 +46,7 @@ namespace SourceGit.Models
set => SetProperty(ref _urlTemplate, value); set => SetProperty(ref _urlTemplate, value);
} }
public void Matches(List<IssueTrackerMatch> outs, string message) public void Matches(List<Hyperlink> outs, string message)
{ {
if (_regex == null || string.IsNullOrEmpty(_urlTemplate)) if (_regex == null || string.IsNullOrEmpty(_urlTemplate))
return; return;
@ -92,17 +73,15 @@ namespace SourceGit.Models
if (intersect) if (intersect)
continue; continue;
var range = new IssueTrackerMatch(); var link = _urlTemplate;
range.Start = start;
range.Length = len;
range.URL = _urlTemplate;
for (var j = 1; j < match.Groups.Count; j++) for (var j = 1; j < match.Groups.Count; j++)
{ {
var group = match.Groups[j]; var group = match.Groups[j];
if (group.Success) if (group.Success)
range.URL = range.URL.Replace($"${j}", group.Value); link = link.Replace($"${j}", group.Value);
} }
var range = new Hyperlink(start, len, link);
outs.Add(range); outs.Add(range);
} }
} }

View file

@ -125,19 +125,6 @@ namespace SourceGit.Models
return rule; return rule;
} }
public IssueTrackerRule AddCommitLinkIssueTracker()
{
var rule = new IssueTrackerRule()
{
Name = "Commit Link",
RegexString = @"\b([0-9a-fA-F]{8,40})\b",
URLTemplate = "https://test/$1",
};
IssueTrackerRules.Add(rule);
return rule;
}
public IssueTrackerRule AddGithubIssueTracker(string repoURL) public IssueTrackerRule AddGithubIssueTracker(string repoURL)
{ {
var rule = new IssueTrackerRule() var rule = new IssueTrackerRule()

View file

@ -131,7 +131,6 @@
<x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">Email address</x:String> <x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">Email address</x:String>
<x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT</x:String> <x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT</x:String>
<x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE TRACKER</x:String> <x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE TRACKER</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleCommitLink" xml:space="preserve">Add Sample Commit HASH Rule</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">Add Sample Github Rule</x:String> <x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">Add Sample Github Rule</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">Add Sample Jira Rule</x:String> <x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">Add Sample Jira Rule</x:String>
<x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">New Rule</x:String> <x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">New Rule</x:String>

View file

@ -134,7 +134,6 @@
<x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">邮箱地址</x:String> <x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">邮箱地址</x:String>
<x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT配置</x:String> <x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT配置</x:String>
<x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE追踪</x:String> <x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE追踪</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleCommitLink" xml:space="preserve">新增匹配提交HASH规则</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">新增匹配Github Issue规则</x:String> <x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">新增匹配Github Issue规则</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">新增匹配Jira规则</x:String> <x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">新增匹配Jira规则</x:String>
<x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">新增自定义规则</x:String> <x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">新增自定义规则</x:String>

View file

@ -134,7 +134,6 @@
<x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">郵箱地址</x:String> <x:String x:Key="Text.Configure.Email.Placeholder" xml:space="preserve">郵箱地址</x:String>
<x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT配置</x:String> <x:String x:Key="Text.Configure.Git" xml:space="preserve">GIT配置</x:String>
<x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE追蹤</x:String> <x:String x:Key="Text.Configure.IssueTracker" xml:space="preserve">ISSUE追蹤</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleCommitLink" xml:space="preserve">新增匹配提交HASH規則</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">新增匹配Github Issue規則</x:String> <x:String x:Key="Text.Configure.IssueTracker.AddSampleGithub" xml:space="preserve">新增匹配Github Issue規則</x:String>
<x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">新增匹配Jira規則</x:String> <x:String x:Key="Text.Configure.IssueTracker.AddSampleJira" xml:space="preserve">新增匹配Jira規則</x:String>
<x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">新增自定義規則</x:String> <x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">新增自定義規則</x:String>

View file

@ -284,6 +284,10 @@
<Style Selector="Run.issue_link"> <Style Selector="Run.issue_link">
<Setter Property="Foreground" Value="{DynamicResource Brush.Link}"/> <Setter Property="Foreground" Value="{DynamicResource Brush.Link}"/>
</Style> </Style>
<Style Selector="Run.commit_link">
<Setter Property="Foreground" Value="DarkOrange"/>
<Setter Property="TextDecorations" Value="Underline"/>
</Style>
<Style Selector="SelectableTextBlock"> <Style Selector="SelectableTextBlock">
<Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="HorizontalAlignment" Value="Left"/>

View file

@ -102,11 +102,6 @@ namespace SourceGit.ViewModels
SelectedCommitTemplate = null; SelectedCommitTemplate = null;
} }
public void AddSampleCommitLinkTracker()
{
SelectedIssueTrackerRule = _repo.Settings.AddCommitLinkIssueTracker();
}
public void AddSampleGithubIssueTracker() public void AddSampleGithubIssueTracker()
{ {
foreach (var remote in _repo.Remotes) foreach (var remote in _repo.Remotes)

View file

@ -1,17 +1,21 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
using Avalonia; using Avalonia;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Documents; using Avalonia.Controls.Documents;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Utilities; using Avalonia.VisualTree;
namespace SourceGit.Views namespace SourceGit.Views
{ {
public class CommitMessagePresenter : SelectableTextBlock public partial class CommitMessagePresenter : SelectableTextBlock
{ {
[GeneratedRegex(@"\b([0-9a-fA-F]{8,40})\b")]
private static partial Regex REG_SHA_FORMAT();
public static readonly StyledProperty<string> MessageProperty = public static readonly StyledProperty<string> MessageProperty =
AvaloniaProperty.Register<CommitMessagePresenter, string>(nameof(Message)); AvaloniaProperty.Register<CommitMessagePresenter, string>(nameof(Message));
@ -46,16 +50,35 @@ namespace SourceGit.Views
if (string.IsNullOrEmpty(message)) if (string.IsNullOrEmpty(message))
return; return;
var rules = IssueTrackerRules; var matches = new List<Models.Hyperlink>();
if (rules == null || rules.Count == 0) if (IssueTrackerRules is { Count: > 0 } rules)
{ {
Inlines.Add(new Run(message));
return;
}
var matches = new List<Models.IssueTrackerMatch>();
foreach (var rule in rules) foreach (var rule in rules)
rule.Matches(matches, message); rule.Matches(matches, message);
}
var shas = REG_SHA_FORMAT().Matches(message);
for (int i = 0; i < shas.Count; i++)
{
var sha = shas[i];
if (!sha.Success)
continue;
var start = sha.Index;
var len = sha.Length;
var intersect = false;
foreach (var match in matches)
{
if (match.Intersect(start, len))
{
intersect = true;
break;
}
}
if (!intersect)
matches.Add(new Models.Hyperlink(start, len, sha.Groups[1].Value, true));
}
if (matches.Count == 0) if (matches.Count == 0)
{ {
@ -72,9 +95,9 @@ namespace SourceGit.Views
if (match.Start > pos) if (match.Start > pos)
Inlines.Add(new Run(message.Substring(pos, match.Start - pos))); Inlines.Add(new Run(message.Substring(pos, match.Start - pos)));
match.Link = new Run(message.Substring(match.Start, match.Length)); var link = new Run(message.Substring(match.Start, match.Length));
match.Link.Classes.Add("issue_link"); link.Classes.Add(match.IsCommitSHA ? "commit_link" : "issue_link");
Inlines.Add(match.Link); Inlines.Add(link);
pos = match.Start + match.Length; pos = match.Start + match.Length;
} }
@ -90,11 +113,10 @@ namespace SourceGit.Views
if (e.Pointer.Captured == null && _matches != null) if (e.Pointer.Captured == null && _matches != null)
{ {
var padding = Padding; var point = e.GetPosition(this) - new Point(Padding.Left, Padding.Top);
var point = e.GetPosition(this) - new Point(padding.Left, padding.Top); var x = Math.Min(Math.Max(point.X, 0), Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0));
point = new Point( var y = Math.Min(Math.Max(point.Y, 0), Math.Max(TextLayout.Height, 0));
MathUtilities.Clamp(point.X, 0, Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0)), point = new Point(x, y);
MathUtilities.Clamp(point.Y, 0, Math.Max(TextLayout.Height, 0)));
var pos = TextLayout.HitTestPoint(point).TextPosition; var pos = TextLayout.HitTestPoint(point).TextPosition;
foreach (var match in _matches) foreach (var match in _matches)
@ -105,12 +127,15 @@ namespace SourceGit.Views
if (match == _lastHover) if (match == _lastHover)
return; return;
_lastHover = match;
//_lastHover.Link.Classes.Add("issue_link_hovered");
SetCurrentValue(CursorProperty, Cursor.Parse("Hand")); SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
ToolTip.SetTip(this, match.URL);
_lastHover = match;
if (!_lastHover.IsCommitSHA)
{
ToolTip.SetTip(this, match.Link);
ToolTip.SetIsOpen(this, true); ToolTip.SetIsOpen(this, true);
}
return; return;
} }
@ -123,7 +148,18 @@ namespace SourceGit.Views
if (_lastHover != null) if (_lastHover != null)
{ {
e.Pointer.Capture(null); e.Pointer.Capture(null);
Native.OS.OpenBrowser(_lastHover.URL);
if (_lastHover.IsCommitSHA)
{
var parentView = this.FindAncestorOfType<CommitBaseInfo>();
if (parentView is { DataContext: ViewModels.CommitDetail detail })
detail.NavigateTo(_lastHover.Link);
}
else
{
Native.OS.OpenBrowser(_lastHover.Link);
}
e.Handled = true; e.Handled = true;
return; return;
} }
@ -143,12 +179,11 @@ namespace SourceGit.Views
{ {
ToolTip.SetTip(this, null); ToolTip.SetTip(this, null);
SetCurrentValue(CursorProperty, Cursor.Parse("IBeam")); SetCurrentValue(CursorProperty, Cursor.Parse("IBeam"));
//_lastHover.Link.Classes.Remove("issue_link_hovered");
_lastHover = null; _lastHover = null;
} }
} }
private List<Models.IssueTrackerMatch> _matches = null; private List<Models.Hyperlink> _matches = null;
private Models.IssueTrackerMatch _lastHover = null; private Models.Hyperlink _lastHover = null;
} }
} }

View file

@ -200,7 +200,7 @@ namespace SourceGit.Views
return; return;
} }
var matches = new List<Models.IssueTrackerMatch>(); var matches = new List<Models.Hyperlink>();
foreach (var rule in rules) foreach (var rule in rules)
rule.Matches(matches, subject); rule.Matches(matches, subject);
@ -219,9 +219,9 @@ namespace SourceGit.Views
if (match.Start > pos) if (match.Start > pos)
Inlines.Add(new Run(subject.Substring(pos, match.Start - pos))); Inlines.Add(new Run(subject.Substring(pos, match.Start - pos)));
match.Link = new Run(subject.Substring(match.Start, match.Length)); var link = new Run(subject.Substring(match.Start, match.Length));
match.Link.Classes.Add("issue_link"); link.Classes.Add("issue_link");
Inlines.Add(match.Link); Inlines.Add(link);
pos = match.Start + match.Length; pos = match.Start + match.Length;
} }
@ -239,11 +239,10 @@ namespace SourceGit.Views
if (_matches != null) if (_matches != null)
{ {
var padding = Padding; var point = e.GetPosition(this) - new Point(Padding.Left, Padding.Top);
var point = e.GetPosition(this) - new Point(padding.Left, padding.Top); var x = Math.Min(Math.Max(point.X, 0), Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0));
point = new Point( var y = Math.Min(Math.Max(point.Y, 0), Math.Max(TextLayout.Height, 0));
MathUtilities.Clamp(point.X, 0, Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0)), point = new Point(x, y);
MathUtilities.Clamp(point.Y, 0, Math.Max(TextLayout.Height, 0)));
var textPosition = TextLayout.HitTestPoint(point).TextPosition; var textPosition = TextLayout.HitTestPoint(point).TextPosition;
foreach (var match in _matches) foreach (var match in _matches)
@ -255,10 +254,8 @@ namespace SourceGit.Views
return; return;
_lastHover = match; _lastHover = match;
//_lastHover.Link.Classes.Add("issue_link_hovered");
SetCurrentValue(CursorProperty, Cursor.Parse("Hand")); SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
ToolTip.SetTip(this, match.URL); ToolTip.SetTip(this, match.Link);
ToolTip.SetIsOpen(this, true); ToolTip.SetIsOpen(this, true);
e.Handled = true; e.Handled = true;
return; return;
@ -273,7 +270,7 @@ namespace SourceGit.Views
base.OnPointerPressed(e); base.OnPointerPressed(e);
if (_lastHover != null) if (_lastHover != null)
Native.OS.OpenBrowser(_lastHover.URL); Native.OS.OpenBrowser(_lastHover.Link);
} }
protected override void OnPointerExited(PointerEventArgs e) protected override void OnPointerExited(PointerEventArgs e)
@ -288,13 +285,12 @@ namespace SourceGit.Views
{ {
ToolTip.SetTip(this, null); ToolTip.SetTip(this, null);
SetCurrentValue(CursorProperty, Cursor.Parse("Arrow")); SetCurrentValue(CursorProperty, Cursor.Parse("Arrow"));
//_lastHover.Link.Classes.Remove("issue_link_hovered");
_lastHover = null; _lastHover = null;
} }
} }
private List<Models.IssueTrackerMatch> _matches = null; private List<Models.Hyperlink> _matches = null;
private Models.IssueTrackerMatch _lastHover = null; private Models.Hyperlink _lastHover = null;
} }
public class CommitTimeTextBlock : TextBlock public class CommitTimeTextBlock : TextBlock

View file

@ -241,7 +241,6 @@
<MenuFlyout Placement="BottomEdgeAlignedLeft"> <MenuFlyout Placement="BottomEdgeAlignedLeft">
<MenuItem Header="{DynamicResource Text.Configure.IssueTracker.NewRule}" Command="{Binding NewIssueTracker}"/> <MenuItem Header="{DynamicResource Text.Configure.IssueTracker.NewRule}" Command="{Binding NewIssueTracker}"/>
<MenuItem Header="-"/> <MenuItem Header="-"/>
<MenuItem Header="{DynamicResource Text.Configure.IssueTracker.AddSampleCommitLink}" Command="{Binding AddSampleCommitLinkTracker}"/>
<MenuItem Header="{DynamicResource Text.Configure.IssueTracker.AddSampleGithub}" Command="{Binding AddSampleGithubIssueTracker}"/> <MenuItem Header="{DynamicResource Text.Configure.IssueTracker.AddSampleGithub}" Command="{Binding AddSampleGithubIssueTracker}"/>
<MenuItem Header="{DynamicResource Text.Configure.IssueTracker.AddSampleJira}" Command="{Binding AddSampleJiraIssueTracker}"/> <MenuItem Header="{DynamicResource Text.Configure.IssueTracker.AddSampleJira}" Command="{Binding AddSampleJiraIssueTracker}"/>
</MenuFlyout> </MenuFlyout>