-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.ts
73 lines (70 loc) · 3.3 KB
/
pipeline.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
import { dirname, join } from 'path';
import * as rpc from '@codingame/monaco-jsonrpc';
import { fs } from '@hydrooj/utils';
import * as lsp from 'vscode-languageserver';
export function getPipeline(tmpFolder: string, folders = []) {
let slashStyle = false;
const pendingFolder = new Set<string>();
let forceUri = '';
function removeHydroPrefix(uri: string, replacement = '') {
if (uri.startsWith('hydro://')) {
slashStyle = true;
return uri.replace('hydro://', replacement);
}
if (uri.startsWith('hydro:')) return uri.replace('hydro:', replacement);
return uri;
}
return function pipeline(message: rpc.Message) {
if (rpc.isRequestMessage(message) || rpc.isNotificationMessage(message)) {
const params = message.params as any;
if (!params) return message;
if (message.method === lsp.InitializeRequest.type.method) {
params.processId = process.pid;
}
if (params.textDocument?.uri?.startsWith('hydro:')) {
if (params.textDocument?.uri?.endsWith('.java')) {
forceUri = params.textDocument.uri;
params.textDocument.uri = `file://${tmpFolder}/Main.java`;
if (params.textDocument.text) {
fs.writeFileSync(`${tmpFolder}/Main.java`, params.textDocument.text);
}
} else {
pendingFolder.add(removeHydroPrefix(params.textDocument.uri));
params.textDocument.uri = removeHydroPrefix(params.textDocument.uri, `file://${tmpFolder}/`);
}
}
if (params.uri?.startsWith('hydro:')) {
if (params.uri?.endsWith('.java')) {
params.uri = `file://${tmpFolder}/Main.java`;
} else {
pendingFolder.add(removeHydroPrefix(params.uri));
params.uri = removeHydroPrefix(params.uri, `file://${tmpFolder}/`);
}
} else if (params.uri?.startsWith(`file://${tmpFolder}/`)) {
params.uri = forceUri || params.uri.replace(`file://${tmpFolder}/`, slashStyle ? 'hydro://' : 'hydro:');
}
} else if (rpc.isResponseMessage(message)) {
if (message.result instanceof Array) {
for (const re of message.result) {
if (re.uri?.startsWith(`file://${tmpFolder}/`)) {
re.uri = forceUri || re.uri.replace(`file://${tmpFolder}/`, slashStyle ? 'hydro://' : 'hydro:');
}
}
} else if (typeof message.result === 'object' && message.result !== null) {
const result = message.result as any;
if (typeof result.changes === 'object') {
result.changes = Object.fromEntries(
Object.entries(result.changes)
.map(([k, v]) => [k.replace(`file://${tmpFolder}/`, slashStyle ? 'hydro://' : 'hydro:'), v]),
);
}
}
}
for (const f of pendingFolder) {
if (!folders.includes(f) && !f.includes('..')) {
fs.ensureDirSync(dirname(join(tmpFolder, f)));
}
}
return message;
};
}