Skip to content

Commit bd903d3

Browse files
committed
Refactor.
1 parent f280c9f commit bd903d3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pylsp_isort/plugin.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ def pylsp_settings() -> Dict[str, Any]:
3434

3535
@hookimpl(hookwrapper=True)
3636
def pylsp_format_document(config: Config, document: Document) -> Generator:
37-
range = Range(
38-
start={"line": 0, "character": 0},
39-
end={"line": len(document.lines), "character": 0},
40-
)
4137
outcome = yield
42-
_format(outcome, config, document, range)
38+
_format(outcome, config, document)
4339

4440

4541
@hookimpl(hookwrapper=True)
@@ -48,13 +44,21 @@ def pylsp_format_range(config: Config, document: Document, range: Range) -> Gene
4844
_format(outcome, config, document, range)
4945

5046

51-
def _format(outcome, config: Config, document: Document, range: Range) -> None:
47+
def _format(
48+
outcome, config: Config, document: Document, range: Optional[Range] = None
49+
) -> None:
5250
result = outcome.get_result()
5351
if result:
5452
text = result[0]["newText"]
5553
range = result[0]["range"]
56-
else:
54+
elif range:
5755
text = "".join(document.lines[range["start"]["line"] : range["end"]["line"]])
56+
else:
57+
text = document.source
58+
range = Range(
59+
start={"line": 0, "character": 0},
60+
end={"line": len(document.lines), "character": 0},
61+
)
5862

5963
IGNORE_KEYS = {"enabled"}
6064
settings = config.plugin_settings("isort", document_path=document.path)

0 commit comments

Comments
 (0)