From b290cd578260f307e872a95f971e5a7c656a80ed Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 20 Apr 2021 19:28:18 +0200 Subject: Add cov_marks to insert_use tests --- crates/ide_db/src/helpers/insert_use.rs | 17 ++++++++++++++++- crates/ide_db/src/helpers/insert_use/tests.rs | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'crates/ide_db/src/helpers') diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index 498d76f72..a43504a27 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -38,13 +38,18 @@ impl ImportScope { } /// Determines the containing syntax node in which to insert a `use` statement affecting `position`. - pub fn find_insert_use_container( + pub fn find_insert_use_container_with_macros( position: &SyntaxNode, sema: &Semantics<'_, RootDatabase>, ) -> Option { sema.ancestors_with_macros(position.clone()).find_map(Self::from) } + /// Determines the containing syntax node in which to insert a `use` statement affecting `position`. + pub fn find_insert_use_container(position: &SyntaxNode) -> Option { + std::iter::successors(Some(position.clone()), SyntaxNode::parent).find_map(Self::from) + } + pub fn as_syntax_node(&self) -> &SyntaxNode { match self { ImportScope::File(file) => file.syntax(), @@ -446,8 +451,10 @@ fn insert_use_( if !group_imports { if let Some((_, _, node)) = path_node_iter.last() { + cov_mark::hit!(insert_no_grouping_last); ted::insert(ted::Position::after(node), use_item.syntax()); } else { + cov_mark::hit!(insert_no_grouping_last2); ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line()); ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax()); } @@ -471,10 +478,12 @@ fn insert_use_( }); if let Some((.., node)) = post_insert { + cov_mark::hit!(insert_group); // insert our import before that element return ted::insert(ted::Position::before(node), use_item.syntax()); } if let Some(node) = last { + cov_mark::hit!(insert_group_last); // there is no element after our new import, so append it to the end of the group return ted::insert(ted::Position::after(node), use_item.syntax()); } @@ -487,6 +496,7 @@ fn insert_use_( .inspect(|(.., node)| last = Some(node.clone())) .find(|(p, ..)| ImportGroup::new(p) > group); if let Some((.., node)) = post_group { + cov_mark::hit!(insert_group_new_group); ted::insert(ted::Position::before(&node), use_item.syntax()); if let Some(node) = algo::non_trivia_sibling(node.into(), Direction::Prev) { ted::insert(ted::Position::after(node), make::tokens::single_newline()); @@ -495,6 +505,7 @@ fn insert_use_( } // there is no such group, so append after the last one if let Some(node) = last { + cov_mark::hit!(insert_group_no_group); ted::insert(ted::Position::after(&node), use_item.syntax()); ted::insert(ted::Position::after(node), make::tokens::single_newline()); return; @@ -508,22 +519,26 @@ fn insert_use_( }) .last() { + cov_mark::hit!(insert_group_empty_inner_attr); ted::insert(ted::Position::after(&last_inner_element), use_item.syntax()); ted::insert(ted::Position::after(last_inner_element), make::tokens::single_newline()); return; } match scope { ImportScope::File(_) => { + cov_mark::hit!(insert_group_empty_file); ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line()); ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax()) } // don't insert the imports before the item list's opening curly brace ImportScope::Module(item_list) => match item_list.l_curly_token() { Some(b) => { + cov_mark::hit!(insert_group_empty_module); ted::insert(ted::Position::after(&b), make::tokens::single_newline()); ted::insert(ted::Position::after(&b), use_item.syntax()); } None => { + // This should never happens, broken module syntax node ted::insert( ted::Position::first_child_of(scope_syntax), make::tokens::blank_line(), diff --git a/crates/ide_db/src/helpers/insert_use/tests.rs b/crates/ide_db/src/helpers/insert_use/tests.rs index a3464d606..048c213e2 100644 --- a/crates/ide_db/src/helpers/insert_use/tests.rs +++ b/crates/ide_db/src/helpers/insert_use/tests.rs @@ -5,6 +5,7 @@ use test_utils::assert_eq_text; #[test] fn insert_not_group() { + cov_mark::check!(insert_no_grouping_last); check( "use external_crate2::bar::A", r" @@ -26,6 +27,21 @@ use external_crate2::bar::A;", ); } +#[test] +fn insert_not_group_empty() { + cov_mark::check!(insert_no_grouping_last2); + check( + "use external_crate2::bar::A", + r"", + r"use external_crate2::bar::A; + +", + None, + false, + false, + ); +} + #[test] fn insert_existing() { check_full("std::fs", "use std::fs;", "use std::fs;") @@ -65,6 +81,7 @@ fn insert_start_indent() { #[test] fn insert_middle() { + cov_mark::check!(insert_group); check_none( "std::bar::EE", r" @@ -101,6 +118,7 @@ fn insert_middle_indent() { #[test] fn insert_end() { + cov_mark::check!(insert_group_last); check_none( "std::bar::ZZ", r" @@ -199,6 +217,7 @@ fn insert_first_matching_group() { #[test] fn insert_missing_group_std() { + cov_mark::check!(insert_group_new_group); check_none( "std::fmt", r" @@ -214,6 +233,7 @@ fn insert_missing_group_std() { #[test] fn insert_missing_group_self() { + cov_mark::check!(insert_group_no_group); check_none( "self::fmt", r" @@ -240,6 +260,7 @@ fn main() {}", #[test] fn insert_empty_file() { + cov_mark::check!(insert_group_empty_file); // empty files will get two trailing newlines // this is due to the test case insert_no_imports above check_full( @@ -253,6 +274,7 @@ fn insert_empty_file() { #[test] fn insert_empty_module() { + cov_mark::check!(insert_group_empty_module); check( "foo::bar", "mod x {}", @@ -267,6 +289,7 @@ fn insert_empty_module() { #[test] fn insert_after_inner_attr() { + cov_mark::check!(insert_group_empty_inner_attr); check_full( "foo::bar", r"#![allow(unused_imports)]", -- cgit v1.2.3