-
Notifications
You must be signed in to change notification settings - Fork 40
/
linter.py
74 lines (61 loc) · 2.75 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
import os
from SublimeLinter.lint import Linter
class RubyLinter(Linter):
def context_sensitive_executable_path(self, cmd):
# The default implementation will look for a user defined `executable`
# setting.
success, executable = super().context_sensitive_executable_path(cmd)
if success:
return True, executable
gem_name = cmd[0] if isinstance(cmd, list) else cmd
if self.settings.get('use_bundle_exec', False):
return True, ['bundle', 'exec', gem_name]
rvm = self.which('rvm-auto-ruby')
if rvm:
return True, [rvm, '-S', gem_name]
return False, None
class Rubocop(RubyLinter):
defaults = {
'selector': 'source.ruby - text.html - text.haml'
}
regex = (
r'^.+?:(?P<line>\d+):(?P<col>\d+): '
r'(:?(?P<warning>[RCW])|(?P<error>[EF])): '
r'(?P<fixable>\[Correctable\] )?'
r'((?P<code>\w+/\w+): )?'
r'(?P<message>.+)$'
)
word_re = r'^((@|@@|\$)?\w+[!?]?)'
def cmd(self):
"""Build command, using STDIN if a file path can be determined."""
command = ['rubocop', '--format', 'emacs', '--display-cop-names']
path = self.filename
if not path:
# File is unsaved, and by default unsaved files use the default
# rubocop config because they do not technically belong to a folder
# that might contain a custom .rubocop.yml. This means the lint
# results may not match the rules for the currently open project.
#
# If the current window has open folders then we can use the
# first open folder as a best-guess for the current projects
# root folder - we can then pretend that this unsaved file is
# inside this root folder, and rubocop will pick up on any
# config file if it does exist:
folders = self.view.window().folders()
if folders:
path = os.path.join(folders[0], 'untitled.rb')
if path:
# With this path we can instead pass the file contents in via STDIN
# and then tell rubocop to use this path (to search for config
# files and to use for matching against configured paths - i.e. for
# inheritance, inclusions and exclusions).
#
# The 'force-exclusion' overrides rubocop's behavior of ignoring
# global excludes when the file path is explicitly provided:
command += ['--force-exclusion', '--stdin', path]
# Ensure the files contents are passed in via STDIN:
self.tempfile_suffix = None
else:
self.tempfile_suffix = 'rb'
command += ['${temp_file}']
return command