forked from intersystems-community/vscode-objectscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudioMigration.ts
372 lines (362 loc) · 15.2 KB
/
studioMigration.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import * as vscode from "vscode";
import cmd = require("node-cmd");
import util = require("util");
import { gte } from "semver";
import { fileExists, outputChannel } from "../utils";
import { clsLangId, cspLangId, incLangId, intLangId, macLangId } from "../extension";
/** Run a command using `node-cmd` and return a Promise */
const runCmd = util.promisify(cmd.run);
/** The languages supported by the Language Server's `isclexer.node` parser library. */
const languages = {
CLS: 3,
COS: 1,
XML: 9,
CSS: 15,
HTML: 5,
JAVA: 13,
JAVASCRIPT: 11,
SQL: 2,
PYTHON: 7,
};
/**
* Convert the registry's `0x00BBGGRR` COLORREF to the CSS-style `#RRGGBB` used by VS Code.
*/
function registryToCSS(regColor: string): string {
const tmp = regColor.slice(2).padStart(8, "0");
return `#${tmp.slice(6)}${tmp.slice(4, 6)}${tmp.slice(2, 4)}`;
}
/**
* * Read Studio snippets from file paths stored in the registry.
* * Convert them to VS Code JSON format.
* * Store them in global snippets file `isc-studio.code-snippets`.
*/
export async function loadStudioSnippets(): Promise<void> {
// Check that we're on windows
if (process.platform != "win32") {
vscode.window.showErrorMessage("Loading Studio snippets is only supported on Windows.", "Dismiss");
return;
}
vscode.window
.withProgress<vscode.Uri | string>(
{
cancellable: false,
location: vscode.ProgressLocation.Notification,
title: "Loading Studio snippets",
},
async (): Promise<vscode.Uri | string> => {
// Read Studio snippet file paths from Windows registry
const files: vscode.Uri[] = [];
const keyRegex = /^USER[1-3] location$/;
const sep = " ";
const regKey = "HKEY_CURRENT_USER\\SOFTWARE\\InterSystems\\Cache Studio\\Code Snippets";
const regData: string = await runCmd(`reg query "${regKey}" /f location`);
for (const line of regData.split("\r\n")) {
const lineArr = line.trim().split(sep);
if (lineArr.length >= 3 && keyRegex.test(lineArr[0])) {
// This line contains one of the 3 file paths
const studioUri = vscode.Uri.file(lineArr.slice(2).join(sep));
// Check that the file exists
if (await fileExists(studioUri)) {
files.push(studioUri);
}
}
}
if (files.length > 0) {
const textDecoder = new TextDecoder();
const vscodeUri = vscode.Uri.file(
`${process.env.APPDATA}\\Code${
vscode.env.appName.includes("Insiders") ? " - Insiders" : ""
}\\User\\snippets\\isc-studio.code-snippets`
);
// Check if the destination file exists
if (await fileExists(vscodeUri)) {
const overwrite = await vscode.window.showWarningMessage(
`Snippets file ${vscodeUri.fsPath} already exists. Overwrite it?`,
"Yes",
"No"
);
if (overwrite != "Yes") {
return "Load of Studio snippets aborted by the user.";
}
}
const vscodeSnippets = {};
for (const file of files) {
// Read the file and convert its snippets to VS Code format
textDecoder
.decode(await vscode.workspace.fs.readFile(file))
// $CHAR(1) is the delimiter between snippets
.split(String.fromCharCode(1))
.forEach((studioSnippet) => {
// $CHAR(2) is the delimiter between snippet parts: Name_$CHAR(2)_Language_$CHAR(2)_Body
const parts = studioSnippet.split(String.fromCharCode(2));
// Skip snippets that are malformed
if (parts.length < 3) {
return;
}
// Trim whitespace from the Name and Language
parts[0] = parts[0].trim();
parts[1] = parts[1].trim();
// Only convert snippets for COS, UDL and HTML
if (["1", "3", "5"].includes(parts[1])) {
vscodeSnippets[parts[0]] = {
// Use the first word of Name as the prefix
// Use ... as the separator (like default Studio snippets) if it's present, else space
prefix: parts[0].includes("...") ? parts[0].split("...")[0] : parts[0].split(" ")[0],
// Need to escape any $ within the body
body: parts.slice(2).join(String.fromCharCode(2)).replace(/\$/g, "\\$").split(/\r?\n/),
// Use Name as the description since Studio doesn't support snippet descriptions/documentation
description: parts[0],
// Use Language to determine the scope
scope:
parts[1] == "5"
? cspLangId
: parts[1] == "3"
? clsLangId
: `${macLangId},${intLangId},${incLangId},${clsLangId},${cspLangId}`,
};
}
});
}
// Write the converted Studio snippets to the file
await vscode.workspace.fs.writeFile(
vscodeUri,
new TextEncoder().encode(JSON.stringify(vscodeSnippets, null, 2))
);
// Return the uri we wrote to
return vscodeUri;
} else {
return "There are no user defined Studio snippet files to load from.";
}
}
)
.then(
(uriOrReason: vscode.Uri | string) => {
if (uriOrReason instanceof vscode.Uri) {
// Report success
vscode.window
.showInformationMessage(
`Successfully loaded Studio snippets into global file ${uriOrReason.fsPath}`,
"Open File",
"Dismiss"
)
.then((answer) => {
if (answer == "Open File") {
vscode.window.showTextDocument(uriOrReason, { preview: false });
}
});
} else if (typeof uriOrReason == "string") {
// Show the user the reason the command was aborted
vscode.window.showInformationMessage(uriOrReason, "Dismiss");
}
},
(error) => {
outputChannel.appendLine(
typeof error == "string" ? error : error instanceof Error ? error.message : JSON.stringify(error)
);
vscode.window.showErrorMessage(
"An error occurred while loading Studio snippets. Check 'ObjectScript' Output channel for details.",
"Dismiss"
);
}
);
}
/**
* * Read Studio syntax and editor background colors from the registry.
* * Convert the syntax colors to an `editor.semanticTokenColorCustomizations` setting object.
* * Store the setting object in the user `settings.json` file.
* * Store the editor background color in the user `settings.json` file under `workbench.colorCustomizations`.
* * Activate the modified theme.
*/
export async function loadStudioColors(languageServerExt: vscode.Extension<any> | undefined): Promise<void> {
// Check that we're on windows
if (process.platform != "win32") {
vscode.window.showErrorMessage("Loading Studio syntax colors is only supported on Windows.", "Dismiss");
return;
}
// Check that the Language Server is installed
if (!languageServerExt) {
vscode.window.showErrorMessage(
"Loading Studio syntax colors requires the [InterSystems Language Server extension](https://marketplace.visualstudio.com/items?itemName=${extId}).",
"Dismiss"
);
return;
}
vscode.window
.withProgress<void>(
{
cancellable: false,
location: vscode.ProgressLocation.Notification,
title: "Loading Studio syntax colors",
},
async (): Promise<void> => {
// Make sure Language Server is active
if (!languageServerExt.isActive) {
await languageServerExt.activate();
}
// Get the semantic tokens legend from the Language Server
// NOTE: The `vscode.provideDocumentSemanticTokensLegend` command requires the URI of an open document
// as an argument. To ensure that color loading works regardless of what workspace (if any) is open,
// I create, open and then delete a temporary MAC routine in a folder that is known to exist so the
// command always has a file to work with.
const tempRoutineUri = vscode.Uri.file(
`${process.env.APPDATA}\\Code${vscode.env.appName.includes("Insiders") ? " - Insiders" : ""}\\User\\temp.mac`
);
await vscode.workspace.fs.writeFile(tempRoutineUri, new TextEncoder().encode("ROUTINE temp\n"));
await vscode.workspace.openTextDocument(tempRoutineUri);
let legend: vscode.SemanticTokensLegend = await vscode.commands
.executeCommand<vscode.SemanticTokensLegend>("vscode.provideDocumentSemanticTokensLegend", tempRoutineUri)
// Swallow any errors
.then(
(result) => result,
() => undefined
);
if (!legend) {
// The Language Server might be active but not ready for requests yet
// Attempt to get the legend 10 more times at 100 ms intervals
let numAttempts = 0;
await new Promise<void>((resolve) => {
const interval = setInterval(async () => {
numAttempts++;
if (numAttempts > 10) {
clearInterval(interval);
resolve();
}
legend = await vscode.commands
.executeCommand<vscode.SemanticTokensLegend>(
"vscode.provideDocumentSemanticTokensLegend",
tempRoutineUri
)
// Swallow any errors
.then(
(result) => result,
() => undefined
);
if (legend) {
clearInterval(interval);
resolve();
}
}, 100);
});
}
await vscode.workspace.fs.delete(tempRoutineUri, { useTrash: false });
if (!legend) {
// This shouldn't happen since we already activated the Language Server
throw "Failed to get the SemanticTokensLegend from the Language Server extension. Check that it is installed and active.";
}
// Find the index of first token for each language in the legend
const languageOffsets: { [index: number]: number } = {};
legend.tokenTypes.forEach((token, index) => {
const [lang, attr] = token.split("_");
if (attr == "Error") {
languageOffsets[languages[lang]] = index;
}
});
// Read Studio syntax colors from Windows registry and build the rules object
let editorBackground = "";
const rules = {};
const sep = " ";
const langRegex = /^Language(1?[0-9])$/;
const regKey = "HKEY_CURRENT_USER\\SOFTWARE\\InterSystems\\Cache Studio\\Editor";
const regData: string = await runCmd(`reg query "${regKey}" /s`);
let currentLanguage = -1;
let currentTokenBackground = "";
for (const line of regData.split("\r\n")) {
const lineTrim = line.trim();
if (lineTrim.length == 0) {
// Skip empty lines
continue;
}
if (!line.startsWith(" ")) {
// This is a header line so check if it's the start of a Language
const langMatch = lineTrim.split("\\").pop().match(langRegex);
if (langMatch != null) {
currentLanguage = Number(langMatch[1]);
}
} else {
const lineArr = lineTrim.split(sep);
if (lineArr.length == 3) {
if (currentLanguage == -1) {
// Check if this line contains the background color
if (lineArr[0] == "Background Color") {
editorBackground = registryToCSS(lineArr[2]);
}
} else {
// Check if this language is one that we provide tokens for
if (languageOffsets[currentLanguage] != undefined) {
// In the reg query result, the background color always immediately precedes the foreground color
if (lineArr[0].startsWith("B")) {
// This is a background color
currentTokenBackground = lineArr[2];
} else {
// This is a foreground color
const attr = Number(lineArr[0].split(" ").pop());
if (
// The background color is the default
currentTokenBackground == "0x80000005" &&
// The foreground color is NOT the default
lineArr[2] != "0x80000008" &&
// The token isn't whitespace
![1, 2].includes(attr)
) {
// Store the color in our rules
const token = legend.tokenTypes[languageOffsets[currentLanguage] + attr];
if (token != undefined) {
rules[token] = registryToCSS(lineArr[2]);
}
}
}
}
}
}
}
}
// Determine which theme we should modify and activate
const darkLight =
0.2126 * Number(`0x${editorBackground.slice(1, 3)}`) +
0.7152 * Number(`0x${editorBackground.slice(3, 5)}`) +
0.0722 * Number(`0x${editorBackground.slice(5)}`) <
128
? "Dark"
: "Light";
const themeName = `InterSystems Default ${darkLight}${
gte(languageServerExt.packageJSON.version, "2.4.0") ? " Modern" : ""
}`;
// Modify the theme
const editorConfig = vscode.workspace.getConfiguration("editor");
const workbenchConfig = vscode.workspace.getConfiguration("workbench");
const tokensConfig = editorConfig.get("semanticTokenColorCustomizations");
tokensConfig[`[${themeName}]`] = { rules };
await editorConfig.update("semanticTokenColorCustomizations", tokensConfig, true);
const colorsConfig = workbenchConfig.get("colorCustomizations");
colorsConfig[`[${themeName}]`] = { "editor.background": editorBackground };
await workbenchConfig.update("colorCustomizations", colorsConfig, true);
// Activate it globally
await workbenchConfig.update("colorTheme", themeName, true);
}
)
.then(
() => {
// Report success
vscode.window
.showInformationMessage(
"Successfully loaded Studio syntax colors into User Settings.",
"Open Settings",
"Dismiss"
)
.then((answer) => {
if (answer == "Open Settings") {
vscode.commands.executeCommand("workbench.action.openSettingsJson");
}
});
},
(error) => {
outputChannel.appendLine(
typeof error == "string" ? error : error instanceof Error ? error.message : JSON.stringify(error)
);
vscode.window.showErrorMessage(
"An error occurred while loading Studio syntax colors. Check 'ObjectScript' Output channel for details.",
"Dismiss"
);
}
);
}