-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathKvFileMemory.ts
More file actions
80 lines (64 loc) · 2.88 KB
/
Copy pathKvFileMemory.ts
File metadata and controls
80 lines (64 loc) · 2.88 KB
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
import * as vscode from "vscode";
import * as main from "./main";
export function init(context: vscode.ExtensionContext): void {
const rememberCommand = vscode.commands.registerTextEditorCommand("kv.remember", (e) => _handleOnRemember(context, e));
const forgetCommand = vscode.commands.registerTextEditorCommand("kv.forget", (e) => _handleOnForget(context, e));
context.subscriptions.push(rememberCommand, forgetCommand);
_loadMemoryFromGlobalState(context);
}
export function updateForgetRememberCommandContext(document: vscode.TextDocument): void {
if(isRemembered(document.uri.fsPath)) {
vscode.commands.executeCommand("setContext", "sourceEngine.showKvForget", true);
vscode.commands.executeCommand("setContext", "sourceEngine.showKvRemember", false);
} else {
vscode.commands.executeCommand("setContext", "sourceEngine.showKvForget", false);
vscode.commands.executeCommand("setContext", "sourceEngine.showKvRemember", true);
}
}
export function isRemembered(filePath: string): boolean {
return kvFileMemory.includes(filePath);
}
let kvFileMemory: string[] = [];
function _loadMemoryFromGlobalState(context: vscode.ExtensionContext): void {
const loadedMemory = context.globalState.get("kv_memory");
if(loadedMemory === undefined) {
main.output.appendLine("KeyValue memory is empty");
return;
}
if(!Array.isArray(loadedMemory)) {
main.output.appendLine("Error: Tried loading KeyValue file memory from global state but it is not an array");
return;
}
kvFileMemory = [];
let i = 0;
for(const m of loadedMemory) {
if(typeof(m) !== "string") {
main.output.appendLine("Error: Tried loading KeyValue file memory from global state but one of the array entries is not a string");
continue;
}
kvFileMemory.push(m);
i++;
}
main.output.appendLine(`Loaded ${i} remembered keyvalue file paths from global store`);
}
function _handleOnRemember(context: vscode.ExtensionContext, editor: vscode.TextEditor): void {
const filePath = editor.document.uri.fsPath;
if(isRemembered(filePath)) {
return;
}
kvFileMemory.push(filePath);
context.globalState.update("kv_memory", kvFileMemory);
updateForgetRememberCommandContext(editor.document);
vscode.languages.setTextDocumentLanguage(editor.document, "keyvalue");
}
function _handleOnForget(context: vscode.ExtensionContext, editor: vscode.TextEditor): void {
const filePath = editor.document.uri.fsPath;
if(!isRemembered(filePath)) {
return;
}
// Remember that splice deletes in place
kvFileMemory.splice(kvFileMemory.indexOf(filePath), 1);
context.globalState.update("kv_memory", kvFileMemory);
updateForgetRememberCommandContext(editor.document);
vscode.languages.setTextDocumentLanguage(editor.document, "plaintext");
}