Skip to content

Commit

Permalink
cannot use an input handler to rename folder because it always displa…
Browse files Browse the repository at this point in the history
…ys LSP: Rename File

I cannot tweak the text inside the TextInputHandler
use show_input_panel instead...
  • Loading branch information
predragnikolic committed Jul 2, 2024
1 parent 057eaff commit cb82086
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Side Bar.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"caption": "LSP: Rename...",
"mnemonic": "l",
"command": "lsp_rename_path",
"command": "lsp_rename_path_sidebar",
"args": {"paths": []}
}
]
2 changes: 2 additions & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -148,6 +149,7 @@
"LspSymbolReferencesCommand",
"LspSymbolRenameCommand",
"LspRenamePathCommand",
"LspRenamePathSidebarCommand",
"LspSymbolTypeDefinitionCommand",
"LspToggleCodeLensesCommand",
"LspToggleHoverPopupsCommand",
Expand Down
52 changes: 33 additions & 19 deletions plugin/rename_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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):
Expand Down

0 comments on commit cb82086

Please sign in to comment.