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

Add the reveal option #7

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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
19 changes: 13 additions & 6 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
]
1 change: 1 addition & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
@@ -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" } },
]
70 changes: 69 additions & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -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",
}
}
]
}
]
}
]
}
]
8 changes: 4 additions & 4 deletions Side Bar.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
)
44 changes: 36 additions & 8 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import time
from functools import partial

import sublime
Expand All @@ -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():
Expand Down Expand Up @@ -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]))
Expand All @@ -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)