Skip to content

Commit 8b12e26

Browse files
committed
Drop hookimpl
1 parent 6acfde0 commit 8b12e26

File tree

3 files changed

+41
-137
lines changed

3 files changed

+41
-137
lines changed

pyls_black/plugin.py

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,38 @@
1-
from typing import NamedTuple
2-
31
import black
42
from pyls import hookimpl
53

64

7-
@hookimpl(hookwrapper=True)
5+
@hookimpl
86
def pyls_format_document(document):
97
return format_document(document)
108

119

12-
@hookimpl(hookwrapper=True)
10+
@hookimpl
1311
def pyls_format_range(document, range):
14-
selection = Selection(start=range["start"]["line"], end=range["end"]["line"] + 1)
15-
return format_document(document, selection)
16-
17-
18-
class Selection(NamedTuple):
19-
start: int
20-
end: int
21-
22-
def to_range(self):
23-
return {
24-
"start": {"line": self.start, "character": 0},
25-
"end": {"line": self.end, "character": 0},
26-
}
27-
28-
29-
def format_document(document, selection=None):
30-
outcome = yield
12+
range["start"]["character"] = 0
13+
range["end"]["line"] += 1
14+
range["end"]["character"] = 0
15+
return format_document(document, range)
3116

32-
result = outcome.get_result()
3317

34-
if result:
35-
text = result[0]["newText"]
18+
def format_document(document, range=None):
19+
if range:
20+
start = range["start"]["line"]
21+
end = range["end"]["line"]
22+
text = "".join(document.lines[start:end])
3623
else:
3724
text = document.source
38-
39-
if selection:
40-
text = select_text(text, selection)
41-
else:
42-
selection = Selection(0, len(document.lines))
25+
range = {
26+
"start": {"line": 0, "character": 0},
27+
"end": {"line": len(document.lines), "character": 0},
28+
}
4329

4430
try:
4531
formatted_text = format_text(text)
4632
except (ValueError, black.NothingChanged):
47-
return
48-
49-
new_result = [{"range": selection.to_range(), "newText": formatted_text}]
50-
51-
outcome.force_result(new_result)
52-
33+
return []
5334

54-
def select_text(text, selection):
55-
lines = text.splitlines(True)
56-
return "".join(lines[selection.start : selection.end])
35+
return [{"range": range, "newText": formatted_text}]
5736

5837

5938
def format_text(text):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
"Programming Language :: Python :: 3",
2121
"Programming Language :: Python :: 3.6",
2222
"License :: OSI Approved :: MIT License",
23-
"Operating System :: OS Independent"
23+
"Operating System :: OS Independent",
2424
),
2525
)

tests/test_plugin.py

Lines changed: 23 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -30,79 +30,36 @@ def invalid_document():
3030
return Document(uri)
3131

3232

33-
def run(g, outcome):
34-
next(g)
35-
36-
with pytest.raises(StopIteration):
37-
g.send(outcome)
38-
39-
4033
def test_pyls_format_document(unformatted_document, formatted_document):
4134
mock = Mock()
4235
mock.get_result.return_value = None
4336

44-
g = pyls_format_document(unformatted_document)
45-
run(g, mock)
46-
47-
mock.force_result.assert_called_once_with(
48-
[
49-
{
50-
"range": {
51-
"start": {"line": 0, "character": 0},
52-
"end": {"line": 2, "character": 0},
53-
},
54-
"newText": formatted_document.source,
55-
}
56-
]
57-
)
37+
result = pyls_format_document(unformatted_document)
5838

59-
60-
def test_pyls_format_document_with_result(unformatted_document):
61-
mock = Mock()
62-
mock.get_result.return_value = [
39+
assert result == [
6340
{
6441
"range": {
6542
"start": {"line": 0, "character": 0},
66-
"end": {"line": 1, "character": 0},
43+
"end": {"line": 2, "character": 0},
6744
},
68-
"newText": "x = 1+2\n",
45+
"newText": formatted_document.source,
6946
}
7047
]
7148

72-
g = pyls_format_document(unformatted_document)
73-
run(g, mock)
74-
75-
mock.force_result.assert_called_once_with(
76-
[
77-
{
78-
"range": {
79-
"start": {"line": 0, "character": 0},
80-
"end": {"line": 2, "character": 0},
81-
},
82-
"newText": "x = 1 + 2\n",
83-
}
84-
]
85-
)
86-
8749

8850
def test_pyls_format_document_unchanged(formatted_document):
8951
mock = Mock()
9052
mock.get_result.return_value = None
9153

92-
g = pyls_format_document(formatted_document)
93-
run(g, mock)
94-
95-
mock.force_result.assert_not_called()
54+
result = pyls_format_document(formatted_document)
9655

56+
assert result == []
9757

98-
def test_pyls_format_document_supresses_syntax_errors(invalid_document):
99-
mock = Mock()
100-
mock.get_result.return_value = None
10158

102-
g = pyls_format_document(invalid_document)
103-
run(g, mock)
59+
def test_pyls_format_document_syntax_error(invalid_document):
60+
result = pyls_format_document(invalid_document)
10461

105-
mock.force_result.assert_not_called()
62+
assert result == []
10663

10764

10865
@pytest.mark.parametrize(
@@ -115,62 +72,30 @@ def test_pyls_format_range(unformatted_document, start, end, expected):
11572
"end": {"line": end, "character": 0},
11673
}
11774

118-
mock = Mock()
119-
mock.get_result.return_value = None
120-
121-
g = pyls_format_range(unformatted_document, range=range)
122-
run(g, mock)
75+
result = pyls_format_range(unformatted_document, range=range)
12376

124-
mock.force_result.assert_called_once_with(
125-
[
126-
{
127-
"range": {
128-
"start": {"line": start, "character": 0},
129-
"end": {"line": end + 1, "character": 0},
130-
},
131-
"newText": expected,
132-
}
133-
]
134-
)
135-
136-
137-
def test_pyls_format_range_with_result(unformatted_document):
138-
range = {"start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 0}}
139-
140-
mock = Mock()
141-
mock.get_result.return_value = [
77+
assert result == [
14278
{
14379
"range": {
144-
"start": {"line": 0, "character": 0},
145-
"end": {"line": 2, "character": 0},
80+
"start": {"line": start, "character": 0},
81+
"end": {"line": end + 1, "character": 0},
14682
},
147-
"newText": "x = 1+2\ny = 3+4\n",
83+
"newText": expected,
14884
}
14985
]
15086

151-
g = pyls_format_range(unformatted_document, range=range)
152-
run(g, mock)
153-
154-
mock.force_result.assert_called_once_with(
155-
[
156-
{
157-
"range": {
158-
"start": {"line": 0, "character": 0},
159-
"end": {"line": 1, "character": 0},
160-
},
161-
"newText": "x = 1 + 2\n",
162-
}
163-
]
164-
)
165-
16687

16788
def test_pyls_format_range_unchanged(formatted_document):
16889
range = {"start": {"line": 0, "character": 0}, "end": {"line": 1, "character": 0}}
16990

170-
mock = Mock()
171-
mock.get_result.return_value = None
91+
result = pyls_format_range(formatted_document, range=range)
92+
93+
assert result == []
94+
95+
96+
def test_pyls_format_range_syntax_error(invalid_document):
97+
range = {"start": {"line": 0, "character": 0}, "end": {"line": 1, "character": 0}}
17298

173-
g = pyls_format_range(formatted_document, range=range)
174-
run(g, mock)
99+
result = pyls_format_range(invalid_document, range=range)
175100

176-
mock.force_result.assert_not_called()
101+
assert result == []

0 commit comments

Comments
 (0)