From 36da0249b0ba6438416c335c31c6ce16e05e3486 Mon Sep 17 00:00:00 2001 From: Daniel Zvara Date: Sun, 18 Oct 2020 12:33:05 +0200 Subject: [PATCH 1/6] Add jedi lint plugin --- pyls/plugins/jedi_lint.py | 28 ++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 29 insertions(+) create mode 100644 pyls/plugins/jedi_lint.py diff --git a/pyls/plugins/jedi_lint.py b/pyls/plugins/jedi_lint.py new file mode 100644 index 00000000..44bc739c --- /dev/null +++ b/pyls/plugins/jedi_lint.py @@ -0,0 +1,28 @@ +from pyls import hookimpl, lsp + +@hookimpl +def pyls_lint(config, document): + errors = document.jedi_script().get_syntax_errors() + diagnostics = [] + + for error in errors: + err_range = { + 'start': { + 'line': error.line - 1, + # Index columns start from 0 + 'character': error.column, + }, + 'end': { + 'line': error.until_line - 1, + # It's possible that we're linting an empty file. Even an empty + # file might fail linting if it isn't named properly. + 'character': error.until_column, + }, + } + diagnostics.append({ + 'source': 'jedi', + 'range': err_range, + 'message': error.get_message(), + 'severity': lsp.DiagnosticSeverity.Error, + }) + return diagnostics diff --git a/setup.py b/setup.py index 12782990..7a4dc490 100755 --- a/setup.py +++ b/setup.py @@ -97,6 +97,7 @@ 'jedi_rename = pyls.plugins.jedi_rename', 'jedi_signature_help = pyls.plugins.signature', 'jedi_symbols = pyls.plugins.symbols', + 'jedi_lint = pyls.plugins.jedi_lint', 'mccabe = pyls.plugins.mccabe_lint', 'preload = pyls.plugins.preload_imports', 'pycodestyle = pyls.plugins.pycodestyle_lint', From 13876e876ef090bdaf826ed480ca334c1e6d5a79 Mon Sep 17 00:00:00 2001 From: Daniel Zvara Date: Mon, 19 Oct 2020 00:15:33 +0200 Subject: [PATCH 2/6] Fix linting --- pyls/plugins/jedi_lint.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyls/plugins/jedi_lint.py b/pyls/plugins/jedi_lint.py index 44bc739c..50554289 100644 --- a/pyls/plugins/jedi_lint.py +++ b/pyls/plugins/jedi_lint.py @@ -1,17 +1,17 @@ from pyls import hookimpl, lsp + @hookimpl -def pyls_lint(config, document): +def pyls_lint(document): errors = document.jedi_script().get_syntax_errors() diagnostics = [] for error in errors: err_range = { 'start': { - 'line': error.line - 1, - # Index columns start from 0 - 'character': error.column, - }, + 'line': error.line - 1, + 'character': error.column, + }, 'end': { 'line': error.until_line - 1, # It's possible that we're linting an empty file. Even an empty From 590cd100cebeef03e297d381682498010de4850c Mon Sep 17 00:00:00 2001 From: Daniel Zvara Date: Mon, 19 Oct 2020 00:15:42 +0200 Subject: [PATCH 3/6] Add tests --- test/plugins/test_jedi_lint.py | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/plugins/test_jedi_lint.py diff --git a/test/plugins/test_jedi_lint.py b/test/plugins/test_jedi_lint.py new file mode 100644 index 00000000..c2f3cc32 --- /dev/null +++ b/test/plugins/test_jedi_lint.py @@ -0,0 +1,60 @@ +from pyls import lsp, uris +from pyls.workspace import Document +from pyls.plugins import jedi_lint + +DOC_URI = uris.from_fs_path(__file__) +DOC = """import sys + +def hello(): +\tpass + +import json +""" + +DOC_SYNTAX_ERR = """def hello() + pass +""" + +DOC_INDENT_ERR = """def hello(): + x = 1 + pass +""" + +DOC_ENCODING = u"""# encoding=utf-8 +import sys +""" + + +def test_jedi_lint(workspace): + doc = Document(DOC_URI, workspace, DOC) + diags = jedi_lint.pyls_lint(doc) + + assert len(diags) == 0 + + +def test_syntax_error_jedi(workspace): + doc = Document(DOC_URI, workspace, DOC_SYNTAX_ERR) + diag = jedi_lint.pyls_lint(doc)[0] + + assert diag['message'] == 'SyntaxError: invalid syntax' + assert diag['range']['start'] == {'line': 0, 'character': 11} + assert diag['range']['end'] == {'line': 1, 'character': 0} + assert diag['severity'] == lsp.DiagnosticSeverity.Error + + +def test_indent_error_jedi(workspace): + doc = Document(DOC_URI, workspace, DOC_INDENT_ERR) + diag = jedi_lint.pyls_lint(doc)[0] + + assert diag['message'] == "IndentationError: unindent does not match \ +any outer indentation level" + assert diag['range']['start'] == {'line': 2, 'character': 0} + assert diag['range']['end'] == {'line': 2, 'character': 2} + assert diag['severity'] == lsp.DiagnosticSeverity.Error + + +def test_encoding_jedi(workspace): + doc = Document(DOC_URI, workspace, DOC_ENCODING) + diags = jedi_lint.pyls_lint(doc) + + assert len(diags) == 0 From f38197437bdb1d6983563a4cc31473f9b82a0c05 Mon Sep 17 00:00:00 2001 From: Daniel Zvara Date: Mon, 19 Oct 2020 11:22:00 +0200 Subject: [PATCH 4/6] Fix tests --- pyls/plugins/jedi_lint.py | 10 +++++----- test/plugins/test_jedi_lint.py | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pyls/plugins/jedi_lint.py b/pyls/plugins/jedi_lint.py index 50554289..0a5d73f8 100644 --- a/pyls/plugins/jedi_lint.py +++ b/pyls/plugins/jedi_lint.py @@ -20,9 +20,9 @@ def pyls_lint(document): }, } diagnostics.append({ - 'source': 'jedi', - 'range': err_range, - 'message': error.get_message(), - 'severity': lsp.DiagnosticSeverity.Error, - }) + 'source': 'jedi', + 'range': err_range, + 'message': error.get_message(), + 'severity': lsp.DiagnosticSeverity.Error, + }) return diagnostics diff --git a/test/plugins/test_jedi_lint.py b/test/plugins/test_jedi_lint.py index c2f3cc32..db7fee59 100644 --- a/test/plugins/test_jedi_lint.py +++ b/test/plugins/test_jedi_lint.py @@ -29,7 +29,7 @@ def test_jedi_lint(workspace): doc = Document(DOC_URI, workspace, DOC) diags = jedi_lint.pyls_lint(doc) - assert len(diags) == 0 + assert not diags def test_syntax_error_jedi(workspace): @@ -57,4 +57,4 @@ def test_encoding_jedi(workspace): doc = Document(DOC_URI, workspace, DOC_ENCODING) diags = jedi_lint.pyls_lint(doc) - assert len(diags) == 0 + assert not diags From 8858b067c713b72c598db8ab1093fa095a6afbbd Mon Sep 17 00:00:00 2001 From: Daniel Zvara Date: Mon, 19 Oct 2020 12:00:50 +0200 Subject: [PATCH 5/6] Fix pycodestyle errors --- pyls/plugins/jedi_lint.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyls/plugins/jedi_lint.py b/pyls/plugins/jedi_lint.py index 0a5d73f8..e81fe63b 100644 --- a/pyls/plugins/jedi_lint.py +++ b/pyls/plugins/jedi_lint.py @@ -11,14 +11,14 @@ def pyls_lint(document): 'start': { 'line': error.line - 1, 'character': error.column, - }, + }, 'end': { 'line': error.until_line - 1, # It's possible that we're linting an empty file. Even an empty # file might fail linting if it isn't named properly. 'character': error.until_column, - }, - } + }, + } diagnostics.append({ 'source': 'jedi', 'range': err_range, From cdad103c5e525a754ea0d6f41fdbd87b7c6a1ca2 Mon Sep 17 00:00:00 2001 From: Daniel Zvara Date: Mon, 19 Oct 2020 12:23:23 +0200 Subject: [PATCH 6/6] Remove useless comment --- pyls/plugins/jedi_lint.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyls/plugins/jedi_lint.py b/pyls/plugins/jedi_lint.py index e81fe63b..739092cd 100644 --- a/pyls/plugins/jedi_lint.py +++ b/pyls/plugins/jedi_lint.py @@ -14,8 +14,6 @@ def pyls_lint(document): }, 'end': { 'line': error.until_line - 1, - # It's possible that we're linting an empty file. Even an empty - # file might fail linting if it isn't named properly. 'character': error.until_column, }, }