Skip to content

Commit ee94ea0

Browse files
authored
Merge pull request #1 from danzvara/develop
Add syntax check support
2 parents 1dccd30 + cdad103 commit ee94ea0

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

pyls/plugins/jedi_lint.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pyls import hookimpl, lsp
2+
3+
4+
@hookimpl
5+
def pyls_lint(document):
6+
errors = document.jedi_script().get_syntax_errors()
7+
diagnostics = []
8+
9+
for error in errors:
10+
err_range = {
11+
'start': {
12+
'line': error.line - 1,
13+
'character': error.column,
14+
},
15+
'end': {
16+
'line': error.until_line - 1,
17+
'character': error.until_column,
18+
},
19+
}
20+
diagnostics.append({
21+
'source': 'jedi',
22+
'range': err_range,
23+
'message': error.get_message(),
24+
'severity': lsp.DiagnosticSeverity.Error,
25+
})
26+
return diagnostics

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
'jedi_rename = pyls.plugins.jedi_rename',
9898
'jedi_signature_help = pyls.plugins.signature',
9999
'jedi_symbols = pyls.plugins.symbols',
100+
'jedi_lint = pyls.plugins.jedi_lint',
100101
'mccabe = pyls.plugins.mccabe_lint',
101102
'preload = pyls.plugins.preload_imports',
102103
'pycodestyle = pyls.plugins.pycodestyle_lint',

test/plugins/test_jedi_lint.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from pyls import lsp, uris
2+
from pyls.workspace import Document
3+
from pyls.plugins import jedi_lint
4+
5+
DOC_URI = uris.from_fs_path(__file__)
6+
DOC = """import sys
7+
8+
def hello():
9+
\tpass
10+
11+
import json
12+
"""
13+
14+
DOC_SYNTAX_ERR = """def hello()
15+
pass
16+
"""
17+
18+
DOC_INDENT_ERR = """def hello():
19+
x = 1
20+
pass
21+
"""
22+
23+
DOC_ENCODING = u"""# encoding=utf-8
24+
import sys
25+
"""
26+
27+
28+
def test_jedi_lint(workspace):
29+
doc = Document(DOC_URI, workspace, DOC)
30+
diags = jedi_lint.pyls_lint(doc)
31+
32+
assert not diags
33+
34+
35+
def test_syntax_error_jedi(workspace):
36+
doc = Document(DOC_URI, workspace, DOC_SYNTAX_ERR)
37+
diag = jedi_lint.pyls_lint(doc)[0]
38+
39+
assert diag['message'] == 'SyntaxError: invalid syntax'
40+
assert diag['range']['start'] == {'line': 0, 'character': 11}
41+
assert diag['range']['end'] == {'line': 1, 'character': 0}
42+
assert diag['severity'] == lsp.DiagnosticSeverity.Error
43+
44+
45+
def test_indent_error_jedi(workspace):
46+
doc = Document(DOC_URI, workspace, DOC_INDENT_ERR)
47+
diag = jedi_lint.pyls_lint(doc)[0]
48+
49+
assert diag['message'] == "IndentationError: unindent does not match \
50+
any outer indentation level"
51+
assert diag['range']['start'] == {'line': 2, 'character': 0}
52+
assert diag['range']['end'] == {'line': 2, 'character': 2}
53+
assert diag['severity'] == lsp.DiagnosticSeverity.Error
54+
55+
56+
def test_encoding_jedi(workspace):
57+
doc = Document(DOC_URI, workspace, DOC_ENCODING)
58+
diags = jedi_lint.pyls_lint(doc)
59+
60+
assert not diags

0 commit comments

Comments
 (0)