aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-13 11:00:05 +0100
committerGitHub <[email protected]>2020-08-13 11:00:05 +0100
commit8870a5ea138bb4ba048140d90d728721c4b4ad4b (patch)
treee355901a2d197f2b7cceb35a732a111afed58198 /crates/ra_assists
parentb5cb16fb90b4a1076604c5795552ee4abe07a057 (diff)
parentc81f6230da98fd3e3fa91c0896d65922a1ed4a24 (diff)
Merge #5743
5743: Remove ra_fmt crate r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/Cargo.toml1
-rw-r--r--crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs16
-rw-r--r--crates/ra_assists/src/utils/insert_use.rs27
3 files changed, 30 insertions, 14 deletions
diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml
index abc290463..83e44c124 100644
--- a/crates/ra_assists/Cargo.toml
+++ b/crates/ra_assists/Cargo.toml
@@ -17,7 +17,6 @@ stdx = { path = "../stdx" }
17 17
18syntax = { path = "../syntax" } 18syntax = { path = "../syntax" }
19text_edit = { path = "../text_edit" } 19text_edit = { path = "../text_edit" }
20ra_fmt = { path = "../ra_fmt" }
21profile = { path = "../profile" } 20profile = { path = "../profile" }
22ra_db = { path = "../ra_db" } 21ra_db = { path = "../ra_db" }
23ra_ide_db = { path = "../ra_ide_db" } 22ra_ide_db = { path = "../ra_ide_db" }
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..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,12 +1,11 @@
1use hir::{EnumVariant, Module, ModuleDef, Name}; 1use hir::{EnumVariant, Module, ModuleDef, Name};
2use ra_db::FileId; 2use ra_db::FileId;
3use ra_fmt::leading_indent;
4use ra_ide_db::{defs::Definition, search::Reference, RootDatabase}; 3use ra_ide_db::{defs::Definition, search::Reference, RootDatabase};
5use rustc_hash::FxHashSet; 4use rustc_hash::FxHashSet;
6use syntax::{ 5use syntax::{
7 algo::find_node_at_offset, 6 algo::find_node_at_offset,
8 ast::{self, ArgListOwner, AstNode, NameOwner, VisibilityOwner}, 7 ast::{self, edit::IndentLevel, ArgListOwner, AstNode, NameOwner, VisibilityOwner},
9 SourceFile, SyntaxNode, TextRange, TextSize, 8 SourceFile, TextRange, TextSize,
10}; 9};
11 10
12use crate::{ 11use crate::{
@@ -72,7 +71,7 @@ pub(crate) fn extract_struct_from_enum_variant(
72 } 71 }
73 extract_struct_def( 72 extract_struct_def(
74 builder, 73 builder,
75 enum_ast.syntax(), 74 &enum_ast,
76 &variant_name, 75 &variant_name,
77 &field_list.to_string(), 76 &field_list.to_string(),
78 start_offset, 77 start_offset,
@@ -112,9 +111,10 @@ fn insert_import(
112 Some(()) 111 Some(())
113} 112}
114 113
114// FIXME: this should use strongly-typed `make`, rather than string manipulation.
115fn extract_struct_def( 115fn extract_struct_def(
116 builder: &mut AssistBuilder, 116 builder: &mut AssistBuilder,
117 enum_ast: &SyntaxNode, 117 enum_: &ast::Enum,
118 variant_name: &str, 118 variant_name: &str,
119 variant_list: &str, 119 variant_list: &str,
120 start_offset: TextSize, 120 start_offset: TextSize,
@@ -126,11 +126,7 @@ fn extract_struct_def(
126 } else { 126 } else {
127 "".to_string() 127 "".to_string()
128 }; 128 };
129 let indent = if let Some(indent) = leading_indent(enum_ast) { 129 let indent = IndentLevel::from_node(enum_.syntax());
130 indent.to_string()
131 } else {
132 "".to_string()
133 };
134 let struct_def = format!( 130 let struct_def = format!(
135 r#"{}struct {}{}; 131 r#"{}struct {}{};
136 132
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 @@
2// FIXME: rewrite according to the plan, outlined in 2// FIXME: rewrite according to the plan, outlined in
3// https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553 3// https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553
4 4
5use std::iter::successors;
6
5use either::Either; 7use either::Either;
6use hir::{self, ModPath}; 8use hir::{self, ModPath};
7use syntax::{ 9use syntax::{
8 ast::{self, NameOwner, VisibilityOwner}, 10 ast::{self, NameOwner, VisibilityOwner},
9 AstNode, Direction, SmolStr, 11 AstNode, AstToken, Direction, SmolStr,
10 SyntaxKind::{PATH, PATH_SEGMENT}, 12 SyntaxKind::{PATH, PATH_SEGMENT},
11 SyntaxNode, T, 13 SyntaxNode, SyntaxToken, T,
12}; 14};
13use text_edit::TextEditBuilder; 15use text_edit::TextEditBuilder;
14 16
@@ -442,7 +444,7 @@ fn make_assist_add_new_use(
442 edit: &mut TextEditBuilder, 444 edit: &mut TextEditBuilder,
443) { 445) {
444 if let Some(anchor) = anchor { 446 if let Some(anchor) = anchor {
445 let indent = ra_fmt::leading_indent(anchor); 447 let indent = leading_indent(anchor);
446 let mut buf = String::new(); 448 let mut buf = String::new();
447 if after { 449 if after {
448 buf.push_str("\n"); 450 buf.push_str("\n");
@@ -524,3 +526,22 @@ fn make_assist_add_nested_import(
524 edit.insert(end, "}".to_string()); 526 edit.insert(end, "}".to_string());
525 } 527 }
526} 528}
529
530/// If the node is on the beginning of the line, calculate indent.
531fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
532 for token in prev_tokens(node.first_token()?) {
533 if let Some(ws) = ast::Whitespace::cast(token.clone()) {
534 let ws_text = ws.text();
535 if let Some(pos) = ws_text.rfind('\n') {
536 return Some(ws_text[pos + 1..].into());
537 }
538 }
539 if token.text().contains('\n') {
540 break;
541 }
542 }
543 return None;
544 fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
545 successors(token.prev_token(), |token| token.prev_token())
546 }
547}