Skip to content

Commit

Permalink
feat: ruff format (DocumentFormatting) feature (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaegassy authored Sep 4, 2023
1 parent 22d0810 commit a0f4a32
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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": [
Expand Down
15 changes: 10 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -39,6 +47,7 @@ type RuffLspInitializationOptions = {
enable: boolean;
};
};
enableExperimentalFormatter: boolean;
};
};

Expand All @@ -63,6 +72,7 @@ function convertFromWorkspaceConfigToInitializationOptions() {
enable: settings.get('codeAction.disableRuleComment.enable'),
},
},
enableExperimentalFormatter: settings.get('enableExperimentalFormatter'),
},
};

Expand All @@ -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<boolean>('disableDocumentFormatting', true);
}

function getConfigDisableHover() {
return workspace.getConfiguration('ruff').get<boolean>('disableHover', false);
}

0 comments on commit a0f4a32

Please sign in to comment.