From a936d938ead4488da1dd0bcc4469e456405631c0 Mon Sep 17 00:00:00 2001 From: Ultraman Date: Wed, 18 Oct 2023 22:09:55 +0800 Subject: [PATCH 1/5] fix: capability with documentSelector --- plugin/core/sessions.py | 12 ++++++++---- plugin/session_buffer.py | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index f0080bc36..3c45a95e0 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -1338,6 +1338,9 @@ def session_buffers_async(self) -> Generator[SessionBufferProtocol, None, None]: """ yield from self._session_buffers + def get_session_buffer_by_id(self, buffer_id: int) -> SessionBufferProtocol: + return next(filter(lambda buffer: buffer.is_self(buffer_id), self._session_buffers), None) + def get_session_buffer_for_uri_async(self, uri: DocumentUri) -> Optional[SessionBufferProtocol]: scheme, path = parse_uri(uri) if scheme == "file": @@ -1619,7 +1622,8 @@ def run_code_action_async( # A code action can have an edit and/or command. Note that it can have *both*. In case both are present, we # must apply the edits before running the command. code_action = cast(CodeAction, code_action) - return self._maybe_resolve_code_action(code_action) \ + session_buffer = self.get_session_buffer_by_id(view.buffer_id()) + return self._maybe_resolve_code_action(code_action, session_buffer) \ .then(lambda code_action: self._apply_code_action_async(code_action, view)) def open_uri_async( @@ -1704,9 +1708,9 @@ def notify_plugin_on_session_buffer_change(self, session_buffer: SessionBufferPr if self._plugin: self._plugin.on_session_buffer_changed_async(session_buffer) - def _maybe_resolve_code_action(self, code_action: CodeAction) -> Promise[Union[CodeAction, Error]]: - if "edit" not in code_action and self.has_capability("codeActionProvider.resolveProvider"): - # TODO: Should we accept a SessionBuffer? What if this capability is registered with a documentSelector? + def _maybe_resolve_code_action(self, code_action: CodeAction, session_buffer: SessionBufferProtocol) -> Promise[Union[CodeAction, Error]]: + has_capability = self.has_capability("codeActionProvider.resolveProvider") or session_buffer.has_capability("codeActionProvider.resolveProvider") + if "edit" not in code_action and has_capability: # We must first resolve the command and edit properties, because they can potentially be absent. request = Request("codeAction/resolve", code_action) return self.send_request_task(request) diff --git a/plugin/session_buffer.py b/plugin/session_buffer.py index 2012b97c7..55aa3b8d0 100644 --- a/plugin/session_buffer.py +++ b/plugin/session_buffer.py @@ -185,6 +185,9 @@ def get_view_in_group(self, group: int) -> sublime.View: return view return next(iter(self.session_views)).view + def is_self(self, id: int) -> bool: + return self._id == id; + @property def language_id(self) -> str: """ From 5d6e7957142641ff9d702cc03982fe1bda9dbce1 Mon Sep 17 00:00:00 2001 From: Ultraman Date: Thu, 19 Oct 2023 00:35:27 +0800 Subject: [PATCH 2/5] misc(style): update style --- plugin/core/sessions.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 3c45a95e0..5d00fdfc4 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -605,6 +605,9 @@ def get_language_id(self) -> Optional[str]: def get_view_in_group(self, group: int) -> sublime.View: ... + def is_self(self, buffer_id: int) -> bool: + ... + def register_capability_async( self, registration_id: str, @@ -1338,7 +1341,7 @@ def session_buffers_async(self) -> Generator[SessionBufferProtocol, None, None]: """ yield from self._session_buffers - def get_session_buffer_by_id(self, buffer_id: int) -> SessionBufferProtocol: + def get_session_buffer_by_id(self, buffer_id: int) -> Optional[SessionBufferProtocol]: return next(filter(lambda buffer: buffer.is_self(buffer_id), self._session_buffers), None) def get_session_buffer_for_uri_async(self, uri: DocumentUri) -> Optional[SessionBufferProtocol]: @@ -1622,8 +1625,7 @@ def run_code_action_async( # A code action can have an edit and/or command. Note that it can have *both*. In case both are present, we # must apply the edits before running the command. code_action = cast(CodeAction, code_action) - session_buffer = self.get_session_buffer_by_id(view.buffer_id()) - return self._maybe_resolve_code_action(code_action, session_buffer) \ + return self._maybe_resolve_code_action(code_action, view) \ .then(lambda code_action: self._apply_code_action_async(code_action, view)) def open_uri_async( @@ -1708,12 +1710,19 @@ def notify_plugin_on_session_buffer_change(self, session_buffer: SessionBufferPr if self._plugin: self._plugin.on_session_buffer_changed_async(session_buffer) - def _maybe_resolve_code_action(self, code_action: CodeAction, session_buffer: SessionBufferProtocol) -> Promise[Union[CodeAction, Error]]: - has_capability = self.has_capability("codeActionProvider.resolveProvider") or session_buffer.has_capability("codeActionProvider.resolveProvider") - if "edit" not in code_action and has_capability: - # We must first resolve the command and edit properties, because they can potentially be absent. - request = Request("codeAction/resolve", code_action) - return self.send_request_task(request) + def _maybe_resolve_code_action(self, code_action: CodeAction, view: Optional[sublime.View]) -> Promise[Union[CodeAction, Error]]: + if "edit" not in code_action: + has_capability = self.has_capability("codeActionProvider.resolveProvider") + + if not has_capability and view: + session_buffer = self.get_session_buffer_by_id(view.buffer_id()) + if session_buffer: + has_capability = session_buffer.has_capability("codeActionProvider.resolveProvider") + + if has_capability: + # We must first resolve the command and edit properties, because they can potentially be absent. + request = Request("codeAction/resolve", code_action) + return self.send_request_task(request) return Promise.resolve(code_action) def _apply_code_action_async( From 9eb4d7f5455df91efcc170022c0170f10ef60ced Mon Sep 17 00:00:00 2001 From: Ultraman Date: Thu, 19 Oct 2023 02:41:44 +0800 Subject: [PATCH 3/5] style: simplify and follow style convention --- plugin/core/sessions.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 5d00fdfc4..9eb5030bb 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -1341,9 +1341,6 @@ def session_buffers_async(self) -> Generator[SessionBufferProtocol, None, None]: """ yield from self._session_buffers - def get_session_buffer_by_id(self, buffer_id: int) -> Optional[SessionBufferProtocol]: - return next(filter(lambda buffer: buffer.is_self(buffer_id), self._session_buffers), None) - def get_session_buffer_for_uri_async(self, uri: DocumentUri) -> Optional[SessionBufferProtocol]: scheme, path = parse_uri(uri) if scheme == "file": @@ -1713,12 +1710,10 @@ def notify_plugin_on_session_buffer_change(self, session_buffer: SessionBufferPr def _maybe_resolve_code_action(self, code_action: CodeAction, view: Optional[sublime.View]) -> Promise[Union[CodeAction, Error]]: if "edit" not in code_action: has_capability = self.has_capability("codeActionProvider.resolveProvider") - if not has_capability and view: - session_buffer = self.get_session_buffer_by_id(view.buffer_id()) - if session_buffer: - has_capability = session_buffer.has_capability("codeActionProvider.resolveProvider") - + session_view = self.session_view_for_view_async(view) + if session_view: + has_capability = session_view.has_capability_async("codeActionProvider.resolveProvider") if has_capability: # We must first resolve the command and edit properties, because they can potentially be absent. request = Request("codeAction/resolve", code_action) From 86c63ce6dd5c790e386e69d7329c1a1f5cc7ce8a Mon Sep 17 00:00:00 2001 From: Ultraman Date: Thu, 19 Oct 2023 13:12:04 +0800 Subject: [PATCH 4/5] fix: Remove unnecessary changes --- plugin/core/sessions.py | 3 --- plugin/session_buffer.py | 3 --- 2 files changed, 6 deletions(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 9eb5030bb..95da1177a 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -605,9 +605,6 @@ def get_language_id(self) -> Optional[str]: def get_view_in_group(self, group: int) -> sublime.View: ... - def is_self(self, buffer_id: int) -> bool: - ... - def register_capability_async( self, registration_id: str, diff --git a/plugin/session_buffer.py b/plugin/session_buffer.py index 55aa3b8d0..2012b97c7 100644 --- a/plugin/session_buffer.py +++ b/plugin/session_buffer.py @@ -185,9 +185,6 @@ def get_view_in_group(self, group: int) -> sublime.View: return view return next(iter(self.session_views)).view - def is_self(self, id: int) -> bool: - return self._id == id; - @property def language_id(self) -> str: """ From e03356f6f778b11df4ccef4a15e777c3a4b503ac Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Thu, 19 Oct 2023 14:48:28 +0200 Subject: [PATCH 5/5] Update plugin/core/sessions.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rafał Chłodnicki --- plugin/core/sessions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 95da1177a..08b8ee996 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -1704,7 +1704,9 @@ def notify_plugin_on_session_buffer_change(self, session_buffer: SessionBufferPr if self._plugin: self._plugin.on_session_buffer_changed_async(session_buffer) - def _maybe_resolve_code_action(self, code_action: CodeAction, view: Optional[sublime.View]) -> Promise[Union[CodeAction, Error]]: + def _maybe_resolve_code_action( + self, code_action: CodeAction, view: Optional[sublime.View] + ) -> Promise[Union[CodeAction, Error]]: if "edit" not in code_action: has_capability = self.has_capability("codeActionProvider.resolveProvider") if not has_capability and view: