22"""Linter plugin for pylint."""
33import collections
44import json
5+ import logging
56import sys
67
78from pylint .epylint import py_run
89from pyls import hookimpl , lsp
910
1011
12+ log = logging .getLogger (__name__ )
13+
14+
1115class PylintLinter (object ):
1216 last_diags = collections .defaultdict (list )
1317
@@ -55,14 +59,21 @@ def lint(cls, document, is_saved, flags=''):
5559 path = document .path
5660 if sys .platform .startswith ('win' ):
5761 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 )
6173
6274 # pylint prints nothing rather than [] when there are no diagnostics.
6375 # 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 ():
6677 cls .last_diags [document .path ] = []
6778 return []
6879
@@ -88,24 +99,21 @@ def lint(cls, document, is_saved, flags=''):
8899 # * refactor
89100 # * warning
90101 diagnostics = []
91- for diag in json .loads (json_str ):
102+ for diag in json .loads (json_out ):
92103 # pylint lines index from 1, pyls lines index from 0
93104 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
100105
101106 err_range = {
102107 'start' : {
103108 'line' : line ,
104- 'character' : col ,
109+ # Index columns start from 0
110+ 'character' : diag ['column' ],
105111 },
106112 'end' : {
107113 '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 ,
109117 },
110118 }
111119
@@ -131,6 +139,17 @@ def lint(cls, document, is_saved, flags=''):
131139 return diagnostics
132140
133141
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+
134150@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