Skip to content

Commit 135d81e

Browse files
jwortmannrwols
authored andcommitted
Add preview for resource files in LocationPicker
1 parent 5fc0db5 commit 135d81e

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

plugin/core/views.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,9 @@ def location_to_human_readable(
770770
pathname = config.map_server_uri_to_client_path(uri)
771771
if base_dir and is_subpath_of(pathname, base_dir):
772772
pathname = pathname[len(os.path.commonprefix((pathname, base_dir))) + 1:]
773+
elif scheme == "res":
774+
fmt = "{}:{}"
775+
pathname = uri
773776
else:
774777
# https://tools.ietf.org/html/rfc5147
775778
fmt = "{}#line={}"

plugin/locationpicker.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .core.views import get_uri_and_position_from_location
77
from .core.views import location_to_human_readable
88
from .core.views import to_encoded_filename
9+
from urllib.request import url2pathname
910
import functools
1011
import sublime
1112
import weakref
@@ -36,10 +37,18 @@ def open_basic_file(
3637
position: Position,
3738
flags: int = 0,
3839
group: Optional[int] = None
39-
) -> sublime.View:
40-
filename = session.config.map_server_uri_to_client_path(uri)
40+
) -> Optional[sublime.View]:
4141
if group is None:
4242
group = session.window.active_group()
43+
if uri.startswith("file:"):
44+
filename = session.config.map_server_uri_to_client_path(uri)
45+
else:
46+
prefix = 'res://Packages' # Note: keep in sync with core/url.py#_to_resource_uri
47+
assert uri.startswith(prefix)
48+
filename = sublime.packages_path() + url2pathname(uri[len(prefix):])
49+
# Window.open_file can only focus and scroll to a location in a resource file if it is already opened
50+
if not session.window.find_open_file(filename):
51+
return None
4352
return session.window.open_file(to_encoded_filename(filename, position), flags=flags, group=group)
4453

4554

@@ -86,10 +95,12 @@ def _select_entry(self, index: int) -> None:
8695
return
8796
# Note: this has to run on the main thread (and not via open_location_async)
8897
# otherwise the bevior feels weird. It's the only reason why open_basic_file exists.
89-
if uri.startswith("file:"):
98+
if uri.startswith(("file:", "res:")):
9099
flags = sublime.ENCODED_POSITION
91100
if not self._side_by_side:
92-
open_basic_file(session, uri, position, flags)
101+
view = open_basic_file(session, uri, position, flags)
102+
if not view:
103+
self._window.status_message("Unable to open {}".format(uri))
93104
else:
94105
sublime.set_timeout_async(
95106
functools.partial(open_location_async, session, location, self._side_by_side, True))
@@ -106,7 +117,7 @@ def _highlight_entry(self, index: int) -> None:
106117
session, _, uri, position = self._unpack(index)
107118
if not session:
108119
return
109-
if uri.startswith("file:"):
120+
if uri.startswith(("file:", "res:")):
110121
flags = sublime.ENCODED_POSITION | sublime.FORCE_GROUP
111122
if self._side_by_side:
112123
if self._highlighted_view and self._highlighted_view.is_valid():
@@ -117,7 +128,11 @@ def _highlight_entry(self, index: int) -> None:
117128
flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT
118129
else:
119130
flags |= sublime.TRANSIENT
120-
self._highlighted_view = open_basic_file(session, uri, position, flags, self._window.active_group())
131+
view = open_basic_file(session, uri, position, flags, self._window.active_group())
132+
# Don't overwrite self._highlighted_view if resource uri can't preview, so that side-by-side view will still
133+
# be closed upon canceling
134+
if view:
135+
self._highlighted_view = view
121136
else:
122-
# TODO: Preview non-file uris?
137+
# TODO: Preview for other uri schemes?
123138
debug("no preview for", uri)

0 commit comments

Comments
 (0)