Skip to content

Commit a84e31a

Browse files
committed
feat: support formatting
1 parent 60cc1aa commit a84e31a

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

pylsp_ruff/plugin.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
from pathlib import PurePath
66
from subprocess import PIPE, Popen
7-
from typing import Dict, List
7+
from typing import Dict, Generator, List, Optional
88

99
from lsprotocol.types import (
1010
CodeAction,
@@ -64,6 +64,34 @@ def pylsp_settings():
6464
return converter.unstructure(settings)
6565

6666

67+
@hookimpl(hookwrapper=True)
68+
def pylsp_format_document(workspace: Workspace, document: Document) -> Generator:
69+
"""
70+
Provide formatting through ruff.
71+
72+
Parameters
73+
----------
74+
workspace : pylsp.workspace.Workspace
75+
Current workspace.
76+
document : pylsp.workspace.Document
77+
Document to apply ruff on.
78+
"""
79+
log.debug(f"textDocument/formatting: {document}")
80+
outcome = yield
81+
results = outcome.get_result()
82+
if results:
83+
document.source = results[0]["new_text"]
84+
85+
new_text = run_ruff_format(workspace, document)
86+
range = Range(
87+
start=Position(line=0, character=0),
88+
end=Position(line=len(document.lines), character=0),
89+
)
90+
text_edit = TextEdit(range=range, new_text=new_text)
91+
92+
outcome.force_result(converter.unstructure([text_edit]))
93+
94+
6795
@hookimpl
6896
def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
6997
"""
@@ -315,7 +343,20 @@ def run_ruff_fix(workspace: Workspace, document: Document) -> str:
315343
return result
316344

317345

318-
def run_ruff(workspace: Workspace, document: Document, fix: bool = False) -> str:
346+
def run_ruff_format(workspace: Workspace, document: Document) -> str:
347+
settings = load_settings(workspace, document)
348+
settings.select = settings.format
349+
settings.extend_select = None
350+
result = run_ruff(workspace, document, settings=settings, fix=True)
351+
return result
352+
353+
354+
def run_ruff(
355+
workspace: Workspace,
356+
document: Document,
357+
settings: Optional[PluginSettings] = None,
358+
fix: bool = False,
359+
) -> str:
319360
"""
320361
Run ruff on the given document and the given arguments.
321362
@@ -332,7 +373,8 @@ def run_ruff(workspace: Workspace, document: Document, fix: bool = False) -> str
332373
-------
333374
String containing the result in json format.
334375
"""
335-
settings = load_settings(workspace, document)
376+
if settings is None:
377+
settings = load_settings(workspace, document)
336378
executable = settings.executable
337379
arguments = build_arguments(document, settings, fix)
338380

@@ -456,6 +498,7 @@ def load_settings(workspace: Workspace, document: Document) -> PluginSettings:
456498
executable=plugin_settings.executable,
457499
extend_ignore=plugin_settings.extend_ignore,
458500
extend_select=plugin_settings.extend_select,
501+
format=plugin_settings.format,
459502
)
460503

461504
return plugin_settings

pylsp_ruff/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class PluginSettings:
2222
extend_ignore: Optional[List[str]] = None
2323
per_file_ignores: Optional[Dict[str, List[str]]] = None
2424

25+
format: Optional[List[str]] = None
26+
2527

2628
def to_camel_case(snake_str: str) -> str:
2729
components = snake_str.split("_")

0 commit comments

Comments
 (0)