diff --git a/Default.sublime-commands b/Default.sublime-commands index c1aaed9..5ba4212 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -14,19 +14,26 @@ "args": { "base_file": "${packages}/Projectionist/Default.sublime-keymap", "user_file": "${packages}/User/Default ($platform).sublime-keymap", - "default": "[\n\t$0\n]\n", + "default": "[\n\t$0\n]\n" } }, { - "caption": "Projectionist: Open alternate", + "caption": "Projectionist: Open Alternate", + "command": "projectionist_open_alternate" + }, + { + "caption": "Projectionist: Go to Alternate", "command": "projectionist_open_alternate", + "args": { + "reveal": true + } }, { - "caption": "Projectionist: Output projections", - "command": "projectionist_output_projections", + "caption": "Projectionist: Output Projections", + "command": "projectionist_output_projections" }, { - "caption": "Projectionist: Clear cache", - "command": "projectionist_clear_cache", + "caption": "Projectionist: Clear Cache", + "command": "projectionist_clear_cache" }, ] diff --git a/Default.sublime-keymap b/Default.sublime-keymap index 6dd850e..2ea3b7a 100644 --- a/Default.sublime-keymap +++ b/Default.sublime-keymap @@ -1,4 +1,5 @@ [ // { "keys": ["super+alt+up"], "command": "projectionist_open_alternate" }, + // { "keys": ["UNBOUND"], "command": "projectionist_open_alternate", "args": { "reveal": true } }, // { "keys": ["super+alt+shift+up"], "command": "projectionist_open_alternate", "args": { "mode": "side_by_side" } }, ] diff --git a/Main.sublime-menu b/Main.sublime-menu index fe51488..5bd1f69 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -1 +1,69 @@ -[] +[ + { + "id": "tools", + "children": [ + { + "id": "Projectionist", + "caption": "Projectionist", + "children": [ + { + "caption": "Open Alternate", + "command": "projectionist_open_alternate", + }, + { + "caption": "Go to Alternate", + "command": "projectionist_open_alternate", + "args": { + "reveal": true + } + }, + { + "caption": "-" + }, + { + "caption": "Output Projections", + "command": "projectionist_output_projections", + }, + { + "caption": "Clear Cache", + "command": "projectionist_clear_cache", + }, + ] + } + ] + }, + { + "id": "preferences", + "children": [ + { + "id": "package-settings", + "children": [ + { + "caption": "Projectionist", + "id": "sublime-projectionist", + "children": [ + { + "caption": "Settings", + "command": "edit_settings", + "args": { + "base_file": "${packages}/Projectionist/Projectionist.sublime-settings", + "user_file": "${packages}/User/Projectionist.sublime-settings", + "default": "// Settings in here override those in \"Projectionist/Projectionist.sublime-settings\"\n{\n\t$0\n}\n" + } + }, + { + "caption": "Key Bindings", + "command": "edit_settings", + "args": { + "base_file": "${packages}/Projectionist/Default.sublime-keymap", + "user_file": "${packages}/User/Default ($platform).sublime-keymap", + "default": "[\n\t$0\n]\n", + } + } + ] + } + ] + } + ] + } +] diff --git a/Side Bar.sublime-menu b/Side Bar.sublime-menu index 18d371a..3386b8a 100644 --- a/Side Bar.sublime-menu +++ b/Side Bar.sublime-menu @@ -3,17 +3,17 @@ "caption": "-" }, { - "caption": "Open Alternate File", + "caption": "Open Alternate", "command": "projectionist_sidebar_open_alternate", - "mnemonic": "A", + "mnemonic": "O", "args": { "files": [] } }, { - "caption": "Go to Alternate File", + "caption": "Go to Alternate", "command": "projectionist_sidebar_open_alternate", - "mnemonic": "R", + "mnemonic": "G", "args": { "files": [], "reveal": true diff --git a/main.py b/main.py index c1f8e56..9495c9f 100644 --- a/main.py +++ b/main.py @@ -22,8 +22,12 @@ class ProjectionistOpenAlternateCommand(sublime_plugin.TextCommand): - def run(self, _, mode=None): - Plugin(self.view.window()).open_alternate(self.view.file_name(), mode=mode) + def run(self, _, mode=None, reveal=None): + Plugin(self.view.window()).open_alternate( + self.view.file_name(), + mode=mode, + focus=(Plugin.FOCUS_SIDEBAR if reveal else None), + ) class ProjectionistOutputProjectionsCommand(sublime_plugin.TextCommand): @@ -41,4 +45,6 @@ def run(self, files=[], reveal=False): if not files: return - Plugin(self.window).open_alternate(files[0], focus=not reveal) + Plugin(self.window).open_alternate( + files[0], focus=(Plugin.FOCUS_SIDEBAR if reveal else Plugin.FOCUS_VIEW) + ) diff --git a/plugin/__init__.py b/plugin/__init__.py index 29df89d..fbd423f 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1,4 +1,5 @@ import logging +import time from functools import partial import sublime @@ -13,7 +14,12 @@ class Plugin: - SIDE_BY_SIDE = "side_by_side" + MODE_SIDE_BY_SIDE = "side_by_side" + FOCUS_VIEW = "view" + FOCUS_SIDEBAR = "sidebar" + OPEN_VIEW_TIMEOUT = 3 # seconds + OPEN_VIEW_WAIT_FOR = 0.2 # 200 ms + OPEN_VIEW_MAX_ATTEMPTS = int(OPEN_VIEW_TIMEOUT / OPEN_VIEW_WAIT_FOR) @staticmethod def clear_cache(): @@ -65,12 +71,22 @@ def open_alternate(self, file_name, folders=[], focus=None, mode=None): status.update("No alternate file {}".format(suffix)) else: flags = 0 - if mode == self.SIDE_BY_SIDE and not ST3: + if mode == self.MODE_SIDE_BY_SIDE and not ST3: flags |= sublime.ADD_TO_SELECTION view = self.window.open_file(alternate.path, flags=flags) - if focus is True: - sublime.set_timeout(partial(self._focus_on_view, view), 0) + if focus == self.FOCUS_VIEW: + self._on_view_loaded( + view, + lambda view: self.window.focus_view(view), + ) + elif focus == self.FOCUS_SIDEBAR: + self._on_view_loaded( + view, + lambda _: sublime.set_timeout_async( + partial(self.window.run_command, "focus_side_bar"), 100 + ), + ) def output_projections(self): storage = Storage(Root(self.window.folders()[0])) @@ -81,7 +97,19 @@ def output_projections(self): print(" -> {}".format(projection.pattern)) print(" {}".format(projection.options)) - def _focus_on_view(self, view): - while view.is_loading(): - pass - self.window.focus_view(view) + def _on_view_loaded(self, view, callback): + def on_load(view, callback): + i = 0 + + while view.is_loading() and i < self.OPEN_VIEW_MAX_ATTEMPTS: + i += 1 + time.sleep(self.OPEN_VIEW_WAIT_FOR) + + if view.is_loading(): + logger.error( + "{} is still loading after {}s".format(view, self.OPEN_VIEW_TIMEOUT) + ) + else: + callback(view) + + sublime.set_timeout_async(partial(on_load, view, callback), 0)