Skip to content

Commit

Permalink
Improve buffer URI format (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann authored Nov 14, 2023
1 parent d5be23a commit 6243f5d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions plugin/core/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from .logging import printf
from .types import ClientConfig
from .typing import Generator, List, Optional, Set, Dict, Deque
from .url import parse_uri
from .workspace import enable_in_project, disable_in_project
from abc import ABCMeta
from abc import abstractmethod
from collections import deque
from datetime import datetime, timedelta
from weakref import WeakSet
import sublime
import urllib.parse


RETRY_MAX_COUNT = 5
Expand Down Expand Up @@ -51,7 +51,7 @@ def match_view(self, view: sublime.View, include_disabled: bool = False) -> Gene
uri = view.settings().get("lsp_uri")
if not isinstance(uri, str):
return
scheme = urllib.parse.urlparse(uri).scheme
scheme = parse_uri(uri)[0]
for config in self.all.values():
if config.match_view(view, scheme) and (config.enabled or include_disabled):
yield config
Expand Down
3 changes: 1 addition & 2 deletions plugin/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import socket
import sublime
import time
import urllib.parse


TCP_CONNECT_TIMEOUT = 5 # seconds
Expand Down Expand Up @@ -394,7 +393,7 @@ def __call__(self, view: sublime.View) -> bool:
return False
if self.scheme:
uri = view.settings().get("lsp_uri")
if isinstance(uri, str) and urllib.parse.urlparse(uri).scheme != self.scheme:
if isinstance(uri, str) and parse_uri(uri)[0] != self.scheme:
return False
if self.pattern:
if not globmatch(view.file_name() or "", self.pattern, flags=GLOBSTAR | BRACE):
Expand Down
5 changes: 4 additions & 1 deletion plugin/core/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def filename_to_uri(file_name: str) -> str:
def view_to_uri(view: sublime.View) -> str:
file_name = view.file_name()
if not file_name:
return "buffer://{}".format(view.buffer_id())
return "buffer:{}".format(view.buffer_id())
return filename_to_uri(file_name)


Expand Down Expand Up @@ -60,6 +60,9 @@ def parse_uri(uri: str) -> Tuple[str, str]:
else:
return parsed.scheme, path
return parsed.scheme, path
elif parsed.scheme == '' and ':' in parsed.path.split('/')[0]:
# workaround for bug in urllib.parse.urlparse
return parsed.path.split(':')[0], uri
return parsed.scheme, uri


Expand Down
5 changes: 2 additions & 3 deletions plugin/core/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import json
import sublime
import threading
import urllib.parse


_NO_DIAGNOSTICS_PLACEHOLDER = " No diagnostics. Well done!"
Expand Down Expand Up @@ -184,7 +183,7 @@ def _dequeue_listener_async(self) -> None:

def _publish_sessions_to_listener_async(self, listener: AbstractViewListener) -> None:
inside_workspace = self._workspace.contains(listener.view)
scheme = urllib.parse.urlparse(listener.get_uri()).scheme
scheme = parse_uri(listener.get_uri())[0]
for session in self._sessions:
if session.can_handle(listener.view, scheme, capability=None, inside_workspace=inside_workspace):
# debug("registering session", session.config.name, "to listener", listener)
Expand All @@ -200,7 +199,7 @@ def sessions(self, view: sublime.View, capability: Optional[str] = None) -> Gene
uri = view.settings().get("lsp_uri")
if not isinstance(uri, str):
return
scheme = urllib.parse.urlparse(uri).scheme
scheme = parse_uri(uri)[0]
for session in sessions:
if session.can_handle(view, scheme, capability, inside_workspace):
yield session
Expand Down
2 changes: 1 addition & 1 deletion plugin/hover.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def on_select(targets: List[str], idx: int) -> None:
position = {"line": row, "character": col_utf16} # type: Position
r = {"start": position, "end": position} # type: Range
sublime.set_timeout_async(partial(session.open_uri_async, uri, r))
elif urlparse(href).scheme.lower() not in ("", "http", "https"):
elif parse_uri(href)[0].lower() not in ("", "http", "https"):
sublime.set_timeout_async(partial(self.try_open_custom_uri_async, href))
else:
open_in_browser(href)
Expand Down
8 changes: 7 additions & 1 deletion tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ def test_buffer_uri(self):
view.file_name = unittest.mock.MagicMock(return_value=None)
view.buffer_id = unittest.mock.MagicMock(return_value=42)
uri = view_to_uri(view)
self.assertEqual(uri, "buffer://42")
self.assertEqual(uri, "buffer:42")

def test_parse_uri(self):
scheme, _ = parse_uri("buffer:42")
self.assertEqual(scheme, "buffer")
scheme, _ = parse_uri("www.example.com/foo:bar")
self.assertEqual(scheme, "")

0 comments on commit 6243f5d

Please sign in to comment.