Skip to content

Commit 6059aa3

Browse files
authored
Pass a single copy of the document's source around for flake8 (#441)
1 parent 8377b2c commit 6059aa3

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

pylsp/plugins/flake8_lint.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ def pylsp_lint(workspace, document):
7979
flake8_executable = settings.get("executable", "flake8")
8080

8181
args = build_args(opts)
82-
output = run_flake8(flake8_executable, args, document)
83-
return parse_stdout(document, output)
8482

83+
# ensure the same source is used for flake8 execution and result parsing;
84+
# single source access improves performance as it is only one disk access
85+
source = document.source
86+
output = run_flake8(flake8_executable, args, document, source)
87+
return parse_stdout(source, output)
8588

86-
def run_flake8(flake8_executable, args, document):
89+
90+
def run_flake8(flake8_executable, args, document, source):
8791
"""Run flake8 with the provided arguments, logs errors
8892
from stderr if any.
8993
"""
@@ -127,7 +131,7 @@ def run_flake8(flake8_executable, args, document):
127131
p = Popen( # pylint: disable=consider-using-with
128132
cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, **popen_kwargs
129133
)
130-
(stdout, stderr) = p.communicate(document.source.encode())
134+
(stdout, stderr) = p.communicate(source.encode())
131135
if stderr:
132136
log.error("Error while running flake8 '%s'", stderr.decode())
133137
return stdout.decode()
@@ -155,7 +159,7 @@ def build_args(options):
155159
return args
156160

157161

158-
def parse_stdout(document, stdout):
162+
def parse_stdout(source, stdout):
159163
"""
160164
Build a diagnostics from flake8's output, it should extract every result and format
161165
it into a dict that looks like this:
@@ -183,6 +187,7 @@ def parse_stdout(document, stdout):
183187
A list of dictionaries.
184188
"""
185189

190+
document_lines = source.splitlines(True)
186191
diagnostics = []
187192
lines = stdout.splitlines()
188193
for raw_line in lines:
@@ -212,7 +217,7 @@ def parse_stdout(document, stdout):
212217
"end": {
213218
"line": line,
214219
# no way to determine the column
215-
"character": len(document.lines[line]),
220+
"character": len(document_lines[line]),
216221
},
217222
},
218223
"message": msg,

0 commit comments

Comments
 (0)