-
Notifications
You must be signed in to change notification settings - Fork 0
/
todoListProvider.ts
343 lines (312 loc) · 9.73 KB
/
todoListProvider.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { execSync } from "child_process";
import * as vscode from "vscode";
import { ShouldHaveBeenIncludedSearchWordError } from "./error";
import { log } from "./logger";
const searchWord = /TODO:|FIXME:|HACK:|NOTE:/;
const prefixes = ["TODO", "FIXME", "HACK", "NOTE"] as const;
type Prefix = (typeof prefixes)[number];
export type TodoList = {
prefix: Prefix;
fileAbsPath: string;
currentLine: number;
committedLine?: number;
character: number;
preview: string;
isIgnored: boolean;
author: string;
commitHash?: string;
}[];
export type CommandOpenFile = {
fileAbsPath: string;
selection: vscode.Range;
};
export class TodoListProvider implements vscode.TreeDataProvider<TodoTreeItem> {
private contentMessage?: string;
private workspaceRoot?: string;
private workspaceState?: TypedWorkspaceState;
constructor(contentMessage: string);
constructor(workspaceRoot: string, workspaceState: TypedWorkspaceState);
constructor(...args: unknown[]) {
if (args.length === 1) {
this.contentMessage = args[0] as string;
return;
}
this.workspaceRoot = args[0] as string;
this.workspaceState = args[1] as TypedWorkspaceState;
}
getTreeItem(element: TodoTreeItem): TodoTreeItem {
return element;
}
getChildren(element?: TodoTreeItem): TodoTreeItem[] {
if (this.contentMessage) {
return [
new TodoTreeItem(
this.contentMessage,
vscode.TreeItemCollapsibleState.None,
),
];
}
const todoList = this.workspaceState?.get("todoList");
if (!todoList) {
throw new Error("todoList is undefined");
}
// Top view
const isFirstViewElement = !element;
if (isFirstViewElement) {
const groups = [...prefixes, "IGNORE LIST"];
return groups.map((groupName) => {
return new TodoTreeItem(
groupName,
vscode.TreeItemCollapsibleState.Expanded,
);
});
}
// Nested view
const todoListFilteredByGroup = todoList.filter((todo) => {
if (element.label === "IGNORE LIST") {
return todo.isIgnored === true;
}
return todo.prefix === element.label && todo.isIgnored === false;
});
return todoListFilteredByGroup.map((todo) => {
return new TodoTreeItem(
todo.preview,
vscode.TreeItemCollapsibleState.None,
todo,
);
});
}
private _onDidChangeTreeData: vscode.EventEmitter<
TodoTreeItem | undefined | null
> = new vscode.EventEmitter<TodoTreeItem | undefined | null>();
readonly onDidChangeTreeData: vscode.Event<TodoTreeItem | undefined | null> =
this._onDidChangeTreeData.event;
refresh(): void {
this.workspaceState?.update("todoList", this.generateTodoList());
this._onDidChangeTreeData.fire(undefined);
console.log("refreshed");
}
generateTodoList(isReset = false): TodoList {
const ignoredList = isReset
? []
: this.workspaceState
?.get("todoList")
?.filter((todo) => todo.isIgnored) || [];
const grepTrackedFiles = execSync(`bash -c '
cd "${this.workspaceRoot}"
git grep -n -E \"${searchWord.source}" \
| while IFS=: read -r i j _; do \
/bin/echo "filePath $i"
git blame -L "$j","$j" "$i" --porcelain
/bin/echo ""
done
'`).toString();
const trackedTodoList: TodoList =
grepTrackedFiles === ""
? []
: grepTrackedFiles
.slice(0, -2) // Remove last "\n\n"
.split("\n\n")
.map((output) => {
// output is like:
// filePath src/test/suite/extension.test.ts
// c86dbc4aa3e590d61868dfb242c6bf7fdecc55fc 134 48 1
// author senkenn
// author-mail <senken32@gmail.com>
// author-time 1706716246
// author-tz +0900
// committer senkenn
// committer-mail <senken32@gmail.com>
// committer-time 1706716246
// committer-tz +0900
// summary Add support for Windows in CI workflow and update extension tests
// previous a7c0a2a6b40802b2ab46bdca59fe961f0a0f1525 src/test/suite/extension.test.ts
// filename src/test/suite/extension.test.ts
// editBuilder.insert(new vscode.Position(0, 0), "<!-- TODO: test todo -->");
const outputLines = output.split("\n");
const currentFilePathMatch =
outputLines[0].match(/filePath (.*)/);
const commitHashMatch = outputLines[1].match(
/([a-f0-9]{40}) \d+ \d+ \d/,
);
const currentLineMatch = outputLines[1].match(
/[a-f0-9]{40} \d+ (\d+) \d/,
);
const committedLineMatch = outputLines[1].match(
/[a-f0-9]{40} (\d+) \d+ \d/,
);
const authorMatch = outputLines[2].match(/author (.*)/);
const fullPreview = outputLines.slice(-1)[0].slice(1);
if (
!currentFilePathMatch ||
!commitHashMatch ||
!currentLineMatch ||
!committedLineMatch ||
!authorMatch ||
!fullPreview
) {
log.call({
output,
currentFilePathMatch,
commitHashMatch,
currentLineMatch,
authorMatch,
fullPreview,
});
throw new ShouldHaveBeenIncludedSearchWordError(output);
}
const fileAbsPath = `${this.workspaceRoot}/${currentFilePathMatch[1]}`;
const commitHashIncludingUncommitted = commitHashMatch[1].slice(
0,
8,
);
const currentLine = Number(currentLineMatch[1]);
const committedLine = Number(committedLineMatch[1]);
const author = authorMatch[1];
const matchedWord = fullPreview.match(searchWord);
if (matchedWord?.index === undefined) {
log.call({ output, matchedWord });
throw new ShouldHaveBeenIncludedSearchWordError(output);
}
const prefix = this.getPrefix(matchedWord[0]);
const preview = fullPreview.slice(matchedWord.index);
const character = matchedWord.index;
const nonCommittedHashType = ["Not Committed Yet", "00000000"];
const commitHash = nonCommittedHashType.includes(
commitHashIncludingUncommitted,
)
? undefined
: commitHashIncludingUncommitted;
// Ignore if it is included in ignoredList
const isIgnored = ignoredList.some(
(ignored) =>
ignored.commitHash === commitHash &&
ignored.fileAbsPath === fileAbsPath &&
ignored.committedLine === committedLine,
);
return {
prefix,
fileAbsPath,
currentLine,
committedLine,
character,
preview,
isIgnored,
commitHash,
author,
};
});
const grepResultUntrackedFiles = execSync(`bash -c '
cd "${this.workspaceRoot}"
# IFS is used to split by newline
IFS=$"\n"
files=$(git ls-files --others --exclude-standard) # exclude directory
for file in $files
do
grep --with-filename -n -E "${searchWord.source}" "$file" \
|| [ $? -eq 1 ] # ignore if grep returns 1
done
'`).toString();
const untrackedTodoList: TodoList =
grepResultUntrackedFiles === ""
? []
: grepResultUntrackedFiles
.slice(0, -1) // cut last "\n"
.split("\n")
.map((output) => {
// Format: {filePath}:{line}:{fullPreview}
const [filePath, line, ...rest] = output.split(":");
const matchedWord = rest.join(":").match(searchWord);
if (
matchedWord?.index === undefined ||
matchedWord.input === undefined
) {
log.call({ output, matchedWord });
throw new ShouldHaveBeenIncludedSearchWordError(output);
}
const prefix = this.getPrefix(matchedWord[0]);
const character = matchedWord.index;
return {
author: "Not Committed Yet",
prefix,
fileAbsPath: `${this.workspaceRoot}/${filePath}`,
currentLine: Number(line),
character,
preview: matchedWord.input.slice(matchedWord.index),
isIgnored: false,
};
});
console.log("Todo list was generated successfully");
return [...trackedTodoList, ...untrackedTodoList];
}
private getPrefix(matchPrefixStr: string): Prefix {
const prefix = matchPrefixStr.slice(0, -1); // cut last ":"
if (!Object.values(prefixes).some((p) => p === prefix)) {
throw new Error("prefixStr is not included in prefixes");
}
return prefix as Prefix;
}
}
export class TodoTreeItem extends vscode.TreeItem {
public contextValue?:
| "committed-item"
| "non-committed-item"
| "ignored-item";
constructor(
public readonly label: string,
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
public readonly todoItemMetaData?: TodoList[number],
) {
super(label, collapsibleState);
if (!todoItemMetaData) {
return;
}
this.contextValue = todoItemMetaData.isIgnored
? "ignored-item"
: todoItemMetaData.commitHash
? "committed-item"
: "non-committed-item";
const { fileAbsPath, currentLine: line, character } = todoItemMetaData;
const zeroBasedLine = line - 1;
this.command = {
command: "todo-list-for-teams.openFile",
title: "Todo List For Teams: Open File", // TODO: get from package.json
arguments: [
{
fileAbsPath,
selection: new vscode.Range(
zeroBasedLine,
character,
zeroBasedLine,
character,
),
} satisfies CommandOpenFile,
],
};
}
}
type WorkspaceStateKeys = "todoList";
type WorkspaceStateValueSelector<T extends WorkspaceStateKeys> =
T extends "todoList" ? TodoList : never;
export class TypedWorkspaceState {
constructor(
private workspaceState: vscode.ExtensionContext["workspaceState"],
) {
this.workspaceState = workspaceState;
}
public get<T extends WorkspaceStateKeys>(
key: T,
): WorkspaceStateValueSelector<T> {
const getResult = this.workspaceState.get(key);
if (!getResult) {
throw new Error(`workspaceState does not have "${key}"`);
}
return <WorkspaceStateValueSelector<T>>getResult;
}
public update<T extends WorkspaceStateKeys>(
key: T,
value: WorkspaceStateValueSelector<T>,
): void {
this.workspaceState.update(key, value);
}
}