This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlinter.py
81 lines (68 loc) · 2.82 KB
/
linter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Alec Thomas
# Copyright (c) 2014 Alec Thomas
#
# License: MIT
#
"""This module exports the Gometalinter plugin class."""
import os
import shlex
import tempfile
from SublimeLinter.lint import Linter, highlight, util
from SublimeLinter.lint.persist import settings
class Gometalinter(Linter):
"""Provides an interface to gometalinter."""
cmd = 'gometalinter --fast .'
regex = r'(?:[^:]+):(?P<line>\d+):(?P<col>\d+)?:(?:(?P<warning>warning)|(?P<error>error)):\s*(?P<message>.*)'
error_stream = util.STREAM_BOTH
default_type = highlight.ERROR
defaults = {
'selector': 'source.go'
}
def run(self, cmd, code):
if settings.get('lint_mode') == 'background':
return self._live_lint(cmd, code)
else:
return self._in_place_lint(cmd)
def _live_lint(self, cmd, code):
dir = os.path.dirname(self.filename)
if not dir:
print('gometalinter: skipped linting of unsaved file')
return
filename = os.path.basename(self.filename)
cmd = cmd + ['-I', '^'+filename]
print('gometalinter: live linting {} in {}: {}'.format(filename, dir, ' '.join(map(shlex.quote, cmd))))
files = [f for f in os.listdir(dir) if f.endswith('.go')]
if len(files) > 40:
print("gometalinter: too many files ({}), live linting skipped".format(len(files)))
return ''
return self.tmpdir(cmd, dir, files, self.filename, code)
def _in_place_lint(self, cmd):
dir = os.path.dirname(self.filename)
if not dir:
print('gometalinter: skipped linting of unsaved file')
return
filename = os.path.basename(self.filename)
cmd = cmd + ['-I', '^'+filename]
print('gometalinter: in-place linting {}: {}'.format(filename, ' '.join(map(shlex.quote, cmd))))
out = self.communicate(cmd)
return out or ''
def tmpdir(self, cmd, dir, files, filename, code):
"""Run an external executable using a temp dir filled with files and return its output."""
with tempfile.TemporaryDirectory(dir=dir, prefix=".gometalinter-") as tmpdir:
for f in files:
target = os.path.join(tmpdir, f)
f = os.path.join(dir, f)
if os.path.basename(target) == os.path.basename(filename):
# source file hasn't been saved since change, so update it from our live buffer
with open(target, 'wb') as f:
if isinstance(code, str):
code = code.encode('utf8')
f.write(code)
else:
os.link(f, target)
out = self.communicate(cmd)
return out or ''