forked from mikefowler/simple-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_clone.py
206 lines (165 loc) · 7.38 KB
/
simple_clone.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import sublime
import sublime_plugin
import re
# Clone the current view
class SimpleCloneCommand(sublime_plugin.WindowCommand):
def run(self, location): # --Matt Bolt: Renamed direction variable to the more symantically appropriate name 'location' due to additional options
# activeGroup = self.window.active_group() --Matt Bolt: Commented out unused variable
layout = self.window.get_layout()
rows = len(layout['rows']) - 1
cols = len(layout['cols']) - 1
# Clone to new window if requested
if location == 'new_window':
# Create new window
self.window.run_command("new_window")
# Open the current file in the new window
sublime.windows()[-1:][0].open_file(self.window.active_view().file_name())
# Handle cloning to view
else:
# Start by cloning the file...
self.window.run_command('clone_file')
# Now modify the layout if necessary...
if location == 'down':
active = self.window.get_view_index(self.window.active_view())[0]
total = self.window.num_groups()
if rows > 1:
if active == total - 1:
self.window.set_layout(createLayout(rows + 1, cols))
newGroup = active + cols
else:
total = self.window.num_groups()
if active + cols < total:
newGroup = active + cols
elif active - cols >= 0:
newGroup = active - cols
else:
newGroup = active
else:
self.window.set_layout(createLayout(rows + 1, cols))
total = self.window.num_groups()
newGroup = active + cols
elif location == 'right':
active = self.window.get_view_index(self.window.active_view())[0]
total = self.window.num_groups()
if cols > 1:
if active == total - 1:
self.window.set_layout(createLayout(rows, cols + 1))
newGroup = active + 1
else:
total = self.window.num_groups()
if active + 1 < total:
newGroup = active + 1
elif active - 1 >= 0:
newGroup = active - 1
else:
newGroup = active
else:
self.window.set_layout(createLayout(rows, cols + 1))
newGroup = active + 1
# And now move the file to the appropriate group
self.window.run_command('move_to_group', {"group": newGroup})
# Clone the targeted tab context
class SimpleCloneTabContextCommand(sublime_plugin.WindowCommand):
def run(self, location, group, index):
# activeGroup = self.window.active_group() --Matt Bolt: Commented out unused variable
layout = self.window.get_layout()
rows = len(layout['rows']) - 1
cols = len(layout['cols']) - 1
# Clone to new window if requested
if location == 'new_window':
# Create new window
self.window.run_command("new_window")
# Open the targeted file in the new window
sublime.windows()[-1:][0].open_file(self.window.views_in_group(group)[index].file_name())
# Handle cloning to view
else:
# Store currently active view
activeView = self.window.active_view()
# Show targeted view
self.window.focus_view(self.window.views_in_group(group)[index])
# Start by cloning the file...
self.window.run_command('clone_file')
# Now modify the layout if necessary...
if location == 'down':
active = self.window.get_view_index(self.window.active_view())[0]
total = self.window.num_groups()
if rows > 1:
if active == total - 1:
self.window.set_layout(createLayout(rows + 1, cols))
newGroup = active + cols
else:
total = self.window.num_groups()
if active + cols < total:
newGroup = active + cols
elif active - cols >= 0:
newGroup = active - cols
else:
newGroup = active
else:
self.window.set_layout(createLayout(rows + 1, cols))
total = self.window.num_groups()
newGroup = active + cols
elif location == 'right':
active = self.window.get_view_index(self.window.active_view())[0]
total = self.window.num_groups()
if cols > 1:
if active == total - 1:
self.window.set_layout(createLayout(rows, cols + 1))
newGroup = active + 1
else:
total = self.window.num_groups()
if active + 1 < total:
newGroup = active + 1
elif active - 1 >= 0:
newGroup = active - 1
else:
newGroup = active
else:
self.window.set_layout(createLayout(rows, cols + 1))
newGroup = active + 1
# And now move the file to the appropriate group
self.window.run_command('move_to_group', {"group": newGroup})
# Restore active view
self.window.focus_view(activeView)
def createLayout(rows, cols):
numCells = rows * cols
rowIncrement = 1.0 / rows
colIncrement = 1.0 / cols
# Add initial layout arrays
layoutRows = [0]
layoutCols = [0]
layoutCells = [[0] * 4] * numCells
# Create rows array
if rows > 1:
for x in xrange(1, rows):
increment = rowIncrement * x
layoutRows.append(increment)
layoutRows.append(1.0)
# Create columns arraydown
if cols > 1:
for y in xrange(1, cols):
increment = colIncrement * y
layoutCols.append(increment)
layoutCols.append(1.0)
# Create cell definitions (a,b)
counter = 0
for a in range(rows):
for b in range(cols):
layoutCells[counter] = [b, a, b + 1, a + 1]
counter += 1
return {'cells': layoutCells, 'rows': layoutRows, 'cols': layoutCols}
# Used to disable keymaps
def should_perform_clone(location):
disabled_keymaps = settings.get('disabled_keymaps', '')
if not disabled_keymaps:
return True
if disabled_keymaps == 'all':
return False
return location not in re.split(r'\s*,\s*', disabled_keymaps.strip())
class ActionContextHandler(sublime_plugin.EventListener):
def on_query_context(self, view, key, op, operand, match_all):
if not key.startswith('simpleclone_keymap_enabled.'):
return None
prefix, location = key.split('.')
return should_perform_clone(location)
settings = sublime.load_settings('SimpleClone.sublime-settings')