-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow attaching to URI schemes other than the 'file' scheme (#1758)
This allows servers to attach to schemes like: - `buffer`: in-memory buffers - `res`: ST resource files in .sublime-packages - other foreign schemes, like `deno`, `json-schema`, `jdt`, etc The way this works is that we have a new client configuration setting called `"schemes"` which is a list of allowable URI schemes. If `"schemes"` is not specified, then it's by default `["file"]`.
- Loading branch information
Showing
12 changed files
with
348 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,63 @@ | ||
from .typing import Any | ||
from urllib.parse import quote | ||
from urllib.parse import urljoin | ||
from urllib.parse import urlparse | ||
from urllib.request import pathname2url | ||
from urllib.request import url2pathname | ||
import os | ||
import re | ||
|
||
import sublime | ||
|
||
def filename_to_uri(path: str) -> str: | ||
return urljoin('file:', pathname2url(path)) | ||
|
||
def filename_to_uri(file_name: str) -> str: | ||
""" | ||
Convert a file name obtained from view.file_name() into an URI | ||
""" | ||
prefix = sublime.installed_packages_path() | ||
if file_name.startswith(prefix): | ||
return _to_resource_uri(file_name, prefix) | ||
prefix = sublime.packages_path() | ||
if file_name.startswith(prefix) and not os.path.exists(file_name): | ||
return _to_resource_uri(file_name, prefix) | ||
path = pathname2url(file_name) | ||
re.sub(r"^([A-Z]):/", _lowercase_driveletter, path) | ||
return urljoin("file:", path) | ||
|
||
|
||
def view_to_uri(view: sublime.View) -> str: | ||
file_name = view.file_name() | ||
if not file_name: | ||
return "buffer://sublime/{}".format(view.buffer_id()) | ||
return filename_to_uri(file_name) | ||
|
||
|
||
def uri_to_filename(uri: str) -> str: | ||
""" | ||
DEPRECATED: An URI associated to a view does not necessarily have a "file:" scheme. | ||
Use urllib.parse.urlparse to determine the scheme and go from there. | ||
Use urllib.parse.unquote to unquote the path. | ||
""" | ||
parsed = urlparse(uri) | ||
assert parsed.scheme == "file" | ||
if os.name == 'nt': | ||
# url2pathname does not understand %3A (VS Code's encoding forced on all servers :/) | ||
return url2pathname(parsed.path).strip('\\') | ||
else: | ||
return url2pathname(parsed.path) | ||
|
||
|
||
def _to_resource_uri(path: str, prefix: str) -> str: | ||
""" | ||
Terrible hacks from ST core leak into packages as well. | ||
See: https://github.com/sublimehq/sublime_text/issues/3742 | ||
""" | ||
return "res://Packages{}".format(quote(path[len(prefix):])) | ||
|
||
|
||
def _lowercase_driveletter(match: Any) -> str: | ||
""" | ||
For compatibility with certain other language clients. | ||
""" | ||
return "{}:/".format(match.group(1).lower()) |
Oops, something went wrong.