-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathexpandMacro.ts
More file actions
108 lines (94 loc) · 2.77 KB
/
expandMacro.ts
File metadata and controls
108 lines (94 loc) · 2.77 KB
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
import * as vscode from "vscode";
import {
type ExecuteCommandParams,
ExecuteCommandRequest,
State,
} from "vscode-languageclient";
import { ELIXIR_LS_EXTENSION_NAME } from "../constants";
import type { LanguageClientManager } from "../languageClientManager";
const ExpandMacroTitle = "Expand macro result";
function getExpandMacroWebviewContent(content: Record<string, string>) {
let body = "";
for (const [key, value] of Object.entries(content)) {
body += `<div>
<h4>${key}</h4>
<code><pre>${value}</pre></code>
</div>`;
}
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${ExpandMacroTitle}</title>
</head>
<body>
${body}
</body>
</html>`;
}
export function configureExpandMacro(
context: vscode.ExtensionContext,
languageClientManager: LanguageClientManager,
) {
const disposable = vscode.commands.registerCommand(
"extension.expandMacro",
async () => {
const extension = vscode.extensions.getExtension(
ELIXIR_LS_EXTENSION_NAME,
);
const editor = vscode.window.activeTextEditor;
if (!extension || !editor) {
return;
}
if (editor.selection.isEmpty) {
console.error("ElixirLS: selection is empty");
return;
}
const uri = editor.document.uri;
const clientPromise = languageClientManager.getClientPromiseByDocument(
editor.document,
);
if (!clientPromise) {
console.error(
`ElixirLS: no language client for document ${uri.fsPath}`,
);
return;
}
const client = await clientPromise;
if (!client.initializeResult) {
console.error(
`ElixirLS: unable to execute command on server ${
client.name
} in state ${State[client.state]}`,
);
return;
}
const command =
// biome-ignore lint/style/noNonNullAssertion: server capabilities guarantee this command exists
client.initializeResult.capabilities.executeCommandProvider?.commands.find(
(c) => c.startsWith("expandMacro:"),
)!;
const params: ExecuteCommandParams = {
command: command,
arguments: [
uri.toString(),
editor.document.getText(editor.selection),
editor.selection.start.line,
],
};
const res: Record<string, string> = await client.sendRequest(
ExecuteCommandRequest.type,
params,
);
const panel = vscode.window.createWebviewPanel(
"expandMacro",
ExpandMacroTitle,
vscode.ViewColumn.One,
{},
);
panel.webview.html = getExpandMacroWebviewContent(res);
},
);
context.subscriptions.push(disposable);
}