-
Notifications
You must be signed in to change notification settings - Fork 2
/
expand_group.py
200 lines (152 loc) · 6.16 KB
/
expand_group.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
"""
ExpandGroup v0.1
by Evan Liomain
https://github.com/iamjessu/sublime-SplitScreen-Resizer
A fork of:
SplitScreen-Resizer v3.0.0
by Jesus Leon
https://github.com/iamjessu/sublime-SplitScreen-Resizer
"""
import sublime, sublime_plugin
import logging
def getLogger():
logging.basicConfig(format='%(message)s')
logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)
return logger
def getLayoutStructure(window):
layout = window.get_layout()
nbRows = len(layout["rows"]) - 1
nbCols = len(layout["cols"]) - 1
nbCells = len(layout["cells"])
if 1 == nbCells: return 'single'
if 2 == nbCells:
if 2 == nbCols and 1 == nbRows: return '2cols'
if 1 == nbCols and 2 == nbRows: return '2rows'
if 3 == nbCells:
if 3 == nbCols and 1 == nbRows: return '3cols'
if 1 == nbCols and 3 == nbRows: return '3rows'
# TODO: other cases:
# 2 rows, 2 cols on top 1 on bottom
# 2 rows, 1 cols on top 2 on bottom
# 2 cols, 2 rows on left 1 on right
# 2 cols, 1 cols on left 2 on right
if 4 == nbCells:
if 2 == nbCols and 2 == nbRows: return '4grid'
if 4 == nbCols and 0 == nbRows: return '4cols'
if 0 == nbCols and 4 == nbRows: return '4rows'
# TODO: other cases:
return 'unknown'
class PanelChangedCommand(sublime_plugin.EventListener):
last_work_group = None
settings = None
def on_activated(self, view):
# Load settings.
win = view.window()
if self.settings == None:
self.settings = sublime.load_settings("ExpandGroup.sublime-settings")
# If mouse focus disabled. Exit.
if self.settings.get('resize_on_focus') == False: return 0
# If theres only one group in current window. Do nothing.
if win.num_groups() == 1: return 0
# Current active group.
current_active_group = win.active_group()
# Working group not changed. Do nothing.
if self.last_work_group == current_active_group: return 0
# Update last work group.
self.last_work_group = current_active_group
# By default we show left side.
# Note the extra parameter ignore_focus_on_resize.
# It prevents an infinite loop. A short circuit! :O
args = {"ignore_focus_on_resize":True}
win.run_command("expand_group", args)
class GoToGroupCommand(sublime_plugin.WindowCommand):
settings = None
def run(self, direction, withCurrentFile=False):
win = self.window
num = win.num_groups()
act = win.active_group()
group = 0
action = "focus_group"
structure = getLayoutStructure(win)
logger = getLogger()
#logger.info('GoToGroupCommand===================================')
#logger.info('structure: %s', structure)
#logger.info('num : %d', num)
#logger.info('act : %d', act)
if withCurrentFile: action = "move_to_group"
if '2cols' != structure and '2rows' != structure and '4grid' != structure:
return 0
if direction == 'left' : group = act - 1
if direction == 'right' : group = act + 1
if '2cols' == structure or '4grid' == structure:
if direction == 'up' : group = act - 2
if direction == 'down' : group = act + 2
if '2rows' == structure:
if direction == 'up' : group = act - 1
if direction == 'down' : group = act + 1
#logger.info('group : %d', group)
#logger.info('action: %s', action)
win.run_command(action, {"group": group})
#logger.info('===================================================')
class ExpandGroupCommand(sublime_plugin.WindowCommand):
settings = None
def run(self, ignore_focus_on_resize=False):
# Load settings
if self.settings == None:
self.settings = sublime.load_settings("ExpandGroup.sublime-settings")
win = self.window
num = win.num_groups()
act = win.active_group()
ratioConf = self.settings.get('ratio')
ratio = 0.3
middle = 0.5
structure = getLayoutStructure(win)
logger = getLogger()
#logger.info('ExpandGroupCommand=================================')
#logger.info('structure: %s', structure)
#logger.info('act : %d', act)
#logger.info('ratio : %f', ratio)
if ratioConf != None: ratio = ratioConf
if '2cols' != structure and '2rows' != structure and '4grid' != structure:
return 0
if '2cols' == structure:
if act == 0: colSep = middle + ratio
if act == 1: colSep = middle - ratio
#logger.info('colSep: %f', colSep)
self.window.run_command('set_layout', {
"cols" : [0, colSep, 1],
"rows" : [0, 1],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
})
if '2rows' == structure:
if act == 0: rowSep = middle + ratio
if act == 1: rowSep = middle - ratio
#logger.info('rowSep: %f', rowSep)
self.window.run_command('set_layout', {
"cols" : [0, 1],
"rows" : [0, rowSep, 1],
"cells": [[0, 0, 1, 1], [0, 1, 1, 2]]
})
if '4grid' == structure:
if act == 0:
colSep = middle + ratio
rowSep = middle + ratio
if act == 1:
colSep = middle - ratio
rowSep = middle + ratio
if act == 2:
colSep = middle + ratio
rowSep = middle - ratio
if act == 3:
colSep = middle - ratio
rowSep = middle - ratio
#logger.info('colSep: %f', colSep)
#logger.info('rowSep: %f', rowSep)
self.window.run_command('set_layout', {
"cols" : [0, colSep, 1],
"rows" : [0, rowSep, 1],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1],
[0, 1, 1, 2], [1, 1, 2, 2]]
})
#logger.info('===================================================')