@@ -79,11 +79,15 @@ def pylsp_lint(workspace, document):
79
79
flake8_executable = settings .get ("executable" , "flake8" )
80
80
81
81
args = build_args (opts )
82
- output = run_flake8 (flake8_executable , args , document )
83
- return parse_stdout (document , output )
84
82
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 )
85
88
86
- def run_flake8 (flake8_executable , args , document ):
89
+
90
+ def run_flake8 (flake8_executable , args , document , source ):
87
91
"""Run flake8 with the provided arguments, logs errors
88
92
from stderr if any.
89
93
"""
@@ -127,7 +131,7 @@ def run_flake8(flake8_executable, args, document):
127
131
p = Popen ( # pylint: disable=consider-using-with
128
132
cmd , stdin = PIPE , stdout = PIPE , stderr = PIPE , ** popen_kwargs
129
133
)
130
- (stdout , stderr ) = p .communicate (document . source .encode ())
134
+ (stdout , stderr ) = p .communicate (source .encode ())
131
135
if stderr :
132
136
log .error ("Error while running flake8 '%s'" , stderr .decode ())
133
137
return stdout .decode ()
@@ -155,7 +159,7 @@ def build_args(options):
155
159
return args
156
160
157
161
158
- def parse_stdout (document , stdout ):
162
+ def parse_stdout (source , stdout ):
159
163
"""
160
164
Build a diagnostics from flake8's output, it should extract every result and format
161
165
it into a dict that looks like this:
@@ -183,6 +187,7 @@ def parse_stdout(document, stdout):
183
187
A list of dictionaries.
184
188
"""
185
189
190
+ document_lines = source .splitlines (True )
186
191
diagnostics = []
187
192
lines = stdout .splitlines ()
188
193
for raw_line in lines :
@@ -212,7 +217,7 @@ def parse_stdout(document, stdout):
212
217
"end" : {
213
218
"line" : line ,
214
219
# no way to determine the column
215
- "character" : len (document . lines [line ]),
220
+ "character" : len (document_lines [line ]),
216
221
},
217
222
},
218
223
"message" : msg ,
0 commit comments