-
Notifications
You must be signed in to change notification settings - Fork 1.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
minor: Migrate (un)wrap_return_type
assists to use SyntaxEditor
#18524
base: master
Are you sure you want to change the base?
minor: Migrate (un)wrap_return_type
assists to use SyntaxEditor
#18524
Conversation
f2c9293
to
8542c04
Compare
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 can't add a comment to the file in question, but it would be good also add a test in syntax_editor::tests
testing that token edits in a parent node won't be considered ancestors of edits in the child node:
#[test]
fn test_replace_token_in_parent() {
let parent_fn = make::fn_(
None,
make::name("it"),
None,
None,
make::param_list(None, []),
make::block_expr([], Some(make::expr_unit())),
Some(make::ret_type(make::ty_unit())),
false,
false,
false,
false,
);
let mut editor = SyntaxEditor::new(parent_fn.syntax().clone());
if let Some(ret_ty) = parent_fn.ret_type() {
editor.delete(ret_ty.syntax().clone());
if let Some(SyntaxElement::Token(token)) = ret_ty.syntax().next_sibling_or_token() {
if token.kind().is_trivia() {
editor.delete(token);
}
}
}
if let Some(tail) = parent_fn.body().unwrap().tail_expr() {
editor.delete(tail.syntax().clone());
}
let edit = editor.finish();
let expect = expect![[r#"
fn it() {
}"#]];
expect.assert_eq(&edit.new_root.to_string());
}
@@ -73,7 +73,7 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit { | |||
}) | |||
.all(|(l, r)| { | |||
get_node_depth(l.target_parent()) != get_node_depth(r.target_parent()) | |||
|| l.target_range().intersect(r.target_range()).is_none() | |||
|| (l.target_range().end() <= r.target_range().start()) |
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.
Can also do
|| (l.target_range().end() <= r.target_range().start()) | |
|| l.target_range().intersect(r.target_range()).is_none_or(|it| it.is_empty()) |
TextRange::intersect
returns an empty range if the ranges are touching but not overlapping.
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 considered that but figured I'd rather just be straightforward with what's being checked especially since the ranges are sorted so there's no need to check if l
starts inside of r
Forgot to mention that the final tabstop snippet for |
Bordering replacements should not be considered intersecting
Also changes `make::expr_empty_block()` to return `ast::BlockExpr` instead of `ast::Expr`
8542c04
to
842bcdc
Compare
Part of #15710 and #18285
This PR also includes some small fixes to
SyntaxEditor
, in particular a fix to the problem withaffected_range
outlined here.I also updated
unwrap_return_type
to use placeholders when unwrappingNone
. I had tried to do this with the mutable tree API before and it was kind of a pain so it's nice to see improvements like this :)