From 479235ff0b25dbac977d28475b9203e3207b117b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 11:47:31 +0200 Subject: Minor --- .../ra_assists/src/handlers/extract_struct_from_enum_variant.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs index 6e9f2d0fc..b4e19b3dc 100644 --- a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs @@ -6,7 +6,7 @@ use rustc_hash::FxHashSet; use syntax::{ algo::find_node_at_offset, ast::{self, ArgListOwner, AstNode, NameOwner, VisibilityOwner}, - SourceFile, SyntaxNode, TextRange, TextSize, + SourceFile, TextRange, TextSize, }; use crate::{ @@ -72,7 +72,7 @@ pub(crate) fn extract_struct_from_enum_variant( } extract_struct_def( builder, - enum_ast.syntax(), + &enum_ast, &variant_name, &field_list.to_string(), start_offset, @@ -112,9 +112,10 @@ fn insert_import( Some(()) } +// FIXME: this should use strongly-typed `make`, rather than string manipulation1 fn extract_struct_def( builder: &mut AssistBuilder, - enum_ast: &SyntaxNode, + enum_: &ast::Enum, variant_name: &str, variant_list: &str, start_offset: TextSize, @@ -126,7 +127,7 @@ fn extract_struct_def( } else { "".to_string() }; - let indent = if let Some(indent) = leading_indent(enum_ast) { + let indent = if let Some(indent) = leading_indent(enum_.syntax()) { indent.to_string() } else { "".to_string() -- cgit v1.2.3 From c81f6230da98fd3e3fa91c0896d65922a1ed4a24 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 11:56:11 +0200 Subject: Remove ra_fmt crate --- .../handlers/extract_struct_from_enum_variant.rs | 11 +++------ crates/ra_assists/src/utils/insert_use.rs | 27 +++++++++++++++++++--- 2 files changed, 27 insertions(+), 11 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs index b4e19b3dc..497f887cd 100644 --- a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs @@ -1,11 +1,10 @@ use hir::{EnumVariant, Module, ModuleDef, Name}; use ra_db::FileId; -use ra_fmt::leading_indent; use ra_ide_db::{defs::Definition, search::Reference, RootDatabase}; use rustc_hash::FxHashSet; use syntax::{ algo::find_node_at_offset, - ast::{self, ArgListOwner, AstNode, NameOwner, VisibilityOwner}, + ast::{self, edit::IndentLevel, ArgListOwner, AstNode, NameOwner, VisibilityOwner}, SourceFile, TextRange, TextSize, }; @@ -112,7 +111,7 @@ fn insert_import( Some(()) } -// FIXME: this should use strongly-typed `make`, rather than string manipulation1 +// FIXME: this should use strongly-typed `make`, rather than string manipulation. fn extract_struct_def( builder: &mut AssistBuilder, enum_: &ast::Enum, @@ -127,11 +126,7 @@ fn extract_struct_def( } else { "".to_string() }; - let indent = if let Some(indent) = leading_indent(enum_.syntax()) { - indent.to_string() - } else { - "".to_string() - }; + let indent = IndentLevel::from_node(enum_.syntax()); let struct_def = format!( r#"{}struct {}{}; diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs index f89c288da..50a62ee82 100644 --- a/crates/ra_assists/src/utils/insert_use.rs +++ b/crates/ra_assists/src/utils/insert_use.rs @@ -2,13 +2,15 @@ // FIXME: rewrite according to the plan, outlined in // https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553 +use std::iter::successors; + use either::Either; use hir::{self, ModPath}; use syntax::{ ast::{self, NameOwner, VisibilityOwner}, - AstNode, Direction, SmolStr, + AstNode, AstToken, Direction, SmolStr, SyntaxKind::{PATH, PATH_SEGMENT}, - SyntaxNode, T, + SyntaxNode, SyntaxToken, T, }; use text_edit::TextEditBuilder; @@ -442,7 +444,7 @@ fn make_assist_add_new_use( edit: &mut TextEditBuilder, ) { if let Some(anchor) = anchor { - let indent = ra_fmt::leading_indent(anchor); + let indent = leading_indent(anchor); let mut buf = String::new(); if after { buf.push_str("\n"); @@ -524,3 +526,22 @@ fn make_assist_add_nested_import( edit.insert(end, "}".to_string()); } } + +/// If the node is on the beginning of the line, calculate indent. +fn leading_indent(node: &SyntaxNode) -> Option { + for token in prev_tokens(node.first_token()?) { + if let Some(ws) = ast::Whitespace::cast(token.clone()) { + let ws_text = ws.text(); + if let Some(pos) = ws_text.rfind('\n') { + return Some(ws_text[pos + 1..].into()); + } + } + if token.text().contains('\n') { + break; + } + } + return None; + fn prev_tokens(token: SyntaxToken) -> impl Iterator { + successors(token.prev_token(), |token| token.prev_token()) + } +} -- cgit v1.2.3