forked from SublimeLinter/SublimeLinter-golint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linter.py
64 lines (51 loc) · 2.24 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
#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3
#
# Written by Jon Surrell and Jeremy Jay
# Copyright (c) 2014 Jon Surrell
#
# License: MIT
#
"""This module exports the Golint plugin class."""
from SublimeLinter.lint import Linter, util, highlight, persist
import os
class Golint(Linter):
"""Provides an interface to golint."""
syntax = ('go', 'gosublime-go')
cmd = 'golint'
regex = r'^.+:(?P<line>\d+):(?P<col>\d+):\s+(?P<message>.+)'
tempfile_suffix = 'go'
error_stream = util.STREAM_STDOUT
default_type = highlight.WARNING
def find_gopaths(self):
""" search for potential GOPATHs. """
# collect existing Go path info
goroot = set(os.path.normpath(s) for s in os.environ.get('GOROOT', '').split(os.pathsep))
gopath = set(os.path.normpath(s) for s in os.environ.get('GOPATH', '').split(os.pathsep))
if '.' in gopath:
gopath.remove('.')
gopath = list(gopath)
# search for potential GOPATHs upstream from filename
# (reversed to ensure deepest path is first searched)
dirparts = os.path.dirname(self.filename).split(os.sep)
for i in range(len(dirparts) - 1, 1, -1):
if dirparts[i].lower() != "src":
continue
p = os.path.normpath(os.sep.join(dirparts[:i]))
if p not in goroot and p not in gopath:
gopath.append(p)
if persist.debug_mode():
persist.printf("{}: {} {}".format(self.name,
os.path.basename(self.filename or '<unsaved>'),
"guessed GOPATH=" + os.pathsep.join(gopath)))
return os.pathsep.join(gopath)
def run(self, cmd, code):
""" transparently add potential GOPATHs before running. """
self.env = {'GOPATH': self.find_gopaths()}
# copy debug output from Linter.run()
if persist.debug_mode():
persist.printf('{}: {} {}'.format(self.name,
os.path.basename(self.filename or '<unsaved>'),
cmd or '<builtin>'))
return self.tmpfile(cmd, code, suffix=self.get_tempfile_suffix())