-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run
ruff format
when lsp formatting is invoked
Adds the Subcommand enum to indicate which `ruff` subcommand should be executed by `run_ruff`. At this time, only `check` and `format` are supported. As different subcommands support different parameters, argument generation is delegated based on the specific subcommand value. The `ruff format` subcommand does not currently organize imports and there does not appear to be a way to convince it to do so. Until a unified command exists the approach taken here is to format and then make a second run of `ruff check` that _only_ performs import formatting.
- Loading branch information
Showing
4 changed files
with
184 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import contextlib | ||
import tempfile | ||
import textwrap as tw | ||
from typing import Any, Mapping, Optional | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from pylsp import uris | ||
from pylsp.config.config import Config | ||
from pylsp.workspace import Document, Workspace | ||
|
||
import pylsp_ruff.plugin as plugin | ||
|
||
|
||
@pytest.fixture() | ||
def workspace(tmp_path): | ||
"""Return a workspace.""" | ||
ws = Workspace(tmp_path.absolute().as_uri(), Mock()) | ||
ws._config = Config(ws.root_uri, {}, 0, {}) | ||
return ws | ||
|
||
|
||
def temp_document(doc_text, workspace): | ||
with tempfile.NamedTemporaryFile( | ||
mode="w", dir=workspace.root_path, delete=False | ||
) as temp_file: | ||
name = temp_file.name | ||
temp_file.write(doc_text) | ||
doc = Document(uris.from_fs_path(name), workspace) | ||
return name, doc | ||
|
||
|
||
def run_plugin_format(workspace: Workspace, doc: Document) -> str: | ||
class TestResult: | ||
result: Optional[list[Mapping[str, Any]]] | ||
|
||
def __init__(self): | ||
self.result = None | ||
|
||
def get_result(self): | ||
return self.result | ||
|
||
def force_result(self, r): | ||
self.result = r | ||
|
||
generator = plugin.pylsp_format_document(workspace, doc) | ||
result = TestResult() | ||
with contextlib.suppress(StopIteration): | ||
generator.send(None) | ||
generator.send(result) | ||
|
||
if result.result: | ||
return result.result[0]["newText"] | ||
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 normaly eighty-eight columns") | ||
def bar(): | ||
pass | ||
""" | ||
).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 normaly eighty-eight columns" | ||
) | ||
def bar(): | ||
pass | ||
""" | ||
).lstrip() | ||
_, doc = temp_document(txt, workspace) | ||
got = run_plugin_format(workspace, doc) | ||
assert want == got, f"want:\n{want}\n\ngot:\n{got}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters