Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Focus file on pane" and "Focus or clone file on pane" commands added #138

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Other contributors:

* David Baumgold (singingwolfboy), <[email protected]>
* Ryan Morrissey (23maverick23), <[email protected]>
* Carlos L. Perez (clperez), <[email protected]>
20 changes: 20 additions & 0 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@
{ "command": "pull_file_from_pane", "args": {"direction": "right"}, "caption": "Right" },
{ "command": "pull_file_from_pane", "args": {"direction": "left"}, "caption": "Left" }
]
},
{
"caption": "Focus on",
"children":
[
{ "command": "focus_file_on_pane", "args": {"direction": "up"}, "caption": "Above" },
{ "command": "focus_file_on_pane", "args": {"direction": "down"}, "caption": "Below" },
{ "command": "focus_file_on_pane", "args": {"direction": "right"}, "caption": "Right" },
{ "command": "focus_file_on_pane", "args": {"direction": "left"}, "caption": "Left" }
]
},
{
"caption": "Focus or Clone to",
"children":
[
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"}, "caption": "Above" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"}, "caption": "Below" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"}, "caption": "Right" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"}, "caption": "Left" }
]
}
]
},
Expand Down
14 changes: 14 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["ctrl+k", "ctrl+alt+shift+up"], "command": "focus_file_on_pane", "args": {"direction": "up"} },
{ "keys": ["ctrl+k", "ctrl+alt+shift+right"], "command": "focus_file_on_pane", "args": {"direction": "right"} },
{ "keys": ["ctrl+k", "ctrl+alt+shift+down"], "command": "focus_file_on_pane", "args": {"direction": "down"} },
{ "keys": ["ctrl+k", "ctrl+alt+shift+left"], "command": "focus_file_on_pane", "args": {"direction": "left"} },
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+up"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"} },
{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+right"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"} },
{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+down"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"} },
{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+left"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"} },
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["ctrl+k", "ctrl+up"], "command": "create_pane", "args": {"direction": "up"} },
{ "keys": ["ctrl+k", "ctrl+right"], "command": "create_pane", "args": {"direction": "right"} },
{ "keys": ["ctrl+k", "ctrl+down"], "command": "create_pane", "args": {"direction": "down"} },
Expand Down
14 changes: 14 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["super+k", "super+alt+shift+up"], "command": "focus_file_on_pane", "args": {"direction": "up"} },
{ "keys": ["super+k", "super+alt+shift+right"], "command": "focus_file_on_pane", "args": {"direction": "right"} },
{ "keys": ["super+k", "super+alt+shift+down"], "command": "focus_file_on_pane", "args": {"direction": "down"} },
{ "keys": ["super+k", "super+alt+shift+left"], "command": "focus_file_on_pane", "args": {"direction": "left"} },
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["super+k", "super+i" , "super+alt+shift+up"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"} },
{ "keys": ["super+k", "super+i" , "super+alt+shift+right"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"} },
{ "keys": ["super+k", "super+i" , "super+alt+shift+down"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"} },
{ "keys": ["super+k", "super+i" , "super+alt+shift+left"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"} },
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["super+k", "super+up"], "command": "create_pane", "args": {"direction": "up"} },
{ "keys": ["super+k", "super+right"], "command": "create_pane", "args": {"direction": "right"} },
{ "keys": ["super+k", "super+down"], "command": "create_pane", "args": {"direction": "down"} },
Expand Down
14 changes: 14 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["ctrl+k", "ctrl+alt+shift+up"], "command": "focus_file_on_pane", "args": {"direction": "up"} },
{ "keys": ["ctrl+k", "ctrl+alt+shift+right"], "command": "focus_file_on_pane", "args": {"direction": "right"} },
{ "keys": ["ctrl+k", "ctrl+alt+shift+down"], "command": "focus_file_on_pane", "args": {"direction": "down"} },
{ "keys": ["ctrl+k", "ctrl+alt+shift+left"], "command": "focus_file_on_pane", "args": {"direction": "left"} },
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+up"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"} },
{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+right"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"} },
{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+down"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"} },
{ "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+left"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"} },
// You can also create the pane automatically with the following command (insert empty parameters):
// { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} }

{ "keys": ["ctrl+k", "ctrl+up"], "command": "create_pane", "args": {"direction": "up"} },
{ "keys": ["ctrl+k", "ctrl+right"], "command": "create_pane", "args": {"direction": "right"} },
{ "keys": ["ctrl+k", "ctrl+down"], "command": "create_pane", "args": {"direction": "down"} },
Expand Down
22 changes: 21 additions & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,27 @@
{ "command": "pull_file_from_pane", "args": {"direction": "right"}, "caption": "Right" },
{ "command": "pull_file_from_pane", "args": {"direction": "left"}, "caption": "Left" }
]
}
},
{
"caption": "Focus on",
"children":
[
{ "command": "focus_file_on_pane", "args": {"direction": "up"}, "caption": "Above" },
{ "command": "focus_file_on_pane", "args": {"direction": "down"}, "caption": "Below" },
{ "command": "focus_file_on_pane", "args": {"direction": "right"}, "caption": "Right" },
{ "command": "focus_file_on_pane", "args": {"direction": "left"}, "caption": "Left" }
]
},
{
"caption": "Focus/clone to",
"children":
[
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"}, "caption": "Above" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"}, "caption": "Below" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"}, "caption": "Right" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"}, "caption": "Left" }
]
},
]
},
{
Expand Down
13 changes: 12 additions & 1 deletion Origami.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@

{ "command": "clone_file_to_pane", "args": {"direction": "up"}, "caption": "Origami: Clone File to Pane Above" },
{ "command": "clone_file_to_pane", "args": {"direction": "right"}, "caption": "Origami: Clone File to Pane on the Right" },
{ "command": "clone_file_to_pane", "args": {"direction": "down"}, "caption": "Origami: Clone File to Pane Below" },
{ "command": "clone_file_to_pane", "args": {"direction": "down"}, "cakption": "Origami: Clone File to Pane Below" },
{ "command": "clone_file_to_pane", "args": {"direction": "left"}, "caption": "Origami: Clone File to Pane on the Left" },

{ "command": "focus_file_on_pane", "args": {"direction": "up"}, "caption": "Origami: Focus File on the Pane Above" },
{ "command": "focus_file_on_pane", "args": {"direction": "right"}, "caption": "Origami: Focus File on the Pane on the Right" },
{ "command": "focus_file_on_pane", "args": {"direction": "down"}, "caption": "Origami: Focus File on the Pane Below" },
{ "command": "focus_file_on_pane", "args": {"direction": "left"}, "caption": "Origami: Focus File on the Pane on the Left" },

{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"}, "caption": "Origami: Focus or Clone File on the Pane Above" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"}, "caption": "Origami: Focus or Clone File on the Pane on the Right" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"}, "caption": "Origami: Focus or Clone File on the Pane Below" },
{ "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"}, "caption": "Origami: Focus or Clone File on the Pane on the Left" },
{ "command": "focus_or_clone_file_on_closest_pane", "caption": "Origami: Focus or Clone File on the closest panel" },

{ "command": "create_pane", "args": {"direction": "up"}, "caption": "Origami: Create Pane Above" },
{ "command": "create_pane", "args": {"direction": "right"}, "caption": "Origami: Create Pane on the Right" },
{ "command": "create_pane", "args": {"direction": "down"}, "caption": "Origami: Create Pane Below" },
Expand Down
95 changes: 89 additions & 6 deletions origami.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ def adjacent_cell(self, direction):
return adjacent_cells[cell_index]
return None

def get_closest_panel_direction(self):
group = self.window.active_group()
if group == None:
# If we're in an empty group, there's no active group
return

cell = self.adjacent_cell('left')
if cell is None:
cell = self.adjacent_cell('right')
if cell is None:
return None
else:
return 'right'
else:
return 'left'


def duplicated_views(self, original_group, duplicating_group):
original_views = self.window.views_in_group(original_group)
original_buffers = [v.buffer_id() for v in original_views]
Expand All @@ -116,6 +133,29 @@ def duplicated_views(self, original_group, duplicating_group):
dupe_views.append(pd)
return dupe_views

def is_view_in_group(self, view, group):
views = self.window.views_in_group(group)
buffers = [v.buffer_id() for v in views]

buffer_id = view.buffer_id()
buffer_exists = False
target_view = None
for current_view in views:
if current_view.buffer_id() == buffer_id:
buffer_exists = True
target_view = current_view
break

return buffer_exists, target_view

def clone_viewport (self, view, new_view):
new_sel = new_view.sel()
new_sel.clear()
for s in view.sel():
new_sel.add(s)

sublime.set_timeout(lambda : new_view.set_viewport_position(view.viewport_position(), False), 0)

def travel_to_pane(self, direction, create_new_if_necessary=False):
adjacent_cell = self.adjacent_cell(direction)
if adjacent_cell:
Expand All @@ -130,6 +170,7 @@ def carry_file_to_pane(self, direction, create_new_if_necessary=False):
if view == None:
# If we're in an empty group, there's no active view
return

window = self.window
self.travel_to_pane(direction, create_new_if_necessary)
window.set_view_index(view, window.active_group(), 0)
Expand All @@ -150,14 +191,31 @@ def clone_file_to_pane(self, direction, create_new_if_necessary=False):
window.set_view_index(new_view, group, original_index)

# Fix the new view's selection and viewport
new_sel = new_view.sel()
new_sel.clear()
for s in view.sel():
new_sel.add(s)
sublime.set_timeout(lambda : new_view.set_viewport_position(view.viewport_position(), False), 0)

self.clone_viewport (view, new_view)
self.carry_file_to_pane(direction, create_new_if_necessary)

def focus_file_on_pane(self, direction, create_new_if_necessary=False):
window = self.window
view = window.active_view()
if view == None:
# If we're in an empty group, there's no active view
# Therefore there is nothing to focus
return

source_group = window.active_group()
self.travel_to_pane(direction, create_new_if_necessary)
target_group = window.active_group()

buffer_exists, target_view = self.is_view_in_group(view, target_group)

if buffer_exists:
self.clone_viewport (view, target_view)
window.focus_view(target_view)
else:
window.focus_group(source_group)

return buffer_exists

def reorder_panes(self, leave_files_at_position = True):
_, _, cells = self.get_layout()
current_cell = cells[self.window.active_group()]
Expand Down Expand Up @@ -520,6 +578,31 @@ def run(self, direction, create_new_if_necessary=None):
create_new_if_necessary = self.settings().get('create_new_pane_if_necessary')
self.carry_file_to_pane(direction, create_new_if_necessary)

class FocusFileOnPaneCommand(PaneCommand, WithSettings):
def run(self, direction, create_new_if_necessary=None):
if create_new_if_necessary is None:
create_new_if_necessary = self.settings().get('create_new_pane_if_necessary')
self.focus_file_on_pane(direction, create_new_if_necessary)

class FocusOrCloneFileOnPaneCommand(PaneCommand, WithSettings):
def run(self, direction, create_new_if_necessary=None):
if create_new_if_necessary is None:
create_new_if_necessary = self.settings().get('create_new_pane_if_necessary')
if not self.focus_file_on_pane(direction, create_new_if_necessary):
self.clone_file_to_pane(direction, create_new_if_necessary)

class FocusOrCloneFileOnClosestPaneCommand(PaneCommand, WithSettings):
def run(self, create_new_if_necessary=None):
if create_new_if_necessary is None:
create_new_if_necessary = self.settings().get('create_new_pane_if_necessary')
direction = self.get_closest_panel_direction()
if direction is None and create_new_if_necessary is False:
return
elif direction is None:
direction = 'right'

if not self.focus_file_on_pane(direction, create_new_if_necessary):
self.clone_file_to_pane(direction, create_new_if_necessary)

class CloneFileToPaneCommand(PaneCommand, WithSettings):
def run(self, direction, create_new_if_necessary=None):
Expand Down