-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathextension.ts
179 lines (155 loc) · 5.03 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { FSWatcher, watch } from "fs";
import { readFile } from "fs/promises";
import { join } from "path";
import {
commands,
ExtensionContext,
languages,
workspace,
window,
debug,
DebugConfiguration,
} from "vscode";
import { getWingBin, updateStatusBar } from "./bin-helper";
import { CFG_WING, CFG_WING_BIN, COMMAND_OPEN_CONSOLE } from "./constants";
import { Loggers } from "./logging";
import { LanguageServerManager } from "./lsp";
let wingBinWatcher: FSWatcher | undefined;
let languageServerManager: LanguageServerManager | undefined;
export async function deactivate() {
wingBinWatcher?.close();
await languageServerManager?.stop();
}
export async function activate(context: ExtensionContext) {
// For some reason, the word pattern is not set correctly by the language config file
// https://github.com/microsoft/vscode/issues/42649
const languageConfig = JSON.parse(
await readFile(join(__dirname, "..", "language-configuration.json"), "utf8")
);
languages.setLanguageConfiguration("wing", {
brackets: languageConfig.brackets,
comments: languageConfig.comments,
autoClosingPairs: languageConfig.autoClosingPairs,
wordPattern: new RegExp(languageConfig.wordPattern, "g"),
indentationRules: {
increaseIndentPattern: new RegExp(
languageConfig.indentationRules.increaseIndentPattern
),
decreaseIndentPattern: new RegExp(
languageConfig.indentationRules.decreaseIndentPattern
),
},
});
languageServerManager = new LanguageServerManager();
debug.registerDebugConfigurationProvider("wing", {
async resolveDebugConfiguration(_, _config: DebugConfiguration) {
Loggers.default.appendLine(
`Resolving debug configuration... ${JSON.stringify(_config)}`
);
const editor = window.activeTextEditor;
const currentFilename = editor?.document.fileName;
let chosenFile;
if (
currentFilename?.endsWith("main.w") ||
currentFilename?.endsWith(".test.w")
) {
chosenFile = currentFilename;
} else {
let uriOptions = await workspace.findFiles(
`**/*.{main,test}.w`,
"**/{node_modules,target}/**"
);
uriOptions.concat(
await workspace.findFiles(`**/main.w`, "**/{node_modules,target}/**")
);
const entrypoint = await window.showQuickPick(
uriOptions.map((f) => f.fsPath),
{
placeHolder: "Choose entrypoint to debug",
}
);
if (!entrypoint) {
return;
}
chosenFile = entrypoint;
}
const command = await window.showInputBox({
title: `Debugging ${chosenFile}`,
prompt: "Wing CLI arguments",
value: "test",
});
if (!command) {
return;
}
const currentWingBin = await getWingBin();
// Use builtin node debugger
return {
name: `Debug ${chosenFile}`,
request: "launch",
type: "node",
args: [currentWingBin, command, chosenFile],
runtimeSourcemapPausePatterns: [
"${workspaceFolder}/**/target/**/*.cjs",
],
autoAttachChildProcesses: true,
pauseForSourceMap: true,
};
},
});
const wingBinChanged = async () => {
Loggers.default.appendLine(`Setting up wing bin...`);
const currentWingBin = await getWingBin();
if (currentWingBin) {
const hasWingBin = await updateStatusBar(currentWingBin);
wingBinWatcher?.close();
wingBinWatcher = watch(
currentWingBin,
{
persistent: false,
},
async () => {
// wait for a second to make sure the file is done moving or being written
await new Promise((resolve) => setTimeout(resolve, 1000, undefined));
await wingBinChanged();
}
);
if (hasWingBin) {
Loggers.default.appendLine(`Using wing from "${currentWingBin}"`);
languageServerManager?.setWingBin(currentWingBin);
await languageServerManager?.start();
} else {
void window.showErrorMessage(`wing not found at "${currentWingBin}"`);
}
} else {
Loggers.default.appendLine(`wing not found`);
}
};
const wingIt = async () => {
const filePath = window.activeTextEditor?.document.fileName;
if (!filePath) {
return;
}
const terminalName = `Wing it: ${filePath.split("/").pop()}`;
const existingTerminal = window.terminals.find(
(t) => t.name === terminalName
);
if (existingTerminal) {
existingTerminal.dispose();
}
const terminal = window.createTerminal(terminalName);
terminal.show();
terminal.sendText(`wing it "${filePath}"`);
};
//watch for config changes
workspace.onDidChangeConfiguration(async (e) => {
// see if WING_BIN has changed
if (e.affectsConfiguration(`${CFG_WING}.${CFG_WING_BIN}`)) {
await wingBinChanged();
}
});
// add command to preview wing files
context.subscriptions.push(
commands.registerCommand(COMMAND_OPEN_CONSOLE, wingIt)
);
await wingBinChanged();
}