-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
open_env.py
156 lines (125 loc) · 6.13 KB
/
open_env.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import sublime, sublime_plugin
import os
# Note: This plugin uses 'Verbose' plugin available in 'Package Control' to log some messages for debug purpose but it works fine without.
PluginName = 'Open-Include'
def verbose(**kwargs):
kwargs.update({'plugin_name': PluginName})
sublime.run_command("verbose", kwargs)
class Prefs:
@staticmethod
def load():
print('Load settings: ' + PluginName)
settings = sublime.load_settings(PluginName + '.sublime-settings')
os_specific_settings = {}
if sublime.platform() == 'windows':
os_specific_settings = sublime.load_settings(PluginName + ' (Windows).sublime-settings')
elif sublime.platform() == 'osx':
os_specific_settings = sublime.load_settings(PluginName + ' (OSX).sublime-settings')
else:
os_specific_settings = sublime.load_settings(PluginName + ' (Linux).sublime-settings')
Prefs.environment = os_specific_settings.get('environment', settings.get('environment', []))
Prefs.expand_alias = os_specific_settings.get('expand_alias', settings.get('expand_alias', True))
@staticmethod
def show():
verbose(log="############################################################")
for env in Prefs.environment:
for key, values in env.items():
verbose(log=key + ": " + ';'.join(values))
verbose(log="############################################################")
def plugin_loaded():
Prefs.load()
### OpenFileFromEnv ###
# Find root directory
# Get base directory with name
# foreach env test if file exit
class OpenFileFromEnvCommand(sublime_plugin.TextCommand):
# Set by is_enabled()
initial_env_name = ''
base_name = ''
# List of existing files in other environments
env_files = []
def run(self, edit):
verbose(log="run()")
verbose(log="initial_env_name: " + self.initial_env_name)
verbose(log="base_name: " + self.base_name)
if len(self.base_name) > 0:
# Create a list of files which exist in other environment
self.env_files = []
for env in Prefs.environment:
for env_name, root_alias in env.items():
# Bypass initial environment
if env_name == self.initial_env_name:
continue
# Loop in path alias of the current environment
available_file_names = []
for root in root_alias:
env_file_name = os.path.join(os.path.expandvars(root), self.base_name)
state = ' '
if os.path.exists(env_file_name):
state = 'X'
if Prefs.expand_alias:
self.env_files.append([env_name, env_file_name])
else:
available_file_names.append(env_file_name)
verbose(log='[%s] %15s %s' % (state, env_name, env_file_name))
if len(available_file_names) > 0:
# available_file_names used only with expand_alias = False
current_id = self.view.id()
is_file_opened = False
# Find the first file of the environment which is already opened in st
for v in self.view.window().views():
if v.id() == current_id or v.file_name() is None:
continue
for file_name in available_file_names:
if file_name.lower() == v.file_name().lower():
self.env_files.append([env_name, file_name])
is_file_opened = True
break
if is_file_opened:
break
# Or choose the file of the environment of the main path
if not is_file_opened:
self.env_files.append([env_name, available_file_names[0]])
if len(self.env_files) > 0:
self.view.window().show_quick_panel(self.env_files, self.quick_panel_done)
else:
sublime.status_message("No file found in other environments")
def quick_panel_done(self, index):
if index > -1:
# Open selected file in an another environment
self.view.window().open_file(self.env_files[index][1])
def is_filename_part_of_env(self, file_name, root_alias):
for root in root_alias:
# Remove trailing os.sep
root = os.path.expandvars(root)
root = os.path.normpath(root).lower()
if file_name.startswith(root):
base_name = file_name.replace(root.lower(), "")
if base_name[0] == os.sep:
# Get back the original case
file_name = self.view.file_name()
# Remove first os.sep character and get base name
self.base_name = file_name[len(file_name)-len(base_name)+1:]
return True
return False
# Return True if the file is part of an environment
def is_enabled(self):
Prefs.show()
verbose(log="is_enabled()")
file_name = self.view.file_name()
self.initial_env_name = ''
base_name = ''
if file_name is not None and len(file_name) > 0:
file_name = file_name.lower()
verbose(log="file_name: " + file_name)
# Loop into registered environment
for env in Prefs.environment:
for env_name, root_alias in env.items():
# Test if file_name is part of an environment
if self.is_filename_part_of_env(file_name, root_alias):
self.initial_env_name = env_name
return True
sublime.status_message("The current file is not part of an environment")
return False
if int(sublime.version()) < 3000:
plugin_loaded()