-
Notifications
You must be signed in to change notification settings - Fork 2
/
Extension.ts
162 lines (139 loc) · 4.8 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
/**
#### ######## ####### ## ## ## ## ## ####### ####### #######
## ## ## ## ## ### ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## #### ## ## ## ## ## ## ##
## ######## ## ## ## ## ## ## ## ## ####### ####### #######
## ## ## ## ## ## #### ## ## ## ## ## ##
## ## ## ## ## ## ### ## ## ## ## ## ## ## ##
#### ## ## ####### ## ## ######## ####### ######### ####### #######
* @author IronLu233 <lrironsora@gmail.com>
* @date 2022-06-12 22:33:53
*/
import * as vscode from "vscode";
import { ConfigSection, CUSTOM_TEMPLATE_FILE_NAME } from "./constants";
import { extensionConfigManager } from "./ExtensionConfigManager";
import { FileheaderLanguageProvider } from "./FileheaderLanguageProviders";
import { fileheaderManager } from "./FileheaderManager";
class Extension {
private disposers: vscode.Disposable[] = [];
private customTemplateFileheaderWatcher: vscode.FileSystemWatcher | undefined;
activate = async (context: vscode.ExtensionContext) => {
await fileheaderManager.loadProviders();
this.disposers.push(
vscode.commands.registerCommand(
"fileheader-pro.fileheader",
this.addFileheader,
this
)
);
this.disposers.push(
vscode.commands.registerCommand(
"fileheader-pro.generateCustomTemplate",
this.createCustomTemplate,
this
)
);
this.disposers.push(
vscode.commands.registerCommand(
"fileheader-pro.reloadCustomTemplateProvider",
fileheaderManager.loadProviders,
fileheaderManager
)
);
this.createCustomTemplateFileListener();
this.disposers.push(
vscode.workspace.onDidChangeWorkspaceFolders(
this.createCustomTemplateFileListener
)
);
this.disposers.push(
vscode.workspace.onDidCreateFiles(this.onCreateDocument, this)
);
this.disposers.push(
vscode.workspace.onWillSaveTextDocument(this.onSaveDocument, this)
);
this.onDidChangeVisibleTextEditors(vscode.window.visibleTextEditors);
this.disposers.push(
vscode.window.onDidChangeVisibleTextEditors(
this.onDidChangeVisibleTextEditors
)
);
};
deactivate = () => {
for (let disposer of this.disposers) {
disposer.dispose();
}
};
private async addFileheader(...args: any[]) {
const currentDocument = vscode.window.activeTextEditor?.document;
if (!currentDocument) {
return vscode.window.showErrorMessage(
"Fileheader Pro: You should open a file first."
);
}
return fileheaderManager.updateFileheader(currentDocument);
}
createCustomTemplate() {
return FileheaderLanguageProvider.createCustomTemplate();
}
private async onCreateDocument(e: vscode.FileCreateEvent) {
const enabled = extensionConfigManager.get<boolean>(
ConfigSection.autoInsertOnCreateFile
);
if (!enabled) {
return;
}
for (let file of e.files) {
const document = await vscode.workspace.openTextDocument(file);
if (
document.lineCount > 1 ||
document.lineAt(0).text.trim().length !== 0
) {
return;
}
await fileheaderManager.updateFileheader(document, {
silent: true,
});
}
}
private onSaveDocument(e: vscode.TextDocumentWillSaveEvent) {
const enabled = extensionConfigManager.get<boolean>(
ConfigSection.autoUpdateOnSave
);
if (!enabled) {
return;
}
if (e.document.uri.path.includes(`.vscode/${CUSTOM_TEMPLATE_FILE_NAME}`)) {
return;
}
// disable insert new fileheader because it will cause some issues
// we only support update origin fileheader
const updatePromise = fileheaderManager.updateFileheader(e.document, {
allowInsert: false,
silent: true,
});
e.waitUntil(updatePromise);
}
private onDidChangeVisibleTextEditors(e: readonly vscode.TextEditor[]) {
fileheaderManager.recordOriginFileHash(e.map((it) => it.document));
}
private async createCustomTemplateFileListener() {
this.customTemplateFileheaderWatcher?.dispose();
this.customTemplateFileheaderWatcher =
vscode.workspace.createFileSystemWatcher(
"**/.vscode/fileheader.template.js"
);
const reloadProviders = () => {
fileheaderManager.loadProviders();
};
this.customTemplateFileheaderWatcher.onDidCreate(reloadProviders);
this.customTemplateFileheaderWatcher.onDidChange(reloadProviders);
this.customTemplateFileheaderWatcher.onDidDelete(reloadProviders);
}
}
/**
* @singleton
*/
const extension = new Extension();
export const activate = extension.activate;
export const deactivate = extension.deactivate;