From cb82086537649f152a298e2dbf1ac4eba054ab48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=80=D0=B5=D0=B4=D1=80=D0=B0=D0=B3=20=D0=9D=D0=B8?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB=D0=B8=D1=9B?= Date: Tue, 2 Jul 2024 20:32:47 +0200 Subject: [PATCH] cannot use an input handler to rename folder because it always displays LSP: Rename File I cannot tweak the text inside the TextInputHandler use show_input_panel instead... --- Side Bar.sublime-menu | 2 +- boot.py | 2 ++ plugin/rename_file.py | 52 +++++++++++++++++++++++++++---------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/Side Bar.sublime-menu b/Side Bar.sublime-menu index a82bbd17c..ae55009a7 100644 --- a/Side Bar.sublime-menu +++ b/Side Bar.sublime-menu @@ -2,7 +2,7 @@ { "caption": "LSP: Rename...", "mnemonic": "l", - "command": "lsp_rename_path", + "command": "lsp_rename_path_sidebar", "args": {"paths": []} } ] diff --git a/boot.py b/boot.py index 32de129c4..020add216 100644 --- a/boot.py +++ b/boot.py @@ -70,6 +70,7 @@ from .plugin.rename import LspHideRenameButtonsCommand from .plugin.rename import LspSymbolRenameCommand from .plugin.rename_file import LspRenamePathCommand +from .plugin.rename_file import LspRenamePathSidebarCommand from .plugin.save_command import LspSaveAllCommand from .plugin.save_command import LspSaveCommand from .plugin.selection_range import LspExpandSelectionCommand @@ -148,6 +149,7 @@ "LspSymbolReferencesCommand", "LspSymbolRenameCommand", "LspRenamePathCommand", + "LspRenamePathSidebarCommand", "LspSymbolTypeDefinitionCommand", "LspToggleCodeLensesCommand", "LspToggleHoverPopupsCommand", diff --git a/plugin/rename_file.py b/plugin/rename_file.py index 9098f9774..ff791089e 100644 --- a/plugin/rename_file.py +++ b/plugin/rename_file.py @@ -9,9 +9,32 @@ import os import sublime import sublime_plugin - - -class RenameFileInputHandler(sublime_plugin.TextInputHandler): +import functools + + +class LspRenamePathSidebarCommand(LspWindowCommand): + def run(self, paths: list[str] | None = None) -> None: + old_path = paths[0] if paths else None + path_name = Path(old_path or "").name + v = self.window.show_input_panel( + "(LSP) New Name:", + path_name, + functools.partial(self.on_done, old_path), + None, + None) + v.sel().clear() + name, _ext = os.path.splitext(path_name) + v.sel().add(sublime.Region(0, len(name))) + + def on_done(self, old_path: str | None, new_name: str) -> None: + if new_name: + self.window.run_command('lsp_rename_path', { + "new_name": new_name, + "old_path": old_path + }) + + +class RenamePathInputHandler(sublime_plugin.TextInputHandler): def __init__(self, path: str) -> None: self.path = path @@ -44,17 +67,18 @@ def want_event(self) -> bool: def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: if "new_name" in args: return None - old_path = self.get_old_path(args.get('paths'), self.window.active_view()) - return RenameFileInputHandler(Path(old_path or "").name) + view = self.window.active_view() + old_path = view.file_name() if view else None + return RenamePathInputHandler(Path(old_path or "").name) def run( self, new_name: str, # new_name can be: FILE_NAME.xy OR ./FILE_NAME.xy OR ../../FILE_NAME.xy - paths: list[str] | None = None, # exist when invoked from the sidebar with "LSP: Rename..." + old_path: str | None = None, ) -> None: session = self.session() view = self.window.active_view() - old_path = self.get_old_path(paths, view) + old_path = old_path or view.file_name() if view else None if old_path is None: # handle renaming buffers if view: view.set_name(new_name) @@ -69,12 +93,8 @@ def run( "oldUri": urljoin("file:", old_path), }] } - if not session: - self.rename_path(old_path, new_path) - self.notify_did_rename(rename_file_params, new_path, view) - return - file_operation_options = session.get_capability('workspace.fileOperations.willRename') - if file_operation_options and match_file_operation_filters(file_operation_options, old_path, view): + file_operation_options = session.get_capability('workspace.fileOperations.willRename') if session else None + if session and file_operation_options and match_file_operation_filters(file_operation_options, old_path, view): request = Request.willRenameFiles(rename_file_params) session.send_request( request, @@ -84,12 +104,6 @@ def run( self.rename_path(old_path, new_path) self.notify_did_rename(rename_file_params, new_path, view) - def get_old_path(self, paths: list[str] | None, view: sublime.View | None) -> str | None: - if paths: - return paths[0] - if view: - return view.file_name() - def handle(self, res: WorkspaceEdit | None, session_name: str, old_path: str, new_path: str, rename_file_params: RenameFilesParams, view: sublime.View | None) -> None: if session := self.session_by_name(session_name):