From 301d0cc3def5942c014f48d053b17040a23220d5 Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sat, 11 Nov 2023 09:53:34 +0100 Subject: [PATCH 1/5] Test --- plugin/core/url.py | 2 +- tests/test_url.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/core/url.py b/plugin/core/url.py index 696ef9056..44e8f7b82 100644 --- a/plugin/core/url.py +++ b/plugin/core/url.py @@ -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) diff --git a/tests/test_url.py b/tests/test_url.py index 532278da5..41e2fc9c5 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -70,4 +70,4 @@ 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") From 0fe23507ed4e80342e2c5ba9c3ff80bcd4bfe04f Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sat, 11 Nov 2023 11:06:57 +0100 Subject: [PATCH 2/5] Workaround for bug in urllib --- plugin/core/configurations.py | 3 ++- plugin/core/types.py | 2 +- plugin/core/url.py | 3 +++ plugin/core/windows.py | 4 ++-- plugin/hover.py | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/plugin/core/configurations.py b/plugin/core/configurations.py index 96aecfe2e..badfd0dad 100644 --- a/plugin/core/configurations.py +++ b/plugin/core/configurations.py @@ -3,6 +3,7 @@ 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 @@ -51,7 +52,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 diff --git a/plugin/core/types.py b/plugin/core/types.py index c90805070..125e77ee7 100644 --- a/plugin/core/types.py +++ b/plugin/core/types.py @@ -394,7 +394,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): diff --git a/plugin/core/url.py b/plugin/core/url.py index 44e8f7b82..98f3af921 100644 --- a/plugin/core/url.py +++ b/plugin/core/url.py @@ -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: + # workaround for bug in urllib.parse.urlparse + return parsed.path.split(':')[0], uri return parsed.scheme, uri diff --git a/plugin/core/windows.py b/plugin/core/windows.py index 5ebebcc36..2bbc14b00 100644 --- a/plugin/core/windows.py +++ b/plugin/core/windows.py @@ -184,7 +184,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) @@ -200,7 +200,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 diff --git a/plugin/hover.py b/plugin/hover.py index 3b82e91fc..4397c8fd6 100644 --- a/plugin/hover.py +++ b/plugin/hover.py @@ -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) From 824f2ad71b6f0128548752fdd705dd604e5595ba Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sun, 12 Nov 2023 12:50:41 +0100 Subject: [PATCH 3/5] Unused imports --- plugin/core/configurations.py | 1 - plugin/core/types.py | 1 - plugin/core/windows.py | 1 - 3 files changed, 3 deletions(-) diff --git a/plugin/core/configurations.py b/plugin/core/configurations.py index badfd0dad..8fc51c566 100644 --- a/plugin/core/configurations.py +++ b/plugin/core/configurations.py @@ -11,7 +11,6 @@ from datetime import datetime, timedelta from weakref import WeakSet import sublime -import urllib.parse RETRY_MAX_COUNT = 5 diff --git a/plugin/core/types.py b/plugin/core/types.py index 125e77ee7..0d16b9132 100644 --- a/plugin/core/types.py +++ b/plugin/core/types.py @@ -16,7 +16,6 @@ import socket import sublime import time -import urllib.parse TCP_CONNECT_TIMEOUT = 5 # seconds diff --git a/plugin/core/windows.py b/plugin/core/windows.py index 2bbc14b00..589f2f12d 100644 --- a/plugin/core/windows.py +++ b/plugin/core/windows.py @@ -44,7 +44,6 @@ import json import sublime import threading -import urllib.parse _NO_DIAGNOSTICS_PLACEHOLDER = " No diagnostics. Well done!" From 637ee56fa0c4a29cb1cf616630e4592a3a09071e Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sun, 12 Nov 2023 16:02:04 +0100 Subject: [PATCH 4/5] Account for actual colons in the path part --- plugin/core/url.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/core/url.py b/plugin/core/url.py index 98f3af921..024379105 100644 --- a/plugin/core/url.py +++ b/plugin/core/url.py @@ -60,7 +60,7 @@ def parse_uri(uri: str) -> Tuple[str, str]: else: return parsed.scheme, path return parsed.scheme, path - elif parsed.scheme == '' and ':' in parsed.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 From 2769e442bb54aa1ad1ec925eb83c12c35136384f Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Sun, 12 Nov 2023 16:11:09 +0100 Subject: [PATCH 5/5] Add test for scheme parsing --- tests/test_url.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_url.py b/tests/test_url.py index 41e2fc9c5..9d782418a 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -71,3 +71,9 @@ def test_buffer_uri(self): view.buffer_id = unittest.mock.MagicMock(return_value=42) uri = view_to_uri(view) 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, "")