Skip to content

Commit

Permalink
Expose LSP's format action (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Sep 27, 2023
1 parent f7da1fa commit 1bf35ef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@
"category": "Ruff",
"command": "ruff.executeAutofix"
},
{
"title": "Format document",
"category": "Ruff",
"command": "ruff.executeFormat"
},
{
"title": "Format imports",
"category": "Ruff",
Expand Down
25 changes: 25 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,31 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
);
});
}),
registerCommand(`${serverId}.executeFormat`, async () => {
if (!lsClient) {
return;
}

const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return;
}

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 () => {
await 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;
Expand Down

0 comments on commit 1bf35ef

Please sign in to comment.