44 Disposable ,
55 env ,
66 type MessageItem ,
7+ type TextDocument ,
78 type TextEditor ,
89 ThemeIcon ,
910 window ,
@@ -20,7 +21,11 @@ import {
2021 normalizeCommitInfoTokens ,
2122 parseTokens ,
2223} from "../util/text-decorator.js" ;
23- import { type Document , validEditor } from "../util/valid-editor.js" ;
24+ import {
25+ type Document ,
26+ type PartialTextEditor ,
27+ validEditor ,
28+ } from "../util/valid-editor.js" ;
2429import { StatusBarView } from "../view.js" ;
2530import { Blamer } from "./blame.js" ;
2631import { HeadWatch } from "./head-watch.js" ;
@@ -49,14 +54,15 @@ export class Extension {
4954 public async blameLink ( ) : Promise < void > {
5055 const lineAware = await this . commit ( true ) ;
5156 if ( lineAware === undefined ) {
57+ await errorMessage ( "No commit to copy link from" ) ;
5258 return ;
5359 }
5460 const toolUrl = await getToolUrl ( lineAware ) ;
5561
5662 if ( toolUrl ) {
57- commands . executeCommand ( "vscode.open" , toolUrl ) ;
63+ await commands . executeCommand ( "vscode.open" , toolUrl ) ;
5864 } else {
59- errorMessage ( "Empty gitblame.commitUrl" ) ;
65+ await errorMessage ( "Empty gitblame.commitUrl" ) ;
6066 }
6167 }
6268
@@ -65,6 +71,7 @@ export class Extension {
6571
6672 if ( ! lineAware || isUncommitted ( lineAware . commit ) ) {
6773 this . view . clear ( ) ;
74+ await errorMessage ( "No commit to show" ) ;
6875 return ;
6976 }
7077
@@ -73,48 +80,51 @@ export class Extension {
7380 normalizeCommitInfoTokens ( lineAware . commit ) ,
7481 ) ;
7582 const toolUrl = await getToolUrl ( lineAware ) ;
76- const action : ActionableMessageItem [ ] = [ ] ;
83+ const actions : ActionableMessageItem [ ] = [ ] ;
7784
7885 if ( toolUrl ) {
79- action . push ( {
86+ actions . push ( {
8087 title : "Online" ,
8188 action ( ) {
8289 commands . executeCommand ( "vscode.open" , toolUrl ) ;
8390 } ,
8491 } ) ;
8592 }
8693
87- action . push ( {
94+ actions . push ( {
8895 title : "Terminal" ,
8996 action : ( ) => this . runGitShow ( ) ,
9097 } ) ;
9198
9299 this . view . set ( lineAware . commit , getActiveTextEditor ( ) ) ;
93100
94- ( await infoMessage ( message , action ) ) ?. action ( ) ;
101+ ( await infoMessage ( message , actions ) ) ?. action ( ) ;
95102 }
96103
97104 public async copyHash ( ) : Promise < void > {
98105 const lineAware = await this . commit ( true ) ;
99106
100107 if ( lineAware && ! isUncommitted ( lineAware . commit ) ) {
101108 await env . clipboard . writeText ( lineAware . commit . hash ) ;
102- infoMessage ( "Copied hash" ) ;
109+ await infoMessage ( "Copied hash" ) ;
110+ } else {
111+ await errorMessage ( "No commit to copy hash from" ) ;
103112 }
104113 }
105114
106115 public async copyToolUrl ( ) : Promise < void > {
107116 const lineAware = await this . commit ( true ) ;
108117 if ( lineAware === undefined ) {
118+ await errorMessage ( "No commit to copy link from" ) ;
109119 return ;
110120 }
111121 const toolUrl = await getToolUrl ( lineAware ) ;
112122
113123 if ( toolUrl ) {
114124 await env . clipboard . writeText ( toolUrl . toString ( ) ) ;
115- infoMessage ( "Copied tool URL" ) ;
125+ await infoMessage ( "Copied tool URL" ) ;
116126 } else {
117- errorMessage ( "gitblame.commitUrl config empty" ) ;
127+ await errorMessage ( "gitblame.commitUrl config empty" ) ;
118128 }
119129 }
120130
@@ -132,7 +142,7 @@ export class Extension {
132142 const { hash } = currentLine . commit ;
133143
134144 // Only ever allow HEAD or a git hash
135- if ( ! isHash ( hash , true ) ) {
145+ if ( hash !== "HEAD" && ! isHash ( hash ) ) {
136146 return ;
137147 }
138148
@@ -148,25 +158,19 @@ export class Extension {
148158 }
149159
150160 public async updateView (
151- textEditor = getActiveTextEditor ( ) ,
161+ editor = getActiveTextEditor ( ) ,
152162 useDelay = true ,
153163 ) : Promise < void > {
154- if ( ! this . view . preUpdate ( textEditor ) ) {
164+ if ( ! this . view . preUpdate ( editor ) ) {
155165 return ;
156166 }
157167
158- if ( textEditor . document . lineCount > getProperty ( "maxLineCount" ) ) {
159- this . view . fileToLong ( ) ;
168+ if ( this . isFileMaxLineCount ( editor . document ) ) {
160169 return ;
161170 }
162171
163- this . headWatcher . addFile ( textEditor . document . fileName ) ;
164-
165- const before = getFilePosition ( textEditor ) ;
166- const lineAware = await this . blame . getLine (
167- textEditor . document . fileName ,
168- textEditor . selection . active . line ,
169- ) ;
172+ const before = getFilePosition ( editor ) ;
173+ const line = await this . getLine ( editor ) ;
170174
171175 const textEditorAfter = getActiveTextEditor ( ) ;
172176 if ( ! validEditor ( textEditorAfter ) ) {
@@ -177,7 +181,7 @@ export class Extension {
177181 // Only update if we haven't moved since we started blaming
178182 // or if we no longer have focus on any file
179183 if ( before === after || after === NO_FILE_OR_PLACE ) {
180- this . view . set ( lineAware ?. commit , textEditor , useDelay ) ;
184+ this . view . set ( line ?. commit , textEditorAfter , useDelay ) ;
181185 }
182186 }
183187
@@ -213,8 +217,10 @@ export class Extension {
213217 window . onDidChangeTextEditorSelection ( ( { textEditor } ) => {
214218 changeTextEditorSelection ( textEditor ) ;
215219 } ) ,
216- workspace . onDidSaveTextDocument ( ( ) : void => {
217- this . updateView ( ) ;
220+ workspace . onDidSaveTextDocument ( ( document : TextDocument ) : void => {
221+ if ( getActiveTextEditor ( ) ?. document === document ) {
222+ this . updateView ( ) ;
223+ }
218224 } ) ,
219225 workspace . onDidCloseTextDocument ( ( document : Document ) : void => {
220226 this . blame . remove ( document . fileName ) ;
@@ -229,39 +235,52 @@ export class Extension {
229235 }
230236
231237 private async commit (
232- noVisibleActivity : boolean ,
238+ hideActivity : boolean ,
233239 ) : Promise < LineAttachedCommit | undefined > {
234240 const editor = getActiveTextEditor ( ) ;
235241
236242 if ( ! validEditor ( editor ) ) {
237- errorMessage (
243+ void errorMessage (
238244 "Unable to blame current line. Active view is not a file on disk." ,
239245 ) ;
240246 return ;
241247 }
242248
243- if ( editor . document . lineCount > getProperty ( "maxLineCount" ) ) {
244- errorMessage ( "Git Blame is disabled for the current file" ) ;
245- this . view . fileToLong ( ) ;
249+ if ( this . isFileMaxLineCount ( editor . document ) ) {
250+ void errorMessage ( "Git Blame is disabled for the current file" ) ;
246251 return ;
247252 }
248253
249- if ( ! noVisibleActivity ) {
254+ if ( ! hideActivity ) {
250255 this . view . activity ( ) ;
251256 }
252257
253- this . headWatcher . addFile ( editor . document . fileName ) ;
254- const line = await this . blame . getLine (
255- editor . document . fileName ,
256- editor . selection . active . line ,
257- ) ;
258+ const line = await this . getLine ( editor ) ;
258259
259260 if ( ! line ) {
260- errorMessage (
261+ void errorMessage (
261262 "Unable to blame current line. Unable to get blame information for line." ,
262263 ) ;
263264 }
264265
265266 return line ;
266267 }
268+
269+ private isFileMaxLineCount ( document : Document ) : boolean {
270+ if ( document . lineCount > getProperty ( "maxLineCount" ) ) {
271+ this . view . fileTooLong ( ) ;
272+ return true ;
273+ }
274+ return false ;
275+ }
276+
277+ private async getLine (
278+ editor : PartialTextEditor ,
279+ ) : Promise < LineAttachedCommit | undefined > {
280+ this . headWatcher . addFile ( editor . document . fileName ) ;
281+ return await this . blame . getLine (
282+ editor . document . fileName ,
283+ editor . selection . active . line ,
284+ ) ;
285+ }
267286}
0 commit comments