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

Move command implementation to commands.ts #693

Merged
merged 1 commit into from
Feb 18, 2025
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
38 changes: 38 additions & 0 deletions src/common/commands.ts
Original file line number Diff line number Diff line change
@@ -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.`,
);
});
}
75 changes: 7 additions & 68 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -174,79 +173,19 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
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;
Expand Down
Loading