Skip to content

Commit 2471c8a

Browse files
committed
Clearer feedback to users and small refactor
1 parent 285b42b commit 2471c8a

File tree

7 files changed

+77
-56
lines changed

7 files changed

+77
-56
lines changed

src/git/extension.ts

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
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";
2429
import { StatusBarView } from "../view.js";
2530
import { Blamer } from "./blame.js";
2631
import { 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
}

src/git/head-watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ export class HeadWatch {
7272
return "";
7373
}
7474

75-
return path[0].toLowerCase() + path.substr(1);
75+
return path[0].toLowerCase() + path.slice(1);
7676
}
7777
}

src/git/util/is-hash.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { Commit } from "./stream-parsing.js";
22

3-
export function isHash(hash: string, allowHead = false): boolean {
4-
if (allowHead && hash === "HEAD") {
5-
return true;
6-
}
3+
export function isHash(hash: string): boolean {
74
const length = hash.length;
85
return (length === 40 || length === 64) && /^[a-z0-9]+$/.test(hash);
96
}

src/git/util/stream-parsing.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export type Commit = {
1717
summary: string;
1818
};
1919

20-
export type FileAttachedCommit<T = Commit> = {
20+
type FileAttachedCommit<T = Commit> = {
2121
commit: T;
2222
filename: string;
2323
};
2424

25-
export type Line = {
25+
type Line = {
2626
source: number;
2727
result: number;
2828
};
@@ -185,13 +185,15 @@ export async function* processChunk(
185185
coverageGenerator = processCoverage(value);
186186
}
187187

188-
if (commitLocation) {
189-
if (key === "filename") {
190-
commitLocation.filename = value;
191-
yield* commitFilter(commitLocation, coverageGenerator, commitRegistry);
192-
} else {
193-
processLine(key, value, commitLocation.commit, currentUserEmail);
194-
}
188+
if (!commitLocation) {
189+
continue;
190+
}
191+
192+
if (key === "filename") {
193+
commitLocation.filename = value;
194+
yield* commitFilter(commitLocation, coverageGenerator, commitRegistry);
195+
} else {
196+
processLine(key, value, commitLocation.commit, currentUserEmail);
195197
}
196198
}
197199

src/util/split.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export const split = (target: string, char = " "): [string, string] => {
55
return [target, ""];
66
}
77

8-
return [target.substr(0, index), target.substr(index + 1).trim()];
8+
return [target.slice(0, index), target.slice(index + 1).trim()];
99
};

src/util/text-decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const normalizeCommitInfoTokens = ({
4545
const shortness =
4646
(target: string, fallbackLength: string) =>
4747
(length = ""): string => {
48-
return target.substr(0, Number.parseInt(length || fallbackLength, 10));
48+
return target.slice(0, Number.parseInt(length || fallbackLength, 10));
4949
};
5050
const currentUserAlias = getProperty("currentUserAlias");
5151

src/view.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class StatusBarView {
8787
);
8888
}
8989

90-
public fileToLong(): void {
90+
public fileTooLong(): void {
9191
const maxLineCount = getProperty("maxLineCount");
9292
this.updateTextNoCommand(
9393
"",
@@ -96,7 +96,7 @@ export class StatusBarView {
9696
}
9797

9898
public dispose(): void {
99-
this.statusBar?.dispose();
99+
this.statusBar.dispose();
100100
this.decorationType.dispose();
101101
this.configChange.dispose();
102102
}
@@ -261,7 +261,10 @@ export class StatusBarView {
261261
try {
262262
return await new Promise((resolve, reject) => {
263263
this.ongoingViewUpdateRejects.add(reject);
264-
setTimeout(() => resolve(true), delay);
264+
setTimeout(() => {
265+
this.ongoingViewUpdateRejects.delete(reject);
266+
resolve(true);
267+
}, delay);
265268
});
266269
} catch {
267270
return false;

0 commit comments

Comments
 (0)