Skip to content

Commit 84fb39f

Browse files
committed
code_review: PR #1402
- it's unnecessary to implement `IEnumerable` interface - we should check `IsIntersecting` before creating `InlineElement` to avoid unnecessary works suck as running `git cat-file -t <hash>` - sort whold list after all elements have been added to avoid unnecessary memmove in `Insert` Signed-off-by: leo <[email protected]>
1 parent fe54d30 commit 84fb39f

File tree

7 files changed

+42
-78
lines changed

7 files changed

+42
-78
lines changed

src/Models/Commit.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static double OpacityForNotMerged
3333
public User Committer { get; set; } = User.Invalid;
3434
public ulong CommitterTime { get; set; } = 0;
3535
public string Subject { get; set; } = string.Empty;
36-
public List<string> Parents { get; set; } = new List<string>();
37-
public List<Decorator> Decorators { get; set; } = new List<Decorator>();
36+
public List<string> Parents { get; set; } = new();
37+
public List<Decorator> Decorators { get; set; } = new();
3838
public bool HasDecorators => Decorators.Count > 0;
3939

4040
public string AuthorTimeStr => DateTime.UnixEpoch.AddSeconds(AuthorTime).ToLocalTime().ToString(DateTimeFormat.Active.DateTime);
@@ -49,7 +49,7 @@ public static double OpacityForNotMerged
4949
public int Color { get; set; } = 0;
5050
public double Opacity => IsMerged ? 1 : OpacityForNotMerged;
5151
public FontWeight FontWeight => IsCurrentHead ? FontWeight.Bold : FontWeight.Regular;
52-
public Thickness Margin { get; set; } = new Thickness(0);
52+
public Thickness Margin { get; set; } = new(0);
5353
public IBrush Brush => CommitGraph.Pens[Color].Brush;
5454

5555
public void ParseDecorators(string data)
@@ -121,6 +121,6 @@ public void ParseDecorators(string data)
121121
public class CommitFullMessage
122122
{
123123
public string Message { get; set; } = string.Empty;
124-
public InlineElementCollector Inlines { get; set; } = [];
124+
public InlineElementCollector Inlines { get; set; } = new();
125125
}
126126
}

src/Models/InlineElement.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
{
33
public enum InlineElementType
44
{
5-
None = 0,
6-
Keyword,
5+
Keyword = 0,
76
Link,
87
CommitSHA,
98
Code,
109
}
1110

1211
public class InlineElement
1312
{
14-
public InlineElementType Type { get; set; } = InlineElementType.None;
15-
public int Start { get; set; } = 0;
16-
public int Length { get; set; } = 0;
17-
public string Link { get; set; } = "";
13+
public InlineElementType Type { get; }
14+
public int Start { get; }
15+
public int Length { get; }
16+
public string Link { get; }
1817

1918
public InlineElement(InlineElementType type, int start, int length, string link)
2019
{
@@ -24,7 +23,7 @@ public InlineElement(InlineElementType type, int start, int length, string link)
2423
Link = link;
2524
}
2625

27-
public bool Intersect(int start, int length)
26+
public bool IsIntersecting(int start, int length)
2827
{
2928
if (start == Start)
3029
return true;
Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,38 @@
1-
using System.Collections;
21
using System.Collections.Generic;
3-
using System.Diagnostics;
42

53
namespace SourceGit.Models
64
{
7-
public class InlineElementCollector : IEnumerable<InlineElement>
5+
public class InlineElementCollector
86
{
9-
private readonly List<InlineElement> _implementation = [];
10-
11-
public void Clear()
12-
{
13-
_implementation.Clear();
14-
15-
AssertInvariant();
16-
}
17-
187
public int Count => _implementation.Count;
8+
public InlineElement this[int index] => _implementation[index];
199

20-
public void Add(InlineElement element)
10+
public InlineElement Intersect(int start, int length)
2111
{
22-
23-
var index = FindIndex(element.Start);
24-
if (!IsIntersection(index, element.Start, element.Length))
25-
_implementation.Insert(index, element);
26-
27-
AssertInvariant();
28-
}
29-
30-
[Conditional("DEBUG")]
31-
private void AssertInvariant()
32-
{
33-
if (_implementation.Count == 0)
34-
return;
35-
36-
for (var index = 1; index < _implementation.Count; index++)
12+
foreach (var elem in _implementation)
3713
{
38-
var prev = _implementation[index - 1];
39-
var curr = _implementation[index];
40-
41-
Debug.Assert(prev.Start + prev.Length <= curr.Start);
14+
if (elem.IsIntersecting(start, length))
15+
return elem;
4216
}
17+
18+
return null;
4319
}
4420

45-
public InlineElement Lookup(int position)
21+
public void Add(InlineElement element)
4622
{
47-
var index = FindIndex(position);
48-
return IsIntersection(index, position, 1)
49-
? _implementation[index]
50-
: null;
23+
_implementation.Add(element);
5124
}
5225

53-
private int FindIndex(int start)
26+
public void Sort()
5427
{
55-
var index = 0;
56-
while (index < _implementation.Count && _implementation[index].Start <= start)
57-
index++;
58-
59-
return index;
28+
_implementation.Sort((l, r) => l.Start.CompareTo(r.Start));
6029
}
6130

62-
private bool IsIntersection(int index, int start, int length)
31+
public void Clear()
6332
{
64-
if (index > 0)
65-
{
66-
var predecessor = _implementation[index - 1];
67-
if (predecessor.Start + predecessor.Length >= start)
68-
return true;
69-
}
70-
71-
if (index < _implementation.Count)
72-
{
73-
var successor = _implementation[index];
74-
if (start + length >= successor.Start)
75-
return true;
76-
}
77-
78-
return false;
33+
_implementation.Clear();
7934
}
8035

81-
public IEnumerator<InlineElement> GetEnumerator() => _implementation.GetEnumerator();
82-
83-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
36+
private readonly List<InlineElement> _implementation = [];
8437
}
8538
}

src/Models/IssueTrackerRule.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public void Matches(InlineElementCollector outs, string message)
5959

6060
var start = match.Index;
6161
var len = match.Length;
62+
if (outs.Intersect(start, len) != null)
63+
continue;
6264

6365
var link = _urlTemplate;
6466
for (var j = 1; j < match.Groups.Count; j++)

src/ViewModels/CommitDetail.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ private Models.InlineElementCollector ParseInlinesInMessage(string message)
638638

639639
var start = match.Index;
640640
var len = match.Length;
641+
if (inlines.Intersect(start, len) != null)
642+
continue;
641643

642644
var url = message.Substring(start, len);
643645
if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
@@ -653,13 +655,16 @@ private Models.InlineElementCollector ParseInlinesInMessage(string message)
653655

654656
var start = match.Index;
655657
var len = match.Length;
658+
if (inlines.Intersect(start, len) != null)
659+
continue;
656660

657661
var sha = match.Groups[1].Value;
658662
var isCommitSHA = new Commands.IsCommitSHA(_repo.FullPath, sha).Result();
659663
if (isCommitSHA)
660664
inlines.Add(new Models.InlineElement(Models.InlineElementType.CommitSHA, start, len, sha));
661665
}
662666

667+
inlines.Sort();
663668
return inlines;
664669
}
665670

src/Views/CommitMessagePresenter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
4848

4949
var inlines = new List<Inline>();
5050
var pos = 0;
51-
foreach (var link in links)
51+
for (var i = 0; i < links.Count; i++)
5252
{
53+
var link = links[i];
5354
if (link.Start > pos)
5455
inlines.Add(new Run(message.Substring(pos, link.Start - pos)));
5556

@@ -95,8 +96,7 @@ protected override void OnPointerMoved(PointerEventArgs e)
9596
point = new Point(x, y);
9697

9798
var pos = TextLayout.HitTestPoint(point).TextPosition;
98-
99-
if (links.Lookup(pos) is { } link)
99+
if (links.Intersect(pos, 1) is { } link)
100100
SetHoveredIssueLink(link);
101101
else
102102
ClearHoveredIssueLink();

src/Views/CommitSubjectPresenter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,17 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
167167

168168
var start = match.Index;
169169
var len = match.Length;
170+
if (_elements.Intersect(start, len) != null)
171+
continue;
172+
170173
_elements.Add(new Models.InlineElement(Models.InlineElementType.Code, start, len, string.Empty));
171174
}
172175

173176
var rules = IssueTrackerRules ?? [];
174177
foreach (var rule in rules)
175178
rule.Matches(_elements, subject);
176179

180+
_elements.Sort();
177181
_needRebuildInlines = true;
178182
InvalidateVisual();
179183
}
@@ -247,8 +251,9 @@ private void GenerateFormattedTextElements()
247251
var codeTypeface = new Typeface(codeFontFamily, FontStyle.Normal, FontWeight);
248252
var pos = 0;
249253
var x = 0.0;
250-
foreach (var elem in _elements)
254+
for (var i = 0; i < _elements.Count; i++)
251255
{
256+
var elem = _elements[i];
252257
if (elem.Start > pos)
253258
{
254259
var normal = new FormattedText(
@@ -350,7 +355,7 @@ public Inline(double x, FormattedText text, Models.InlineElement elem)
350355
}
351356
}
352357

353-
private Models.InlineElementCollector _elements = [];
358+
private Models.InlineElementCollector _elements = new();
354359
private List<Inline> _inlines = [];
355360
private Models.InlineElement _lastHover = null;
356361
private bool _needRebuildInlines = false;

0 commit comments

Comments
 (0)