Skip to content

Commit c02829a

Browse files
authored
Clean up some diagnostics code (#92)
* Clean up some diagnostics code * Fix tests
1 parent 75793ff commit c02829a

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

pyls/lsp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class CompletionItemKind(object):
2626
Reference = 18
2727

2828

29+
class DiagnosticSeverity(object):
30+
Error = 1
31+
Warning = 2
32+
Information = 3
33+
Hint = 4
34+
35+
2936
class MessageType(object):
3037
Error = 1
3138
Warning = 2

pyls/plugins/pycodestyle_lint.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import logging
33
import pycodestyle
4-
from pyls import config as pyls_config, hookimpl
4+
from pyls import config as pyls_config, hookimpl, lsp
55

66
log = logging.getLogger(__name__)
77

@@ -10,7 +10,7 @@
1010

1111

1212
@hookimpl
13-
def pyls_lint(config, workspace, document):
13+
def pyls_lint(config, document):
1414
# Read config from all over the place
1515
config_files = config.find_parents(document.path, CONFIG_FILES)
1616
pycodestyle_conf = pyls_config.build_config('pycodestyle', config_files)
@@ -56,24 +56,18 @@ def error(self, lineno, offset, text, check):
5656
},
5757
}
5858
code, _message = text.split(" ", 1)
59-
severity = self._get_severity(code)
6059

6160
self.diagnostics.append({
6261
'source': 'pycodestyle',
6362
'range': range,
6463
'message': text,
6564
'code': code,
66-
'severity': severity
65+
'severity': _get_severity(code)
6766
})
6867

69-
def _get_severity(self, code):
70-
""" VSCode Severity Mapping
71-
ERROR: 1,
72-
WARNING: 2,
73-
INFO: 3,
74-
HINT: 4
75-
"""
76-
if code[0] == 'E':
77-
return 1
78-
elif code[0] == 'W':
79-
return 2
68+
69+
def _get_severity(code):
70+
if code[0] == 'E':
71+
return lsp.DiagnosticSeverity.Error
72+
elif code[0] == 'W':
73+
return lsp.DiagnosticSeverity.Warning

pyls/plugins/pyflakes_lint.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
from pyflakes import api as pyflakes_api
3-
from pyls import hookimpl
3+
from pyls import hookimpl, lsp
44

55

66
@hookimpl
@@ -39,5 +39,6 @@ def flake(self, message):
3939
self.diagnostics.append({
4040
'source': 'pyflakes',
4141
'range': range,
42-
'message': message.message % message.message_args
42+
'message': message.message % message.message_args,
43+
'severity': lsp.DiagnosticSeverity.Warning
4344
})

test/plugins/test_lint.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def hello():
2121
"""
2222

2323

24-
def test_pycodestyle(config, workspace):
24+
def test_pycodestyle(config):
2525
doc = Document(DOC_URI, DOC)
26-
diags = pycodestyle_lint.pyls_lint(config, workspace, doc)
26+
diags = pycodestyle_lint.pyls_lint(config, doc)
2727

2828
assert all([d['source'] == 'pycodestyle' for d in diags])
2929

@@ -59,7 +59,7 @@ def test_pycodestyle_config():
5959
config = Config(workspace.root_uri, {})
6060

6161
# Make sure we get a warning for 'indentation contains tabs'
62-
diags = pycodestyle_lint.pyls_lint(config, workspace, doc)
62+
diags = pycodestyle_lint.pyls_lint(config, doc)
6363
assert [d for d in diags if d['code'] == 'W191']
6464

6565
content = {
@@ -74,7 +74,7 @@ def test_pycodestyle_config():
7474
f.write(content)
7575

7676
# And make sure we don't get any warnings
77-
diags = pycodestyle_lint.pyls_lint(config, workspace, doc)
77+
diags = pycodestyle_lint.pyls_lint(config, doc)
7878
assert len([d for d in diags if d['code'] == 'W191']) == 0 if working else 1
7979

8080
os.unlink(os.path.join(tmp, conf_file))

0 commit comments

Comments
 (0)