-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprefer-standard.py
105 lines (81 loc) · 3.52 KB
/
prefer-standard.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sys
import os
import sublime_plugin
class StandardRBPreferStandardListener(sublime_plugin.EventListener):
def on_activated_async(self, view):
self.view = view
self.rubocop_installed = 'SublimeLinter-rubocop' in sys.modules
key = 'SublimeLinter.linters.rubocop.disable'
self.rubocop_disabled = view.settings().get(key)
key = 'SublimeLinter.linters.standardrb.disable'
self.standard_disabled = view.settings().get(key)
key = 'SublimeLinter.linters.standardrb.prefer_standard'
self.should_prefer_standard = view.settings().get(key, False)
# Don't proceed if we shouldn't be selecting
# a linter in the first place
if not self.should_select_linter_to_activate():
return
the_linter_we_should_use = self.the_linter_we_should_use()
if the_linter_we_should_use == 'standard':
key = 'SublimeLinter.linters.standardrb.disable'
view.settings().set(key, False)
key = 'SublimeLinter.linters.rubocop.disable'
view.settings().set(key, True)
elif the_linter_we_should_use == 'rubocop':
key = 'SublimeLinter.linters.standardrb.disable'
view.settings().set(key, True)
key = 'SublimeLinter.linters.rubocop.disable'
view.settings().set(key, False)
else:
key = 'SublimeLinter.linters.standardrb.disable'
view.settings().set(key, False)
key = 'SublimeLinter.linters.rubocop.disable'
view.settings().set(key, True)
def should_select_linter_to_activate(self):
# If rubocop is not installed we assume the user will set their
# settings as desired for using standardrb. We can return without
# doing any decision making
if not self.rubocop_installed:
return False
# If rubocop or standardrb is explicitly enabled / disabled we respect the user's
# preference and leave things alone
if (self.rubocop_disabled is not None
or self.standard_disabled is not None):
return False
# If the user has not said that they want us to prefer standardrb then
# we assume they want to switch between the linters manually
if not self.should_prefer_standard:
return False
return True
def the_linter_we_should_use(self):
closest_config = self.get_closest_config_file_path()
if closest_config == 'standard':
return 'standard'
elif closest_config == 'rubocop':
return 'rubocop'
else:
return None
def get_closest_config_file_path(self):
project_folders = self.view.window().folders()
if not project_folders:
return None
project_folder = project_folders[0]
dirs_to_check = []
current_subpath = []
path_segments = project_folder.split("/")
path_segments.remove("")
for part in path_segments:
current_subpath.append(part)
d = os.path.normpath("/" + "/".join(current_subpath))
dirs_to_check.append(d)
dirs_to_check.reverse()
for dir in dirs_to_check:
print(dir)
for dir in dirs_to_check:
standard_config = dir + "/.standard.yml"
rubocop_config = dir + "/.rubocop.yml"
if os.path.exists(standard_config):
return "standard"
if os.path.exists(rubocop_config):
return "rubocop"
return None