-
Notifications
You must be signed in to change notification settings - Fork 454
/
Copy pathfileEventHandler.ts
254 lines (218 loc) · 9.11 KB
/
fileEventHandler.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
'use strict';
import { lstatSync } from 'fs-extra';
import * as path from 'path';
import { workspace, FileCreateEvent, ExtensionContext, window, TextDocument, SnippetString, commands, Uri, FileRenameEvent, ProgressLocation, WorkspaceEdit as CodeWorkspaceEdit, FileWillRenameEvent, Position, FileType, ConfigurationTarget, Disposable } from 'vscode';
import { LanguageClient } from 'vscode-languageclient/node';
import { ListCommandResult } from './buildpath';
import { Commands } from './commands';
import { WillRenameFiles } from './protocol';
import { getJavaConfiguration } from './utils';
import { userInfo } from 'os';
import * as stringInterpolate from 'fmtr';
let serverReady: boolean = false;
export function setServerStatus(ready: boolean) {
serverReady = ready;
}
export function registerFileEventHandlers(client: LanguageClient, context: ExtensionContext) {
if (workspace.onDidCreateFiles) {// Theia doesn't support workspace.onDidCreateFiles yet
context.subscriptions.push(workspace.onDidCreateFiles(handleNewJavaFiles));
}
if (workspace.onWillRenameFiles) {
context.subscriptions.push(workspace.onWillRenameFiles(getWillRenameHandler(client)));
}
}
async function handleNewJavaFiles(e: FileCreateEvent) {
const emptyFiles: Uri[] = [];
const textDocuments: TextDocument[] = [];
for (const uri of e.files) {
if (!isJavaFile(uri)) {
continue;
}
const textDocument = await workspace.openTextDocument(uri);
if (textDocument.getText()) { // ignore the non-empty files
continue;
}
emptyFiles.push(uri);
textDocuments.push(textDocument);
}
if (!emptyFiles.length) {
return;
}
let sourcePaths: string[] = [];
if (serverReady) {
const result: ListCommandResult = await commands.executeCommand<ListCommandResult>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.LIST_SOURCEPATHS);
if (result && result.data && result.data.length) {
sourcePaths = result.data.map((sourcePath) => sourcePath.path).sort((a, b) => b.length - a.length);
}
}
const formatNumber = (num => num > 9 ? String(num) : `0${num}`);
for (let i = 0; i < emptyFiles.length; i++) {
const typeName: string = resolveTypeName(textDocuments[i].fileName);
const isPackageInfo = typeName === 'package-info';
const isModuleInfo = typeName === 'module-info';
const date = new Date();
const context: any = {
fileName: path.basename(textDocuments[i].fileName),
packageName: "",
typeName: typeName,
user: userInfo().username,
date: date.toLocaleDateString(undefined, {month: "short", day: "2-digit", year: "numeric"}),
time: date.toLocaleTimeString(),
year: date.getFullYear(),
month: formatNumber(date.getMonth() + 1),
shortmonth: date.toLocaleDateString(undefined, {month: "short"}),
day: formatNumber(date.getDate()),
hour: formatNumber(date.getHours()),
minute: formatNumber(date.getMinutes()),
};
if (!isModuleInfo) {
context.packageName = resolvePackageName(sourcePaths, emptyFiles[i].fsPath);
}
const snippets: string[] = [];
const fileHeader = getJavaConfiguration().get<string[]>("templates.fileHeader");
if (fileHeader && fileHeader.length) {
for (const template of fileHeader) {
snippets.push(stringInterpolate(template, context));
}
}
if (!isModuleInfo) {
if (context.package_name) {
snippets.push(`package ${context.packageName};`);
snippets.push("");
}
}
if (!isPackageInfo) {
const typeComment = getJavaConfiguration().get<string[]>("templates.typeComment");
if (typeComment && typeComment.length) {
for (const template of typeComment) {
snippets.push(stringInterpolate(template, context));
}
}
if (isModuleInfo) {
snippets.push(`module \${1:name} {`);
} else if (!serverReady || await isVersionLessThan(emptyFiles[i].toString(), 14)) {
snippets.push(`public \${1|class,interface,enum,abstract class,@interface|} ${typeName} {`);
} else {
snippets.push(`public \${1|class ${typeName},interface ${typeName},enum ${typeName},record ${typeName}(),abstract class ${typeName},@interface ${typeName}|} {`);
}
snippets.push("\t${0}");
snippets.push("}");
snippets.push("");
}
const textEditor = await window.showTextDocument(textDocuments[i]);
textEditor.insertSnippet(new SnippetString(snippets.join("\n")));
}
}
function getWillRenameHandler(client: LanguageClient) {
return function handleWillRenameFiles(e: FileWillRenameEvent): void {
if (!serverReady) {
return;
}
e.waitUntil(new Promise(async (resolve, reject) => {
try {
const javaRenameEvents: Array<{ oldUri: string; newUri: string }> = [];
for (const file of e.files) {
if (await isJavaFileWillRename(file.oldUri, file.newUri)
|| await isFolderWillRename(file.oldUri, file.newUri)
|| await isJavaWillMove(file.oldUri, file.newUri)) {
javaRenameEvents.push({
oldUri: file.oldUri.toString(),
newUri: file.newUri.toString(),
});
}
}
if (!javaRenameEvents.length) {
resolve(undefined);
return;
}
const edit = await client.sendRequest(WillRenameFiles.type, {
files: javaRenameEvents
});
resolve(client.protocol2CodeConverter.asWorkspaceEdit(edit));
} catch (ex) {
reject(ex);
}
}));
};
}
function isJavaFile(uri: Uri): boolean {
return uri.fsPath && uri.fsPath.endsWith(".java");
}
async function isFile(uri: Uri): Promise<boolean> {
try {
return (await workspace.fs.stat(uri)).type === FileType.File;
} catch {
return lstatSync(uri.fsPath).isFile();
}
}
async function isDirectory(uri: Uri): Promise<boolean> {
try {
return (await workspace.fs.stat(uri)).type === FileType.Directory;
} catch {
return lstatSync(uri.fsPath).isDirectory();
}
}
async function isJavaFileWillRename(oldUri: Uri, newUri: Uri): Promise<boolean> {
if (isInSameDirectory(oldUri, newUri)) {
return await isFile(oldUri) && isJavaFile(oldUri) && isJavaFile(newUri);
}
return false;
}
async function isFolderWillRename(oldUri: Uri, newUri: Uri): Promise<boolean> {
return await isDirectory(oldUri);
}
async function isJavaWillMove(oldUri: Uri, newUri: Uri): Promise<boolean> {
return await isFile(oldUri) && isJavaFile(oldUri) && isJavaFile(newUri)
&& !isInSameDirectory(oldUri, newUri);
}
function isInSameDirectory(oldUri: Uri, newUri: Uri): boolean {
const oldDir = path.dirname(oldUri.fsPath);
const newDir = path.dirname(newUri.fsPath);
return !path.relative(oldDir, newDir);
}
function resolveTypeName(filePath: string): string {
const fileName: string = path.basename(filePath);
const extName: string = path.extname(fileName);
return fileName.substring(0, fileName.length - extName.length);
}
function resolvePackageName(sourcePaths: string[], filePath: string): string {
if (!sourcePaths || !sourcePaths.length) {
return "";
}
for (const sourcePath of sourcePaths) {
if (isPrefix(sourcePath, filePath)) {
const relative = path.relative(sourcePath, path.dirname(filePath));
return relative.replace(/[\/\\]/g, ".");
}
}
return "";
}
function isPrefix(parentPath: string, filePath: string): boolean {
const relative = path.relative(parentPath, filePath);
return !relative || (!relative.startsWith('..') && !path.isAbsolute(relative));
}
const COMPLIANCE = "org.eclipse.jdt.core.compiler.compliance";
async function isVersionLessThan(fileUri: string, targetVersion: number): Promise<boolean> {
let projectSettings = {};
try {
projectSettings = await commands.executeCommand<Object>(
Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_PROJECT_SETTINGS, fileUri, [ COMPLIANCE ]);
} catch (err) {
// do nothing.
}
let javaVersion = 0;
let complianceVersion = projectSettings[COMPLIANCE];
if (complianceVersion) {
// Ignore '1.' prefix for legacy Java versions
if (complianceVersion.startsWith('1.')) {
complianceVersion = complianceVersion.substring(2);
}
// look into the interesting bits now
const regexp = /\d+/g;
const match = regexp.exec(complianceVersion);
if (match) {
javaVersion = parseInt(match[0]);
}
}
return javaVersion < targetVersion;
}