From a0f4a322fb0c23454ceb5bacbd288e7b08853543 Mon Sep 17 00:00:00 2001 From: yaegassy Date: Mon, 4 Sep 2023 10:15:06 +0900 Subject: [PATCH] feat: ruff format (DocumentFormatting) feature (#17) --- README.md | 20 ++++++++++++++++++++ package.json | 11 ++++++----- src/client.ts | 15 ++++++++++----- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e55bc95..40bbcb2 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,26 @@ If you do not need this feature, set `ruff.useDetectRuffCommand` to `false`. } ``` +### Format (DocumentFormatting) + +The [black](https://github.com/psf/black) equivalent formatting feature has been added to `ruff`. To enable this feature in `coc-ruff`, please follow the steps below: + +1. Please set `ruff.enableExperimentalFormatter` to `true`. +1. If you are using other Python-related coc-extensions alongside` coc-ruff`, please disable the formatting feature of those coc-extensions. + - e.g. `coc-pyright`: + - Please set `python.formatting.provider` to `none`. + +--- + +**coc-settings.json**: + +```jsonc +{ + "ruff.enableExperimentalFormatter": true, + "python.formatting.provider": "none" +} +``` + ### Auto-fixing Auto-fixing can be executed via the `ruff.executeAutofix` command or CodeAction. diff --git a/package.json b/package.json index 8686676..f2a4218 100644 --- a/package.json +++ b/package.json @@ -82,11 +82,6 @@ "default": true, "description": "Enable coc-ruff extension" }, - "ruff.disableDocumentFormatting": { - "type": "boolean", - "default": true, - "description": "Disable document formatting only." - }, "ruff.disableHover": { "type": "boolean", "default": false, @@ -209,6 +204,12 @@ "scope": "window", "type": "boolean" }, + "ruff.enableExperimentalFormatter": { + "default": false, + "markdownDescription": "Controls whether Ruff registers as capable of code formatting.", + "scope": "machine", + "type": "boolean" + }, "ruff.trace.server": { "type": "string", "enum": [ diff --git a/src/client.ts b/src/client.ts index d680036..740dedf 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3,10 +3,18 @@ import { LanguageClient, LanguageClientOptions, ServerOptions, workspace } from import which from 'which'; export function createLanguageClient(command: string) { + const settings = workspace.getConfiguration('ruff'); + const newEnv = { ...process.env }; + const serverOptions: ServerOptions = { command, + options: { env: newEnv }, }; + if (settings.enableExperimentalFormatter) { + newEnv.RUFF_EXPERIMENTAL_FORMATTER = '1'; + } + const clientOptions: LanguageClientOptions = { documentSelector: ['python'], initializationOptions: getInitializationOptions(), @@ -39,6 +47,7 @@ type RuffLspInitializationOptions = { enable: boolean; }; }; + enableExperimentalFormatter: boolean; }; }; @@ -63,6 +72,7 @@ function convertFromWorkspaceConfigToInitializationOptions() { enable: settings.get('codeAction.disableRuleComment.enable'), }, }, + enableExperimentalFormatter: settings.get('enableExperimentalFormatter'), }, }; @@ -85,16 +95,11 @@ function getInitializationOptions() { function getLanguageClientDisabledFeatures() { const r: string[] = []; - if (getConfigDisableDocumentFormatting()) r.push('documentFormatting'); if (getConfigDisableHover()) r.push('hover'); return r; } -function getConfigDisableDocumentFormatting() { - return workspace.getConfiguration('ruff').get('disableDocumentFormatting', true); -} - function getConfigDisableHover() { return workspace.getConfiguration('ruff').get('disableHover', false); }