Skip to content

Commit

Permalink
feat(formatters): add ruff format support
Browse files Browse the repository at this point in the history
needs ruff 0.1.1, no stdin support by now
  • Loading branch information
fannheyward committed Oct 20, 2023
1 parent 4a5381a commit 0e2051b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,21 @@
"description": "Path to Black, you can use a custom version of Black by modifying this setting to include the full path.",
"scope": "resource"
},
"python.formatting.ruffArgs": {
"type": "array",
"description": "Arguments passed in. Each argument is a separate item in the array.",
"default": [],
"items": {
"type": "string"
},
"scope": "resource"
},
"python.formatting.ruffPath": {
"type": "string",
"default": "ruff",
"description": "Path to ruff, you can use a custom version of ruff by modifying this setting to include the full path.",
"scope": "resource"
},
"python.formatting.pyinkArgs": {
"type": "array",
"description": "Arguments passed in. Each argument is a separate item in the array.",
Expand All @@ -1356,7 +1371,7 @@
"python.formatting.pyinkPath": {
"type": "string",
"default": "pyink",
"description": "Path to Pyink, you can use a custom version of Black by modifying this setting to include the full path.",
"description": "Path to Pyink, you can use a custom version of pyink by modifying this setting to include the full path.",
"scope": "resource"
},
"python.formatting.darkerArgs": {
Expand Down Expand Up @@ -1433,6 +1448,7 @@
"pyink",
"blackd",
"yapf",
"ruff",
"none"
],
"scope": "resource"
Expand Down
1 change: 1 addition & 0 deletions src/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class PythonSettings implements IPythonSettings {
}
this.formatting.autopep8Path = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.autopep8Path));
this.formatting.yapfPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.yapfPath));
this.formatting.ruffPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.ruffPath));
this.formatting.blackPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.blackPath));
this.formatting.pyinkPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.pyinkPath));
this.formatting.blackdPath = this.getAbsolutePath(systemVariables.resolveAny(this.formatting.blackdPath));
Expand Down
29 changes: 29 additions & 0 deletions src/features/formatters/ruff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CancellationToken, FormattingOptions, OutputChannel, Range, TextDocument, TextEdit, Thenable, window } from 'coc.nvim';
import { IPythonSettings } from '../../types';
import { BaseFormatter } from './baseFormatter';

export class RuffFormatter extends BaseFormatter {
constructor(public readonly pythonSettings: IPythonSettings, public readonly outputChannel: OutputChannel) {
super('ruff', pythonSettings, outputChannel);
}

public formatDocument(document: TextDocument, options: FormattingOptions, token: CancellationToken, range?: Range): Thenable<TextEdit[]> {
const formatSelection = range ? range : false;

if (formatSelection) {
const errorMessage = async () => {
this.outputChannel.appendLine('Ruff does not support the "Format Selection" command');
window.showErrorMessage('Ruff does not support the "Format Selection" command');
return [] as TextEdit[];
};

return errorMessage();
}

const ruffArgs = ['format', '--diff', '--silent'];
if (this.pythonSettings.formatting.ruffArgs.length > 0) {
ruffArgs.push(...this.pythonSettings.formatting.ruffArgs);
}
return super.provideDocumentFormattingEdits(document, options, token, ruffArgs);
}
}
4 changes: 4 additions & 0 deletions src/features/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { BlackFormatter } from './formatters/black';
import { BlackdFormatter } from './formatters/blackd';
import { DarkerFormatter } from './formatters/darker';
import { PyinkFormatter } from './formatters/pyink';
import { RuffFormatter } from './formatters/ruff';
import { YapfFormatter } from './formatters/yapf';

export class PythonFormattingEditProvider implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider {
Expand All @@ -45,6 +46,9 @@ export class PythonFormattingEditProvider implements DocumentFormattingEditProvi
case 'yapf':
this.formatters.set('yapf', new YapfFormatter(this.pythonSettings, this.outputChannel));
break;
case 'ruff':
this.formatters.set('ruff', new RuffFormatter(this.pythonSettings, this.outputChannel));
break;
case 'autopep8':
this.formatters.set('autopep8', new AutoPep8Formatter(this.pythonSettings, this.outputChannel));
break;
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export enum Product {
}

export type LinterId = 'bandit' | 'flake8' | 'mypy' | 'ruff' | 'pycodestyle' | 'prospector' | 'pydocstyle' | 'pyflakes' | 'pylama' | 'pylint' | 'pytype';
export type FormatterId = 'yapf' | 'black' | 'autopep8' | 'darker' | 'blackd' | 'pyink';
export type FormatterId = 'yapf' | 'black' | 'autopep8' | 'darker' | 'blackd' | 'pyink' | 'ruff';
export type TestingFramework = 'unittest' | 'pytest';

export interface ILinterInfo {
Expand Down Expand Up @@ -158,6 +158,8 @@ export interface IFormattingSettings {
readonly pyinkArgs: string[];
yapfPath: string;
readonly yapfArgs: string[];
ruffPath: string;
readonly ruffArgs: string[];
darkerPath: string;
readonly darkerArgs: string[];
blackdPath: string;
Expand Down

0 comments on commit 0e2051b

Please sign in to comment.