Skip to content

Commit 0edafb7

Browse files
committed
Add config support
1 parent 8b12e26 commit 0edafb7

File tree

11 files changed

+145
-14
lines changed

11 files changed

+145
-14
lines changed

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
lint: flake8 black-check isort-check
2+
3+
flake8:
4+
flake8 .
5+
6+
black:
7+
black .
8+
9+
black-check:
10+
black --diff --check .
11+
12+
isort:
13+
isort --recursive .
14+
15+
isort-check:
16+
isort --recursive --check-only .
17+
18+
mypy:
19+
mypy .
20+
21+
build:
22+
python3 setup.py sdist bdist_wheel
23+
24+
test-upload:
25+
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
26+
27+
upload:
28+
twine upload dist/*

pyls_black/plugin.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from typing import Dict
2+
13
import black
4+
import toml
25
from pyls import hookimpl
36

47

@@ -27,13 +30,54 @@ def format_document(document, range=None):
2730
"end": {"line": len(document.lines), "character": 0},
2831
}
2932

33+
config = load_config(document.path)
34+
3035
try:
31-
formatted_text = format_text(text)
36+
formatted_text = format_text(text=text, config=config)
3237
except (ValueError, black.NothingChanged):
3338
return []
3439

3540
return [{"range": range, "newText": formatted_text}]
3641

3742

38-
def format_text(text):
39-
return black.format_file_contents(text, line_length=88, fast=False)
43+
def format_text(*, text, config):
44+
line_length = config["line_length"]
45+
fast = config["fast"]
46+
mode = black.FileMode.from_configuration(
47+
py36=config["py36"],
48+
pyi=config["pyi"],
49+
skip_string_normalization=config["skip_string_normalization"],
50+
)
51+
print(line_length)
52+
return black.format_file_contents(
53+
text, line_length=line_length, fast=fast, mode=mode
54+
)
55+
56+
57+
def load_config(filename: str) -> Dict:
58+
defaults = {
59+
"line_length": 88,
60+
"fast": False,
61+
"py36": False,
62+
"pyi": False,
63+
"skip_string_normalization": False,
64+
}
65+
66+
root = black.find_project_root((filename,))
67+
68+
pyproject_filename = root / "pyproject.toml"
69+
70+
if not pyproject_filename.is_file():
71+
return defaults
72+
73+
try:
74+
pyproject_toml = toml.load(str(pyproject_filename))
75+
except (toml.TomlDecodeError, OSError) as e:
76+
return defaults
77+
78+
config = pyproject_toml.get("tool", {}).get("black", {})
79+
config = {
80+
key.replace("--", "").replace("-", "_"): value for key, value in config.items()
81+
}
82+
83+
return {**defaults, **config}

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
py36 = true

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ ignore = E203
44

55
[mypy]
66
ignore_missing_imports = true
7+
8+
[isort]
9+
line_length = 88

tests/fixtures/config/config.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run(these, arguments, should, be, wrapped)

tests/fixtures/config/pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[tool.black]
2+
line-length = 20
3+
--fast = true
4+
py36 = true
5+
pyi = true
6+
skip-string-normalization = true
File renamed without changes.
File renamed without changes.

tests/fixtures/pyproject.toml

Whitespace-only changes.
File renamed without changes.

0 commit comments

Comments
 (0)