Skip to content
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

WIP Add extract function execute command #78

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
"command": "lexical.server.restart",
"category": "Lexical",
"title": "Restart Lexical's language server"
},
{
"category": "Elixir",
"command": "extension.extractFunction",
"title": "Extract function"
}
],
"configurationDefaults": {
Expand All @@ -144,6 +149,15 @@
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true
}
},
"menus": {
"commandPalette": [
{
"category": "Elixir",
"command": "extension.extractFunction",
"when": "editorLangId == elixir"
}
]
}
},
"scripts": {
Expand Down
76 changes: 76 additions & 0 deletions src/commands/extract-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
commands,
extensions,
Range,
SnippetString,
window,
} from "vscode";

import {
ExecuteCommandParams,
LanguageClient,
} from "vscode-languageclient/node";
import Commands from ".";

interface Context {
client: LanguageClient;
showWarning: (message: string) => void;
}

const extractFunction: Commands.T<Context> = {
id: "extension.extractFunction",
createHandler: ({ client, showWarning }) => {
async function handle() {
const extension = extensions.getExtension("lexical-lsp.lexical");
const editor = window.activeTextEditor;
if (!extension || !editor || !client) {
return;
}

const command =
client.initializeResult!.capabilities.executeCommandProvider!.commands.find(
(c: string) => c.startsWith("extractFunction")
)!;
if (!command) {
return;
}

const uriStr = editor.document.uri.toString();
const { start, end } = editor.selection;
const args = [uriStr, start.line, end.line, "extracted_function"];
const params: ExecuteCommandParams = { command, arguments: args };
await client.sendRequest("workspace/executeCommand", params);

const editor2 = window.activeTextEditor;
if (!editor2) {
return;
}
const document = editor2.document;
const text = document.getText();
const startIndex = text.indexOf("extracted_function");
const endIndex = text.lastIndexOf("extracted_function");

if (startIndex !== -1 && endIndex !== -1) {
const startPosition = document.positionAt(startIndex);
const endPosition = document.positionAt(
endIndex + "extracted_function".length
);
const range = new Range(startPosition, endPosition);
const toReplace = document.getText(range);
// Use snippet to allow user to input the new function name
const snippet = toReplace
.replace("extracted_function", "${1:function_extracted}$0")
.replace("extracted_function", "${1:function_extracted}")
.replace(/function_extracted/g, "extracted_function")
.replace(/^ +/gm, "");
editor2.insertSnippet(new SnippetString(snippet), range);
// insertSnippet breaks whitspace formatting, so we run format cmd:
commands.executeCommand("editor.action.formatDocument");
}
}

return handle;
},
};

export default extractFunction;
13 changes: 10 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { join } from "path";
import * as fs from "fs";
import Commands from "./commands";
import extractFunction from "./commands/extract-function";
import restartServer from "./commands/restart-server";
import { URI } from "vscode-uri";
import Logger from "./logger";
Expand All @@ -28,9 +29,15 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(commands.registerCommand(id, handler));
});

registerCommand(restartServer, {
client,
});
if (client !== undefined) {
registerCommand(extractFunction, {
client,
showWarning: window.showWarningMessage,
});
registerCommand(restartServer, {
client,
});
}
}
}

Expand Down