Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider priority_selector when using best_session during rename #2538

Merged
merged 10 commits into from
Oct 27, 2024
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions plugin/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +65,8 @@
]
})

PREPARE_RENAME_CAPABILITY = "renameProvider.prepareProvider"


def is_range_response(result: PrepareRenameResult) -> TypeGuard[Range]:
return 'start' in result
Expand Down Expand Up @@ -122,6 +123,7 @@ def is_visible(
self,
new_name: str = "",
placeholder: str = "",
session_name: str | None = None,
event: dict | None = None,
point: int | None = None
) -> bool:
Expand All @@ -132,20 +134,17 @@ 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
prepare_provider_session = self.best_session("renameProvider.prepareProvider")
return
jwortmann marked this conversation as resolved.
Show resolved Hide resolved
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:
# Defer to "run" and trigger "prepare" request.
return None
return
placeholder = args.get("placeholder", "")
if not placeholder:
point = args.get("point")
if point is None:
return
# 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)

Expand All @@ -154,29 +153,35 @@ def run(
edit: sublime.Edit,
new_name: str = "",
placeholder: str = "",
session_name: str | None = None,
event: dict | None = None,
point: int | None = None
) -> None:
listener = self.get_listener()
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._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)
self._do_rename(location, new_name, prepare_provider_session)
return
# Trigger InputHandler manually.
raise TypeError("required positional argument")
if location is None:
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
jwortmann marked this conversation as resolved.
Show resolved Hide resolved
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 _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) -> None:
session = self.best_session(self.capability)
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)
Expand Down Expand Up @@ -204,7 +209,7 @@ def _on_rename_result_async(self, session: Session, response: WorkspaceEdit | No
elif choice == sublime.DialogResult.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, session_name: str | None, response: PrepareRenameResult | None) -> None:
if response is None:
sublime.error_message("The current selection cannot be renamed")
return
Expand All @@ -217,7 +222,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": session_name}
self.view.run_command("lsp_symbol_rename", args)

def _on_prepare_error(self, error: Any) -> None:
Expand Down