Repository: palantir/python-language-server
Pull request #92 (2017-08-10 14:30:57)

Clean up some diagnostics code


Patch:
diff --git a/pyls/lsp.py b/pyls/lsp.py
index fc2d39c..728b104 100644
--- a/pyls/lsp.py
+++ b/pyls/lsp.py
@@ -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
diff --git a/pyls/plugins/pycodestyle_lint.py b/pyls/plugins/pycodestyle_lint.py
index 9f9b751..efe7d41 100644
--- a/pyls/plugins/pycodestyle_lint.py
+++ b/pyls/plugins/pycodestyle_lint.py
@@ -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__)
 
@@ -10,7 +10,7 @@ CONFIG_FILES = ['tox.ini', 'pep8.cfg', 'setup.cfg', 'pycodestyle.cfg']
 
 
 @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)
@@ -56,24 +56,18 @@ class PyCodeStyleDiagnosticReport(pycodestyle.BaseReport):
             },
         }
         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
diff --git a/pyls/plugins/pyflakes_lint.py b/pyls/plugins/pyflakes_lint.py
index 2d16244..d521ecb 100644
--- a/pyls/plugins/pyflakes_lint.py
+++ b/pyls/plugins/pyflakes_lint.py
@@ -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
@@ -39,5 +39,6 @@ class PyflakesDiagnosticReport(object):
         self.diagnostics.append({
             'source': 'pyflakes',
             'range': range,
-            'message': message.message % message.message_args
+            'message': message.message % message.message_args,
+            'severity': lsp.DiagnosticSeverity.Warning
         })


Test patch:
diff --git a/test/plugins/test_lint.py b/test/plugins/test_lint.py
index eb8ff6a..9d5aa9a 100644
--- a/test/plugins/test_lint.py
+++ b/test/plugins/test_lint.py
@@ -21,9 +21,9 @@ DOC_SYNTAX_ERR = """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])
 
@@ -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 = {
@@ -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))