diff --git a/plugin/core/configurations.py b/plugin/core/configurations.py index 96aecfe2e..8fc51c566 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 @@ -10,7 +11,6 @@ from datetime import datetime, timedelta from weakref import WeakSet import sublime -import urllib.parse RETRY_MAX_COUNT = 5 @@ -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 diff --git a/plugin/core/types.py b/plugin/core/types.py index c90805070..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 @@ -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): diff --git a/plugin/core/url.py b/plugin/core/url.py index 696ef9056..024379105 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) @@ -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 diff --git a/plugin/core/windows.py b/plugin/core/windows.py index 5ebebcc36..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!" @@ -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) @@ -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 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) diff --git a/tests/test_url.py b/tests/test_url.py index 532278da5..9d782418a 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -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, "")