Skip to content

Commit

Permalink
Clean up some diagnostics code (#92)
Browse files Browse the repository at this point in the history
* Clean up some diagnostics code

* Fix tests
  • Loading branch information
gatesn authored Aug 20, 2017
1 parent 75793ff commit c02829a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
7 changes: 7 additions & 0 deletions pyls/lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class CompletionItemKind(object):
Reference = 18


class DiagnosticSeverity(object):
Error = 1
Warning = 2
Information = 3
Hint = 4


class MessageType(object):
Error = 1
Warning = 2
Expand Down
24 changes: 9 additions & 15 deletions pyls/plugins/pycodestyle_lint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2017 Palantir Technologies, Inc.
import logging
import pycodestyle
from pyls import config as pyls_config, hookimpl
from pyls import config as pyls_config, hookimpl, lsp

log = logging.getLogger(__name__)

Expand All @@ -10,7 +10,7 @@


@hookimpl
def pyls_lint(config, workspace, document):
def pyls_lint(config, document):
# Read config from all over the place
config_files = config.find_parents(document.path, CONFIG_FILES)
pycodestyle_conf = pyls_config.build_config('pycodestyle', config_files)
Expand Down Expand Up @@ -56,24 +56,18 @@ def error(self, lineno, offset, text, check):
},
}
code, _message = text.split(" ", 1)
severity = self._get_severity(code)

self.diagnostics.append({
'source': 'pycodestyle',
'range': range,
'message': text,
'code': code,
'severity': severity
'severity': _get_severity(code)
})

def _get_severity(self, code):
""" VSCode Severity Mapping
ERROR: 1,
WARNING: 2,
INFO: 3,
HINT: 4
"""
if code[0] == 'E':
return 1
elif code[0] == 'W':
return 2

def _get_severity(code):
if code[0] == 'E':
return lsp.DiagnosticSeverity.Error
elif code[0] == 'W':
return lsp.DiagnosticSeverity.Warning
5 changes: 3 additions & 2 deletions pyls/plugins/pyflakes_lint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2017 Palantir Technologies, Inc.
from pyflakes import api as pyflakes_api
from pyls import hookimpl
from pyls import hookimpl, lsp


@hookimpl
Expand Down Expand Up @@ -39,5 +39,6 @@ def flake(self, message):
self.diagnostics.append({
'source': 'pyflakes',
'range': range,
'message': message.message % message.message_args
'message': message.message % message.message_args,
'severity': lsp.DiagnosticSeverity.Warning
})
8 changes: 4 additions & 4 deletions test/plugins/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def hello():
"""


def test_pycodestyle(config, workspace):
def test_pycodestyle(config):
doc = Document(DOC_URI, DOC)
diags = pycodestyle_lint.pyls_lint(config, workspace, doc)
diags = pycodestyle_lint.pyls_lint(config, doc)

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

Expand Down Expand Up @@ -59,7 +59,7 @@ def test_pycodestyle_config():
config = Config(workspace.root_uri, {})

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

content = {
Expand All @@ -74,7 +74,7 @@ def test_pycodestyle_config():
f.write(content)

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

os.unlink(os.path.join(tmp, conf_file))
Expand Down

0 comments on commit c02829a

Please sign in to comment.