Skip to content

Commit b845381

Browse files
authored
Add expandMacro command (#176)
* add expandMacro command * update elixir-ls * cleanup * get command name with server instance id from initialise result * update ex
1 parent 1d1d9df commit b845381

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

elixir-ls

Submodule elixir-ls updated 34 files

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,21 @@
394394
{
395395
"command": "extension.copyDebugInfo",
396396
"title": "ElixirLS: Copy Debug Info"
397+
},
398+
{
399+
"command": "extension.expandMacro",
400+
"title": "ElixirLS: Expand macro"
397401
}
398402
],
399403
"menus": {
400404
"commandPalette": [
401405
{
402406
"command": "extension.copyDebugInfo",
403407
"when": "editorLangId == elixir || editorLangId == eex || editorLangId == html-eex"
408+
},
409+
{
410+
"command": "extension.expandMacro",
411+
"title": "editorLangId == elixir"
404412
}
405413
]
406414
}

src/extension.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import * as path from "path";
1111

1212
import { workspace, ExtensionContext, WorkspaceFolder, Uri } from "vscode";
1313
import {
14+
ExecuteCommandParams,
1415
LanguageClient,
1516
LanguageClientOptions,
1617
RevealOutputChannelOn,
17-
ServerOptions,
18+
ServerOptions
1819
} from "vscode-languageclient";
1920
import * as os from "os";
2021
import Commands from "./constants/commands";
@@ -28,6 +29,8 @@ interface TerminalLinkWithData extends vscode.TerminalLink {
2829
}
2930
}
3031

32+
const ExpandMacroTitle = 'Expand macro result'
33+
3134
export let defaultClient: LanguageClient;
3235
const clients: Map<string, LanguageClient> = new Map();
3336
let _sortedWorkspaceFolders: string[] | undefined;
@@ -139,6 +142,79 @@ function configureCopyDebugInfo(context: ExtensionContext) {
139142
context.subscriptions.push(disposable);
140143
}
141144

145+
function getExpandMacroWebviewContent(content: Record<string, string>) {
146+
let body = "";
147+
for (const [key, value] of Object.entries(content)) {
148+
body += `<div>
149+
<h4>${key}</h4>
150+
<code><pre>${value}</pre></code>
151+
</div>`
152+
}
153+
154+
return `<!DOCTYPE html>
155+
<html lang="en">
156+
<head>
157+
<meta charset="UTF-8">
158+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
159+
<title>${ExpandMacroTitle}</title>
160+
</head>
161+
<body>
162+
${body}
163+
</body>
164+
</html>`;
165+
}
166+
167+
function configureExpandMacro(context: ExtensionContext) {
168+
const disposable = vscode.commands.registerCommand("extension.expandMacro", async () => {
169+
const extension = vscode.extensions.getExtension("jakebecker.elixir-ls");
170+
const editor = vscode.window.activeTextEditor;
171+
if (!extension || !editor) {
172+
return;
173+
}
174+
175+
const uri = editor.document.uri;
176+
let client = null;
177+
if (uri.scheme === "untitled") {
178+
client = defaultClient;
179+
} else {
180+
let folder = workspace.getWorkspaceFolder(uri);
181+
182+
if (folder) {
183+
folder = getOuterMostWorkspaceFolder(folder);
184+
client = clients.get(folder.uri.toString())
185+
}
186+
}
187+
188+
if (!client) {
189+
return;
190+
}
191+
192+
if (editor.selection.isEmpty) {
193+
return;
194+
}
195+
196+
const command = client.initializeResult!.capabilities.executeCommandProvider!.commands
197+
.find(c => c.startsWith("expandMacro:"))!;
198+
199+
const params: ExecuteCommandParams = {
200+
command: command,
201+
arguments: [uri.toString(), editor.document.getText(editor.selection), editor.selection.start.line]
202+
};
203+
204+
const res: Record<string, string> = await client.sendRequest("workspace/executeCommand", params);
205+
206+
const panel = vscode.window.createWebviewPanel(
207+
'expandMacro',
208+
ExpandMacroTitle,
209+
vscode.ViewColumn.One,
210+
{}
211+
);
212+
panel.webview.html = getExpandMacroWebviewContent(res);
213+
});
214+
215+
context.subscriptions.push(disposable);
216+
}
217+
142218
class DebugAdapterExecutableFactory implements vscode.DebugAdapterDescriptorFactory {
143219
createDebugAdapterDescriptor(session: vscode.DebugSession, executable: vscode.DebugAdapterExecutable): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
144220
if (session.workspaceFolder) {
@@ -233,6 +309,7 @@ export function activate(context: ExtensionContext): void {
233309

234310
configureRunTestFromCodeLens()
235311
configureCopyDebugInfo(context);
312+
configureExpandMacro(context);
236313
configureDebugger(context);
237314
configureTerminalLinkProvider(context);
238315

0 commit comments

Comments
 (0)