|
1 | 1 | # Copyright 2017 Mikael Knutsson |
| 2 | +import re |
2 | 3 | from mypy import api as mypy_api |
3 | 4 | from pyls import hookimpl |
4 | 5 |
|
| 6 | +line_pattern = r"(.*):(\d+):(\d+): (\w+): (.*)" |
5 | 7 |
|
6 | | -@hookimpl |
7 | | -def pyls_lint(document): |
8 | | - args = ('--ignore-missing-imports', |
9 | | - '--incremental', |
10 | | - '--show-column-numbers', |
11 | | - '--command', document.source) |
12 | 8 |
|
13 | | - report, errors, _ = mypy_api.run(args) |
14 | | - diagnostics = [] |
15 | | - for line in report.splitlines(): |
16 | | - split = line.split(':', 4) |
17 | | - if len(split) == 5: |
18 | | - _, lineno, offset, severity, msg = split |
19 | | - else: |
20 | | - _, lineno, severity, msg = split |
21 | | - offset = 0 |
| 9 | +def parse_line(line): |
| 10 | + result = re.match(line_pattern, line) |
| 11 | + if result: |
| 12 | + _, lineno, offset, severity, msg = result.groups() |
22 | 13 | lineno = int(lineno) |
23 | 14 | offset = int(offset) |
24 | 15 | errno = 2 |
25 | | - if severity.strip() == 'error': |
| 16 | + if severity == 'error': |
26 | 17 | errno = 1 |
27 | | - diagnostics.append({ |
| 18 | + return { |
28 | 19 | 'source': 'mypy', |
29 | 20 | 'range': { |
30 | 21 | 'start': {'line': lineno - 1, 'character': offset}, |
31 | 22 | # There may be a better solution, but mypy does not provide end |
32 | 23 | 'end': {'line': lineno - 1, 'character': offset + 1} |
33 | 24 | }, |
34 | | - 'message': msg.strip(), |
| 25 | + 'message': msg, |
35 | 26 | 'severity': errno |
36 | | - }) |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | +@hookimpl |
| 31 | +def pyls_lint(document): |
| 32 | + args = ('--ignore-missing-imports', |
| 33 | + '--incremental', |
| 34 | + '--show-column-numbers', |
| 35 | + '--command', document.source) |
| 36 | + |
| 37 | + report, errors, _ = mypy_api.run(args) |
| 38 | + |
| 39 | + diagnostics = [] |
| 40 | + for line in report.splitlines(): |
| 41 | + diag = parse_line(line) |
| 42 | + if diag: |
| 43 | + diagnostics.append(diag) |
37 | 44 |
|
38 | 45 | return diagnostics |
0 commit comments