aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/merge_imports.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/merge_imports.rs')
-rw-r--r--crates/ra_assists/src/handlers/merge_imports.rs38
1 files changed, 20 insertions, 18 deletions
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs
index 4be1238f1..972d16241 100644
--- a/crates/ra_assists/src/handlers/merge_imports.rs
+++ b/crates/ra_assists/src/handlers/merge_imports.rs
@@ -6,7 +6,10 @@ use ra_syntax::{
6 AstNode, Direction, InsertPosition, SyntaxElement, T, 6 AstNode, Direction, InsertPosition, SyntaxElement, T,
7}; 7};
8 8
9use crate::{Assist, AssistCtx, AssistId}; 9use crate::{
10 assist_context::{AssistContext, Assists},
11 AssistId,
12};
10 13
11// Assist: merge_imports 14// Assist: merge_imports
12// 15//
@@ -20,10 +23,10 @@ use crate::{Assist, AssistCtx, AssistId};
20// ``` 23// ```
21// use std::{fmt::Formatter, io}; 24// use std::{fmt::Formatter, io};
22// ``` 25// ```
23pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> { 26pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
24 let tree: ast::UseTree = ctx.find_node_at_offset()?; 27 let tree: ast::UseTree = ctx.find_node_at_offset()?;
25 let mut rewriter = SyntaxRewriter::default(); 28 let mut rewriter = SyntaxRewriter::default();
26 let mut offset = ctx.frange.range.start(); 29 let mut offset = ctx.offset();
27 30
28 if let Some(use_item) = tree.syntax().parent().and_then(ast::UseItem::cast) { 31 if let Some(use_item) = tree.syntax().parent().and_then(ast::UseItem::cast) {
29 let (merged, to_delete) = next_prev() 32 let (merged, to_delete) = next_prev()
@@ -52,10 +55,9 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> {
52 } 55 }
53 }; 56 };
54 57
55 ctx.add_assist(AssistId("merge_imports"), "Merge imports", |edit| { 58 let target = tree.syntax().text_range();
56 edit.rewrite(rewriter); 59 acc.add(AssistId("merge_imports"), "Merge imports", target, |builder| {
57 // FIXME: we only need because our diff is imprecise 60 builder.rewrite(rewriter);
58 edit.set_cursor(offset);
59 }) 61 })
60} 62}
61 63
@@ -125,7 +127,7 @@ fn first_path(path: &ast::Path) -> ast::Path {
125 127
126#[cfg(test)] 128#[cfg(test)]
127mod tests { 129mod tests {
128 use crate::helpers::check_assist; 130 use crate::tests::check_assist;
129 131
130 use super::*; 132 use super::*;
131 133
@@ -138,7 +140,7 @@ use std::fmt<|>::Debug;
138use std::fmt::Display; 140use std::fmt::Display;
139", 141",
140 r" 142 r"
141use std::fmt<|>::{Debug, Display}; 143use std::fmt::{Debug, Display};
142", 144",
143 ) 145 )
144 } 146 }
@@ -152,7 +154,7 @@ use std::fmt::Debug;
152use std::fmt<|>::Display; 154use std::fmt<|>::Display;
153", 155",
154 r" 156 r"
155use std::fmt:<|>:{Display, Debug}; 157use std::fmt::{Display, Debug};
156", 158",
157 ); 159 );
158 } 160 }
@@ -165,7 +167,7 @@ use std::fmt:<|>:{Display, Debug};
165use std::{fmt<|>::Debug, fmt::Display}; 167use std::{fmt<|>::Debug, fmt::Display};
166", 168",
167 r" 169 r"
168use std::{fmt<|>::{Debug, Display}}; 170use std::{fmt::{Debug, Display}};
169", 171",
170 ); 172 );
171 check_assist( 173 check_assist(
@@ -174,7 +176,7 @@ use std::{fmt<|>::{Debug, Display}};
174use std::{fmt::Debug, fmt<|>::Display}; 176use std::{fmt::Debug, fmt<|>::Display};
175", 177",
176 r" 178 r"
177use std::{fmt::<|>{Display, Debug}}; 179use std::{fmt::{Display, Debug}};
178", 180",
179 ); 181 );
180 } 182 }
@@ -188,7 +190,7 @@ use std<|>::cell::*;
188use std::str; 190use std::str;
189", 191",
190 r" 192 r"
191use std<|>::{cell::*, str}; 193use std::{cell::*, str};
192", 194",
193 ) 195 )
194 } 196 }
@@ -202,7 +204,7 @@ use std<|>::cell::*;
202use std::str::*; 204use std::str::*;
203", 205",
204 r" 206 r"
205use std<|>::{cell::*, str::*}; 207use std::{cell::*, str::*};
206", 208",
207 ) 209 )
208 } 210 }
@@ -218,7 +220,7 @@ use foo::baz;
218/// Doc comment 220/// Doc comment
219", 221",
220 r" 222 r"
221use foo<|>::{bar, baz}; 223use foo::{bar, baz};
222 224
223/// Doc comment 225/// Doc comment
224", 226",
@@ -237,7 +239,7 @@ use {
237", 239",
238 r" 240 r"
239use { 241use {
240 foo<|>::{bar, baz}, 242 foo::{bar, baz},
241}; 243};
242", 244",
243 ); 245 );
@@ -251,7 +253,7 @@ use {
251", 253",
252 r" 254 r"
253use { 255use {
254 foo::{bar<|>, baz}, 256 foo::{bar, baz},
255}; 257};
256", 258",
257 ); 259 );
@@ -268,7 +270,7 @@ use foo::<|>{
268}; 270};
269", 271",
270 r" 272 r"
271use foo::{<|> 273use foo::{
272 FooBar, 274 FooBar,
273bar::baz}; 275bar::baz};
274", 276",