+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_lsp): renaming symbols #2910

Merged
merged 7 commits into from
Jul 22, 2022
Merged

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Jul 20, 2022

Summary

This PR is the first step to implementing #2903.
It introduces an rename extension method to the BatchMutation.

I still need to understand what to do when the cursor is not at a symbol.

Test Plan

> cargo test -p rome_js_analyze -- rename

or directly using the lsp.

image

@xunilrj xunilrj temporarily deployed to aws July 20, 2022 21:50 Inactive
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 20, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 847b802
Status: ✅  Deploy successful!
Preview URL: https://553abfca.tools-8rn.pages.dev
Branch Preview URL: https://feature-rename-batch-mutatio.tools-8rn.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Jul 20, 2022

@github-actions
Copy link

Parser conformance results on ubuntu-latest

js/262

Test result main count This PR count Difference
Total 45878 45878 0
Passed 44938 44938 0
Failed 940 940 0
Panics 0 0 0
Coverage 97.95% 97.95% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 39 39 0
Passed 36 36 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.31% 92.31% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 5946 5946 0
Passed 395 395 0
Failed 5551 5551 0
Panics 0 0 0
Coverage 6.64% 6.64% 0.00%

ts/babel

Test result main count This PR count Difference
Total 588 588 0
Passed 519 519 0
Failed 69 69 0
Panics 0 0 0
Coverage 88.27% 88.27% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 16257 16257 0
Passed 12393 12393 0
Failed 3864 3864 0
Panics 0 0 0
Coverage 76.23% 76.23% 0.00%

@IWANABETHATGUY
Copy link
Contributor

Could our LSP rename handle the case that reference across the file?

@xunilrj
Copy link
Contributor Author

xunilrj commented Jul 21, 2022

Could our LSP rename handle the case that reference across the file?

Not yet, but it will.

@xunilrj xunilrj temporarily deployed to aws July 21, 2022 05:06 Inactive
@xunilrj xunilrj temporarily deployed to aws July 21, 2022 05:38 Inactive
@xunilrj xunilrj marked this pull request as ready for review July 21, 2022 06:01
@xunilrj xunilrj requested review from ematipico and leops as code owners July 21, 2022 06:01
@xunilrj xunilrj requested a review from a team July 21, 2022 06:01
Comment on lines +146 to +151
if scope
.ancestors()
.filter_map(|scope| scope.get_binding(new_name))
.next()
.is_some()
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to the PR itself but I was surprised to learn that scope.get_binding(new_name) only checks the current scope but not the parent scopes as well to retrieve any binding with that name.

I think, ideally, scope.get_binding(new_name) should check the whole ancestor chain to see if there's any binding with the given name but scope can have a scope.get_own_binding(new_name) method that only checks the current scope. That would most closely match my expectations of get_binding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have no problem with this approach, but as soon as we start adding more methods, we will need to keep consistency: "all_reads", "all_writes", "all_references" etc... will also navigate upwards. Which means we will end up with two sets of methods.

And in the end .ancestors().filter_map(...) solves the problem.

Comment on lines +162 to +164
.node()
.clone()
.cast::<JsIdentifierAssignment>()
Copy link
Contributor

Choose a reason for hiding this comment

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

This last clone should not be necessary if you use all_references.into_iter()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I still need it because Reference::node(...) returns a borrow.
But I indeed can consume the all_references.

}
}

// Now it is safe to pish changes to the batch mutation
Copy link
Contributor

Choose a reason for hiding this comment

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

pish -> push?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ops... and "pish" is not a nice word! 😄

#[cfg(test)]
pub mod tests;

#[macro_export]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to limit these test helpers to test builds only or is this something Rust doesn't support?

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 was annoying me also. I think I managed to improve it.

@@ -56,6 +56,18 @@ struct SemanticModelData {
}

impl SemanticModelData {
pub fn scope(&self, range: &TextRange) -> usize {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: No pub as this is an internal type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -122,6 +122,24 @@ where
});
}

pub fn replace_token(&mut self, prev_token: SyntaxToken<L>, next_token: SyntaxToken<L>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some documentation to these new public methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

match node.try_into() {
Ok(node) => {
let mut batch = root.begin();
batch.rename_with_any_can_be_renamed(&model, node, &new_name);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we return an error if the renaming fails in case new_name is already used?

Copy link
Contributor

Choose a reason for hiding this comment

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

I found that vscode will not emit an error when the new name is already used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we should. Done.

@xunilrj xunilrj temporarily deployed to aws July 21, 2022 07:33 Inactive
@IWANABETHATGUY
Copy link
Contributor

IWANABETHATGUY commented Jul 21, 2022

I think we should add an option like "rome.analysis.enableRenameSymbol": true. Because in vscode javascript-language-service is built in which have more stable functionality on SymbolRename, I am not sure if there would be some side-effects when there are two language server providing SymbolRename service.

@xunilrj
Copy link
Contributor Author

xunilrj commented Jul 21, 2022

I think we should add an option like "rome.analysis.enableRenameSymbol": true. Because in vscode javascript-language-service is built in which have more stable functionality on SymbolRename, I am not sure if there would be some side-effects when there are two language server providing SymbolRename service.

I had to disable the built-in to test ours. I will research later if there is another option.

@xunilrj xunilrj temporarily deployed to aws July 21, 2022 21:26 Inactive
@xunilrj xunilrj temporarily deployed to aws July 22, 2022 04:53 Inactive
@xunilrj xunilrj merged commit e6471f8 into main Jul 22, 2022
@xunilrj xunilrj deleted the feature/rename-batch-mutation-and-lsp branch July 22, 2022 07:57
@xunilrj
Copy link
Contributor Author

xunilrj commented Jul 22, 2022

Given that this changed the LSP, I would include it in the change log of #2835

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载