From d1e9e66dcd669a376951ab92e871c717a6f3c966 Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Sat, 25 Jan 2014 18:02:29 +0800 Subject: [PATCH 1/6] revert c8c7f4d5; restore lost semantics Originally the first such i is returned; this was lost in translation in c8c7f4d5. Revert to the next() one-liner (I'm a sucker for them!), and restore the first-i behaviour by providing a default to next(). --- commands.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/commands.py b/commands.py index 9f999b4..f9c7302 100644 --- a/commands.py +++ b/commands.py @@ -30,9 +30,7 @@ class Pipe(PipeViews): def group_other_than(self, window, group): groups = window.num_groups() - for i in range(groups): - if i != group: - group = i + group = next((i for i in range(groups) if i != group), group) return group, 0 def choose_group(self, window, view): From 5fcf14ba0784136c20956ecfa38e97185235ed13 Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Sun, 26 Jan 2014 00:30:37 +0800 Subject: [PATCH 2/6] factor out placement policy While we're at it, add some documentation, rename group_other_than to show that it's private. --- commands.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/commands.py b/commands.py index f9c7302..a408189 100644 --- a/commands.py +++ b/commands.py @@ -22,23 +22,33 @@ def proxy_settings(pipe, view): set_settings_listener(pipe, "enabled_setting", settings, "bv_enabled") -class Pipe(PipeViews): - dest_view_name = "Build output" - +class PlacementPolicy1(object): + """ + Prefer the group where the build view was last (closed) in; but also avoid + using the group where the source code view is in. + """ last_placed_group = (0, 0) group_to_avoid = None - def group_other_than(self, window, group): + def __group_other_than(self, window, group): groups = window.num_groups() group = next((i for i in range(groups) if i != group), group) return group, 0 def choose_group(self, window, view): + """ + Returns a tuple (group, index), corresponding to + sublime.View.get_view_index()/set_view_index() + """ group = self.last_placed_group if self.group_to_avoid == group[0]: - group = self.group_other_than(window, self.group_to_avoid) + group = self.__group_other_than(window, self.group_to_avoid) return group + +class Pipe(PlacementPolicy1, PipeViews): + dest_view_name = "Build output" + def on_view_created(self, window, view, pipe): proxy_settings(pipe, view) From 0f4352e3d0b0f7e4c64448a42925b746b25ad594 Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Sun, 26 Jan 2014 01:05:47 +0800 Subject: [PATCH 3/6] refactor placement policy code - inline choosing of other group - don't save group-to-avoid, just get it when we need it (since we can access the access the view that launched the build anyway) --- commands.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/commands.py b/commands.py index a408189..98a4c7d 100644 --- a/commands.py +++ b/commands.py @@ -28,12 +28,6 @@ class PlacementPolicy1(object): using the group where the source code view is in. """ last_placed_group = (0, 0) - group_to_avoid = None - - def __group_other_than(self, window, group): - groups = window.num_groups() - group = next((i for i in range(groups) if i != group), group) - return group, 0 def choose_group(self, window, view): """ @@ -41,8 +35,11 @@ def choose_group(self, window, view): sublime.View.get_view_index()/set_view_index() """ group = self.last_placed_group - if self.group_to_avoid == group[0]: - group = self.__group_other_than(window, self.group_to_avoid) + group_to_avoid = window.get_view_index(view)[0] + if group_to_avoid == group[0]: + groups = window.num_groups() + new_group = next((i for i in range(groups) if i != group_to_avoid), group_to_avoid) + group = (new_group, group[1]) return group @@ -52,7 +49,7 @@ class Pipe(PlacementPolicy1, PipeViews): def on_view_created(self, window, view, pipe): proxy_settings(pipe, view) - window.set_view_index(view, *self.choose_group(window, view)) + window.set_view_index(view, *self.choose_group(window, self.view_launched_build)) window.focus_view(self.view_launched_build) @@ -111,7 +108,6 @@ def on_query_context(self, view, key, *args): pipe.prepare_copy(window) pipe.first_run = True pipe.view_launched_build = view - pipe.group_to_avoid = window.get_view_index(view)[0] def hide_panel(): window.run_command("hide_panel") From edf6161aefea80187d08c47791fc1ae95db91219 Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Sun, 26 Jan 2014 05:17:42 +0800 Subject: [PATCH 4/6] reduce role-ladenness of 'group' --- commands.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/commands.py b/commands.py index 98a4c7d..a32d866 100644 --- a/commands.py +++ b/commands.py @@ -34,13 +34,12 @@ def choose_group(self, window, view): Returns a tuple (group, index), corresponding to sublime.View.get_view_index()/set_view_index() """ - group = self.last_placed_group + group_index, view_index = self.last_placed_group group_to_avoid = window.get_view_index(view)[0] - if group_to_avoid == group[0]: + if group_to_avoid == group_index: groups = window.num_groups() - new_group = next((i for i in range(groups) if i != group_to_avoid), group_to_avoid) - group = (new_group, group[1]) - return group + group_index = next((i for i in range(groups) if i != group_to_avoid), group_to_avoid) + return group_index, view_index class Pipe(PlacementPolicy1, PipeViews): From 8737617f1808969561a9c37be73040b68ccd3828 Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Sun, 26 Jan 2014 05:30:18 +0800 Subject: [PATCH 5/6] fix noop-ness of placing view into a group --- commands.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/commands.py b/commands.py index a32d866..1b89cd9 100644 --- a/commands.py +++ b/commands.py @@ -39,6 +39,11 @@ def choose_group(self, window, view): if group_to_avoid == group_index: groups = window.num_groups() group_index = next((i for i in range(groups) if i != group_to_avoid), group_to_avoid) + + # sublime refuses to place view into group_index if view_index exceeds + # number of views in that group + view_index = min(view_index, len(window.views_in_group(group_index))) + return group_index, view_index From d8835d3b91d220a4ab1a16b813575d0729e2f5d7 Mon Sep 17 00:00:00 2001 From: Tay Ray Chuan Date: Sun, 26 Jan 2014 05:52:33 +0800 Subject: [PATCH 6/6] tweak placement policy --- commands.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/commands.py b/commands.py index 1b89cd9..5f5525d 100644 --- a/commands.py +++ b/commands.py @@ -24,21 +24,39 @@ def proxy_settings(pipe, view): class PlacementPolicy1(object): """ - Prefer the group where the build view was last (closed) in; but also avoid - using the group where the source code view is in. + Use the (group, index) where the build view was last (closed) in; + if we haven't had a build view yet, + if the windows has more than one group, + use the first group that doesn't hold the source view; + otherwise, place the build view next to the source view (on the right) """ - last_placed_group = (0, 0) + last_placed_group = None def choose_group(self, window, view): """ Returns a tuple (group, index), corresponding to sublime.View.get_view_index()/set_view_index() """ - group_index, view_index = self.last_placed_group - group_to_avoid = window.get_view_index(view)[0] - if group_to_avoid == group_index: - groups = window.num_groups() - group_index = next((i for i in range(groups) if i != group_to_avoid), group_to_avoid) + + source_group_index, source_view_index = window.get_view_index(view) + + if self.last_placed_group is not None: + group_index, view_index = self.last_placed_group + else: + num_groups = window.num_groups() + if num_groups == 1: + place_to_side = True + else: + group_index = next((i for i in range(num_groups) if i != source_group_index), None) + if group_index is None: + place_to_side = True + else: + view_index = 0 + place_to_side = False + + if place_to_side: + group_index = source_group_index + view_index = source_view_index + 1 # sublime refuses to place view into group_index if view_index exceeds # number of views in that group