Skip to content

Commit ecda0ca

Browse files
committed
Refactor.
1 parent cadf266 commit ecda0ca

File tree

2 files changed

+63
-29
lines changed

2 files changed

+63
-29
lines changed

pylsp_isort/plugin.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import os
33
from pathlib import Path
4-
from typing import Any, Dict, Generator, TypedDict
4+
from typing import Any, Dict, Generator, Optional, TypedDict, Union
55

66
import isort
77
from pylsp import hookimpl
@@ -39,38 +39,72 @@ def pylsp_format_document(config: Config, document: Document) -> Generator:
3939
end={"line": len(document.lines), "character": 0},
4040
)
4141
outcome = yield
42-
_process(outcome, config, document, range)
42+
_format(outcome, config, document, range)
4343

4444

4545
@hookimpl(hookwrapper=True)
4646
def pylsp_format_range(config: Config, document: Document, range: Range) -> Generator:
4747
outcome = yield
48-
_process(outcome, config, document, range)
48+
_format(outcome, config, document, range)
4949

5050

51-
def _process(outcome, config: Config, document: Document, range: Range):
51+
def _format(outcome, config: Config, document: Document, range: Range) -> None:
5252
result = outcome.get_result()
5353
if result:
5454
text = result[0]["newText"]
5555
range = result[0]["range"]
5656
else:
57-
start = range["start"]["line"]
58-
end = range["end"]["line"]
59-
text = "".join(document.lines[start:end])
57+
text = "".join(document.lines[range["start"]["line"] : range["end"]["line"]])
6058

59+
settings = config.plugin_settings("isort", document_path=document.path)
60+
new_text = run_isort(text, settings, file_path=document.path)
61+
62+
if new_text != text:
63+
result = [{"range": range, "newText": new_text}]
64+
outcome.force_result(result)
65+
66+
67+
def run_isort(
68+
text: str,
69+
settings: Optional[Dict[str, Any]] = None,
70+
file_path: Optional[Union[str, bytes, os.PathLike]] = None,
71+
) -> str:
72+
config = isort_config(settings or {}, file_path)
73+
file_path = Path(os.fsdecode(file_path)) if file_path else None
74+
return isort.code(text, config=config, file_path=file_path)
75+
76+
77+
def isort_config(
78+
settings: Dict[str, Any],
79+
target_path: Optional[Union[str, bytes, os.PathLike]] = None,
80+
) -> isort.Config:
6181
config_kwargs = {}
82+
unsupported_kwargs = {}
83+
6284
defined_args = set(getattr(isort.Config, "__dataclass_fields__", {}).keys())
63-
settings = config.plugin_settings("isort", document_path=document.path)
6485
for key, value in settings.items():
6586
if key in defined_args:
6687
config_kwargs[key] = value
67-
config_kwargs["settings_path"] = os.path.dirname(os.path.abspath(document.path))
68-
logger.debug("config_kwargs=%r", config_kwargs)
88+
else:
89+
unsupported_kwargs[key] = value
90+
91+
if "settings_path" in settings:
92+
if os.path.isfile(settings["settings_path"]):
93+
config_kwargs["settings_file"] = os.path.abspath(settings["settings_path"])
94+
config_kwargs["settings_path"] = os.path.dirname(
95+
config_kwargs["settings_file"]
96+
)
97+
else:
98+
config_kwargs["settings_path"] = os.path.abspath(settings["settings_path"])
99+
elif target_path:
100+
config_kwargs["settings_path"] = os.path.abspath(target_path)
101+
if not os.path.isdir(config_kwargs["settings_path"]):
102+
config_kwargs["settings_path"] = os.path.dirname(
103+
config_kwargs["settings_path"]
104+
)
69105

70-
new_text = isort.code(
71-
text, config=isort.Config(**config_kwargs), file_path=Path(document.path)
72-
)
106+
logger.debug("config_kwargs=%r", config_kwargs)
107+
if unsupported_kwargs:
108+
logger.info("unsupported_kwargs=%r", unsupported_kwargs)
73109

74-
if new_text != text:
75-
result = [{"range": range, "newText": new_text}]
76-
outcome.force_result(result)
110+
return isort.Config(**config_kwargs)

setup.cfg

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
[metadata]
32
name = pylsp-isort
43
version = 0.1
@@ -18,22 +17,23 @@ classifiers =
1817
Topic :: Software Development
1918
License :: OSI Approved :: MIT License
2019

21-
2220
[options]
23-
2421
packages = find:
25-
26-
install_requires =
27-
python-lsp-server
28-
29-
# python_requires = >= 3.6
22+
install_requires = python-lsp-server; isort>=5.0
23+
python_requires = >= 3.7
3024

3125
[options.entry_points]
3226
pylsp = isort = pylsp_isort.plugin
3327

34-
3528
[options.extras_require]
36-
dev =
37-
pytest
38-
build
39-
twine
29+
dev = pytest
30+
31+
[flake8]
32+
max-line-length = 88
33+
extend-ignore = E203
34+
35+
[mypy]
36+
ignore_missing_imports = true
37+
38+
[isort]
39+
profile = black

0 commit comments

Comments
 (0)