6
6
from .core .views import get_uri_and_position_from_location
7
7
from .core .views import location_to_human_readable
8
8
from .core .views import to_encoded_filename
9
+ from urllib .request import url2pathname
9
10
import functools
10
11
import sublime
11
12
import weakref
@@ -36,10 +37,18 @@ def open_basic_file(
36
37
position : Position ,
37
38
flags : int = 0 ,
38
39
group : Optional [int ] = None
39
- ) -> sublime .View :
40
- filename = session .config .map_server_uri_to_client_path (uri )
40
+ ) -> Optional [sublime .View ]:
41
41
if group is None :
42
42
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
43
52
return session .window .open_file (to_encoded_filename (filename , position ), flags = flags , group = group )
44
53
45
54
@@ -86,10 +95,12 @@ def _select_entry(self, index: int) -> None:
86
95
return
87
96
# Note: this has to run on the main thread (and not via open_location_async)
88
97
# 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:" ) ):
90
99
flags = sublime .ENCODED_POSITION
91
100
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 ))
93
104
else :
94
105
sublime .set_timeout_async (
95
106
functools .partial (open_location_async , session , location , self ._side_by_side , True ))
@@ -106,7 +117,7 @@ def _highlight_entry(self, index: int) -> None:
106
117
session , _ , uri , position = self ._unpack (index )
107
118
if not session :
108
119
return
109
- if uri .startswith ("file:" ):
120
+ if uri .startswith (( "file:" , "res:" ) ):
110
121
flags = sublime .ENCODED_POSITION | sublime .FORCE_GROUP
111
122
if self ._side_by_side :
112
123
if self ._highlighted_view and self ._highlighted_view .is_valid ():
@@ -117,7 +128,11 @@ def _highlight_entry(self, index: int) -> None:
117
128
flags |= sublime .ADD_TO_SELECTION | sublime .SEMI_TRANSIENT
118
129
else :
119
130
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
121
136
else :
122
- # TODO: Preview non-file uris ?
137
+ # TODO: Preview for other uri schemes ?
123
138
debug ("no preview for" , uri )
0 commit comments