aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/assists.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api_light/src/assists.rs')
-rw-r--r--crates/ra_ide_api_light/src/assists.rs11
1 files changed, 3 insertions, 8 deletions
diff --git a/crates/ra_ide_api_light/src/assists.rs b/crates/ra_ide_api_light/src/assists.rs
index 83eabfc85..3495ad967 100644
--- a/crates/ra_ide_api_light/src/assists.rs
+++ b/crates/ra_ide_api_light/src/assists.rs
@@ -15,10 +15,11 @@ use ra_text_edit::{TextEdit, TextEditBuilder};
15use ra_syntax::{ 15use ra_syntax::{
16 Direction, SyntaxNode, TextUnit, TextRange, SourceFile, AstNode, 16 Direction, SyntaxNode, TextUnit, TextRange, SourceFile, AstNode,
17 algo::{find_leaf_at_offset, find_node_at_offset, find_covering_node, LeafAtOffset}, 17 algo::{find_leaf_at_offset, find_node_at_offset, find_covering_node, LeafAtOffset},
18 ast::{self, AstToken},
19}; 18};
20use itertools::Itertools; 19use itertools::Itertools;
21 20
21use crate::formatting::leading_indent;
22
22pub use self::{ 23pub use self::{
23 flip_comma::flip_comma, 24 flip_comma::flip_comma,
24 add_derive::add_derive, 25 add_derive::add_derive,
@@ -165,7 +166,7 @@ impl AssistBuilder {
165 } 166 }
166 fn replace_node_and_indent(&mut self, node: &SyntaxNode, replace_with: impl Into<String>) { 167 fn replace_node_and_indent(&mut self, node: &SyntaxNode, replace_with: impl Into<String>) {
167 let mut replace_with = replace_with.into(); 168 let mut replace_with = replace_with.into();
168 if let Some(indent) = calc_indent(node) { 169 if let Some(indent) = leading_indent(node) {
169 replace_with = reindent(&replace_with, indent) 170 replace_with = reindent(&replace_with, indent)
170 } 171 }
171 self.replace(node.range(), replace_with) 172 self.replace(node.range(), replace_with)
@@ -182,12 +183,6 @@ impl AssistBuilder {
182 } 183 }
183} 184}
184 185
185fn calc_indent(node: &SyntaxNode) -> Option<&str> {
186 let prev = node.prev_sibling()?;
187 let ws_text = ast::Whitespace::cast(prev)?.text();
188 ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..])
189}
190
191fn reindent(text: &str, indent: &str) -> String { 186fn reindent(text: &str, indent: &str) -> String {
192 let indent = format!("\n{}", indent); 187 let indent = format!("\n{}", indent);
193 text.lines().intersperse(&indent).collect() 188 text.lines().intersperse(&indent).collect()