Skip to content

Support ruff as a formatter #296

@felix-cw

Description

@felix-cw

Ruff is a linter for python that also has the ability to fix the lints automatically.
I'm using it as an alternative to isort.

I think it would be good to add it as a supported option in this package.

Here is the code I used to add it as a custom formatter:

from jupyterlab_code_formatter.formatters import (
    SERVER_FORMATTERS,
    CommandLineFormatter,
)


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", "-", "--select=COM,I"]


SERVER_FORMATTERS["ruff_isort"] = RuffFixFormatter()

I'm not sure how to pass args to a custom function via the normal jupyter lab config, so I hardcoded my rule selection argument --select=COM,I.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions