mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-15 10:12:21 +00:00
Merge pull request #20307 from Hmikihiro/migrate_extract_expression_from_format_string
Some checks are pending
metrics / other_metrics (ripgrep-13.0.0) (push) Blocked by required conditions
metrics / build_metrics (push) Waiting to run
metrics / other_metrics (diesel-1.4.8) (push) Blocked by required conditions
metrics / other_metrics (hyper-0.14.18) (push) Blocked by required conditions
metrics / other_metrics (self) (push) Blocked by required conditions
metrics / other_metrics (webrender-2022) (push) Blocked by required conditions
metrics / generate_final_metrics (push) Blocked by required conditions
rustdoc / rustdoc (push) Waiting to run
Some checks are pending
metrics / other_metrics (ripgrep-13.0.0) (push) Blocked by required conditions
metrics / build_metrics (push) Waiting to run
metrics / other_metrics (diesel-1.4.8) (push) Blocked by required conditions
metrics / other_metrics (hyper-0.14.18) (push) Blocked by required conditions
metrics / other_metrics (self) (push) Blocked by required conditions
metrics / other_metrics (webrender-2022) (push) Blocked by required conditions
metrics / generate_final_metrics (push) Blocked by required conditions
rustdoc / rustdoc (push) Waiting to run
Migrate `extract_expressions_from_format_string` assist to use `SyntaxEditor`
This commit is contained in:
commit
ea413f67a8
1 changed files with 11 additions and 8 deletions
|
|
@ -8,8 +8,7 @@ use syntax::{
|
||||||
AstNode, AstToken, NodeOrToken,
|
AstNode, AstToken, NodeOrToken,
|
||||||
SyntaxKind::WHITESPACE,
|
SyntaxKind::WHITESPACE,
|
||||||
T,
|
T,
|
||||||
ast::{self, make},
|
ast::{self, make, syntax_factory::SyntaxFactory},
|
||||||
ted,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Assist: extract_expressions_from_format_string
|
// Assist: extract_expressions_from_format_string
|
||||||
|
|
@ -58,8 +57,6 @@ pub(crate) fn extract_expressions_from_format_string(
|
||||||
"Extract format expressions",
|
"Extract format expressions",
|
||||||
tt.syntax().text_range(),
|
tt.syntax().text_range(),
|
||||||
|edit| {
|
|edit| {
|
||||||
let tt = edit.make_mut(tt);
|
|
||||||
|
|
||||||
// Extract existing arguments in macro
|
// Extract existing arguments in macro
|
||||||
let tokens = tt.token_trees_and_tokens().collect_vec();
|
let tokens = tt.token_trees_and_tokens().collect_vec();
|
||||||
|
|
||||||
|
|
@ -131,8 +128,10 @@ pub(crate) fn extract_expressions_from_format_string(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert new args
|
// Insert new args
|
||||||
let new_tt = make::token_tree(tt_delimiter, new_tt_bits).clone_for_update();
|
let make = SyntaxFactory::with_mappings();
|
||||||
ted::replace(tt.syntax(), new_tt.syntax());
|
let new_tt = make.token_tree(tt_delimiter, new_tt_bits);
|
||||||
|
let mut editor = edit.make_editor(tt.syntax());
|
||||||
|
editor.replace(tt.syntax(), new_tt.syntax());
|
||||||
|
|
||||||
if let Some(cap) = ctx.config.snippet_cap {
|
if let Some(cap) = ctx.config.snippet_cap {
|
||||||
// Add placeholder snippets over placeholder args
|
// Add placeholder snippets over placeholder args
|
||||||
|
|
@ -145,15 +144,19 @@ pub(crate) fn extract_expressions_from_format_string(
|
||||||
};
|
};
|
||||||
|
|
||||||
if stdx::always!(placeholder.kind() == T![_]) {
|
if stdx::always!(placeholder.kind() == T![_]) {
|
||||||
edit.add_placeholder_snippet_token(cap, placeholder);
|
let annotation = edit.make_placeholder_snippet(cap);
|
||||||
|
editor.add_annotation(placeholder, annotation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the final tabstop after the format literal
|
// Add the final tabstop after the format literal
|
||||||
if let Some(NodeOrToken::Token(literal)) = new_tt.token_trees_and_tokens().nth(1) {
|
if let Some(NodeOrToken::Token(literal)) = new_tt.token_trees_and_tokens().nth(1) {
|
||||||
edit.add_tabstop_after_token(cap, literal);
|
let annotation = edit.make_tabstop_after(cap);
|
||||||
|
editor.add_annotation(literal, annotation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
editor.add_mappings(make.finish_with_mappings());
|
||||||
|
edit.add_file_edits(ctx.vfs_file_id(), editor);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue