From 7602b62151b5409b8b030d658ba0d4b92d45c1f0 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Tue, 18 Feb 2025 08:55:33 +0530 Subject: [PATCH] Move command implementation to `commands.ts` --- src/common/commands.ts | 38 +++++++++++++++++++++ src/extension.ts | 75 ++++-------------------------------------- 2 files changed, 45 insertions(+), 68 deletions(-) create mode 100644 src/common/commands.ts diff --git a/src/common/commands.ts b/src/common/commands.ts new file mode 100644 index 0000000..a85afe2 --- /dev/null +++ b/src/common/commands.ts @@ -0,0 +1,38 @@ +import * as vscode from "vscode"; +import { ExecuteCommandRequest, LanguageClient } from "vscode-languageclient/node"; + +const ISSUE_TRACKER = "https://github.com/astral-sh/ruff/issues"; + +export async function executeAutofix(lsClient: LanguageClient, serverId: string) { + await executeCommand(lsClient, `${serverId}.applyAutofix`); +} + +export async function executeFormat(lsClient: LanguageClient, serverId: string) { + await executeCommand(lsClient, `${serverId}.applyFormat`); +} + +export async function executeOrganizeImports(lsClient: LanguageClient, serverId: string) { + await executeCommand(lsClient, `${serverId}.applyOrganizeImports`); +} + +async function executeCommand(lsClient: LanguageClient, command: string) { + const textEditor = vscode.window.activeTextEditor; + if (!textEditor) { + return; + } + + const textDocument = { + uri: textEditor.document.uri.toString(), + version: textEditor.document.version, + }; + const params = { + command, + arguments: [textDocument], + }; + + await lsClient.sendRequest(ExecuteCommandRequest.type, params).then(undefined, async () => { + vscode.window.showErrorMessage( + `Failed to execute the command '${command}'. Please consider opening an issue at ${ISSUE_TRACKER} with steps to reproduce.`, + ); + }); +} diff --git a/src/extension.ts b/src/extension.ts index 1c14069..7775b71 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,8 +23,7 @@ import { registerCommand, } from "./common/vscodeapi"; import { getProjectRoot } from "./common/utilities"; - -const issueTracker = "https://github.com/astral-sh/ruff/issues"; +import { executeAutofix, executeFormat, executeOrganizeImports } from "./common/commands"; let lsClient: LanguageClient | undefined; let restartInProgress = false; @@ -174,79 +173,19 @@ export async function activate(context: vscode.ExtensionContext): Promise await runServer(); }), registerCommand(`${serverId}.executeAutofix`, async () => { - if (!lsClient) { - return; - } - - const textEditor = vscode.window.activeTextEditor; - if (!textEditor) { - return; + if (lsClient) { + await executeAutofix(lsClient, serverId); } - - const textDocument = { - uri: textEditor.document.uri.toString(), - version: textEditor.document.version, - }; - const params = { - command: `${serverId}.applyAutofix`, - arguments: [textDocument], - }; - - await lsClient.sendRequest(ExecuteCommandRequest.type, params).then(undefined, async () => { - vscode.window.showErrorMessage( - "Failed to apply Ruff fixes to the document. Please consider opening an issue with steps to reproduce.", - ); - }); }), registerCommand(`${serverId}.executeFormat`, async () => { - if (!lsClient) { - return; - } - - const textEditor = vscode.window.activeTextEditor; - if (!textEditor) { - return; + if (lsClient) { + await executeFormat(lsClient, serverId); } - - const textDocument = { - uri: textEditor.document.uri.toString(), - version: textEditor.document.version, - }; - const params = { - command: `${serverId}.applyFormat`, - arguments: [textDocument], - }; - - await lsClient.sendRequest(ExecuteCommandRequest.type, params).then(undefined, async () => { - vscode.window.showErrorMessage( - "Failed to apply Ruff formatting to the document. Please consider opening an issue with steps to reproduce.", - ); - }); }), registerCommand(`${serverId}.executeOrganizeImports`, async () => { - if (!lsClient) { - return; - } - - const textEditor = vscode.window.activeTextEditor; - if (!textEditor) { - return; + if (lsClient) { + await executeOrganizeImports(lsClient, serverId); } - - const textDocument = { - uri: textEditor.document.uri.toString(), - version: textEditor.document.version, - }; - const params = { - command: `${serverId}.applyOrganizeImports`, - arguments: [textDocument], - }; - - await lsClient.sendRequest(ExecuteCommandRequest.type, params).then(undefined, async () => { - vscode.window.showErrorMessage( - `Failed to apply Ruff fixes to the document. Please consider opening an issue at ${issueTracker} with steps to reproduce.`, - ); - }); }), registerCommand(`${serverId}.debugInformation`, async () => { let configuration = getConfiguration(serverId) as unknown as ISettings;