mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Move test_utils into tests module
This commit is contained in:
parent
11115ebad8
commit
aa644b5585
@ -322,7 +322,7 @@ mod tests {
|
|||||||
|
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{test_utils::completion_list, CompletionKind};
|
use crate::{tests::filtered_completion_list, CompletionKind};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn attributes_are_sorted() {
|
fn attributes_are_sorted() {
|
||||||
@ -341,7 +341,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Attribute);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Attribute);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ const DEFAULT_DERIVE_COMPLETIONS: &[DeriveDependencies] = &[
|
|||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{test_utils::completion_list, CompletionKind};
|
use crate::{tests::filtered_completion_list, CompletionKind};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let builtin_derives = r#"
|
let builtin_derives = r#"
|
||||||
@ -106,7 +106,7 @@ pub macro PartialOrd {}
|
|||||||
pub macro Ord {}
|
pub macro Ord {}
|
||||||
|
|
||||||
"#;
|
"#;
|
||||||
let actual = completion_list(
|
let actual = filtered_completion_list(
|
||||||
&format!("{} {}", builtin_derives, ra_fixture),
|
&format!("{} {}", builtin_derives, ra_fixture),
|
||||||
CompletionKind::Attribute,
|
CompletionKind::Attribute,
|
||||||
);
|
);
|
||||||
|
@ -34,7 +34,7 @@ pub(super) fn complete_lint(
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use crate::test_utils::check_edit;
|
use crate::tests::check_edit;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_empty() {
|
fn check_empty() {
|
||||||
|
@ -101,10 +101,10 @@ fn complete_methods(
|
|||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{test_utils::completion_list, CompletionKind};
|
use crate::{tests::filtered_completion_list, CompletionKind};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Reference);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,11 +227,11 @@ mod tests {
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
item::CompletionKind,
|
item::CompletionKind,
|
||||||
test_utils::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG},
|
tests::{check_edit, check_edit_with_config, filtered_completion_list, TEST_CONFIG},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Magic);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,10 +64,10 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
|
|||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{test_utils::completion_list, CompletionKind};
|
use crate::{tests::filtered_completion_list, CompletionKind};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Magic);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,12 +190,12 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, completion_list},
|
tests::{check_edit, filtered_completion_list},
|
||||||
CompletionKind,
|
CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Keyword);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Keyword);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
|
tests::{check_edit, filtered_completion_list_with_config, TEST_CONFIG},
|
||||||
CompletionConfig, CompletionKind,
|
CompletionConfig, CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -59,7 +59,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
|
fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
|
let actual =
|
||||||
|
filtered_completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,11 +141,11 @@ fn module_chain_to_containing_module_file(
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{test_utils::completion_list, CompletionKind};
|
use crate::{tests::filtered_completion_list, CompletionKind};
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Magic);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,17 +61,17 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, completion_list},
|
tests::{check_edit, filtered_completion_list},
|
||||||
CompletionKind,
|
CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Reference);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_snippet(ra_fixture: &str, expect: Expect) {
|
fn check_snippet(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Snippet);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,12 +307,12 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, completion_list},
|
tests::{check_edit, filtered_completion_list},
|
||||||
CompletionKind,
|
CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Postfix);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Postfix);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,17 +198,17 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, completion_list},
|
tests::{check_edit, filtered_completion_list},
|
||||||
CompletionKind,
|
CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Reference);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_builtin(ra_fixture: &str, expect: Expect) {
|
fn check_builtin(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::BuiltinType);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::BuiltinType);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,17 +51,17 @@ mod tests {
|
|||||||
use ide_db::helpers::FamousDefs;
|
use ide_db::helpers::FamousDefs;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{self, completion_list},
|
tests::{self, filtered_completion_list},
|
||||||
CompletionKind,
|
CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Reference);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
|
||||||
expect.assert_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_snippet(ra_fixture: &str, expect: Expect) {
|
fn check_snippet(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(
|
let actual = filtered_completion_list(
|
||||||
&format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE),
|
&format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE),
|
||||||
CompletionKind::Snippet,
|
CompletionKind::Snippet,
|
||||||
);
|
);
|
||||||
@ -69,7 +69,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||||
test_utils::check_edit(
|
tests::check_edit(
|
||||||
what,
|
what,
|
||||||
&format!(
|
&format!(
|
||||||
"//- /main.rs crate:main deps:core{}\n{}",
|
"//- /main.rs crate:main deps:core{}\n{}",
|
||||||
|
@ -87,10 +87,10 @@ fn ${1:feature}() {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{test_utils::completion_list, CompletionKind};
|
use crate::{tests::filtered_completion_list, CompletionKind};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Snippet);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,12 +246,12 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, completion_list},
|
tests::{check_edit, filtered_completion_list},
|
||||||
CompletionKind,
|
CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list(ra_fixture, CompletionKind::Magic);
|
let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
|
tests::{check_edit, filtered_completion_list_with_config, TEST_CONFIG},
|
||||||
CompletionConfig, CompletionKind,
|
CompletionConfig, CompletionKind,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -102,7 +102,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
|
fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
|
||||||
let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
|
let actual =
|
||||||
|
filtered_completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
|
||||||
expect.assert_eq(&actual)
|
expect.assert_eq(&actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -695,7 +695,7 @@ mod tests {
|
|||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
use hir::HirDisplay;
|
use hir::HirDisplay;
|
||||||
|
|
||||||
use crate::test_utils::{position, TEST_CONFIG};
|
use crate::tests::{position, TEST_CONFIG};
|
||||||
|
|
||||||
use super::CompletionContext;
|
use super::CompletionContext;
|
||||||
|
|
||||||
|
@ -9,8 +9,6 @@ mod render;
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
#[cfg(test)]
|
|
||||||
mod test_utils;
|
|
||||||
|
|
||||||
use completions::flyimport::position_for_import;
|
use completions::flyimport::position_for_import;
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
|
@ -11,7 +11,7 @@ use syntax::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable};
|
use crate::tests::{check_pattern_is_applicable, check_pattern_is_not_applicable};
|
||||||
|
|
||||||
/// Immediate previous node to what we are completing.
|
/// Immediate previous node to what we are completing.
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
@ -324,7 +324,7 @@ fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use syntax::algo::find_node_at_offset;
|
use syntax::algo::find_node_at_offset;
|
||||||
|
|
||||||
use crate::test_utils::position;
|
use crate::tests::position;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -335,7 +335,7 @@ mod tests {
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
item::CompletionRelevanceTypeMatch,
|
item::CompletionRelevanceTypeMatch,
|
||||||
test_utils::{check_edit, do_completion, get_all_items, TEST_CONFIG},
|
tests::{check_edit, do_completion, get_all_items, TEST_CONFIG},
|
||||||
CompletionKind, CompletionRelevance,
|
CompletionKind, CompletionRelevance,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ impl<'a> EnumRender<'a> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::test_utils::check_edit;
|
use crate::tests::check_edit;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn inserts_parens_for_tuple_enums() {
|
fn inserts_parens_for_tuple_enums() {
|
||||||
|
@ -191,7 +191,7 @@ impl<'a> FunctionRender<'a> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{
|
||||||
test_utils::{check_edit, check_edit_with_config, TEST_CONFIG},
|
tests::{check_edit, check_edit_with_config, TEST_CONFIG},
|
||||||
CompletionConfig,
|
CompletionConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static s
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::test_utils::check_edit;
|
use crate::tests::check_edit;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn dont_insert_macro_call_parens_unncessary() {
|
fn dont_insert_macro_call_parens_unncessary() {
|
||||||
|
@ -1,154 +0,0 @@
|
|||||||
//! Runs completion for testing purposes.
|
|
||||||
|
|
||||||
use hir::{PrefixKind, Semantics};
|
|
||||||
use ide_db::{
|
|
||||||
base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
|
|
||||||
helpers::{
|
|
||||||
insert_use::{ImportGranularity, InsertUseConfig},
|
|
||||||
SnippetCap,
|
|
||||||
},
|
|
||||||
RootDatabase,
|
|
||||||
};
|
|
||||||
use itertools::Itertools;
|
|
||||||
use stdx::{format_to, trim_indent};
|
|
||||||
use syntax::{AstNode, NodeOrToken, SyntaxElement};
|
|
||||||
use test_utils::assert_eq_text;
|
|
||||||
|
|
||||||
use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
|
|
||||||
|
|
||||||
pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
|
|
||||||
enable_postfix_completions: true,
|
|
||||||
enable_imports_on_the_fly: true,
|
|
||||||
enable_self_on_the_fly: true,
|
|
||||||
add_call_parenthesis: true,
|
|
||||||
add_call_argument_snippets: true,
|
|
||||||
snippet_cap: SnippetCap::new(true),
|
|
||||||
insert_use: InsertUseConfig {
|
|
||||||
granularity: ImportGranularity::Crate,
|
|
||||||
prefix_kind: PrefixKind::Plain,
|
|
||||||
enforce_granularity: true,
|
|
||||||
group: true,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Creates analysis from a multi-file fixture, returns positions marked with $0.
|
|
||||||
pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
|
|
||||||
let change_fixture = ChangeFixture::parse(ra_fixture);
|
|
||||||
let mut database = RootDatabase::default();
|
|
||||||
database.apply_change(change_fixture.change);
|
|
||||||
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
|
|
||||||
let offset = range_or_offset.expect_offset();
|
|
||||||
(database, FilePosition { file_id, offset })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
|
|
||||||
do_completion_with_config(TEST_CONFIG, code, kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn do_completion_with_config(
|
|
||||||
config: CompletionConfig,
|
|
||||||
code: &str,
|
|
||||||
kind: CompletionKind,
|
|
||||||
) -> Vec<CompletionItem> {
|
|
||||||
get_all_items(config, code)
|
|
||||||
.into_iter()
|
|
||||||
.filter(|c| c.completion_kind == kind)
|
|
||||||
.sorted_by(|l, r| l.label().cmp(r.label()))
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn completion_list(code: &str, kind: CompletionKind) -> String {
|
|
||||||
completion_list_with_config(TEST_CONFIG, code, kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn completion_list_with_config(
|
|
||||||
config: CompletionConfig,
|
|
||||||
code: &str,
|
|
||||||
kind: CompletionKind,
|
|
||||||
) -> String {
|
|
||||||
let kind_completions: Vec<CompletionItem> =
|
|
||||||
get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
|
|
||||||
let label_width = kind_completions
|
|
||||||
.iter()
|
|
||||||
.map(|it| monospace_width(it.label()))
|
|
||||||
.max()
|
|
||||||
.unwrap_or_default()
|
|
||||||
.min(16);
|
|
||||||
kind_completions
|
|
||||||
.into_iter()
|
|
||||||
.map(|it| {
|
|
||||||
let tag = it.kind().unwrap().tag();
|
|
||||||
let var_name = format!("{} {}", tag, it.label());
|
|
||||||
let mut buf = var_name;
|
|
||||||
if let Some(detail) = it.detail() {
|
|
||||||
let width = label_width.saturating_sub(monospace_width(it.label()));
|
|
||||||
format_to!(buf, "{:width$} {}", "", detail, width = width);
|
|
||||||
}
|
|
||||||
if it.deprecated() {
|
|
||||||
format_to!(buf, " DEPRECATED");
|
|
||||||
}
|
|
||||||
format_to!(buf, "\n");
|
|
||||||
buf
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn monospace_width(s: &str) -> usize {
|
|
||||||
s.chars().count()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
|
||||||
check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn check_edit_with_config(
|
|
||||||
config: CompletionConfig,
|
|
||||||
what: &str,
|
|
||||||
ra_fixture_before: &str,
|
|
||||||
ra_fixture_after: &str,
|
|
||||||
) {
|
|
||||||
let ra_fixture_after = trim_indent(ra_fixture_after);
|
|
||||||
let (db, position) = position(ra_fixture_before);
|
|
||||||
let completions: Vec<CompletionItem> =
|
|
||||||
crate::completions(&db, &config, position).unwrap().into();
|
|
||||||
let (completion,) = completions
|
|
||||||
.iter()
|
|
||||||
.filter(|it| it.lookup() == what)
|
|
||||||
.collect_tuple()
|
|
||||||
.unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
|
|
||||||
let mut actual = db.file_text(position.file_id).to_string();
|
|
||||||
|
|
||||||
let mut combined_edit = completion.text_edit().to_owned();
|
|
||||||
if let Some(import_text_edit) =
|
|
||||||
completion.import_to_add().and_then(|edit| edit.to_text_edit(config.insert_use))
|
|
||||||
{
|
|
||||||
combined_edit.union(import_text_edit).expect(
|
|
||||||
"Failed to apply completion resolve changes: change ranges overlap, but should not",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
combined_edit.apply(&mut actual);
|
|
||||||
assert_eq_text!(&ra_fixture_after, &actual)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
|
|
||||||
let (db, pos) = position(code);
|
|
||||||
|
|
||||||
let sema = Semantics::new(&db);
|
|
||||||
let original_file = sema.parse(pos.file_id);
|
|
||||||
let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
|
|
||||||
assert!(check(NodeOrToken::Token(token)));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
|
|
||||||
let (db, pos) = position(code);
|
|
||||||
let sema = Semantics::new(&db);
|
|
||||||
let original_file = sema.parse(pos.file_id);
|
|
||||||
let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
|
|
||||||
assert!(!check(NodeOrToken::Token(token)));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
|
|
||||||
let (db, position) = position(code);
|
|
||||||
crate::completions(&db, &config, position).unwrap().into()
|
|
||||||
}
|
|
@ -1,11 +1,34 @@
|
|||||||
mod item_list;
|
mod item_list;
|
||||||
|
|
||||||
use expect_test::Expect;
|
use hir::{PrefixKind, Semantics};
|
||||||
use stdx::format_to;
|
use ide_db::{
|
||||||
|
base_db::{fixture::ChangeFixture, FileLoader, FilePosition},
|
||||||
|
helpers::{
|
||||||
|
insert_use::{ImportGranularity, InsertUseConfig},
|
||||||
|
SnippetCap,
|
||||||
|
},
|
||||||
|
RootDatabase,
|
||||||
|
};
|
||||||
|
use itertools::Itertools;
|
||||||
|
use stdx::{format_to, trim_indent};
|
||||||
|
use syntax::{AstNode, NodeOrToken, SyntaxElement};
|
||||||
|
use test_utils::assert_eq_text;
|
||||||
|
|
||||||
use crate::{
|
use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
|
||||||
test_utils::{self, get_all_items, TEST_CONFIG},
|
|
||||||
CompletionConfig, CompletionItem,
|
pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
|
||||||
|
enable_postfix_completions: true,
|
||||||
|
enable_imports_on_the_fly: true,
|
||||||
|
enable_self_on_the_fly: true,
|
||||||
|
add_call_parenthesis: true,
|
||||||
|
add_call_argument_snippets: true,
|
||||||
|
snippet_cap: SnippetCap::new(true),
|
||||||
|
insert_use: InsertUseConfig {
|
||||||
|
granularity: ImportGranularity::Crate,
|
||||||
|
prefix_kind: PrefixKind::Plain,
|
||||||
|
enforce_granularity: true,
|
||||||
|
group: true,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn completion_list(code: &str) -> String {
|
fn completion_list(code: &str) -> String {
|
||||||
@ -13,18 +36,56 @@ fn completion_list(code: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
|
fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
|
||||||
|
render_completion_list(get_all_items(config, code))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates analysis from a multi-file fixture, returns positions marked with $0.
|
||||||
|
pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
|
||||||
|
let change_fixture = ChangeFixture::parse(ra_fixture);
|
||||||
|
let mut database = RootDatabase::default();
|
||||||
|
database.apply_change(change_fixture.change);
|
||||||
|
let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)");
|
||||||
|
let offset = range_or_offset.expect_offset();
|
||||||
|
(database, FilePosition { file_id, offset })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
|
||||||
|
do_completion_with_config(TEST_CONFIG, code, kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn do_completion_with_config(
|
||||||
|
config: CompletionConfig,
|
||||||
|
code: &str,
|
||||||
|
kind: CompletionKind,
|
||||||
|
) -> Vec<CompletionItem> {
|
||||||
|
get_all_items(config, code)
|
||||||
|
.into_iter()
|
||||||
|
.filter(|c| c.completion_kind == kind)
|
||||||
|
.sorted_by(|l, r| l.label().cmp(r.label()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn filtered_completion_list(code: &str, kind: CompletionKind) -> String {
|
||||||
|
filtered_completion_list_with_config(TEST_CONFIG, code, kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn filtered_completion_list_with_config(
|
||||||
|
config: CompletionConfig,
|
||||||
|
code: &str,
|
||||||
|
kind: CompletionKind,
|
||||||
|
) -> String {
|
||||||
|
let kind_completions: Vec<CompletionItem> =
|
||||||
|
get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
|
||||||
|
render_completion_list(kind_completions)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_completion_list(completions: Vec<CompletionItem>) -> String {
|
||||||
fn monospace_width(s: &str) -> usize {
|
fn monospace_width(s: &str) -> usize {
|
||||||
s.chars().count()
|
s.chars().count()
|
||||||
}
|
}
|
||||||
|
let label_width =
|
||||||
let kind_completions: Vec<CompletionItem> = get_all_items(config, code).into_iter().collect();
|
completions.iter().map(|it| monospace_width(it.label())).max().unwrap_or_default().min(16);
|
||||||
let label_width = kind_completions
|
completions
|
||||||
.iter()
|
|
||||||
.map(|it| monospace_width(it.label()))
|
|
||||||
.max()
|
|
||||||
.unwrap_or_default()
|
|
||||||
.min(16);
|
|
||||||
kind_completions
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|it| {
|
.map(|it| {
|
||||||
let tag = it.kind().unwrap().tag();
|
let tag = it.kind().unwrap().tag();
|
||||||
@ -43,23 +104,64 @@ fn completion_list_with_config(config: CompletionConfig, code: &str) -> String {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check(ra_fixture: &str, expect: Expect) {
|
pub(crate) fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||||
let base = r#"#[rustc_builtin_macro]
|
check_edit_with_config(TEST_CONFIG, what, ra_fixture_before, ra_fixture_after)
|
||||||
pub macro Clone {}
|
}
|
||||||
enum Enum { Variant }
|
|
||||||
struct Struct {}
|
pub(crate) fn check_edit_with_config(
|
||||||
#[macro_export]
|
config: CompletionConfig,
|
||||||
macro_rules! foo {}
|
what: &str,
|
||||||
mod bar {}
|
ra_fixture_before: &str,
|
||||||
const CONST: () = ();
|
ra_fixture_after: &str,
|
||||||
trait Trait {}
|
) {
|
||||||
"#;
|
let ra_fixture_after = trim_indent(ra_fixture_after);
|
||||||
let actual = completion_list(&format!("{}{}", base, ra_fixture));
|
let (db, position) = position(ra_fixture_before);
|
||||||
expect.assert_eq(&actual)
|
let completions: Vec<CompletionItem> =
|
||||||
|
crate::completions(&db, &config, position).unwrap().into();
|
||||||
|
let (completion,) = completions
|
||||||
|
.iter()
|
||||||
|
.filter(|it| it.lookup() == what)
|
||||||
|
.collect_tuple()
|
||||||
|
.unwrap_or_else(|| panic!("can't find {:?} completion in {:#?}", what, completions));
|
||||||
|
let mut actual = db.file_text(position.file_id).to_string();
|
||||||
|
|
||||||
|
let mut combined_edit = completion.text_edit().to_owned();
|
||||||
|
if let Some(import_text_edit) =
|
||||||
|
completion.import_to_add().and_then(|edit| edit.to_text_edit(config.insert_use))
|
||||||
|
{
|
||||||
|
combined_edit.union(import_text_edit).expect(
|
||||||
|
"Failed to apply completion resolve changes: change ranges overlap, but should not",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
combined_edit.apply(&mut actual);
|
||||||
|
assert_eq_text!(&ra_fixture_after, &actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn check_pattern_is_applicable(code: &str, check: impl FnOnce(SyntaxElement) -> bool) {
|
||||||
|
let (db, pos) = position(code);
|
||||||
|
|
||||||
|
let sema = Semantics::new(&db);
|
||||||
|
let original_file = sema.parse(pos.file_id);
|
||||||
|
let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
|
||||||
|
assert!(check(NodeOrToken::Token(token)));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
|
||||||
|
let (db, pos) = position(code);
|
||||||
|
let sema = Semantics::new(&db);
|
||||||
|
let original_file = sema.parse(pos.file_id);
|
||||||
|
let token = original_file.syntax().token_at_offset(pos.offset).left_biased().unwrap();
|
||||||
|
assert!(!check(NodeOrToken::Token(token)));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
|
||||||
|
let (db, position) = position(code);
|
||||||
|
crate::completions(&db, &config, position).unwrap().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_no_completion(ra_fixture: &str) {
|
fn check_no_completion(ra_fixture: &str) {
|
||||||
let (db, position) = test_utils::position(ra_fixture);
|
let (db, position) = position(ra_fixture);
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
crate::completions(&db, &TEST_CONFIG, position).is_none(),
|
crate::completions(&db, &TEST_CONFIG, position).is_none(),
|
||||||
|
@ -1,14 +1,26 @@
|
|||||||
use expect_test::expect;
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
use crate::tests::check;
|
use crate::tests::completion_list;
|
||||||
|
|
||||||
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
|
let base = r#"#[rustc_builtin_macro]
|
||||||
|
pub macro Clone {}
|
||||||
|
enum Enum { Variant }
|
||||||
|
struct Struct {}
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! foo {}
|
||||||
|
mod bar {}
|
||||||
|
const CONST: () = ();
|
||||||
|
trait Trait {}
|
||||||
|
"#;
|
||||||
|
let actual = completion_list(&format!("{}{}", base, ra_fixture));
|
||||||
|
expect.assert_eq(&actual)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn in_mod_item_list() {
|
fn in_mod_item_list() {
|
||||||
check(
|
check(
|
||||||
r#"mod tests {
|
r#"mod tests { $0 }"#,
|
||||||
$0
|
|
||||||
}
|
|
||||||
"#,
|
|
||||||
expect![[r##"
|
expect![[r##"
|
||||||
kw pub(crate)
|
kw pub(crate)
|
||||||
kw pub
|
kw pub
|
||||||
@ -164,9 +176,7 @@ fn after_visibility_unsafe() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn in_impl_assoc_item_list() {
|
fn in_impl_assoc_item_list() {
|
||||||
check(
|
check(
|
||||||
r#"impl Struct {
|
r#"impl Struct { $0 }"#,
|
||||||
$0
|
|
||||||
}"#,
|
|
||||||
expect![[r##"
|
expect![[r##"
|
||||||
kw pub(crate)
|
kw pub(crate)
|
||||||
kw pub
|
kw pub
|
||||||
@ -184,9 +194,7 @@ fn in_impl_assoc_item_list() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn in_impl_assoc_item_list_after_attr() {
|
fn in_impl_assoc_item_list_after_attr() {
|
||||||
check(
|
check(
|
||||||
r#"impl Struct {
|
r#"impl Struct { #[attr] $0 }"#,
|
||||||
#[attr] $0
|
|
||||||
}"#,
|
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
kw pub(crate)
|
kw pub(crate)
|
||||||
kw pub
|
kw pub
|
||||||
|
Loading…
Reference in New Issue
Block a user