forked from waderyan/vscode-gitblame
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathextension.ts
198 lines (167 loc) · 6.09 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
commands,
Disposable,
env,
MessageItem,
TextEditor,
window,
workspace,
} from "vscode";
import type { LineAttatchedCommit } from "./util/stream-parsing";
import { Document, validEditor } from "../util/editorvalidator";
import {
normalizeCommitInfoTokens,
parseTokens,
} from "../util/textdecorator";
import { StatusBarView } from "../view";
import { Blamer } from "./blame";
import { getProperty } from "../util/property";
import { getToolUrl } from "./util/get-tool-url";
import { isUncomitted } from "./util/uncommitted";
import { errorMessage, infoMessage } from "../util/message";
import {
getActiveTextEditor,
getFilePosition,
NO_FILE_OR_PLACE,
} from "../util/get-active";
import { HeadWatch } from "./head-watch";
type ActionableMessageItem = MessageItem & {
action: () => void;
}
export class Extension {
private readonly disposable: Disposable;
private readonly blame: Blamer;
private readonly view: StatusBarView;
private readonly headWatcher: HeadWatch;
constructor() {
this.blame = new Blamer;
this.view = new StatusBarView;
this.headWatcher = new HeadWatch;
this.disposable = this.setupListeners();
this.updateView();
}
public async blameLink(): Promise<void> {
const toolUrl = await getToolUrl(await this.commit(true));
if (toolUrl) {
commands.executeCommand("vscode.open", toolUrl);
} else {
errorMessage("Empty gitblame.commitUrl");
}
}
public async showMessage(): Promise<void> {
const lineAware = await this.commit();
if (!lineAware || isUncomitted(lineAware.commit)) {
this.view.clear();
return;
}
const message = parseTokens(
getProperty("infoMessageFormat"),
normalizeCommitInfoTokens(lineAware.commit),
);
const toolUrl = await getToolUrl(lineAware);
const action: ActionableMessageItem[] | undefined = toolUrl ? [{
title: "View",
action() {
commands.executeCommand("vscode.open", toolUrl);
},
}] : undefined;
this.view.set(lineAware.commit, getActiveTextEditor());
(await infoMessage(message, action))?.action();
}
public async copyHash(): Promise<void> {
const lineAware = await this.commit(true);
if (lineAware && !isUncomitted(lineAware.commit)) {
await env.clipboard.writeText(lineAware.commit.hash);
infoMessage("Copied hash");
}
}
public async copyToolUrl(): Promise<void> {
const lineAware = await this.commit(true);
const toolUrl = await getToolUrl(lineAware);
if (toolUrl) {
await env.clipboard.writeText(toolUrl.toString());
infoMessage("Copied tool URL");
} else {
errorMessage("gitblame.commitUrl config empty");
}
}
public dispose(): void {
this.view.dispose();
this.disposable.dispose();
this.blame.dispose();
this.headWatcher.dispose();
}
private setupListeners(): Disposable {
const changeTextEditorSelection = (textEditor: TextEditor): void => {
const { scheme } = textEditor.document.uri;
if (scheme === "file" || scheme === "untitled") {
this.updateView(textEditor);
}
}
this.headWatcher.onChange(({ repositoryRoot }) => {
this.blame.removeFromRepository(repositoryRoot);
});
return Disposable.from(
window.onDidChangeActiveTextEditor((textEditor): void => {
if (validEditor(textEditor)) {
this.view.activity();
this.blame.file(textEditor.document.fileName);
/**
* For unknown reasons files without previous or stored
* selection locations don't trigger the change selection
* event. I have not been able to find a way to detect when
* this happens. Running the event handler twice seames to
* be a good enough workaround.
*/
changeTextEditorSelection(textEditor);
} else {
this.view.clear();
}
}),
window.onDidChangeTextEditorSelection(({ textEditor }) => {
changeTextEditorSelection(textEditor);
}),
workspace.onDidSaveTextDocument((): void => {
this.updateView();
}),
workspace.onDidCloseTextDocument((document: Document): void => {
this.blame.remove(document.fileName);
}),
);
}
private async updateView(
textEditor = getActiveTextEditor(),
): Promise<void> {
if (!validEditor(textEditor)) {
this.view.clear();
return;
}
this.view.activity();
this.headWatcher.addFile(textEditor.document.fileName);
const before = getFilePosition(textEditor);
const lineAware = await this.blame.getLine(textEditor.document.fileName, textEditor.selection.active.line);
const after = getFilePosition(textEditor);
// Only update if we haven't moved since we started blaming
// or if we no longer have focus on any file
if (before === after || after === NO_FILE_OR_PLACE) {
this.view.set(lineAware?.commit, textEditor);
}
}
private async commit(undercover = false): Promise<LineAttatchedCommit | undefined> {
const notBlame = () => errorMessage("Unable to blame current line");
const editor = getActiveTextEditor();
if (!validEditor(editor)) {
notBlame();
return;
}
if (!undercover) {
this.view.activity();
}
this.headWatcher.addFile(editor.document.fileName);
const line = await this.blame.getLine(editor.document.fileName, editor.selection.active.line);
if (!line) {
notBlame();
}
return line;
}
}