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

Added command to rename current document, with lsp support #5531

Closed
wants to merge 21 commits into from

Conversation

ontley
Copy link
Contributor

@ontley ontley commented Jan 14, 2023

#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?

@ontley ontley marked this pull request as draft January 14, 2023 13:26
@the-mikedavis
Copy link
Member

There is already #4514 but it doesn't cover the LSP renaming

@ontley ontley marked this pull request as ready for review January 14, 2023 16:23
@ontley ontley changed the title Added command to rename current document Added command to rename current document, with lsp support Jan 14, 2023
@cor
Copy link
Contributor

cor commented Jan 18, 2023

Nice one! Really happy that it includes LSP rename support 👍🏻

@cor cor mentioned this pull request Jan 18, 2023
@ontley
Copy link
Contributor Author

ontley commented Jan 18, 2023

I added the rnm alias for the command, referring to #4514 (comment)

@ontley
Copy link
Contributor Author

ontley commented Jan 18, 2023

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?

@devgioele
Copy link
Contributor

Looking forward to this!

@edrex
Copy link

edrex commented Mar 23, 2023

I need to test this, but do suggested changes cause non-open files to be opened?

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

IMO it's not needed, as :wa is easy enough, and there's already editor.auto-save. I get wanting transactionality, since the rename is immediate, but IDT it warrants a special case.

@ontley ontley closed this Mar 24, 2023
@ontley ontley reopened this Mar 24, 2023
@ontley
Copy link
Contributor Author

ontley commented Mar 24, 2023

I need to test this, but do suggested changes cause non-open files to be opened?

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

IMO it's not needed, as :wa is easy enough, and there's already editor.auto-save. I get wanting transactionality, since the rename is immediate, but IDT it warrants a special case.

Ah I didn't even know about :wa, I guess it's not needed then

@edrex
Copy link

edrex commented Mar 26, 2023

Ah I didn't even know about :wa

It's a vimism. There's also :wqa.

@ontley
Copy link
Contributor Author

ontley commented Mar 27, 2023

Should the lsp errors be displayed in the status bar, along with the message that the file was successfully renamed?

@pascalkuthe pascalkuthe modified the milestones: 23.03, next Mar 27, 2023
@ghost
Copy link

ghost commented Jul 7, 2023

Why it is not merged already?

@ontley
Copy link
Contributor Author

ontley commented Jul 7, 2023

Why it is not merged already?

Waiting for reviews

@ghost
Copy link

ghost commented Jul 7, 2023

Why it is not merged already?

Waiting for reviews

Can anyone review or contributors only?

@ontley
Copy link
Contributor Author

ontley commented Jul 7, 2023

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

@ontley ontley marked this pull request as draft July 7, 2023 07:46
@ontley ontley marked this pull request as ready for review July 7, 2023 07:46
@@ -2752,6 +2812,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: reset_diff_change,
signature: CommandSignature::none(),
},
TypableCommand {
name: "rename",
aliases: &["rnm"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Contributor Author

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

Copy link
Member

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

helix-term/src/commands/typed.rs Outdated Show resolved Hide resolved
helix-term/src/commands/typed.rs Outdated Show resolved Hide resolved
.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()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Member

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")
Copy link
Member

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)

helix-term/src/commands/typed.rs Outdated Show resolved Hide resolved
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() {
Copy link
Member

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();
Copy link
Member

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();
Copy link
Member

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)

@pascalkuthe
Copy link
Member

Why it is not merged already?

Waiting for reviews

Can anyone review or contributors only?

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.

@pascalkuthe pascalkuthe added A-language-server Area: Language server client S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. A-command Area: Commands C-enhancement Category: Improvements labels Jul 7, 2023
@ghost
Copy link

ghost commented Jul 7, 2023

Why it is not merged already?

Waiting for reviews

Can anyone review or contributors only?

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!

let (_, doc) = current!(cx.editor);
let path = doc
.path()
.ok_or_else(|| anyhow!("Scratch buffers can not be renamed, use :write"))?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.ok_or_else(|| anyhow!("Scratch buffers can not be renamed, use :write"))?
.ok_or_else(|| anyhow!("Scratch buffers can not be renamed, use :write"))?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-command Area: Commands A-language-server Area: Language server client C-enhancement Category: Improvements S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants