diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 0d95b957ba..4ab09b167b 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -1,4 +1,4 @@ -//! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen` +//! Generated file, do not edit by hand, see `xtask/src/codegen` use super::check; @@ -160,21 +160,6 @@ impl Trait for () { ) } -#[test] -fn doctest_add_import() { - check( - "add_import", - r#####" -fn process(map: std::collections::<|>HashMap) {} -"#####, - r#####" -use std::collections::HashMap; - -fn process(map: HashMap) {} -"#####, - ) -} - #[test] fn doctest_add_new() { check( @@ -591,6 +576,21 @@ fn handle(action: Action) { ) } +#[test] +fn doctest_replace_qualified_name_with_use() { + check( + "replace_qualified_name_with_use", + r#####" +fn process(map: std::collections::<|>HashMap) {} +"#####, + r#####" +use std::collections::HashMap; + +fn process(map: HashMap) {} +"#####, + ) +} + #[test] fn doctest_split_import() { check( diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 84b5474f9d..4514b86911 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs @@ -7,7 +7,7 @@ use ra_syntax::{ use crate::{ assist_ctx::{ActionBuilder, Assist, AssistCtx}, - auto_import_text_edit, AssistId, + insert_use_statement, AssistId, }; use std::collections::BTreeSet; @@ -78,7 +78,7 @@ pub(crate) fn auto_import(ctx: AssistCtx) -> Option { fn import_to_action(import: ModPath, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder { let mut action_builder = ActionBuilder::default(); action_builder.label(format!("Import `{}`", &import)); - auto_import_text_edit(position, anchor, &import, action_builder.text_edit_builder()); + insert_use_statement(position, anchor, &import, action_builder.text_edit_builder()); action_builder } diff --git a/crates/ra_assists/src/handlers/add_import.rs b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs similarity index 90% rename from crates/ra_assists/src/handlers/add_import.rs rename to crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs index f03dddac8f..b70c88ec29 100644 --- a/crates/ra_assists/src/handlers/add_import.rs +++ b/crates/ra_assists/src/handlers/replace_qualified_name_with_use.rs @@ -12,10 +12,10 @@ use crate::{ AssistId, }; -/// This function produces sequence of text edits into edit -/// to import the target path in the most appropriate scope given -/// the cursor position -pub fn auto_import_text_edit( +/// Creates and inserts a use statement for the given path to import. +/// The use statement is inserted in the scope most appropriate to the +/// the cursor position given, additionally merged with the existing use imports. +pub fn insert_use_statement( // Ideally the position of the cursor, used to position: &SyntaxNode, // The statement to use as anchor (last resort) @@ -37,9 +37,9 @@ pub fn auto_import_text_edit( } } -// Assist: add_import +// Assist: replace_qualified_name_with_use // -// Adds a use statement for a given fully-qualified path. +// Adds a use statement for a given fully-qualified name. // // ``` // fn process(map: std::collections::<|>HashMap) {} @@ -50,7 +50,7 @@ pub fn auto_import_text_edit( // // fn process(map: HashMap) {} // ``` -pub(crate) fn add_import(ctx: AssistCtx) -> Option { +pub(crate) fn replace_qualified_name_with_use(ctx: AssistCtx) -> Option { let path: ast::Path = ctx.find_node_at_offset()?; // We don't want to mess with use statements if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { @@ -72,9 +72,13 @@ pub(crate) fn add_import(ctx: AssistCtx) -> Option { } }; - ctx.add_assist(AssistId("add_import"), format!("Import {}", fmt_segments(&segments)), |edit| { - apply_auto_import(&position, &path, &segments, edit.text_edit_builder()); - }) + ctx.add_assist( + AssistId("replace_qualified_name_with_use"), + "Replace qualified path with use", + |edit| { + replace_with_use(&position, &path, &segments, edit.text_edit_builder()); + }, + ) } fn collect_path_segments_raw( @@ -107,12 +111,6 @@ fn collect_path_segments_raw( Some(segments.len() - oldlen) } -fn fmt_segments(segments: &[SmolStr]) -> String { - let mut buf = String::new(); - fmt_segments_raw(segments, &mut buf); - buf -} - fn fmt_segments_raw(segments: &[SmolStr], buf: &mut String) { let mut iter = segments.iter(); if let Some(s) = iter.next() { @@ -558,7 +556,7 @@ fn make_assist_add_nested_import( } } -fn apply_auto_import( +fn replace_with_use( container: &SyntaxNode, path: &ast::Path, target: &[SmolStr], @@ -567,7 +565,7 @@ fn apply_auto_import( let action = best_action_for_target(container.clone(), path.syntax().clone(), target); make_assist(&action, target, edit); if let Some(last) = path.segment() { - // Here we are assuming the assist will provide a correct use statement + // Here we are assuming the assist will provide a correct use statement // so we can delete the path qualifier edit.delete(TextRange::from_to( path.syntax().text_range().start(), @@ -603,9 +601,9 @@ mod tests { use super::*; #[test] - fn test_auto_import_add_use_no_anchor() { + fn test_replace_add_use_no_anchor() { check_assist( - add_import, + replace_qualified_name_with_use, " std::fmt::Debug<|> ", @@ -617,9 +615,9 @@ Debug<|> ); } #[test] - fn test_auto_import_add_use_no_anchor_with_item_below() { + fn test_replace_add_use_no_anchor_with_item_below() { check_assist( - add_import, + replace_qualified_name_with_use, " std::fmt::Debug<|> @@ -638,9 +636,9 @@ fn main() { } #[test] - fn test_auto_import_add_use_no_anchor_with_item_above() { + fn test_replace_add_use_no_anchor_with_item_above() { check_assist( - add_import, + replace_qualified_name_with_use, " fn main() { } @@ -659,9 +657,9 @@ Debug<|> } #[test] - fn test_auto_import_add_use_no_anchor_2seg() { + fn test_replace_add_use_no_anchor_2seg() { check_assist( - add_import, + replace_qualified_name_with_use, " std::fmt<|>::Debug ", @@ -674,9 +672,9 @@ fmt<|>::Debug } #[test] - fn test_auto_import_add_use() { + fn test_replace_add_use() { check_assist( - add_import, + replace_qualified_name_with_use, " use stdx; @@ -694,9 +692,9 @@ impl Debug<|> for Foo { } #[test] - fn test_auto_import_file_use_other_anchor() { + fn test_replace_file_use_other_anchor() { check_assist( - add_import, + replace_qualified_name_with_use, " impl std::fmt::Debug<|> for Foo { } @@ -711,9 +709,9 @@ impl Debug<|> for Foo { } #[test] - fn test_auto_import_add_use_other_anchor_indent() { + fn test_replace_add_use_other_anchor_indent() { check_assist( - add_import, + replace_qualified_name_with_use, " impl std::fmt::Debug<|> for Foo { } @@ -728,9 +726,9 @@ impl Debug<|> for Foo { } #[test] - fn test_auto_import_split_different() { + fn test_replace_split_different() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt; @@ -747,9 +745,9 @@ impl io<|> for Foo { } #[test] - fn test_auto_import_split_self_for_use() { + fn test_replace_split_self_for_use() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt; @@ -766,9 +764,9 @@ impl Debug<|> for Foo { } #[test] - fn test_auto_import_split_self_for_target() { + fn test_replace_split_self_for_target() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt::Debug; @@ -785,9 +783,9 @@ impl fmt<|> for Foo { } #[test] - fn test_auto_import_add_to_nested_self_nested() { + fn test_replace_add_to_nested_self_nested() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt::{Debug, nested::{Display}}; @@ -804,9 +802,9 @@ impl nested<|> for Foo { } #[test] - fn test_auto_import_add_to_nested_self_already_included() { + fn test_replace_add_to_nested_self_already_included() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt::{Debug, nested::{self, Display}}; @@ -823,9 +821,9 @@ impl nested<|> for Foo { } #[test] - fn test_auto_import_add_to_nested_nested() { + fn test_replace_add_to_nested_nested() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt::{Debug, nested::{Display}}; @@ -842,9 +840,9 @@ impl Debug<|> for Foo { } #[test] - fn test_auto_import_split_common_target_longer() { + fn test_replace_split_common_target_longer() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt::Debug; @@ -861,9 +859,9 @@ impl Display<|> for Foo { } #[test] - fn test_auto_import_split_common_use_longer() { + fn test_replace_split_common_use_longer() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt::nested::Debug; @@ -880,9 +878,9 @@ impl Display<|> for Foo { } #[test] - fn test_auto_import_use_nested_import() { + fn test_replace_use_nested_import() { check_assist( - add_import, + replace_qualified_name_with_use, " use crate::{ ty::{Substs, Ty}, @@ -903,9 +901,9 @@ fn foo() { lower<|>::trait_env() } } #[test] - fn test_auto_import_alias() { + fn test_replace_alias() { check_assist( - add_import, + replace_qualified_name_with_use, " use std::fmt as foo; @@ -922,9 +920,9 @@ impl Debug<|> for Foo { } #[test] - fn test_auto_import_not_applicable_one_segment() { + fn test_replace_not_applicable_one_segment() { check_assist_not_applicable( - add_import, + replace_qualified_name_with_use, " impl foo<|> for Foo { } @@ -933,9 +931,9 @@ impl foo<|> for Foo { } #[test] - fn test_auto_import_not_applicable_in_use() { + fn test_replace_not_applicable_in_use() { check_assist_not_applicable( - add_import, + replace_qualified_name_with_use, " use std::fmt<|>; ", @@ -943,9 +941,9 @@ use std::fmt<|>; } #[test] - fn test_auto_import_add_use_no_anchor_in_mod_mod() { + fn test_replace_add_use_no_anchor_in_mod_mod() { check_assist( - add_import, + replace_qualified_name_with_use, " mod foo { mod bar { diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index eca6dec4b0..f79189ae80 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -21,7 +21,7 @@ use ra_syntax::{TextRange, TextUnit}; use ra_text_edit::TextEdit; pub(crate) use crate::assist_ctx::{Assist, AssistCtx, AssistHandler}; -pub use crate::handlers::add_import::auto_import_text_edit; +pub use crate::handlers::replace_qualified_name_with_use::insert_use_statement; /// Unique identifier of the assist, should not be shown to the user /// directly. @@ -133,7 +133,7 @@ mod handlers { mod replace_if_let_with_match; mod split_import; mod remove_dbg; - pub(crate) mod add_import; + pub(crate) mod replace_qualified_name_with_use; mod add_missing_impl_members; mod move_guard; mod move_bounds; @@ -158,7 +158,7 @@ mod handlers { replace_if_let_with_match::replace_if_let_with_match, split_import::split_import, remove_dbg::remove_dbg, - add_import::add_import, + replace_qualified_name_with_use::replace_qualified_name_with_use, add_missing_impl_members::add_missing_impl_members, add_missing_impl_members::add_missing_default_members, inline_local_variable::inline_local_variable, diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index 64b04ec2b6..aaa9985d4e 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use ra_assists::auto_import_text_edit; +use ra_assists::insert_use_statement; use ra_syntax::{ast, AstNode, SmolStr}; use ra_text_edit::TextEditBuilder; use rustc_hash::FxHashMap; @@ -26,12 +26,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { let edit = { let mut builder = TextEditBuilder::default(); builder.replace(ctx.source_range(), name.to_string()); - auto_import_text_edit( - &ctx.token.parent(), - &ctx.token.parent(), - &path, - &mut builder, - ); + insert_use_statement(&ctx.token.parent(), &ctx.token.parent(), &path, &mut builder); builder.finish() }; diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index 4b301d67af..e27b27ffab 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -1,4 +1,4 @@ -//! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen` +//! Generated file, do not edit by hand, see `xtask/src/codegen` #![allow(bad_style, missing_docs, unreachable_pub)] #[doc = r" The kind of syntax node, e.g. `IDENT`, `USE_KW`, or `STRUCT_DEF`."] diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 33d5578e70..435135f92d 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1,4 +1,4 @@ -//! Generated file, do not edit by hand, see `crate/ra_tools/src/codegen` +//! Generated file, do not edit by hand, see `xtask/src/codegen` use crate::{ ast::{self, AstChildren, AstNode}, diff --git a/docs/user/assists.md b/docs/user/assists.md index 1e2dd74858..f737a2fa49 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -154,20 +154,6 @@ impl Trait for () { } ``` -## `add_import` - -Adds a use statement for a given fully-qualified path. - -```rust -// BEFORE -fn process(map: std::collections::┃HashMap) {} - -// AFTER -use std::collections::HashMap; - -fn process(map: HashMap) {} -``` - ## `add_new` Adds a new inherent impl for a type. @@ -568,6 +554,20 @@ fn handle(action: Action) { } ``` +## `replace_qualified_name_with_use` + +Adds a use statement for a given fully-qualified name. + +```rust +// BEFORE +fn process(map: std::collections::┃HashMap) {} + +// AFTER +use std::collections::HashMap; + +fn process(map: HashMap) {} +``` + ## `split_import` Wraps the tail of import into braces. diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index 9b0afe8e0c..8fdf43e4a8 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -53,8 +53,7 @@ fn reformat(text: impl std::fmt::Display) -> Result { write!(rustfmt.stdin.take().unwrap(), "{}", text)?; let output = rustfmt.wait_with_output()?; let stdout = String::from_utf8(output.stdout)?; - // TODO: update the preable: replace ra_tools with the relevant path - let preamble = "Generated file, do not edit by hand, see `crate/ra_tools/src/codegen`"; + let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`"; Ok(format!("//! {}\n\n{}", preamble, stdout)) }