Skip to content

Commit b084516

Browse files
lukaszsamsonaxelson
authored andcommitted
add expandMacro command
1 parent 1d1d9df commit b084516

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

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: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { window } from 'vscode';
12
/* --------------------------------------------------------------------------------------------
23
* Copyright (c) Microsoft Corporation. All rights reserved.
34
* Licensed under the MIT License. See License.txt in the project root for license information.
@@ -11,10 +12,11 @@ import * as path from "path";
1112

1213
import { workspace, ExtensionContext, WorkspaceFolder, Uri } from "vscode";
1314
import {
15+
ExecuteCommandParams,
1416
LanguageClient,
1517
LanguageClientOptions,
1618
RevealOutputChannelOn,
17-
ServerOptions,
19+
ServerOptions
1820
} from "vscode-languageclient";
1921
import * as os from "os";
2022
import Commands from "./constants/commands";
@@ -28,6 +30,8 @@ interface TerminalLinkWithData extends vscode.TerminalLink {
2830
}
2931
}
3032

33+
const ExpandMacroTitle = 'Expand macro result'
34+
3135
export let defaultClient: LanguageClient;
3236
const clients: Map<string, LanguageClient> = new Map();
3337
let _sortedWorkspaceFolders: string[] | undefined;
@@ -139,6 +143,76 @@ function configureCopyDebugInfo(context: ExtensionContext) {
139143
context.subscriptions.push(disposable);
140144
}
141145

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

234308
configureRunTestFromCodeLens()
235309
configureCopyDebugInfo(context);
310+
configureExpandMacro(context);
236311
configureDebugger(context);
237312
configureTerminalLinkProvider(context);
238313

0 commit comments

Comments
 (0)