Skip to content

Add expandMacro command #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elixir-ls
Submodule elixir-ls updated 34 files
+2 −2 .github/workflows/ci.yml
+1 −1 .github/workflows/release-asset.yml
+0 −1 .gitignore
+4 −0 README.md
+4 −0 apps/elixir_ls_debugger/lib/debugger/server.ex
+33 −13 apps/elixir_ls_debugger/test/debugger_test.exs
+2 −2 apps/elixir_ls_debugger/test/fixtures/mix_project/lib/mix_project.ex
+2 −0 apps/elixir_ls_debugger/test/fixtures/mix_project/test/mix_project_fixturetest.exs
+10 −0 apps/elixir_ls_utils/lib/output_device.ex
+1 −1 apps/elixir_ls_utils/priv/debugger.bat
+15 −1 apps/elixir_ls_utils/priv/debugger.sh
+1 −1 apps/elixir_ls_utils/priv/language_server.bat
+15 −1 apps/elixir_ls_utils/priv/language_server.sh
+8 −3 apps/elixir_ls_utils/priv/launch.sh
+10 −4 apps/elixir_ls_utils/test/output_device_test.exs
+10 −0 apps/elixir_ls_utils/test/placeholder_test.exs
+18 −0 apps/elixir_ls_utils/test/support/mix_test.case.ex
+1 −0 apps/language_server/lib/language_server/protocol.ex
+1 −1 apps/language_server/lib/language_server/providers/code_lens.ex
+14 −13 apps/language_server/lib/language_server/providers/code_lens/test.ex
+28 −1 apps/language_server/lib/language_server/providers/completion.ex
+13 −86 apps/language_server/lib/language_server/providers/execute_command.ex
+96 −0 apps/language_server/lib/language_server/providers/execute_command/apply_spec.ex
+47 −0 apps/language_server/lib/language_server/providers/execute_command/expand_macro.ex
+3 −3 apps/language_server/lib/language_server/providers/formatting.ex
+22 −16 apps/language_server/lib/language_server/providers/workspace_symbols.ex
+12 −2 apps/language_server/lib/language_server/server.ex
+7 −0 apps/language_server/test/dialyzer_test.exs
+35 −9 apps/language_server/test/providers/code_lens/test_test.exs
+106 −21 apps/language_server/test/providers/completion_test.exs
+100 −0 apps/language_server/test/providers/execute_command/expand_macro_test.exs
+176 −1 apps/language_server/test/providers/formatting_test.exs
+36 −5 apps/language_server/test/server_test.exs
+1 −1 mix.lock
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,21 @@
{
"command": "extension.copyDebugInfo",
"title": "ElixirLS: Copy Debug Info"
},
{
"command": "extension.expandMacro",
"title": "ElixirLS: Expand macro"
}
],
"menus": {
"commandPalette": [
{
"command": "extension.copyDebugInfo",
"when": "editorLangId == elixir || editorLangId == eex || editorLangId == html-eex"
},
{
"command": "extension.expandMacro",
"title": "editorLangId == elixir"
}
]
}
Expand Down
79 changes: 78 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import * as path from "path";

import { workspace, ExtensionContext, WorkspaceFolder, Uri } from "vscode";
import {
ExecuteCommandParams,
LanguageClient,
LanguageClientOptions,
RevealOutputChannelOn,
ServerOptions,
ServerOptions
} from "vscode-languageclient";
import * as os from "os";
import Commands from "./constants/commands";
Expand All @@ -28,6 +29,8 @@ interface TerminalLinkWithData extends vscode.TerminalLink {
}
}

const ExpandMacroTitle = 'Expand macro result'

export let defaultClient: LanguageClient;
const clients: Map<string, LanguageClient> = new Map();
let _sortedWorkspaceFolders: string[] | undefined;
Expand Down Expand Up @@ -139,6 +142,79 @@ function configureCopyDebugInfo(context: ExtensionContext) {
context.subscriptions.push(disposable);
}

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>`;
}

function configureExpandMacro(context: ExtensionContext) {
const disposable = vscode.commands.registerCommand("extension.expandMacro", async () => {
const extension = vscode.extensions.getExtension("jakebecker.elixir-ls");
const editor = vscode.window.activeTextEditor;
if (!extension || !editor) {
return;
}

const uri = editor.document.uri;
let client = null;
if (uri.scheme === "untitled") {
client = defaultClient;
} else {
let folder = workspace.getWorkspaceFolder(uri);

if (folder) {
folder = getOuterMostWorkspaceFolder(folder);
client = clients.get(folder.uri.toString())
}
}

if (!client) {
return;
}

if (editor.selection.isEmpty) {
return;
}

const command = 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("workspace/executeCommand", params);

const panel = vscode.window.createWebviewPanel(
'expandMacro',
ExpandMacroTitle,
vscode.ViewColumn.One,
{}
);
panel.webview.html = getExpandMacroWebviewContent(res);
});

context.subscriptions.push(disposable);
}

class DebugAdapterExecutableFactory implements vscode.DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(session: vscode.DebugSession, executable: vscode.DebugAdapterExecutable): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
if (session.workspaceFolder) {
Expand Down Expand Up @@ -233,6 +309,7 @@ export function activate(context: ExtensionContext): void {

configureRunTestFromCodeLens()
configureCopyDebugInfo(context);
configureExpandMacro(context);
configureDebugger(context);
configureTerminalLinkProvider(context);

Expand Down