44using System . Windows . Controls ;
55using VSGitBlame . Core ;
66using System . Windows . Input ;
7+ using System ;
78
89namespace 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