Skip to content

Commit

Permalink
Merge pull request #303 from felix-cw/ruff-format
Browse files Browse the repository at this point in the history
Add support for formatting with ruff
  • Loading branch information
ryantam626 committed May 5, 2023
2 parents 4262f58 + 5595d23 commit 3d29dd9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions jupyterlab_code_formatter/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,18 @@ def format_code(
return process.stdout


class RuffFixFormatter(CommandLineFormatter):

def __init__(self):
try:
from ruff.__main__ import find_ruff_bin

ruff_command = find_ruff_bin()
except (ImportError, FileNotFoundError):
ruff_command = "ruff"
self.command = [ruff_command, "check", "--fix-only", "-"]


SERVER_FORMATTERS = {
"black": BlackFormatter(),
"blue": BlueFormatter(),
Expand All @@ -485,4 +497,5 @@ def format_code(
"scalafmt": CommandLineFormatter(command=["scalafmt", "--stdin"]),
"rustfmt": CommandLineFormatter(command=["rustfmt"]),
"astyle": CommandLineFormatter(command=["astyle"]),
"ruff": RuffFixFormatter(),
}
15 changes: 15 additions & 0 deletions schema/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@
"additionalProperties": false,
"type": "object"
},
"ruff": {
"properties":{
"args": {"type": "array", "items": {"type": "string"}}
},
"additionalProperties": false,
"type": "object"
},
"formatOnSave": {
"additionalProperties": false,
"type": "boolean"
Expand Down Expand Up @@ -395,6 +402,14 @@
"args": []
}
},
"ruff": {
"title": "Ruff Config",
"description": "Command line options to be passed to ruff.",
"$ref": "#/definitions/ruff",
"default": {
"args": ["--select=I001"]
}
},
"suppressFormatterErrors": {
"title": "Suppress formatter errors",
"description": "Whether to suppress all errors reported by formatter while formatting. Useful when you have format on save mode on.",
Expand Down

0 comments on commit 3d29dd9

Please sign in to comment.