diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-23 14:32:25 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-23 14:32:25 +0000 |
commit | d15edf779bec09a86d05846299f1564b8b27a1d5 (patch) | |
tree | b6a237b53f65aaf96fc65707580ef41ab9a73d2d /crates/ide_assists/src/handlers/merge_imports.rs | |
parent | 258afb8fb8331e43a75e4f19df255d85d2430be7 (diff) | |
parent | b83c7eedccea4c9cb35b1d1cc58231f07a5e3ba2 (diff) |
Merge #8165
8165: Tweak assits API to fit mutable syntax trees r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide_assists/src/handlers/merge_imports.rs')
-rw-r--r-- | crates/ide_assists/src/handlers/merge_imports.rs | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/crates/ide_assists/src/handlers/merge_imports.rs b/crates/ide_assists/src/handlers/merge_imports.rs index cfc472a32..8e0794218 100644 --- a/crates/ide_assists/src/handlers/merge_imports.rs +++ b/crates/ide_assists/src/handlers/merge_imports.rs | |||
@@ -21,12 +21,6 @@ use crate::{ | |||
21 | // ``` | 21 | // ``` |
22 | pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 22 | pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
23 | let tree: ast::UseTree = ctx.find_node_at_offset()?; | 23 | let tree: ast::UseTree = ctx.find_node_at_offset()?; |
24 | let original_parent = tree.syntax().ancestors().last()?; | ||
25 | |||
26 | let tree = tree.clone_for_update(); | ||
27 | let new_parent = tree.syntax().ancestors().last()?; | ||
28 | |||
29 | let mut offset = ctx.offset(); | ||
30 | 24 | ||
31 | let mut imports = None; | 25 | let mut imports = None; |
32 | let mut uses = None; | 26 | let mut uses = None; |
@@ -53,22 +47,20 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<() | |||
53 | target, | 47 | target, |
54 | |builder| { | 48 | |builder| { |
55 | if let Some((to_replace, replacement, to_remove)) = imports { | 49 | if let Some((to_replace, replacement, to_remove)) = imports { |
56 | if to_remove.syntax().text_range().end() < offset { | 50 | let to_replace = builder.make_ast_mut(to_replace); |
57 | offset -= to_remove.syntax().text_range().len(); | 51 | let to_remove = builder.make_ast_mut(to_remove); |
58 | } | 52 | |
59 | ted::replace(to_replace.syntax().clone(), replacement.syntax().clone()); | 53 | ted::replace(to_replace.syntax(), replacement.syntax()); |
60 | to_remove.remove(); | 54 | to_remove.remove(); |
61 | } | 55 | } |
62 | 56 | ||
63 | if let Some((to_replace, replacement, to_remove)) = uses { | 57 | if let Some((to_replace, replacement, to_remove)) = uses { |
64 | if to_remove.syntax().text_range().end() < offset { | 58 | let to_replace = builder.make_ast_mut(to_replace); |
65 | offset -= to_remove.syntax().text_range().len(); | 59 | let to_remove = builder.make_ast_mut(to_remove); |
66 | } | 60 | |
67 | ted::replace(to_replace.syntax().clone(), replacement.syntax().clone()); | 61 | ted::replace(to_replace.syntax(), replacement.syntax()); |
68 | to_remove.remove() | 62 | to_remove.remove() |
69 | } | 63 | } |
70 | |||
71 | builder.replace(original_parent.text_range(), new_parent.to_string()) | ||
72 | }, | 64 | }, |
73 | ) | 65 | ) |
74 | } | 66 | } |