Skip to content

Commit

Permalink
Make import sorting opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcshane committed Nov 13, 2023
1 parent 077f739 commit 20e4761
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 46 deletions.
25 changes: 14 additions & 11 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
from pathlib import PurePath
from subprocess import PIPE, Popen
from typing import Dict, Final, Generator, List, Optional
from typing import Dict, Generator, List, Optional

if sys.version_info >= (3, 11):
import tomllib
Expand Down Expand Up @@ -62,8 +62,6 @@
"H": DiagnosticSeverity.Hint,
}

ISORT_FIXES: Final = "I"


class Subcommand(str, enum.Enum):
CHECK = "check"
Expand Down Expand Up @@ -130,15 +128,20 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
settings=settings, document_path=document.path, document_source=source
)

settings.select = [ISORT_FIXES]
if settings.format:
settings.select.extend(settings.format)
new_text = run_ruff(
settings=settings,
document_path=document.path,
document_source=new_text,
fix=True,
)
# A second pass on the document via `ruff check` with only the rules
# enabled via the format config. This allows for things liek specifying
# `format = ["I"]` to get import sorting as part of formatting.
fmtselect = settings.format
settings = PluginSettings()
settings.ignore = ["ALL"]
settings.select = fmtselect
new_text = run_ruff(
settings=settings,
document_path=document.path,
document_source=new_text,
fix=True,
)

# Avoid applying empty text edit
if not new_text or new_text == source:
Expand Down
91 changes: 56 additions & 35 deletions tests/test_ruff_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,41 @@

import pylsp_ruff.plugin as plugin

_UNSORTED_IMPORTS = tw.dedent(
"""
from thirdparty import x
import io
import asyncio
"""
).strip()

_SORTED_IMPORTS = tw.dedent(
"""
import asyncio
import io
from thirdparty import x
"""
).strip()

_UNFORMATTED_CODE = tw.dedent(
"""
def foo(): pass
def bar(): pass
"""
).strip()

_FORMATTED_CODE = tw.dedent(
"""
def foo():
pass
def bar():
pass
"""
).strip()


@pytest.fixture()
def workspace(tmp_path):
Expand Down Expand Up @@ -54,40 +89,26 @@ def force_result(self, r):
return pytest.fail()


def test_ruff_format(workspace):
# imports incorrectly ordered,
# body of foo has a line that's too long
# def bar() line missing whitespace above
txt = tw.dedent(
"""
from thirdparty import x
import io
import asyncio
def foo():
print("this is a looooooooooooooooooooooooooooooooooooooooooooong line that should exceed the usual line-length limit which is normally eighty-eight columns")
def bar():
pass
""" # noqa: E501
).lstrip()
want = tw.dedent(
"""
import asyncio
import io
from thirdparty import x
def foo():
print(
"this is a looooooooooooooooooooooooooooooooooooooooooooong line that should exceed the usual line-length limit which is normally eighty-eight columns"
)
def bar():
pass
""" # noqa: E501
).lstrip()
def test_ruff_format_only(workspace):
txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
want = f"{_UNSORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"
_, doc = temp_document(txt, workspace)
got = run_plugin_format(workspace, doc)
assert want == got


def test_ruff_format_and_sort_imports(workspace):
txt = f"{_UNSORTED_IMPORTS}\n{_UNFORMATTED_CODE}"
want = f"{_SORTED_IMPORTS}\n\n\n{_FORMATTED_CODE}\n"
_, doc = temp_document(txt, workspace)
workspace._config.update(
{
"plugins": {
"ruff": {
"format": ["I001"],
}
}
}
)
got = run_plugin_format(workspace, doc)
assert want == got, f"want:\n{want}\n\ngot:\n{got}"
assert want == got

0 comments on commit 20e4761

Please sign in to comment.