mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-10-31 13:03:20 -07:00
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:
parent
6485a6f93a
commit
191763e1d8
11 changed files with 113 additions and 90 deletions
31
src/Models/Hyperlink.cs
Normal file
31
src/Models/Hyperlink.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,29 +1,10 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using Avalonia.Controls.Documents;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
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 string Name
|
||||
|
@ -65,7 +46,7 @@ namespace SourceGit.Models
|
|||
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))
|
||||
return;
|
||||
|
@ -92,17 +73,15 @@ namespace SourceGit.Models
|
|||
if (intersect)
|
||||
continue;
|
||||
|
||||
var range = new IssueTrackerMatch();
|
||||
range.Start = start;
|
||||
range.Length = len;
|
||||
range.URL = _urlTemplate;
|
||||
var link = _urlTemplate;
|
||||
for (var j = 1; j < match.Groups.Count; j++)
|
||||
{
|
||||
var group = match.Groups[j];
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,19 +125,6 @@ namespace SourceGit.Models
|
|||
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)
|
||||
{
|
||||
var rule = new IssueTrackerRule()
|
||||
|
|
|
@ -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.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.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.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>
|
||||
|
|
|
@ -134,7 +134,6 @@
|
|||
<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.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.AddSampleJira" xml:space="preserve">新增匹配Jira规则</x:String>
|
||||
<x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">新增自定义规则</x:String>
|
||||
|
|
|
@ -134,7 +134,6 @@
|
|||
<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.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.AddSampleJira" xml:space="preserve">新增匹配Jira規則</x:String>
|
||||
<x:String x:Key="Text.Configure.IssueTracker.NewRule" xml:space="preserve">新增自定義規則</x:String>
|
||||
|
|
|
@ -284,6 +284,10 @@
|
|||
<Style Selector="Run.issue_link">
|
||||
<Setter Property="Foreground" Value="{DynamicResource Brush.Link}"/>
|
||||
</Style>
|
||||
<Style Selector="Run.commit_link">
|
||||
<Setter Property="Foreground" Value="DarkOrange"/>
|
||||
<Setter Property="TextDecorations" Value="Underline"/>
|
||||
</Style>
|
||||
|
||||
<Style Selector="SelectableTextBlock">
|
||||
<Setter Property="HorizontalAlignment" Value="Left"/>
|
||||
|
|
|
@ -102,11 +102,6 @@ namespace SourceGit.ViewModels
|
|||
SelectedCommitTemplate = null;
|
||||
}
|
||||
|
||||
public void AddSampleCommitLinkTracker()
|
||||
{
|
||||
SelectedIssueTrackerRule = _repo.Settings.AddCommitLinkIssueTracker();
|
||||
}
|
||||
|
||||
public void AddSampleGithubIssueTracker()
|
||||
{
|
||||
foreach (var remote in _repo.Remotes)
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Documents;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Utilities;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
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 =
|
||||
AvaloniaProperty.Register<CommitMessagePresenter, string>(nameof(Message));
|
||||
|
||||
|
@ -46,16 +50,35 @@ namespace SourceGit.Views
|
|||
if (string.IsNullOrEmpty(message))
|
||||
return;
|
||||
|
||||
var rules = IssueTrackerRules;
|
||||
if (rules == null || rules.Count == 0)
|
||||
var matches = new List<Models.Hyperlink>();
|
||||
if (IssueTrackerRules is { Count: > 0 } rules)
|
||||
{
|
||||
Inlines.Add(new Run(message));
|
||||
return;
|
||||
}
|
||||
|
||||
var matches = new List<Models.IssueTrackerMatch>();
|
||||
foreach (var rule in rules)
|
||||
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)
|
||||
{
|
||||
|
@ -72,9 +95,9 @@ namespace SourceGit.Views
|
|||
if (match.Start > pos)
|
||||
Inlines.Add(new Run(message.Substring(pos, match.Start - pos)));
|
||||
|
||||
match.Link = new Run(message.Substring(match.Start, match.Length));
|
||||
match.Link.Classes.Add("issue_link");
|
||||
Inlines.Add(match.Link);
|
||||
var link = new Run(message.Substring(match.Start, match.Length));
|
||||
link.Classes.Add(match.IsCommitSHA ? "commit_link" : "issue_link");
|
||||
Inlines.Add(link);
|
||||
|
||||
pos = match.Start + match.Length;
|
||||
}
|
||||
|
@ -90,11 +113,10 @@ namespace SourceGit.Views
|
|||
|
||||
if (e.Pointer.Captured == null && _matches != null)
|
||||
{
|
||||
var padding = Padding;
|
||||
var point = e.GetPosition(this) - new Point(padding.Left, padding.Top);
|
||||
point = new Point(
|
||||
MathUtilities.Clamp(point.X, 0, Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0)),
|
||||
MathUtilities.Clamp(point.Y, 0, Math.Max(TextLayout.Height, 0)));
|
||||
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));
|
||||
var y = Math.Min(Math.Max(point.Y, 0), Math.Max(TextLayout.Height, 0));
|
||||
point = new Point(x, y);
|
||||
|
||||
var pos = TextLayout.HitTestPoint(point).TextPosition;
|
||||
foreach (var match in _matches)
|
||||
|
@ -105,12 +127,15 @@ namespace SourceGit.Views
|
|||
if (match == _lastHover)
|
||||
return;
|
||||
|
||||
_lastHover = match;
|
||||
//_lastHover.Link.Classes.Add("issue_link_hovered");
|
||||
|
||||
SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
|
||||
ToolTip.SetTip(this, match.URL);
|
||||
|
||||
_lastHover = match;
|
||||
if (!_lastHover.IsCommitSHA)
|
||||
{
|
||||
ToolTip.SetTip(this, match.Link);
|
||||
ToolTip.SetIsOpen(this, true);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -123,7 +148,18 @@ namespace SourceGit.Views
|
|||
if (_lastHover != 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;
|
||||
return;
|
||||
}
|
||||
|
@ -143,12 +179,11 @@ namespace SourceGit.Views
|
|||
{
|
||||
ToolTip.SetTip(this, null);
|
||||
SetCurrentValue(CursorProperty, Cursor.Parse("IBeam"));
|
||||
//_lastHover.Link.Classes.Remove("issue_link_hovered");
|
||||
_lastHover = null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Models.IssueTrackerMatch> _matches = null;
|
||||
private Models.IssueTrackerMatch _lastHover = null;
|
||||
private List<Models.Hyperlink> _matches = null;
|
||||
private Models.Hyperlink _lastHover = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -200,7 +200,7 @@ namespace SourceGit.Views
|
|||
return;
|
||||
}
|
||||
|
||||
var matches = new List<Models.IssueTrackerMatch>();
|
||||
var matches = new List<Models.Hyperlink>();
|
||||
foreach (var rule in rules)
|
||||
rule.Matches(matches, subject);
|
||||
|
||||
|
@ -219,9 +219,9 @@ namespace SourceGit.Views
|
|||
if (match.Start > pos)
|
||||
Inlines.Add(new Run(subject.Substring(pos, match.Start - pos)));
|
||||
|
||||
match.Link = new Run(subject.Substring(match.Start, match.Length));
|
||||
match.Link.Classes.Add("issue_link");
|
||||
Inlines.Add(match.Link);
|
||||
var link = new Run(subject.Substring(match.Start, match.Length));
|
||||
link.Classes.Add("issue_link");
|
||||
Inlines.Add(link);
|
||||
|
||||
pos = match.Start + match.Length;
|
||||
}
|
||||
|
@ -239,11 +239,10 @@ namespace SourceGit.Views
|
|||
|
||||
if (_matches != null)
|
||||
{
|
||||
var padding = Padding;
|
||||
var point = e.GetPosition(this) - new Point(padding.Left, padding.Top);
|
||||
point = new Point(
|
||||
MathUtilities.Clamp(point.X, 0, Math.Max(TextLayout.WidthIncludingTrailingWhitespace, 0)),
|
||||
MathUtilities.Clamp(point.Y, 0, Math.Max(TextLayout.Height, 0)));
|
||||
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));
|
||||
var y = Math.Min(Math.Max(point.Y, 0), Math.Max(TextLayout.Height, 0));
|
||||
point = new Point(x, y);
|
||||
|
||||
var textPosition = TextLayout.HitTestPoint(point).TextPosition;
|
||||
foreach (var match in _matches)
|
||||
|
@ -255,10 +254,8 @@ namespace SourceGit.Views
|
|||
return;
|
||||
|
||||
_lastHover = match;
|
||||
//_lastHover.Link.Classes.Add("issue_link_hovered");
|
||||
|
||||
SetCurrentValue(CursorProperty, Cursor.Parse("Hand"));
|
||||
ToolTip.SetTip(this, match.URL);
|
||||
ToolTip.SetTip(this, match.Link);
|
||||
ToolTip.SetIsOpen(this, true);
|
||||
e.Handled = true;
|
||||
return;
|
||||
|
@ -273,7 +270,7 @@ namespace SourceGit.Views
|
|||
base.OnPointerPressed(e);
|
||||
|
||||
if (_lastHover != null)
|
||||
Native.OS.OpenBrowser(_lastHover.URL);
|
||||
Native.OS.OpenBrowser(_lastHover.Link);
|
||||
}
|
||||
|
||||
protected override void OnPointerExited(PointerEventArgs e)
|
||||
|
@ -288,13 +285,12 @@ namespace SourceGit.Views
|
|||
{
|
||||
ToolTip.SetTip(this, null);
|
||||
SetCurrentValue(CursorProperty, Cursor.Parse("Arrow"));
|
||||
//_lastHover.Link.Classes.Remove("issue_link_hovered");
|
||||
_lastHover = null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<Models.IssueTrackerMatch> _matches = null;
|
||||
private Models.IssueTrackerMatch _lastHover = null;
|
||||
private List<Models.Hyperlink> _matches = null;
|
||||
private Models.Hyperlink _lastHover = null;
|
||||
}
|
||||
|
||||
public class CommitTimeTextBlock : TextBlock
|
||||
|
|
|
@ -241,7 +241,6 @@
|
|||
<MenuFlyout Placement="BottomEdgeAlignedLeft">
|
||||
<MenuItem Header="{DynamicResource Text.Configure.IssueTracker.NewRule}" Command="{Binding NewIssueTracker}"/>
|
||||
<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.AddSampleJira}" Command="{Binding AddSampleJiraIssueTracker}"/>
|
||||
</MenuFlyout>
|
||||
|
|
Loading…
Reference in a new issue