2
2
"""Linter plugin for pylint."""
3
3
import collections
4
4
import json
5
+ import logging
5
6
import sys
6
7
7
8
from pylint .epylint import py_run
8
9
from pyls import hookimpl , lsp
9
10
10
11
12
+ log = logging .getLogger (__name__ )
13
+
14
+
11
15
class PylintLinter (object ):
12
16
last_diags = collections .defaultdict (list )
13
17
@@ -55,14 +59,21 @@ def lint(cls, document, is_saved, flags=''):
55
59
path = document .path
56
60
if sys .platform .startswith ('win' ):
57
61
path = path .replace ('\\ ' , '/' )
58
- out , _err = py_run (
59
- '{} -f json {}' .format (path , flags ), return_std = True
60
- )
62
+
63
+ pylint_call = '{} -f json {}' .format (path , flags )
64
+ log .debug ("Calling pylint with '%s'" , pylint_call )
65
+ json_out , err = py_run (pylint_call , return_std = True )
66
+
67
+ # Get strings
68
+ json_out = json_out .getvalue ()
69
+ err = err .getvalue ()
70
+
71
+ if err != '' :
72
+ log .error ("Error calling pylint: '%s'" , err )
61
73
62
74
# pylint prints nothing rather than [] when there are no diagnostics.
63
75
# json.loads will not parse an empty string, so just return.
64
- json_str = out .getvalue ()
65
- if not json_str .strip ():
76
+ if not json_out .strip ():
66
77
cls .last_diags [document .path ] = []
67
78
return []
68
79
@@ -88,24 +99,21 @@ def lint(cls, document, is_saved, flags=''):
88
99
# * refactor
89
100
# * warning
90
101
diagnostics = []
91
- for diag in json .loads (json_str ):
102
+ for diag in json .loads (json_out ):
92
103
# pylint lines index from 1, pyls lines index from 0
93
104
line = diag ['line' ] - 1
94
- # But both index columns from 0
95
- col = diag ['column' ]
96
-
97
- # It's possible that we're linting an empty file. Even an empty
98
- # file might fail linting if it isn't named properly.
99
- end_col = len (document .lines [line ]) if document .lines else 0
100
105
101
106
err_range = {
102
107
'start' : {
103
108
'line' : line ,
104
- 'character' : col ,
109
+ # Index columns start from 0
110
+ 'character' : diag ['column' ],
105
111
},
106
112
'end' : {
107
113
'line' : line ,
108
- 'character' : end_col ,
114
+ # It's possible that we're linting an empty file. Even an empty
115
+ # file might fail linting if it isn't named properly.
116
+ 'character' : len (document .lines [line ]) if document .lines else 0 ,
109
117
},
110
118
}
111
119
@@ -131,6 +139,17 @@ def lint(cls, document, is_saved, flags=''):
131
139
return diagnostics
132
140
133
141
142
+ def _build_pylint_flags (settings ):
143
+ """Build arguments for calling pylint."""
144
+ pylint_args = settings .get ('args' )
145
+ if pylint_args is None :
146
+ return ''
147
+ return ' ' .join (pylint_args )
148
+
149
+
134
150
@hookimpl
135
- def pyls_lint (document , is_saved ):
136
- return PylintLinter .lint (document , is_saved )
151
+ def pyls_lint (config , document , is_saved ):
152
+ settings = config .plugin_settings ('pylint' )
153
+ log .debug ("Got pylint settings: %s" , settings )
154
+ flags = _build_pylint_flags (settings )
155
+ return PylintLinter .lint (document , is_saved , flags = flags )
0 commit comments