Skip to content

Commit

Permalink
Closes issue SublimeText#71
Browse files Browse the repository at this point in the history
- Adds AutoSwitchPane EventListener to auto-group files by syntax
  • Loading branch information
23maverick23 committed Feb 11, 2015
1 parent 5fe92a6 commit ea7b3dd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Origami.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
{
/**
* The "syntax_grouping" key determines if you want
* Origami to attempt to group files with the same syntax
* into the same pane. The default if "false".

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

Should say "is" instead of "if" 🍬

*
* The "enabled_syntaxes" key holds a list of syntaxes
* that qualify for grouping. Each syntax should be entered as
* a string. A blank list means all syntaxes qualify. If you are
* unsure of the correct syntax name, you can open the Sublime console
* and enter the following command:
* sublime.active_window().active_view().settings().get('syntax')

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

In the console, view always refers to the active view, so this could be compressed to view.settings().get('syntax'). Alternatively, it could be import os; os.path.splitext(os.path.basename(view.settings().get('syntax')))[0].lower().

* The name of the tmLanguage file is what you want.
* Ex// ["json", "python"]
*/
"syntax_grouping": false,
"enabled_syntaxes": [],

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

Would it be better to have a boolean and a list, or just a list that can be empty?

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

All the other Origami settings are just in the user settings with origami_ prepended (for historical reasons, but still). The saved_layouts were only here because they're only saved programmatically. I'm not sure if we should move these or what... still thinking about it. What do you think?

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

Re: my first note. Could [] mean disabled and ["*"] mean all syntaxes? That might be more clear—when I see an empty list I think "disabled". What do you think?



/**
* The "saved_layouts" key holds a list of previously
* saved layout settings. These can be set using the
Expand Down
2 changes: 2 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ You can have Origami automatically zoom the active pane by setting `origami_auto

Origami can also automatically close a pane for you once you've closed the last file in it. Just set `origami_auto_close_empty_panes` to true in your user preferences.

Origami can also automatically move files of the same syntax into the same pane group when opening them. Set `syntax_grouping` to `true` in your user or project settings. If you only want specific syntax types to be automatically grouped, you can selectively enable them using the `enabled_syntaxes` list in your user or project settings. (If you don't know the syntax name to use, you can open the Sublime console with <kbd>CTRL</kbd>+<kbd>~</kbd> and type `sublime.active_window().active_view().settings().get('syntax')`. Copy the name of the _tmLanguage_ file without the path parameters.

Install
-------

Expand Down
48 changes: 48 additions & 0 deletions origami.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import division
import sublime, sublime_plugin
import copy
import os
from functools import partial

XMIN, YMIN, XMAX, YMAX = list(range(4))
Expand Down Expand Up @@ -62,6 +63,27 @@ def fixed_set_layout_no_focus_change(window, layout):
active_group = window.active_group()
window.set_layout(layout)

def get_setting(view, key, default=None):

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

Is there a reason we need this function over just sublime.load_settings('Origami.sublime-settings').get(key,default)?

"""
Get a Sublime Text setting value, starting in the project-specific
settings file, then the user-specific settings file, and finally
the package-specific settings file. Also accepts an optional default.
"""
try:
settings = view.settings()
if settings.has('Origami'):
s = settings.get('Origami').get(key)
if s and len(s) > 0:
return s
else:
pass
else:
pass
except:
pass
global_settings = sublime.load_settings('Origami.sublime-settings')
return global_settings.get(key, default)

class PaneCommand(sublime_plugin.WindowCommand):
"Abstract base class for commands."

Expand Down Expand Up @@ -763,3 +785,29 @@ def on_activated(self, view):
self.running = True

sublime.set_timeout(lambda: self.delayed_zoom(view, fraction), 0)

class AutoSwitchPane(sublime_plugin.EventListener):
def get_syntax_name(self, view):
syntax_path = view.settings().get('syntax')
syntax_name = os.path.splitext(os.path.basename(syntax_path))[0].lower()

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

Is there a reason you're making the syntaxes lower case? I don't know of any, but I could imagine that this could cause a name collision.

return syntax_name

def on_load(self, view):
syntax_grouping = get_setting(view, 'syntax_grouping')
enabled_syntaxes = [s.lower() for s in get_setting(view, 'enabled_syntaxes')]

if syntax_grouping:
w = sublime.active_window()
view = w.active_view()
views = w.views()
current_syntax = self.get_syntax_name(view)

if views and (len(enabled_syntaxes) == 0 or current_syntax in enabled_syntaxes):
for v in views:
syntax = self.get_syntax_name(v)
group = w.get_view_index(v)[0]

if syntax and syntax == current_syntax:

This comment has been minimized.

Copy link
@adzenith

adzenith Mar 30, 2015

Will syntax ever be None?

w.set_view_index(view, group, 0)
w.focus_view(view)

0 comments on commit ea7b3dd

Please sign in to comment.