Skip to content

Commit 00051c2

Browse files
Merge pull request #2 from KingofBeast/master
Commit Hash and other fixes
2 parents ceb245f + e0a3512 commit 00051c2

File tree

5 files changed

+228
-178
lines changed

5 files changed

+228
-178
lines changed

VSGitBlame.Core/CommitInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ namespace VSGitBlame.Core;
44

55
public class CommitInfo
66
{
7+
public static readonly CommitInfo InProgress = new CommitInfo() { ShowDetails = false, Summary = "Blame in progress.." };
8+
public static readonly CommitInfo Uncommitted = new CommitInfo() { ShowDetails = false, Summary = "Uncommitted changes"};
9+
10+
public bool ShowDetails { get; set; } = true;
711
public string Hash { get; set; } = string.Empty;
812
public string AuthorName { get; set; } = string.Empty;
913
public string AuthorEmail { get; set; } = string.Empty;

VSGitBlame.Core/FileBlameInfo.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@ namespace VSGitBlame.Core;
66

77
public class FileBlameInfo
88
{
9-
Dictionary<int, string> _lineCommitCache;
9+
private static readonly Dictionary<int, string> defaultEmptyLineCollection = new(0);
10+
11+
Dictionary<int, string> _lineCommitCache = defaultEmptyLineCollection;
1012
static readonly string[] _timeZoneFormats = [@"\+hhmm", @"\-hhmm"];
1113

12-
public FileBlameInfo(string porcelainBlameString)
14+
public void Parse(string porcelainBlameString)
1315
{
14-
Dictionary<int, string> output = null;
1516
try
1617
{
17-
output = ParsePorcelainOutput(porcelainBlameString);
18+
_lineCommitCache = ParsePorcelainOutput(porcelainBlameString);
1819
}
1920
catch
2021
{
2122
// TODO: Stop silent failure and implement logging/telemetry
22-
output = new();
2323
}
24-
25-
_lineCommitCache = output;
2624
}
2725

2826

@@ -79,6 +77,11 @@ Dictionary<int, string> ParsePorcelainOutput(string output)
7977
{
8078
lines.CropTillNth(newLine, 9);
8179
}
80+
else if (int.TryParse(hash, out int zeroHash) && zeroHash == 0)
81+
{
82+
CommitInfoCache.Add(hash, CommitInfo.Uncommitted);
83+
lines.CropTillNth(newLine, 9);
84+
}
8285
else
8386
{
8487
line = lines.SliceTill(newLine);
@@ -97,7 +100,7 @@ Dictionary<int, string> ParsePorcelainOutput(string output)
97100

98101
line = lines.SliceTill(newLine);
99102
lines.CropTillNth(newLine);
100-
103+
101104
line.CropTillNth(space);
102105
string timeZone = line.ToString();
103106
TimeSpan timeZoneOffset = TimeSpan.ParseExact(timeZone, _timeZoneFormats, CultureInfo.InvariantCulture);

VSGitBlame/CommitInfoAdornment.cs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Windows.Controls;
55
using VSGitBlame.Core;
66
using System.Windows.Input;
7+
using System;
78

89
namespace VSGitBlame;
910

@@ -21,10 +22,22 @@ public CommitInfoAdornment(IWpfTextView view)
2122
_textDocument = _view.TextBuffer.Properties.GetProperty<ITextDocument>(typeof(ITextDocument));
2223

2324
// Event Subscriptions
24-
_view.LayoutChanged += OnLayoutChanged;
25+
_view.GotAggregateFocus += (sender, args) => RefreshBlameOnCurrentLine();
26+
_view.LayoutChanged += (sender, args) => RefreshBlameOnCurrentLine();
27+
_view.Closed += (sender, args) => GitBlamer.OnBlameFinished -= OnBlameFinished;
28+
2529
_textDocument.FileActionOccurred += TextDocument_FileActionOccurred;
2630
_view.Caret.PositionChanged += Caret_PositionChanged;
2731
//_view.VisualElement.MouseLeftButtonUp += VisualElement_MouseLeftButtonUp;
32+
33+
GitBlamer.OnBlameFinished += OnBlameFinished;
34+
35+
RefreshBlameOnCurrentLine();
36+
}
37+
38+
private void RefreshBlameOnCurrentLine()
39+
{
40+
OnCaretLineChanged(_lastCaretLine, new CaretPositionChangedEventArgs(_view, _view.Caret.Position, _view.Caret.Position));
2841
}
2942

3043
private void Caret_PositionChanged(object sender, CaretPositionChangedEventArgs e)
@@ -37,31 +50,46 @@ private void Caret_PositionChanged(object sender, CaretPositionChangedEventArgs
3750
}
3851
}
3952

40-
4153
private void TextDocument_FileActionOccurred(object sender, TextDocumentFileActionEventArgs e)
4254
{
4355
GitBlamer.InvalidateCache(_textDocument.FilePath);
56+
RefreshBlameOnCurrentLine();
4457
}
4558

46-
private void OnCaretLineChanged(int lineNumber, CaretPositionChangedEventArgs e)
59+
private void OnBlameFinished(object sender, string filePath)
4760
{
48-
_adornmentLayer.RemoveAllAdornments();
61+
if (filePath != _textDocument.FilePath)
62+
return;
63+
64+
RefreshBlameOnCurrentLine();
65+
}
4966

67+
private void OnCaretLineChanged(int lineNumber, CaretPositionChangedEventArgs e)
68+
{
5069
// Only show commit info if the document is not dirty (no unsaved changes)
5170
if (_textDocument.IsDirty)
71+
{
72+
_adornmentLayer.RemoveAllAdornments();
5273
return;
74+
}
5375

5476
// Get the caret position in the view
5577
var caretPosition = e.NewPosition.BufferPosition;
5678
var textViewLine = _view.GetTextViewLineContainingBufferPosition(caretPosition);
5779

5880
if (textViewLine == null)
81+
{
82+
_adornmentLayer.RemoveAllAdornments();
5983
return;
84+
}
6085

61-
var commitInfo = GitBlamer.GetBlame(_textDocument.FilePath, lineNumber + 1);
86+
var commitInfo = GitBlamer.GetBlame(_textDocument.FilePath, Math.Max(0, lineNumber) + 1);
6287

6388
if (commitInfo == null)
89+
{
90+
_adornmentLayer.RemoveAllAdornments();
6491
return;
92+
}
6593

6694
ShowCommitInfo(commitInfo, textViewLine);
6795
}
@@ -106,28 +134,13 @@ private void VisualElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs
106134
ShowCommitInfo(commitInfo, textView);
107135
}
108136

109-
void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
110-
{
111-
foreach (var line in e.NewOrReformattedLines)
112-
{
113-
CreateVisuals(line);
114-
}
115-
}
116-
117-
void CreateVisuals(ITextViewLine line)
118-
{
119-
// Clear previous adornments
120-
_adornmentLayer.RemoveAllAdornments();
121-
}
122-
123137
void ShowCommitInfo(CommitInfo commitInfo, ITextViewLine line)
124138
{
125139
var container = CommitInfoViewFactory.Get(commitInfo, _adornmentLayer);
126140

127141
Canvas.SetLeft(container, line.Right);
128-
Canvas.SetTop(container, line.Top);
142+
Canvas.SetTop(container, line.TextTop);
129143

130-
_adornmentLayer.RemoveAllAdornments();
131144
SnapshotSpan span = new SnapshotSpan(_adornmentLayer.TextView.TextSnapshot, Span.FromBounds(line.Start, line.End));
132145
_adornmentLayer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, container, null);
133146
}

0 commit comments

Comments
 (0)