-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Added command to rename current document, with lsp support #5531
Conversation
There is already #4514 but it doesn't cover the LSP renaming |
Nice one! Really happy that it includes LSP rename support 👍🏻 |
I added the rnm alias for the command, referring to #4514 (comment) |
I wonder if it would be good to add a feature to automatically save suggested changes from the lsp if a file didn't have changes before, saves the trouble of opening every file and saving manually, or perhaps a status message saying which files changed? |
Looking forward to this! |
I need to test this, but do suggested changes cause non-open files to be opened?
IMO it's not needed, as |
Ah I didn't even know about |
It's a vimism. There's also |
Should the lsp errors be displayed in the status bar, along with the message that the file was successfully renamed? |
Why it is not merged already? |
Waiting for reviews |
Can anyone review or contributors only? |
Only "reviewers with write access" and I can't even request reviews |
@@ -2752,6 +2812,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ | |||
fun: reset_diff_change, | |||
signature: CommandSignature::none(), | |||
}, | |||
TypableCommand { | |||
name: "rename", | |||
aliases: &["rnm"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aliases: &["rnm"], | |
aliases: &["mv"], |
I think rnm
is a bit of a weird alias and not commonly used. Considering how widely used the shell command is I think mv
could be a good alias
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do set the alias to move, we're probably better of with calling the command itself move
or move_file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have other commands that have totally different aliases too so I think leaving it like that is fine too
TypableCommand { | ||
name: "rename", | ||
aliases: &["rnm"], | ||
doc: "Rename the currently selected buffer", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc: "Rename the currently selected buffer", | |
doc: "Move the current buffer and its corresponding file to a different path", |
I think we need to highlight here that this is different than just :w new_path
. Ofcourse this just a short help string but I think calling it move is generally clearer
.canonicalize() | ||
.map_err(|_| anyhow!("Could not get absolute path of the document"))?; | ||
let mut path_new = path.clone(); | ||
path_new.set_file_name(OsStr::new(new_name.as_ref())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
path_new.set_file_name(OsStr::new(new_name.as_ref())); | |
path_new.parent().expect("is a file").join(new_name); |
I think we should allow moving to new paths not just new filenames
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that this means that the destination directory might not exist so we need rename!
too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a sort of weird one, if we change the command name to move
as previously said, probably a good ide, thoguh mv
in bash takes absolute paths, not ones relative to the file but I think it's work
On the other hand I don't think a file
renaming function should be able to move it as most file managers just let you rename it
Imo, move
is probably better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it might make mose sense to just call the entire command move and make the destination a path relative to CWD. If somebody wants to just change the filename they can use %filname/../<new_name>
in the future.
I think that generally move is a better idea than rename because it can do more (and the LS can handle full moves for example RA can move a module to a totally different module
if apply_workspace_edit(cx.editor, helix_lsp::OffsetEncoding::Utf8, &edit) | ||
.is_err() | ||
{ | ||
log::error!(":rename command failed to apply edits") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we already show an error message herer you don't need to display it. The returned result is just forcases where we need to communicate to the LSP that the edit failed (not here)
doc.set_path(Some(path_new.as_path())) | ||
.map_err(|_| anyhow!("File renamed, but could not set path of the document"))?; | ||
|
||
if let Some(lsp_client) = doc.language_server() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the wii rename request must be sent (and processed) before the rename happens so this needs to be moved to the start of the function:
The will rename files request is sent from the client to the server before files are actually renamed as long as the rename is triggered from within the client either by a user action or by applying a workspace edit. The request can return a WorkspaceEdit which will be applied to workspace before the files are renamed.
At the end of the function you must send didRename:
The did rename files notification is sent from the client to the server when files were renamed from within the client.
you also need to support mullti LSP here. Probably ok to just send wiiRename to the first server that supports it and didRename to all of them
} | ||
|
||
pub fn did_rename_files(&self, params: &[lsp::FileRename]) -> impl Future<Output = Result<()>> { | ||
let files = params.to_owned(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this must check the server capabiliy workspace.fileOperations.didRename
&self, | ||
params: &Vec<lsp::FileRename>, | ||
) -> impl Future<Output = Result<lsp::WorkspaceEdit>> { | ||
let files = params.to_owned(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to check workspace.fileOperations.willRename
herer (or even better at the callsite to support multiple LSPs)
you are probably new to opensource. A PR needs to be reviewed by a miantainer before its merged. Only maintainers can review PRs. We can't just trust random people with code review. There are hundreds of issues and PRs for us to get through so it can take a while. |
yeah, I am new. Thanks for providing information about reviews! |
Co-authored-by: Pascal Kuthe <[email protected]>
Co-authored-by: Pascal Kuthe <[email protected]>
Co-authored-by: Pascal Kuthe <[email protected]>
Co-authored-by: Pascal Kuthe <[email protected]>
Co-authored-by: Pascal Kuthe <[email protected]>
Co-authored-by: Pascal Kuthe <[email protected]>
let (_, doc) = current!(cx.editor); | ||
let path = doc | ||
.path() | ||
.ok_or_else(|| anyhow!("Scratch buffers can not be renamed, use :write"))? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.ok_or_else(|| anyhow!("Scratch buffers can not be renamed, use :write"))? | |
.ok_or_else(|| anyhow!("Scratch buffers can not be renamed, use :write"))?; |
#4393
Includes willRenameFiles and didRenameFiles requests to the language server.
Honestly not sure if didRenameFiles is even needed here
Should file changes suggested by the language server be automatically saved if the file didn't have any other changes?