From 0e0ae47b47f80e30fca366e5922c19ff81b0a2e2 Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Tue, 19 Feb 2019 17:54:00 +0100 Subject: auto_import: use TextEditBuilder instead of AssistBuilder to make it more reusable --- crates/ra_assists/src/assist_ctx.rs | 4 ++++ crates/ra_assists/src/auto_import.rs | 40 ++++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 8 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index e80e35738..17a9041c5 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -144,6 +144,10 @@ impl AssistBuilder { self.replace(node.range(), replace_with) } + pub(crate) fn set_edit_builder(&mut self, edit: TextEditBuilder) { + self.edit = edit; + } + #[allow(unused)] pub(crate) fn delete(&mut self, range: TextRange) { self.edit.delete(range) diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 3fdf6b0d9..6bc5caf76 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -1,3 +1,4 @@ +use ra_text_edit::TextEditBuilder; use hir::db::HirDatabase; use ra_syntax::{ @@ -374,7 +375,7 @@ fn best_action_for_target<'b, 'a: 'b>( } } -fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut AssistBuilder) { +fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut TextEditBuilder) { match action { ImportAction::AddNewUse { anchor, add_after_anchor } => { make_assist_add_new_use(anchor, *add_after_anchor, target, edit) @@ -408,7 +409,7 @@ fn make_assist_add_new_use( anchor: &Option<&SyntaxNode>, after: bool, target: &[&ast::PathSegment], - edit: &mut AssistBuilder, + edit: &mut TextEditBuilder, ) { if let Some(anchor) = anchor { let indent = ra_fmt::leading_indent(anchor); @@ -437,7 +438,7 @@ fn make_assist_add_in_tree_list( tree_list: &ast::UseTreeList, target: &[&ast::PathSegment], add_self: bool, - edit: &mut AssistBuilder, + edit: &mut TextEditBuilder, ) { let last = tree_list.use_trees().last(); if let Some(last) = last { @@ -466,7 +467,7 @@ fn make_assist_add_nested_import( first_segment_to_split: &Option<&ast::PathSegment>, target: &[&ast::PathSegment], add_self: bool, - edit: &mut AssistBuilder, + edit: &mut TextEditBuilder, ) { let use_tree = path.syntax().ancestors().find_map(ast::UseTree::cast); if let Some(use_tree) = use_tree { @@ -491,7 +492,7 @@ fn make_assist_add_nested_import( buf.push_str(", "); } edit.insert(start, buf); - edit.insert(end, "}"); + edit.insert(end, "}".to_string()); } } @@ -499,7 +500,7 @@ fn apply_auto_import<'a>( container: &SyntaxNode, path: &ast::Path, target: &[&'a ast::PathSegment], - edit: &mut AssistBuilder, + edit: &mut TextEditBuilder, ) { let action = best_action_for_target(container, path, target); make_assist(&action, target, edit); @@ -513,6 +514,25 @@ fn apply_auto_import<'a>( } } +pub fn auto_import_text_edit<'a>( + position: &SyntaxNode, + path: &ast::Path, + target: &[&'a ast::PathSegment], + edit: &mut TextEditBuilder, +) { + let container = position.ancestors().find_map(|n| { + if let Some(module) = ast::Module::cast(n) { + return module.item_list().map(ast::AstNode::syntax); + } + ast::SourceFile::cast(n).map(ast::AstNode::syntax) + }); + + if let Some(container) = container { + let action = best_action_for_target(container, path, target); + make_assist(&action, target, edit); + } +} + pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option { let path: &ast::Path = ctx.node_at_offset()?; // We don't want to mess with use statements @@ -531,7 +551,9 @@ pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option) -> Option Date: Tue, 19 Feb 2019 22:32:00 +0100 Subject: auto_import: make auto import working with target as a list of SmolStr instead of ast::Path --- crates/ra_assists/src/auto_import.rs | 128 ++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 63 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 6bc5caf76..7527d805d 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -1,21 +1,15 @@ use ra_text_edit::TextEditBuilder; -use hir::db::HirDatabase; +use hir::{ self, db::HirDatabase}; use ra_syntax::{ - ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, + ast::{ self, NameOwner }, AstNode, SyntaxNode, Direction, TextRange, SmolStr, SyntaxKind::{ PATH, PATH_SEGMENT, COLONCOLON, COMMA } }; use crate::{ AssistId, - assist_ctx::{AssistCtx, Assist, AssistBuilder}, + assist_ctx::{AssistCtx, Assist}, }; -fn collect_path_segments(path: &ast::Path) -> Option> { - let mut v = Vec::new(); - collect_path_segments_raw(&mut v, path)?; - return Some(v); -} - fn collect_path_segments_raw<'a>( segments: &mut Vec<&'a ast::PathSegment>, mut path: &'a ast::Path, @@ -46,59 +40,43 @@ fn collect_path_segments_raw<'a>( return Some(segments.len() - oldlen); } -fn fmt_segments(segments: &[&ast::PathSegment]) -> String { +fn fmt_segments(segments: &[SmolStr]) -> String { let mut buf = String::new(); fmt_segments_raw(segments, &mut buf); return buf; } -fn fmt_segments_raw(segments: &[&ast::PathSegment], buf: &mut String) { - let mut first = true; - for s in segments { - if !first { - buf.push_str("::"); - } - match s.kind() { - Some(ast::PathSegmentKind::Name(nameref)) => buf.push_str(nameref.text()), - Some(ast::PathSegmentKind::SelfKw) => buf.push_str("self"), - Some(ast::PathSegmentKind::SuperKw) => buf.push_str("super"), - Some(ast::PathSegmentKind::CrateKw) => buf.push_str("crate"), - None => {} - } - first = false; +fn fmt_segments_raw(segments: &[SmolStr], buf: &mut String) { + let mut iter = segments.iter(); + if let Some(s) = iter.next() { + buf.push_str(s); + } + for s in iter { + buf.push_str("::"); + buf.push_str(s); } } // Returns the numeber of common segments. -fn compare_path_segments(left: &[&ast::PathSegment], right: &[&ast::PathSegment]) -> usize { +fn compare_path_segments(left: &[SmolStr], right: &[&ast::PathSegment]) -> usize { return left.iter().zip(right).filter(|(l, r)| compare_path_segment(l, r)).count(); } -fn compare_path_segment(a: &ast::PathSegment, b: &ast::PathSegment) -> bool { - if let (Some(ka), Some(kb)) = (a.kind(), b.kind()) { - match (ka, kb) { - (ast::PathSegmentKind::Name(nameref_a), ast::PathSegmentKind::Name(nameref_b)) => { - nameref_a.text() == nameref_b.text() - } - (ast::PathSegmentKind::SelfKw, ast::PathSegmentKind::SelfKw) => true, - (ast::PathSegmentKind::SuperKw, ast::PathSegmentKind::SuperKw) => true, - (ast::PathSegmentKind::CrateKw, ast::PathSegmentKind::CrateKw) => true, - (_, _) => false, +fn compare_path_segment(a: &SmolStr, b: &ast::PathSegment) -> bool { + if let Some(kb) = b.kind() { + match kb { + ast::PathSegmentKind::Name(nameref_b) => a == nameref_b.text(), + ast::PathSegmentKind::SelfKw => a == "self", + ast::PathSegmentKind::SuperKw => a == "super", + ast::PathSegmentKind::CrateKw => a == "crate", } } else { false } } -fn compare_path_segment_with_name(a: &ast::PathSegment, b: &ast::Name) -> bool { - if let Some(ka) = a.kind() { - return match (ka, b) { - (ast::PathSegmentKind::Name(nameref_a), _) => nameref_a.text() == b.text(), - (_, _) => false, - }; - } else { - false - } +fn compare_path_segment_with_name(a: &SmolStr, b: &ast::Name) -> bool { + a == b.text() } #[derive(Copy, Clone)] @@ -190,7 +168,7 @@ fn walk_use_tree_for_best_action<'a>( current_path_segments: &mut Vec<&'a ast::PathSegment>, // buffer containing path segments current_parent_use_tree_list: Option<&'a ast::UseTreeList>, // will be Some value if we are in a nested import current_use_tree: &'a ast::UseTree, // the use tree we are currently examinating - target: &[&'a ast::PathSegment], // the path we want to import + target: &[SmolStr], // the path we want to import ) -> ImportAction<'a> { // We save the number of segments in the buffer so we can restore the correct segments // before returning. Recursive call will add segments so we need to delete them. @@ -216,7 +194,7 @@ fn walk_use_tree_for_best_action<'a>( // This can happen only if current_use_tree is a direct child of a UseItem if let Some(name) = alias.and_then(ast::NameOwner::name) { - if compare_path_segment_with_name(target[0], name) { + if compare_path_segment_with_name(&target[0], name) { return ImportAction::Nothing; } } @@ -345,8 +323,8 @@ fn walk_use_tree_for_best_action<'a>( fn best_action_for_target<'b, 'a: 'b>( container: &'a SyntaxNode, - path: &'a ast::Path, - target: &'b [&'a ast::PathSegment], + anchor: &'a SyntaxNode, + target: &'b [SmolStr], ) -> ImportAction<'a> { let mut storage = Vec::with_capacity(16); // this should be the only allocation let best_action = container @@ -368,14 +346,14 @@ fn best_action_for_target<'b, 'a: 'b>( .children() .find_map(ast::ModuleItem::cast) .map(AstNode::syntax) - .or(Some(path.syntax())); + .or(Some(anchor)); return ImportAction::add_new_use(anchor, false); } } } -fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut TextEditBuilder) { +fn make_assist(action: &ImportAction, target: &[SmolStr], edit: &mut TextEditBuilder) { match action { ImportAction::AddNewUse { anchor, add_after_anchor } => { make_assist_add_new_use(anchor, *add_after_anchor, target, edit) @@ -408,7 +386,7 @@ fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut T fn make_assist_add_new_use( anchor: &Option<&SyntaxNode>, after: bool, - target: &[&ast::PathSegment], + target: &[SmolStr], edit: &mut TextEditBuilder, ) { if let Some(anchor) = anchor { @@ -436,7 +414,7 @@ fn make_assist_add_new_use( fn make_assist_add_in_tree_list( tree_list: &ast::UseTreeList, - target: &[&ast::PathSegment], + target: &[SmolStr], add_self: bool, edit: &mut TextEditBuilder, ) { @@ -465,7 +443,7 @@ fn make_assist_add_in_tree_list( fn make_assist_add_nested_import( path: &ast::Path, first_segment_to_split: &Option<&ast::PathSegment>, - target: &[&ast::PathSegment], + target: &[SmolStr], add_self: bool, edit: &mut TextEditBuilder, ) { @@ -496,28 +474,51 @@ fn make_assist_add_nested_import( } } -fn apply_auto_import<'a>( +fn apply_auto_import( container: &SyntaxNode, path: &ast::Path, - target: &[&'a ast::PathSegment], + target: &[SmolStr], edit: &mut TextEditBuilder, ) { - let action = best_action_for_target(container, path, target); + let action = best_action_for_target(container, path.syntax(), target); make_assist(&action, target, edit); - if let (Some(first), Some(last)) = (target.first(), target.last()) { + if let Some(last) = path.segment() { // Here we are assuming the assist will provide a correct use statement // so we can delete the path qualifier edit.delete(TextRange::from_to( - first.syntax().range().start(), + path.syntax().range().start(), last.syntax().range().start(), )); } } -pub fn auto_import_text_edit<'a>( +#[allow(unused)] +pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { + let mut ps = Vec::::with_capacity(10); + match path.kind { + hir::PathKind::Abs => ps.push("".into()), + hir::PathKind::Crate => ps.push("crate".into()), + hir::PathKind::Plain => {}, + hir::PathKind::Self_ => ps.push("self".into()), + hir::PathKind::Super => ps.push("super".into()) + } + for s in path.segments.iter() { + ps.push(s.name.to_smolstr()); + } + ps +} + +// This function produces sequence of text edits into edit +// to import the target path in the most appropriate scope given +// the cursor position +#[allow(unused)] +pub fn auto_import_text_edit( + // Ideally the position of the cursor, used to position: &SyntaxNode, - path: &ast::Path, - target: &[&'a ast::PathSegment], + // The statement to use as anchor (last resort) + anchor: &SyntaxNode, + // The path to import as a sequence of strings + target: &[SmolStr], edit: &mut TextEditBuilder, ) { let container = position.ancestors().find_map(|n| { @@ -528,7 +529,7 @@ pub fn auto_import_text_edit<'a>( }); if let Some(container) = container { - let action = best_action_for_target(container, path, target); + let action = best_action_for_target(container, anchor, target); make_assist(&action, target, edit); } } @@ -540,7 +541,8 @@ pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option Date: Fri, 19 Apr 2019 22:30:05 +0200 Subject: auto_import: better no anchor management --- crates/ra_assists/src/auto_import.rs | 55 +++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 7 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 7527d805d..b002d0e4d 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -168,7 +168,7 @@ fn walk_use_tree_for_best_action<'a>( current_path_segments: &mut Vec<&'a ast::PathSegment>, // buffer containing path segments current_parent_use_tree_list: Option<&'a ast::UseTreeList>, // will be Some value if we are in a nested import current_use_tree: &'a ast::UseTree, // the use tree we are currently examinating - target: &[SmolStr], // the path we want to import + target: &[SmolStr], // the path we want to import ) -> ImportAction<'a> { // We save the number of segments in the buffer so we can restore the correct segments // before returning. Recursive call will add segments so we need to delete them. @@ -341,11 +341,11 @@ fn best_action_for_target<'b, 'a: 'b>( None => { // We have no action and no UseItem was found in container so we find // another item and we use it as anchor. - // If there are no items, we choose the target path itself as anchor. + // If there are no items above, we choose the target path itself as anchor. + // todo: we should include even whitespace blocks as anchor candidates let anchor = container .children() - .find_map(ast::ModuleItem::cast) - .map(AstNode::syntax) + .find(|n| n.range().start() < anchor.range().start()) .or(Some(anchor)); return ImportAction::add_new_use(anchor, false); @@ -498,9 +498,9 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { match path.kind { hir::PathKind::Abs => ps.push("".into()), hir::PathKind::Crate => ps.push("crate".into()), - hir::PathKind::Plain => {}, + hir::PathKind::Plain => {} hir::PathKind::Self_ => ps.push("self".into()), - hir::PathKind::Super => ps.push("super".into()) + hir::PathKind::Super => ps.push("super".into()), } for s in path.segments.iter() { ps.push(s.name.to_smolstr()); @@ -513,7 +513,7 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { // the cursor position #[allow(unused)] pub fn auto_import_text_edit( - // Ideally the position of the cursor, used to + // Ideally the position of the cursor, used to position: &SyntaxNode, // The statement to use as anchor (last resort) anchor: &SyntaxNode, @@ -590,6 +590,47 @@ std::fmt::Debug<|> " use std::fmt::Debug; +Debug<|> + ", + ); + } + #[test] + fn test_auto_import_add_use_no_anchor_with_item_below() { + check_assist( + auto_import, + " +std::fmt::Debug<|> + +fn main() { +} + ", + " +use std::fmt::Debug; + +Debug<|> + +fn main() { +} + ", + ); + } + + #[test] + fn test_auto_import_add_use_no_anchor_with_item_above() { + check_assist( + auto_import, + " +fn main() { +} + +std::fmt::Debug<|> + ", + " +use std::fmt::Debug; + +fn main() { +} + Debug<|> ", ); -- cgit v1.2.3 From 914421495835380b96e0016763fda6eff31a8179 Mon Sep 17 00:00:00 2001 From: Andrea Pretto Date: Mon, 15 Apr 2019 16:11:32 +0200 Subject: complete_import: add new import resolver infrastructure with some hardcoded importable name. Changes complete_scope to support that. --- crates/ra_assists/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index ded401b63..173c004cd 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -98,7 +98,7 @@ mod inline_local_variable; mod replace_if_let_with_match; mod split_import; mod remove_dbg; -mod auto_import; +pub mod auto_import; mod add_missing_impl_members; fn all_assists() -> &'static [fn(AssistCtx) -> Option] { -- cgit v1.2.3 From e01052d1f0b8bf70a418a10538528923c5f400d5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Apr 2019 15:56:28 +0300 Subject: move auto-imoprter into IDE auto-import is purely an IDE concern, so it should be done outside of HIR --- crates/ra_assists/src/auto_import.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index b002d0e4d..7c856c19b 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -492,7 +492,6 @@ fn apply_auto_import( } } -#[allow(unused)] pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { let mut ps = Vec::::with_capacity(10); match path.kind { @@ -503,7 +502,7 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { hir::PathKind::Super => ps.push("super".into()), } for s in path.segments.iter() { - ps.push(s.name.to_smolstr()); + ps.push(s.name.to_string().into()); } ps } @@ -511,7 +510,6 @@ pub fn collect_hir_path_segments(path: &hir::Path) -> Vec { // This function produces sequence of text edits into edit // to import the target path in the most appropriate scope given // the cursor position -#[allow(unused)] pub fn auto_import_text_edit( // Ideally the position of the cursor, used to position: &SyntaxNode, -- cgit v1.2.3