Skip to content

Commit 8778b93

Browse files
authored
Automatically Cull Empty Panes When Traveling Away (#150)
* Added option to automatically cull empty panes when moving away from them (configured via "destroy_empty_panes", defaults to False) * simplified has_content logic into in-place check for any open views * update `window.get_layout()` to newer `window.layout()`
1 parent ca87f26 commit 8778b93

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Origami.sublime-settings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
"saved_layouts": [],
1414

1515
// Create a new pane when switching in a direction without one
16-
"create_new_pane_if_necessary": true
16+
"create_new_pane_if_necessary": true,
17+
18+
// Destroy unused panes when switching away from them
19+
"destroy_empty_panes": false,
1720
}

origami.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ class PaneCommand(sublime_plugin.WindowCommand):
7676

7777
def get_layout(self):
7878
layout = self.window.layout()
79-
cells = layout["cells"]
8079
rows = layout["rows"]
8180
cols = layout["cols"]
81+
cells = layout["cells"]
8282
return rows, cols, cells
8383

8484
def get_cells(self):
@@ -113,16 +113,18 @@ def duplicated_views(self, original_group, duplicating_group):
113113
potential_dupe_views = self.window.views_in_group(duplicating_group)
114114
return [pd for pd in potential_dupe_views if pd.buffer_id() in original_buffers]
115115

116-
def travel_to_pane(self, direction, create_new_if_necessary=False):
116+
def travel_to_pane(self, direction, create_new_if_necessary=False, destroy_old_if_empty=False):
117117
adjacent_cell = self.adjacent_cell(direction)
118118
if adjacent_cell:
119119
cells = self.get_cells()
120120
new_group_index = cells.index(adjacent_cell)
121121
self.window.focus_group(new_group_index)
122+
if destroy_old_if_empty:
123+
self.destroy_pane(opposite_direction(direction), True)
122124
elif create_new_if_necessary:
123-
self.create_pane(direction, True)
125+
self.create_pane(direction, True, destroy_old_if_empty)
124126

125-
def carry_file_to_pane(self, direction, create_new_if_necessary=False):
127+
def carry_file_to_pane(self, direction, create_new_if_necessary=False, destroy_old_if_empty=False):
126128
view = self.window.active_view()
127129
if view is None:
128130
# If we're in an empty group, there's no active view
@@ -135,6 +137,9 @@ def carry_file_to_pane(self, direction, create_new_if_necessary=False):
135137
window.set_view_index(view, active_group, len(views_in_group))
136138
sublime.set_timeout(lambda: window.focus_view(view))
137139

140+
if destroy_old_if_empty:
141+
self.destroy_pane(opposite_direction(direction), True)
142+
138143
def clone_file_to_pane(self, direction, create_new_if_necessary=False):
139144
window = self.window
140145
view = window.active_view()
@@ -374,7 +379,7 @@ def toggle_zoom(self, fraction):
374379
else:
375380
self.unzoom_pane()
376381

377-
def create_pane(self, direction, give_focus=False):
382+
def create_pane(self, direction, give_focus=False, destroy_old_if_empty=False):
378383
window = self.window
379384
rows, cols, cells = self.get_layout()
380385
current_group = window.active_group()
@@ -407,7 +412,7 @@ def create_pane(self, direction, give_focus=False):
407412
window.set_layout(layout)
408413

409414
if give_focus:
410-
self.travel_to_pane(direction)
415+
self.travel_to_pane(direction, False, destroy_old_if_empty)
411416

412417
def destroy_current_pane(self):
413418
# Out of the four adjacent panes, one was split to create this pane.
@@ -431,7 +436,7 @@ def destroy_current_pane(self):
431436
self.travel_to_pane(target_dir)
432437
self.destroy_pane(opposite_direction(target_dir))
433438

434-
def destroy_pane(self, direction):
439+
def destroy_pane(self, direction, only_on_empty=False):
435440
if direction == "self":
436441
self.destroy_current_pane()
437442
return
@@ -450,6 +455,10 @@ def destroy_pane(self, direction):
450455
if cell_to_remove:
451456
active_view = window.active_view()
452457
group_to_remove = cells.index(cell_to_remove)
458+
has_content = len(window.sheets_in_group(group_to_remove)) > 0
459+
if only_on_empty and has_content:
460+
return
461+
453462
dupe_views = self.duplicated_views(current_group, group_to_remove)
454463
for d in dupe_views:
455464
window.focus_view(d)
@@ -502,17 +511,21 @@ def pull_file_from_pane(self, direction):
502511

503512

504513
class TravelToPaneCommand(PaneCommand, WithSettings):
505-
def run(self, direction, create_new_if_necessary=None):
514+
def run(self, direction, create_new_if_necessary=None, destroy_old_if_empty=None):
506515
if create_new_if_necessary is None:
507516
create_new_if_necessary = self.settings().get('create_new_pane_if_necessary')
508-
self.travel_to_pane(direction, create_new_if_necessary)
517+
if destroy_old_if_empty is None:
518+
destroy_old_if_empty = self.settings().get('destroy_empty_panes')
519+
self.travel_to_pane(direction, create_new_if_necessary, destroy_old_if_empty)
509520

510521

511522
class CarryFileToPaneCommand(PaneCommand, WithSettings):
512-
def run(self, direction, create_new_if_necessary=None):
523+
def run(self, direction, create_new_if_necessary=None, destroy_old_if_empty=None):
513524
if create_new_if_necessary is None:
514525
create_new_if_necessary = self.settings().get('create_new_pane_if_necessary')
515-
self.carry_file_to_pane(direction, create_new_if_necessary)
526+
if destroy_old_if_empty is None:
527+
destroy_old_if_empty = self.settings().get('destroy_empty_panes')
528+
self.carry_file_to_pane(direction, create_new_if_necessary, destroy_old_if_empty)
516529

517530

518531
class CloneFileToPaneCommand(PaneCommand, WithSettings):
@@ -716,7 +729,7 @@ def __init__(self, window):
716729
super(NewWindowWithCurrentLayoutCommand, self).__init__(window)
717730

718731
def run(self):
719-
layout = self.window.get_layout()
732+
layout = self.window.layout()
720733
self.window.run_command("new_window")
721734
new_window = sublime.active_window()
722735
new_window.set_layout(layout)

0 commit comments

Comments
 (0)