Skip to content

Commit 3d29dd9

Browse files
authored
Merge pull request #303 from felix-cw/ruff-format
Add support for formatting with ruff
2 parents 4262f58 + 5595d23 commit 3d29dd9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

jupyterlab_code_formatter/formatters.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,18 @@ def format_code(
474474
return process.stdout
475475

476476

477+
class RuffFixFormatter(CommandLineFormatter):
478+
479+
def __init__(self):
480+
try:
481+
from ruff.__main__ import find_ruff_bin
482+
483+
ruff_command = find_ruff_bin()
484+
except (ImportError, FileNotFoundError):
485+
ruff_command = "ruff"
486+
self.command = [ruff_command, "check", "--fix-only", "-"]
487+
488+
477489
SERVER_FORMATTERS = {
478490
"black": BlackFormatter(),
479491
"blue": BlueFormatter(),
@@ -485,4 +497,5 @@ def format_code(
485497
"scalafmt": CommandLineFormatter(command=["scalafmt", "--stdin"]),
486498
"rustfmt": CommandLineFormatter(command=["rustfmt"]),
487499
"astyle": CommandLineFormatter(command=["astyle"]),
500+
"ruff": RuffFixFormatter(),
488501
}

schema/settings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@
296296
"additionalProperties": false,
297297
"type": "object"
298298
},
299+
"ruff": {
300+
"properties":{
301+
"args": {"type": "array", "items": {"type": "string"}}
302+
},
303+
"additionalProperties": false,
304+
"type": "object"
305+
},
299306
"formatOnSave": {
300307
"additionalProperties": false,
301308
"type": "boolean"
@@ -395,6 +402,14 @@
395402
"args": []
396403
}
397404
},
405+
"ruff": {
406+
"title": "Ruff Config",
407+
"description": "Command line options to be passed to ruff.",
408+
"$ref": "#/definitions/ruff",
409+
"default": {
410+
"args": ["--select=I001"]
411+
}
412+
},
398413
"suppressFormatterErrors": {
399414
"title": "Suppress formatter errors",
400415
"description": "Whether to suppress all errors reported by formatter while formatting. Useful when you have format on save mode on.",

0 commit comments

Comments
 (0)