-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathswitch_impl_intf.ts
57 lines (47 loc) · 1.54 KB
/
switch_impl_intf.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
import * as fs from "fs";
import { LanguageClient } from "vscode-languageclient/node";
import { window } from "vscode";
import { createInterfaceRequest } from "./create_interface";
export const switchImplIntf = async (client: LanguageClient) => {
if (!client) {
return window.showInformationMessage("Language server not running");
}
const editor = window.activeTextEditor;
if (!editor) {
return window.showInformationMessage("No active editor");
}
const isIntf = editor.document.uri.path.endsWith(".resi");
const isImpl = editor.document.uri.path.endsWith(".res");
if (!(isIntf || isImpl)) {
await window.showInformationMessage(
"This command only can run on *.res or *.resi files."
);
return;
}
if (isIntf) {
// *.res
const newUri = editor.document.uri.with({
path: editor.document.uri.path.slice(0, -1),
});
await window.showTextDocument(newUri, { preview: false });
return;
}
if (!fs.existsSync(editor.document.uri.fsPath + "i")) {
// if interface doesn't exist, ask the user before creating.
const selection = await window.showInformationMessage(
"Do you want to create an interface *.resi?",
...["No", "Yes"]
);
if (selection !== "Yes") return;
// create interface
await client.sendRequest(createInterfaceRequest, {
uri: editor.document.uri.toString(),
});
}
// *.resi
const newUri = editor.document.uri.with({
path: editor.document.uri.path + "i",
});
await window.showTextDocument(newUri, { preview: false });
return;
};