-
Notifications
You must be signed in to change notification settings - Fork 189
Add willRename and didRename fileOperations #2498
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
Open
predragnikolic
wants to merge
86
commits into
main
Choose a base branch
from
add-will-rename-and-did-rename
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+207
−1
Open
Changes from 1 commit
Commits
Show all changes
86 commits
Select commit
Hold shift + click to select a range
f5ec6c5
add willRename and didRename fileOperations
predragnikolic e26a48e
always enable
predragnikolic e6304c2
add renaming from the sidebar
predragnikolic e5e0722
changes
predragnikolic 5c91301
fix styles
predragnikolic 075e50e
add new line
predragnikolic 814b0bb
hande if new path directory doesn't exist
predragnikolic fb42ffc
Merge branch 'main' into add-will-rename-and-did-rename
predragnikolic d488b06
handle renaming buffers
predragnikolic d1a10cf
ahh...
predragnikolic 08e3af1
add initial_selection
predragnikolic 6f65462
improve initial_selection and rename file_name to path
predragnikolic c9e8772
Split "Lsp: Rename" to "Lsp: Rename Folder" and "Lsp: Rename File"
predragnikolic e0c9c82
new line
predragnikolic a52b5c4
when renaming a directory, it would be good to retarget all open view…
predragnikolic 2acceb6
save some lines
predragnikolic 9ab9dc5
remove more lines
predragnikolic f5c26a6
few less lines
predragnikolic 437659f
simpler conditions
predragnikolic 3647f92
avoid multiple statements on one line
predragnikolic 3389f66
implement FileOperationFilter
predragnikolic 4785d49
remomve few lines
predragnikolic 2f5af31
fix flake
predragnikolic 890667c
fix pyright
predragnikolic af04c56
remove is_visible code
predragnikolic 178adae
remove LSP: Rename File and Rename folder in favor of LSP: Rename... …
predragnikolic 8ef443c
rename LspRenameFileCommand to LspRenamePathCommand
predragnikolic d1342b9
remove FileOperationFilterChecker, FileOperationFilterMatcher in favo…
predragnikolic 057eaff
flake8 fixes
predragnikolic cb82086
cannot use an input handler to rename folder because it always displa…
predragnikolic 728c6ea
Update plugin/core/types.py
predragnikolic 93b024a
remove unnecessary if
predragnikolic 1ff4648
remove LSP: Rename... from sidebar in favor of overriding the existin…
predragnikolic 1ae7ec4
always enable LspRenamePathCommand
predragnikolic 6181644
handle OS errors
predragnikolic 181f17c
except Exception
predragnikolic 18e8549
Remove "LSP: Rename File" commands, instead override Default ST commands
predragnikolic 51962f1
remove unused import
predragnikolic 21f1bf7
Merge branch 'main' into add-will-rename-and-did-rename
predragnikolic 814cb6b
Merge branch 'main' into add-will-rename-and-did-rename
predragnikolic 0e0daa7
prevent save prompt to show up
predragnikolic 51670f9
Introduce "LSP: Rename Path" and rename "LSP: Rename" to "LSP: Rename…
predragnikolic f530756
fix flake
predragnikolic 5df5b43
Merge branch 'main' into add-will-rename-and-did-rename
predragnikolic defedc7
Update plugin/rename_file.py
predragnikolic 848e6cd
Update plugin/rename_file.py
predragnikolic 7cea663
return early in case of exception
predragnikolic d198d3f
rename handle to _handle_response_async
predragnikolic d1c2ba8
move session closer to where it is used
predragnikolic 26f53d3
add return types
predragnikolic f292c0c
use filename_to_uri
predragnikolic 2ffe948
dont use abbreviations
predragnikolic 50a80aa
pattern and filters are required fields in the LSP spec
predragnikolic 54626f4
import from ..protocol
predragnikolic 9c608fe
remove view arg in match_file_operation_filters and update code
predragnikolic 6b8fd30
trigger rename_path and notify_did_rename after apply_workspace_edit_…
predragnikolic 672cdf2
Update boot.py
predragnikolic 4fcac0e
use path resolve and inline run method signature
predragnikolic ca12477
make it more obvious that this is the file_name
predragnikolic c7f26ff
fix rename folder from sidebar
predragnikolic fb9a710
remove if for pattern
predragnikolic 06accb2
Update plugin/core/types.py
predragnikolic 28b1e1b
Update plugin/rename_file.py
predragnikolic add6233
fix flake
predragnikolic e004c0b
early return if new_name is like old name
predragnikolic d0b7d49
have same check for is_case_change as ST native rename command
predragnikolic a5c0609
be consistent, this was the only method that started with _
predragnikolic 9f667a3
notify_did_rename only after a successful rename
predragnikolic 7f1d829
Update plugin/rename_file.py
predragnikolic a83b956
Update plugin/rename_file.py
predragnikolic 4089bfe
Update plugin/rename_file.py
predragnikolic 02bbbd8
Update plugin/rename_file.py
predragnikolic 55dbdd7
Use Path instead of os.path
predragnikolic ca52462
flake
predragnikolic dc23125
move a bit closer to the if branch
predragnikolic 092a179
rename file_operation_options to file_operations for consistency
predragnikolic 4af0ae6
move notify did rename later
predragnikolic be87d19
check if old_path is a directory before we rename
predragnikolic 6afa7c2
fix flake
predragnikolic 72a162d
don't consider matching if filters is an empty array
predragnikolic fbcbec5
send request to all sessions
predragnikolic 140ffca
fix flake
predragnikolic 91c34cf
Merge branch 'main' into add-will-rename-and-did-rename
predragnikolic 057391a
schedule the requests to be done on the async thread
predragnikolic 48650c5
fix flake by renaming the method
predragnikolic 7e87b26
visual indent
predragnikolic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
add willRename and didRename fileOperations
- Loading branch information
commit f5ec6c59f179c1dff93c6b2a25f9286201600e81
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from .core.open import open_file_uri | ||
| from .core.protocol import Notification, RenameFilesParams, Request, WorkspaceEdit | ||
rchl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from .core.registry import LspTextCommand | ||
| from .core.url import parse_uri | ||
| from .core.views import did_open_text_document_params, uri_from_view | ||
| from pathlib import Path | ||
| from urllib.parse import urljoin | ||
| import os | ||
| import sublime | ||
| import sublime_plugin | ||
|
|
||
|
|
||
| class RenameFileInputHandler(sublime_plugin.TextInputHandler): | ||
| def want_event(self) -> bool: | ||
| return False | ||
|
|
||
| def __init__(self, file_name: str) -> None: | ||
| self.file_name = file_name | ||
|
|
||
| def name(self) -> str: | ||
| return "new_name" | ||
|
|
||
| def placeholder(self) -> str: | ||
| return self.file_name | ||
|
|
||
| def initial_text(self) -> str: | ||
| return self.placeholder() | ||
|
|
||
| def validate(self, name: str) -> bool: | ||
| return len(name) > 0 | ||
|
|
||
| class LspRenameFileCommand(LspTextCommand): | ||
| capability = 'workspace.fileOperations.willRename' | ||
|
|
||
| def want_event(self) -> bool: | ||
| return False | ||
|
|
||
| def input(self, args: dict) -> sublime_plugin.TextInputHandler | None: | ||
| if "new_name" in args: | ||
| return None | ||
| return RenameFileInputHandler(Path(self.view.file_name() or "").name) | ||
|
|
||
| def run( | ||
| self, | ||
| _edit: sublime.Edit, | ||
| new_name: str = "", # new_name can be: FILE_NAME.xy OR ./FILE_NAME.xy OR ../../FILE_NAME.xy | ||
| ) -> None: | ||
| session = self.best_session("workspace.fileOperations.willRename") | ||
| if not session: | ||
| return | ||
| current_file_path = self.view.file_name() or '' | ||
| new_file_path = os.path.normpath(Path(current_file_path).parent / new_name) | ||
| window = self.view.window() | ||
| if os.path.exists(new_file_path) and window: | ||
| window.status_message(f'Unable to Rename. File already exists') | ||
| return | ||
| rename_file_params: RenameFilesParams = { | ||
| "files": [{ | ||
| "newUri": urljoin("file:", new_file_path), | ||
| "oldUri": uri_from_view(self.view), | ||
| }] | ||
| } | ||
| request = Request.willRenameFiles(rename_file_params) | ||
| session.send_request(request, lambda res: self.handle(res, session.config.name, new_name, rename_file_params)) | ||
|
|
||
| def handle(self, res: WorkspaceEdit | None, session_name: str, new_name: str, rename_file_params: RenameFilesParams) -> None: | ||
| window = self.view.window() | ||
| session = self.session_by_name(session_name) | ||
| if session and window: | ||
| # LSP spec - Apply WorkspaceEdit before the files are renamed | ||
| if res: | ||
rchl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| session.apply_workspace_edit_async(res, is_refactoring=True) | ||
rchl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| renamed_file = rename_file_params['files'][0] | ||
| old_regions = [region for region in self.view.sel()] | ||
| self.view.close() # LSP spec - send didClose for old file | ||
| # actally rename the file, this will create a new file | ||
| os.rename( | ||
| parse_uri(renamed_file['oldUri'])[1], | ||
| parse_uri(renamed_file['newUri'])[1] | ||
| ) | ||
| # LSP spec - send didOpen for the new file | ||
| open_file_uri(window, renamed_file['newUri']) \ | ||
| .then(lambda v: v and v.sel().add_all(old_regions)) | ||
| for session in self.sessions('workspace.fileOperations.didRename'): | ||
| session.send_notification(Notification.didRenameFiles(rename_file_params)) | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.