From c7e738eb6c993f70b4c45d202dc99302c48c8f4b Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Fri, 25 Oct 2024 22:53:34 +0200 Subject: [PATCH 1/9] fix: consider priority_selector when using best_session during rename --- plugin/rename.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plugin/rename.py b/plugin/rename.py index 309d0e5d4..721d30e70 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -133,19 +133,19 @@ def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: if "new_name" in args: # Defer to "run" and trigger rename. return None - prepare_provider_session = self.best_session("renameProvider.prepareProvider") + point = args.get("point") + if not isinstance(point, int): + region = first_selection_region(self.view) + if region is None: + return None + point = region.b + prepare_provider_session = self.best_session("renameProvider.prepareProvider", point) if prepare_provider_session and "placeholder" not in args: # Defer to "run" and trigger "prepare" request. return None placeholder = args.get("placeholder", "") if not placeholder: - point = args.get("point") # guess the symbol name - if not isinstance(point, int): - region = first_selection_region(self.view) - if region is None: - return None - point = region.b placeholder = self.view.substr(self.view.word(point)) return RenameSymbolInputHandler(self.view, placeholder) @@ -161,7 +161,7 @@ def run( if listener: listener.purge_changes_async() location = get_position(self.view, event, point) - prepare_provider_session = self.best_session("renameProvider.prepareProvider") + prepare_provider_session = self.best_session("renameProvider.prepareProvider", point) if new_name or placeholder or not prepare_provider_session: if location is not None and new_name: self._do_rename(location, new_name) @@ -176,7 +176,7 @@ def run( request, partial(self._on_prepare_result, location), self._on_prepare_error) def _do_rename(self, position: int, new_name: str) -> None: - session = self.best_session(self.capability) + session = self.best_session(self.capability, position) if not session: return position_params = text_document_position_params(self.view, position) From 172201db7dd8aaa5f31844689bdc2743e9686de4 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sat, 26 Oct 2024 16:59:52 +0200 Subject: [PATCH 2/9] remember session between prepare and rename --- plugin/rename.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/plugin/rename.py b/plugin/rename.py index 721d30e70..5d16a094f 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -66,6 +66,8 @@ ] }) +PREPARE_RENAME_CAPABILITY = "renameProvider.prepareProvider" + def is_range_response(result: PrepareRenameResult) -> TypeGuard[Range]: return 'start' in result @@ -122,6 +124,7 @@ def is_visible( self, new_name: str = "", placeholder: str = "", + session_name: str | None = None, event: dict | None = None, point: int | None = None ) -> bool: @@ -132,19 +135,16 @@ def is_visible( def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: if "new_name" in args: # Defer to "run" and trigger rename. - return None - point = args.get("point") - if not isinstance(point, int): - region = first_selection_region(self.view) - if region is None: - return None - point = region.b + return + point = get_position(self.view, point=args.get('point')) prepare_provider_session = self.best_session("renameProvider.prepareProvider", point) if prepare_provider_session and "placeholder" not in args: # Defer to "run" and trigger "prepare" request. - return None + return placeholder = args.get("placeholder", "") if not placeholder: + if point is None: + return # guess the symbol name placeholder = self.view.substr(self.view.word(point)) return RenameSymbolInputHandler(self.view, placeholder) @@ -154,6 +154,7 @@ def run( edit: sublime.Edit, new_name: str = "", placeholder: str = "", + session_name: str | None = None, event: dict | None = None, point: int | None = None ) -> None: @@ -161,10 +162,13 @@ def run( if listener: listener.purge_changes_async() location = get_position(self.view, event, point) - prepare_provider_session = self.best_session("renameProvider.prepareProvider", point) + prepare_provider_session = \ + self.session_by_name(session_name, PREPARE_RENAME_CAPABILITY) if session_name \ + else self.best_session(PREPARE_RENAME_CAPABILITY, point) + prepare_provider_session = self.best_session(PREPARE_RENAME_CAPABILITY, point) if new_name or placeholder or not prepare_provider_session: if location is not None and new_name: - self._do_rename(location, new_name) + self._do_rename(location, new_name, prepare_provider_session) return # Trigger InputHandler manually. raise TypeError("required positional argument") @@ -172,11 +176,12 @@ def run( return params = cast(PrepareRenameParams, text_document_position_params(self.view, location)) request = Request.prepareRename(params, self.view, progress=True) + prepare_provider_session_name = prepare_provider_session.config.name prepare_provider_session.send_request( - request, partial(self._on_prepare_result, location), self._on_prepare_error) + request, partial(self._on_prepare_result, location, prepare_provider_session_name), self._on_prepare_error) - def _do_rename(self, position: int, new_name: str) -> None: - session = self.best_session(self.capability, position) + def _do_rename(self, position: int, new_name: str, preferred_session: Session | None) -> None: + session = preferred_session or self.best_session(self.capability, position) if not session: return position_params = text_document_position_params(self.view, position) @@ -204,7 +209,9 @@ def _on_rename_result_async(self, session: Session, response: WorkspaceEdit | No elif choice == sublime.DIALOG_NO: self._render_rename_panel(response, changes, total_changes, file_count, session.config.name) - def _on_prepare_result(self, pos: int, response: PrepareRenameResult | None) -> None: + def _on_prepare_result( + self, pos: int, preferred_session_name: str | None, response: PrepareRenameResult | None + ) -> None: if response is None: sublime.error_message("The current selection cannot be renamed") return @@ -217,7 +224,7 @@ def _on_prepare_result(self, pos: int, response: PrepareRenameResult | None) -> pos = range_to_region(response["range"], self.view).a # type: ignore else: placeholder = self.view.substr(self.view.word(pos)) - args = {"placeholder": placeholder, "point": pos} + args = {"placeholder": placeholder, "point": pos, "session_name": preferred_session_name} self.view.run_command("lsp_symbol_rename", args) def _on_prepare_error(self, error: Any) -> None: From f09a61a59fabd95345ae98a2c778d4ec596f8a2a Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sat, 26 Oct 2024 17:01:49 +0200 Subject: [PATCH 3/9] lint --- plugin/rename.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin/rename.py b/plugin/rename.py index 5d16a094f..98edbb853 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -13,7 +13,6 @@ from .core.registry import windows from .core.sessions import Session from .core.url import parse_uri -from .core.views import first_selection_region from .core.views import get_line from .core.views import range_to_region from .core.views import text_document_position_params From 51d584b9dbf35124377a319426ec51e82a2160f8 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sat, 26 Oct 2024 17:11:56 +0200 Subject: [PATCH 4/9] missing a case --- plugin/rename.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugin/rename.py b/plugin/rename.py index 98edbb853..0b5470354 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -136,7 +136,7 @@ def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: # Defer to "run" and trigger rename. return point = get_position(self.view, point=args.get('point')) - prepare_provider_session = self.best_session("renameProvider.prepareProvider", point) + prepare_provider_session = self._get_prepare_rename_session(point, args.get('session_name')) if prepare_provider_session and "placeholder" not in args: # Defer to "run" and trigger "prepare" request. return @@ -161,10 +161,7 @@ def run( if listener: listener.purge_changes_async() location = get_position(self.view, event, point) - prepare_provider_session = \ - self.session_by_name(session_name, PREPARE_RENAME_CAPABILITY) if session_name \ - else self.best_session(PREPARE_RENAME_CAPABILITY, point) - prepare_provider_session = self.best_session(PREPARE_RENAME_CAPABILITY, point) + prepare_provider_session = self._get_prepare_rename_session(point, session_name) if new_name or placeholder or not prepare_provider_session: if location is not None and new_name: self._do_rename(location, new_name, prepare_provider_session) @@ -179,6 +176,10 @@ def run( prepare_provider_session.send_request( request, partial(self._on_prepare_result, location, prepare_provider_session_name), self._on_prepare_error) + def _get_prepare_rename_session(self, point: int | None, session_name: str | None) -> Session | None: + return self.session_by_name(session_name, PREPARE_RENAME_CAPABILITY) if session_name \ + else self.best_session(PREPARE_RENAME_CAPABILITY, point) + def _do_rename(self, position: int, new_name: str, preferred_session: Session | None) -> None: session = preferred_session or self.best_session(self.capability, position) if not session: From faad7a7bf7dcb65b3b2bcb45132fe555217b2ee3 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sat, 26 Oct 2024 17:14:24 +0200 Subject: [PATCH 5/9] no need --- plugin/rename.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/rename.py b/plugin/rename.py index 0b5470354..877998792 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -140,7 +140,7 @@ def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: if prepare_provider_session and "placeholder" not in args: # Defer to "run" and trigger "prepare" request. return - placeholder = args.get("placeholder", "") + placeholder = args.get("placeholder") if not placeholder: if point is None: return From 0b46a29c6529adbe1194f92aa9e75658a39b1893 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sat, 26 Oct 2024 17:15:06 +0200 Subject: [PATCH 6/9] Revert "no need" This reverts commit faad7a7bf7dcb65b3b2bcb45132fe555217b2ee3. --- plugin/rename.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/rename.py b/plugin/rename.py index 877998792..0b5470354 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -140,7 +140,7 @@ def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: if prepare_provider_session and "placeholder" not in args: # Defer to "run" and trigger "prepare" request. return - placeholder = args.get("placeholder") + placeholder = args.get("placeholder", "") if not placeholder: if point is None: return From 55e864b55abf3f42803b5a8b54964b4997b46d70 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sat, 26 Oct 2024 17:17:40 +0200 Subject: [PATCH 7/9] rename --- plugin/rename.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugin/rename.py b/plugin/rename.py index 0b5470354..b4c409f5a 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -209,9 +209,7 @@ def _on_rename_result_async(self, session: Session, response: WorkspaceEdit | No elif choice == sublime.DIALOG_NO: self._render_rename_panel(response, changes, total_changes, file_count, session.config.name) - def _on_prepare_result( - self, pos: int, preferred_session_name: str | None, response: PrepareRenameResult | None - ) -> None: + def _on_prepare_result(self, pos: int, session_name: str | None, response: PrepareRenameResult | None) -> None: if response is None: sublime.error_message("The current selection cannot be renamed") return @@ -224,7 +222,7 @@ def _on_prepare_result( pos = range_to_region(response["range"], self.view).a # type: ignore else: placeholder = self.view.substr(self.view.word(pos)) - args = {"placeholder": placeholder, "point": pos, "session_name": preferred_session_name} + args = {"placeholder": placeholder, "point": pos, "session_name": session_name} self.view.run_command("lsp_symbol_rename", args) def _on_prepare_error(self, error: Any) -> None: From a30acde730e0442937116db12f5f0176f7d80637 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sun, 27 Oct 2024 15:59:47 +0100 Subject: [PATCH 8/9] renames --- plugin/rename.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/plugin/rename.py b/plugin/rename.py index a7d22c70f..bde23f62b 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -134,16 +134,19 @@ def is_visible( def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: if "new_name" in args: # Defer to "run" and trigger rename. - return + return None point = get_position(self.view, point=args.get('point')) - prepare_provider_session = self._get_prepare_rename_session(point, args.get('session_name')) - if prepare_provider_session and "placeholder" not in args: + if point is None: + # Defer to "run" and trigger rename. + return None + session = self._get_prepare_rename_session(point, args.get('session_name')) + if session and "placeholder" not in args: # Defer to "run" and trigger "prepare" request. - return + return None placeholder = args.get("placeholder", "") if not placeholder: if point is None: - return + return None # guess the symbol name placeholder = self.view.substr(self.view.word(point)) return RenameSymbolInputHandler(self.view, placeholder) @@ -161,10 +164,10 @@ def run( if listener: listener.purge_changes_async() location = get_position(self.view, event, point) - prepare_provider_session = self._get_prepare_rename_session(point, session_name) - if new_name or placeholder or not prepare_provider_session: + session = self._get_prepare_rename_session(point, session_name) + if new_name or placeholder or not session: if location is not None and new_name: - self._do_rename(location, new_name, prepare_provider_session) + self._do_rename(location, new_name, session) return # Trigger InputHandler manually. raise TypeError("required positional argument") @@ -172,9 +175,8 @@ def run( return params = cast(PrepareRenameParams, text_document_position_params(self.view, location)) request = Request.prepareRename(params, self.view, progress=True) - prepare_provider_session_name = prepare_provider_session.config.name - prepare_provider_session.send_request( - request, partial(self._on_prepare_result, location, prepare_provider_session_name), self._on_prepare_error) + session.send_request( + request, partial(self._on_prepare_result, location, session.config.name), self._on_prepare_error) def _get_prepare_rename_session(self, point: int | None, session_name: str | None) -> Session | None: return self.session_by_name(session_name, PREPARE_RENAME_CAPABILITY) if session_name \ From 8c5b19033dc56c4443583e639ac2eb9f38ba8686 Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Sun, 27 Oct 2024 16:49:59 +0100 Subject: [PATCH 9/9] remove --- plugin/rename.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugin/rename.py b/plugin/rename.py index bde23f62b..bbbf82ea0 100644 --- a/plugin/rename.py +++ b/plugin/rename.py @@ -145,8 +145,6 @@ def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: return None placeholder = args.get("placeholder", "") if not placeholder: - if point is None: - return None # guess the symbol name placeholder = self.view.substr(self.view.word(point)) return RenameSymbolInputHandler(self.view, placeholder)