2023-12-18 13:53:11 +00:00
|
|
|
use hir::PrefixKind;
|
2021-11-03 20:12:36 +00:00
|
|
|
use stdx::trim_indent;
|
2023-12-18 13:53:11 +00:00
|
|
|
use test_fixture::WithFixture;
|
2021-11-03 20:12:36 +00:00
|
|
|
use test_utils::{assert_eq_text, CURSOR_MARKER};
|
2021-06-19 20:31:24 +00:00
|
|
|
|
2021-09-26 09:12:57 +00:00
|
|
|
use super::*;
|
|
|
|
|
2022-05-09 09:52:49 +00:00
|
|
|
#[test]
|
|
|
|
fn trailing_comment_in_empty_file() {
|
|
|
|
check(
|
|
|
|
"foo::bar",
|
|
|
|
r#"
|
|
|
|
struct Struct;
|
|
|
|
// 0 = 1
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use foo::bar;
|
|
|
|
|
|
|
|
struct Struct;
|
|
|
|
// 0 = 1
|
|
|
|
"#,
|
|
|
|
ImportGranularity::Crate,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-19 20:31:24 +00:00
|
|
|
#[test]
|
|
|
|
fn respects_cfg_attr_fn() {
|
|
|
|
check(
|
|
|
|
r"bar::Bar",
|
|
|
|
r#"
|
|
|
|
#[cfg(test)]
|
|
|
|
fn foo() {$0}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[cfg(test)]
|
|
|
|
fn foo() {
|
2021-07-05 13:34:01 +00:00
|
|
|
use bar::Bar;
|
2021-06-19 20:31:24 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
ImportGranularity::Crate,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn respects_cfg_attr_const() {
|
|
|
|
check(
|
|
|
|
r"bar::Bar",
|
|
|
|
r#"
|
|
|
|
#[cfg(test)]
|
|
|
|
const FOO: Bar = {$0};
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[cfg(test)]
|
|
|
|
const FOO: Bar = {
|
2021-07-05 13:34:01 +00:00
|
|
|
use bar::Bar;
|
2021-06-19 20:31:24 +00:00
|
|
|
};
|
|
|
|
"#,
|
|
|
|
ImportGranularity::Crate,
|
|
|
|
);
|
|
|
|
}
|
2020-12-03 15:05:39 +00:00
|
|
|
|
2021-06-18 21:11:56 +00:00
|
|
|
#[test]
|
|
|
|
fn insert_skips_lone_glob_imports() {
|
|
|
|
check(
|
|
|
|
"use foo::baz::A",
|
|
|
|
r"
|
|
|
|
use foo::bar::*;
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
use foo::bar::*;
|
|
|
|
use foo::baz::A;
|
|
|
|
",
|
|
|
|
ImportGranularity::Crate,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-06 11:02:26 +00:00
|
|
|
#[test]
|
|
|
|
fn insert_not_group() {
|
2021-04-20 17:28:18 +00:00
|
|
|
cov_mark::check!(insert_no_grouping_last);
|
2021-06-19 20:31:24 +00:00
|
|
|
check_with_config(
|
2021-03-06 11:02:26 +00:00
|
|
|
"use external_crate2::bar::A",
|
|
|
|
r"
|
|
|
|
use std::bar::B;
|
|
|
|
use external_crate::bar::A;
|
|
|
|
use crate::bar::A;
|
|
|
|
use self::bar::A;
|
|
|
|
use super::bar::A;",
|
|
|
|
r"
|
|
|
|
use std::bar::B;
|
|
|
|
use external_crate::bar::A;
|
|
|
|
use crate::bar::A;
|
|
|
|
use self::bar::A;
|
|
|
|
use super::bar::A;
|
|
|
|
use external_crate2::bar::A;",
|
2021-06-19 20:31:24 +00:00
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Item,
|
|
|
|
enforce_granularity: true,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
|
|
|
group: false,
|
|
|
|
skip_glob_imports: true,
|
|
|
|
},
|
2021-03-06 11:02:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-03 15:05:39 +00:00
|
|
|
#[test]
|
|
|
|
fn insert_existing() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate("std::fs", "use std::fs;", "use std::fs;")
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_start() {
|
|
|
|
check_none(
|
|
|
|
"std::bar::AA",
|
|
|
|
r"
|
|
|
|
use std::bar::B;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
r"
|
|
|
|
use std::bar::AA;
|
|
|
|
use std::bar::B;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_start_indent() {
|
|
|
|
check_none(
|
|
|
|
"std::bar::AA",
|
|
|
|
r"
|
|
|
|
use std::bar::B;
|
2021-04-20 00:05:22 +00:00
|
|
|
use std::bar::C;",
|
2020-12-03 15:05:39 +00:00
|
|
|
r"
|
|
|
|
use std::bar::AA;
|
|
|
|
use std::bar::B;
|
2021-04-20 00:05:22 +00:00
|
|
|
use std::bar::C;",
|
|
|
|
);
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_middle() {
|
2021-04-20 17:28:18 +00:00
|
|
|
cov_mark::check!(insert_group);
|
2020-12-03 15:05:39 +00:00
|
|
|
check_none(
|
|
|
|
"std::bar::EE",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::EE;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_middle_indent() {
|
|
|
|
check_none(
|
|
|
|
"std::bar::EE",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::EE;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_end() {
|
2021-04-20 17:28:18 +00:00
|
|
|
cov_mark::check!(insert_group_last);
|
2020-12-03 15:05:39 +00:00
|
|
|
check_none(
|
|
|
|
"std::bar::ZZ",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;
|
|
|
|
use std::bar::ZZ;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_end_indent() {
|
|
|
|
check_none(
|
|
|
|
"std::bar::ZZ",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;
|
|
|
|
use std::bar::ZZ;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_middle_nested() {
|
|
|
|
check_none(
|
|
|
|
"std::bar::EE",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::{D, Z}; // example of weird imports due to user
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::EE;
|
|
|
|
use std::bar::{D, Z}; // example of weird imports due to user
|
|
|
|
use std::bar::F;
|
|
|
|
use std::bar::G;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_middle_groups() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::GG",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
|
|
|
|
use foo::bar::F;
|
|
|
|
use foo::bar::H;",
|
|
|
|
r"
|
|
|
|
use std::bar::A;
|
|
|
|
use std::bar::D;
|
|
|
|
|
|
|
|
use foo::bar::F;
|
|
|
|
use foo::bar::GG;
|
|
|
|
use foo::bar::H;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_first_matching_group() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::GG",
|
|
|
|
r"
|
|
|
|
use foo::bar::A;
|
|
|
|
use foo::bar::D;
|
|
|
|
|
|
|
|
use std;
|
|
|
|
|
|
|
|
use foo::bar::F;
|
|
|
|
use foo::bar::H;",
|
|
|
|
r"
|
|
|
|
use foo::bar::A;
|
|
|
|
use foo::bar::D;
|
|
|
|
use foo::bar::GG;
|
|
|
|
|
|
|
|
use std;
|
|
|
|
|
|
|
|
use foo::bar::F;
|
|
|
|
use foo::bar::H;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_missing_group_std() {
|
2021-04-20 17:28:18 +00:00
|
|
|
cov_mark::check!(insert_group_new_group);
|
2020-12-03 15:05:39 +00:00
|
|
|
check_none(
|
|
|
|
"std::fmt",
|
|
|
|
r"
|
|
|
|
use foo::bar::A;
|
|
|
|
use foo::bar::D;",
|
|
|
|
r"
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use foo::bar::A;
|
|
|
|
use foo::bar::D;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_missing_group_self() {
|
2021-04-20 17:28:18 +00:00
|
|
|
cov_mark::check!(insert_group_no_group);
|
2020-12-03 15:05:39 +00:00
|
|
|
check_none(
|
|
|
|
"self::fmt",
|
|
|
|
r"
|
|
|
|
use foo::bar::A;
|
|
|
|
use foo::bar::D;",
|
|
|
|
r"
|
|
|
|
use foo::bar::A;
|
|
|
|
use foo::bar::D;
|
|
|
|
|
|
|
|
use self::fmt;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_no_imports() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"foo::bar",
|
|
|
|
"fn main() {}",
|
|
|
|
r"use foo::bar;
|
|
|
|
|
|
|
|
fn main() {}",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_empty_file() {
|
2022-04-03 18:34:06 +00:00
|
|
|
cov_mark::check_count!(insert_empty_file, 2);
|
|
|
|
|
|
|
|
// Default configuration
|
2020-12-03 15:05:39 +00:00
|
|
|
// empty files will get two trailing newlines
|
|
|
|
// this is due to the test case insert_no_imports above
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"foo::bar",
|
|
|
|
"",
|
|
|
|
r"use foo::bar;
|
|
|
|
|
|
|
|
",
|
2022-04-03 18:34:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// "not group" configuration
|
|
|
|
check_with_config(
|
|
|
|
"use external_crate2::bar::A",
|
|
|
|
r"",
|
|
|
|
r"use external_crate2::bar::A;
|
2022-04-03 12:01:11 +00:00
|
|
|
|
|
|
|
",
|
2022-04-03 18:34:06 +00:00
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Item,
|
|
|
|
enforce_granularity: true,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
|
|
|
group: false,
|
|
|
|
skip_glob_imports: true,
|
|
|
|
},
|
|
|
|
);
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_empty_module() {
|
2022-04-03 18:34:06 +00:00
|
|
|
cov_mark::check_count!(insert_empty_module, 2);
|
|
|
|
|
|
|
|
// Default configuration
|
2020-12-03 15:05:39 +00:00
|
|
|
check(
|
|
|
|
"foo::bar",
|
2021-06-19 20:31:24 +00:00
|
|
|
r"
|
|
|
|
mod x {$0}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod x {
|
2020-12-03 15:05:39 +00:00
|
|
|
use foo::bar;
|
2021-06-19 20:31:24 +00:00
|
|
|
}
|
|
|
|
",
|
2021-05-18 17:49:15 +00:00
|
|
|
ImportGranularity::Item,
|
2022-04-03 18:34:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// "not group" configuration
|
|
|
|
check_with_config(
|
|
|
|
"foo::bar",
|
|
|
|
r"mod x {$0}",
|
|
|
|
r"mod x {
|
2022-04-03 12:01:11 +00:00
|
|
|
use foo::bar;
|
|
|
|
}",
|
2022-04-03 18:34:06 +00:00
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Item,
|
|
|
|
enforce_granularity: true,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
|
|
|
group: false,
|
|
|
|
skip_glob_imports: true,
|
|
|
|
},
|
|
|
|
);
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_after_inner_attr() {
|
2022-04-03 18:34:06 +00:00
|
|
|
cov_mark::check_count!(insert_empty_inner_attr, 2);
|
|
|
|
|
|
|
|
// Default configuration
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"foo::bar",
|
|
|
|
r"#![allow(unused_imports)]",
|
|
|
|
r"#![allow(unused_imports)]
|
|
|
|
|
|
|
|
use foo::bar;",
|
2022-04-03 18:34:06 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// "not group" configuration
|
|
|
|
check_with_config(
|
|
|
|
"foo::bar",
|
|
|
|
r"#![allow(unused_imports)]",
|
|
|
|
r"#![allow(unused_imports)]
|
2022-04-03 12:01:11 +00:00
|
|
|
|
|
|
|
use foo::bar;",
|
2022-04-03 18:34:06 +00:00
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Item,
|
|
|
|
enforce_granularity: true,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
|
|
|
group: false,
|
|
|
|
skip_glob_imports: true,
|
|
|
|
},
|
|
|
|
);
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_after_inner_attr2() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"foo::bar",
|
|
|
|
r"#![allow(unused_imports)]
|
|
|
|
|
|
|
|
#![no_std]
|
|
|
|
fn main() {}",
|
|
|
|
r"#![allow(unused_imports)]
|
|
|
|
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
use foo::bar;
|
|
|
|
fn main() {}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_after_single_line_inner_comments() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
"//! Single line inner comments do not allow any code before them.",
|
|
|
|
r#"//! Single line inner comments do not allow any code before them.
|
|
|
|
|
|
|
|
use foo::bar::Baz;"#,
|
|
|
|
);
|
2022-07-14 20:56:56 +00:00
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
r"mod foo {
|
|
|
|
//! Single line inner comments do not allow any code before them.
|
|
|
|
$0
|
|
|
|
}",
|
|
|
|
r"mod foo {
|
|
|
|
//! Single line inner comments do not allow any code before them.
|
|
|
|
|
|
|
|
use foo::bar::Baz;
|
|
|
|
|
|
|
|
}",
|
|
|
|
);
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 22:06:10 +00:00
|
|
|
#[test]
|
|
|
|
fn inserts_after_single_line_comments() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
"// Represents a possible license header and/or general module comments",
|
|
|
|
r#"// Represents a possible license header and/or general module comments
|
|
|
|
|
|
|
|
use foo::bar::Baz;"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-15 16:25:47 +00:00
|
|
|
#[test]
|
|
|
|
fn inserts_after_shebang() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
"#!/usr/bin/env rust",
|
|
|
|
r#"#!/usr/bin/env rust
|
|
|
|
|
|
|
|
use foo::bar::Baz;"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-17 22:12:32 +00:00
|
|
|
#[test]
|
|
|
|
fn inserts_after_multiple_single_line_comments() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
"// Represents a possible license header and/or general module comments
|
|
|
|
// Second single-line comment
|
|
|
|
// Third single-line comment",
|
|
|
|
r#"// Represents a possible license header and/or general module comments
|
|
|
|
// Second single-line comment
|
|
|
|
// Third single-line comment
|
|
|
|
|
|
|
|
use foo::bar::Baz;"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-17 22:06:10 +00:00
|
|
|
#[test]
|
|
|
|
fn inserts_before_single_line_item_comments() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
r#"// Represents a comment about a function
|
|
|
|
fn foo() {}"#,
|
|
|
|
r#"use foo::bar::Baz;
|
|
|
|
|
|
|
|
// Represents a comment about a function
|
|
|
|
fn foo() {}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-17 23:44:43 +00:00
|
|
|
#[test]
|
|
|
|
fn inserts_after_single_line_header_comments_and_before_item() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
r#"// Represents a possible license header
|
|
|
|
// Line two of possible license header
|
|
|
|
|
|
|
|
fn foo() {}"#,
|
|
|
|
r#"// Represents a possible license header
|
|
|
|
// Line two of possible license header
|
|
|
|
|
|
|
|
use foo::bar::Baz;
|
|
|
|
|
|
|
|
fn foo() {}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-03 15:05:39 +00:00
|
|
|
#[test]
|
|
|
|
fn inserts_after_multiline_inner_comments() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
r#"/*! Multiline inner comments do not allow any code before them. */
|
|
|
|
|
|
|
|
/*! Still an inner comment, cannot place any code before. */
|
|
|
|
fn main() {}"#,
|
|
|
|
r#"/*! Multiline inner comments do not allow any code before them. */
|
|
|
|
|
|
|
|
/*! Still an inner comment, cannot place any code before. */
|
|
|
|
|
|
|
|
use foo::bar::Baz;
|
|
|
|
fn main() {}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inserts_after_all_inner_items() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar::Baz",
|
|
|
|
r#"#![allow(unused_imports)]
|
|
|
|
/*! Multiline line comment 2 */
|
|
|
|
|
|
|
|
|
|
|
|
//! Single line comment 1
|
|
|
|
#![no_std]
|
|
|
|
//! Single line comment 2
|
|
|
|
fn main() {}"#,
|
|
|
|
r#"#![allow(unused_imports)]
|
|
|
|
/*! Multiline line comment 2 */
|
|
|
|
|
|
|
|
|
|
|
|
//! Single line comment 1
|
|
|
|
#![no_std]
|
|
|
|
//! Single line comment 2
|
|
|
|
|
|
|
|
use foo::bar::Baz;
|
|
|
|
fn main() {}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module("std::io", r"use std::fmt;", r"use std::{fmt, io};")
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_last() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::io",
|
|
|
|
r"use std::fmt::{Result, Display};",
|
|
|
|
r"use std::fmt::{Result, Display};
|
|
|
|
use std::io;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_last_into_self() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module("foo::bar::baz", r"use foo::bar;", r"use foo::bar::{self, baz};");
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_full() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::io",
|
|
|
|
r"use std::fmt::{Result, Display};",
|
|
|
|
r"use std::{fmt::{Result, Display}, io};",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_long_full() {
|
2021-11-19 09:06:36 +00:00
|
|
|
check_crate("std::foo::bar::Baz", r"use std::foo::bar::Qux;", r"use std::foo::bar::{Qux, Baz};")
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_long_last() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module(
|
|
|
|
"std::foo::bar::Baz",
|
|
|
|
r"use std::foo::bar::Qux;",
|
2021-11-19 09:06:36 +00:00
|
|
|
r"use std::foo::bar::{Qux, Baz};",
|
2021-05-10 19:03:50 +00:00
|
|
|
)
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_long_full_list() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::foo::bar::Baz",
|
|
|
|
r"use std::foo::bar::{Qux, Quux};",
|
2021-11-19 09:06:36 +00:00
|
|
|
r"use std::foo::bar::{Qux, Quux, Baz};",
|
2020-12-03 15:05:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_long_last_list() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::foo::bar::Baz",
|
|
|
|
r"use std::foo::bar::{Qux, Quux};",
|
2021-11-19 09:06:36 +00:00
|
|
|
r"use std::foo::bar::{Qux, Quux, Baz};",
|
2020-12-03 15:05:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_long_full_nested() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::foo::bar::Baz",
|
|
|
|
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
|
2021-11-19 09:06:36 +00:00
|
|
|
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}, Baz};",
|
2020-12-03 15:05:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_long_last_nested() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::foo::bar::Baz",
|
|
|
|
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
|
|
|
|
r"use std::foo::bar::Baz;
|
|
|
|
use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_full_nested_deep() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::foo::bar::quux::Baz",
|
|
|
|
r"use std::foo::bar::{Qux, quux::{Fez, Fizz}};",
|
2021-11-19 09:06:36 +00:00
|
|
|
r"use std::foo::bar::{Qux, quux::{Fez, Fizz, Baz}};",
|
2020-12-03 15:05:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_full_nested_long() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::foo::bar::Baz",
|
|
|
|
r"use std::{foo::bar::Qux};",
|
2021-11-19 09:06:36 +00:00
|
|
|
r"use std::{foo::bar::{Qux, Baz}};",
|
2020-12-03 15:05:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_last_nested_long() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::foo::bar::Baz",
|
|
|
|
r"use std::{foo::bar::Qux};",
|
2021-11-19 09:06:36 +00:00
|
|
|
r"use std::{foo::bar::{Qux, Baz}};",
|
2020-12-03 15:05:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_skip_pub() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::io",
|
|
|
|
r"pub use std::fmt::{Result, Display};",
|
|
|
|
r"pub use std::fmt::{Result, Display};
|
|
|
|
use std::io;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_skip_pub_crate() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::io",
|
|
|
|
r"pub(crate) use std::fmt::{Result, Display};",
|
|
|
|
r"pub(crate) use std::fmt::{Result, Display};
|
|
|
|
use std::io;",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-18 13:30:56 +00:00
|
|
|
#[test]
|
|
|
|
fn merge_groups_skip_attributed() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-18 13:30:56 +00:00
|
|
|
"std::io",
|
|
|
|
r#"
|
|
|
|
#[cfg(feature = "gated")] use std::fmt::{Result, Display};
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
#[cfg(feature = "gated")] use std::fmt::{Result, Display};
|
|
|
|
use std::io;
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-03 15:05:39 +00:00
|
|
|
#[test]
|
|
|
|
fn split_out_merge() {
|
2021-06-15 09:48:05 +00:00
|
|
|
// FIXME: This is suboptimal, we want to get `use std::fmt::{self, Result}`
|
|
|
|
// instead.
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module(
|
2020-12-03 15:05:39 +00:00
|
|
|
"std::fmt::Result",
|
|
|
|
r"use std::{fmt, io};",
|
2021-06-15 09:48:05 +00:00
|
|
|
r"use std::fmt::Result;
|
|
|
|
use std::{fmt, io};",
|
2020-12-03 15:05:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_into_module_import() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate("std::fmt::Result", r"use std::{fmt, io};", r"use std::{fmt::{self, Result}, io};")
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_groups_self() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate("std::fmt::Debug", r"use std::fmt;", r"use std::fmt::{self, Debug};")
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_mod_into_glob() {
|
2021-06-18 21:11:56 +00:00
|
|
|
check_with_config(
|
2021-05-10 19:03:50 +00:00
|
|
|
"token::TokenKind",
|
|
|
|
r"use token::TokenKind::*;",
|
2022-04-01 14:12:50 +00:00
|
|
|
r"use token::TokenKind::{*, self};",
|
2021-06-18 21:11:56 +00:00
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Crate,
|
|
|
|
enforce_granularity: true,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
|
|
|
group: false,
|
|
|
|
skip_glob_imports: false,
|
|
|
|
},
|
2021-05-10 19:03:50 +00:00
|
|
|
)
|
2020-12-03 15:05:39 +00:00
|
|
|
// FIXME: have it emit `use token::TokenKind::{self, *}`?
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_self_glob() {
|
2021-06-18 21:11:56 +00:00
|
|
|
check_with_config(
|
|
|
|
"self",
|
|
|
|
r"use self::*;",
|
2022-04-01 14:12:50 +00:00
|
|
|
r"use self::{*, self};",
|
2021-06-18 21:11:56 +00:00
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Crate,
|
|
|
|
enforce_granularity: true,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
|
|
|
group: false,
|
|
|
|
skip_glob_imports: false,
|
|
|
|
},
|
|
|
|
)
|
2020-12-03 15:05:39 +00:00
|
|
|
// FIXME: have it emit `use {self, *}`?
|
|
|
|
}
|
|
|
|
|
2021-10-03 11:52:46 +00:00
|
|
|
#[test]
|
|
|
|
fn merge_glob() {
|
|
|
|
check_crate(
|
|
|
|
"syntax::SyntaxKind",
|
|
|
|
r"
|
|
|
|
use syntax::{SyntaxKind::*};",
|
|
|
|
r"
|
2022-04-01 14:12:50 +00:00
|
|
|
use syntax::{SyntaxKind::{*, self}};",
|
2021-10-03 11:52:46 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-03 15:05:39 +00:00
|
|
|
#[test]
|
|
|
|
fn merge_glob_nested() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"foo::bar::quux::Fez",
|
|
|
|
r"use foo::bar::{Baz, quux::*};",
|
2022-04-01 14:12:50 +00:00
|
|
|
r"use foo::bar::{Baz, quux::{*, Fez}};",
|
2020-12-03 15:05:39 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_nested_considers_first_segments() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_crate(
|
2020-12-03 15:05:39 +00:00
|
|
|
"hir_ty::display::write_bounds_like_dyn_trait",
|
|
|
|
r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter}, method_resolution};",
|
|
|
|
r"use hir_ty::{autoderef, display::{HirDisplayError, HirFormatter, write_bounds_like_dyn_trait}, method_resolution};",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_merge_last_too_long() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module(
|
2020-12-03 15:05:39 +00:00
|
|
|
"foo::bar",
|
|
|
|
r"use foo::bar::baz::Qux;",
|
|
|
|
r"use foo::bar;
|
|
|
|
use foo::bar::baz::Qux;",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_merge_last_too_long2() {
|
2021-05-10 19:03:50 +00:00
|
|
|
check_module(
|
2020-12-03 15:05:39 +00:00
|
|
|
"foo::bar::baz::Qux",
|
|
|
|
r"use foo::bar;",
|
|
|
|
r"use foo::bar;
|
|
|
|
use foo::bar::baz::Qux;",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_short_before_long() {
|
|
|
|
check_none(
|
|
|
|
"foo::bar",
|
|
|
|
r"use foo::bar::baz::Qux;",
|
|
|
|
r"use foo::bar;
|
|
|
|
use foo::bar::baz::Qux;",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_last_fail() {
|
|
|
|
check_merge_only_fail(
|
|
|
|
r"use foo::bar::{baz::{Qux, Fez}};",
|
|
|
|
r"use foo::bar::{baaz::{Quux, Feez}};",
|
2021-05-10 19:03:50 +00:00
|
|
|
MergeBehavior::Module,
|
2020-12-03 15:05:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_last_fail1() {
|
|
|
|
check_merge_only_fail(
|
|
|
|
r"use foo::bar::{baz::{Qux, Fez}};",
|
|
|
|
r"use foo::bar::baaz::{Quux, Feez};",
|
2021-05-10 19:03:50 +00:00
|
|
|
MergeBehavior::Module,
|
2020-12-03 15:05:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_last_fail2() {
|
|
|
|
check_merge_only_fail(
|
|
|
|
r"use foo::bar::baz::{Qux, Fez};",
|
|
|
|
r"use foo::bar::{baaz::{Quux, Feez}};",
|
2021-05-10 19:03:50 +00:00
|
|
|
MergeBehavior::Module,
|
2020-12-03 15:05:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn merge_last_fail3() {
|
|
|
|
check_merge_only_fail(
|
|
|
|
r"use foo::bar::baz::{Qux, Fez};",
|
|
|
|
r"use foo::bar::baaz::{Quux, Feez};",
|
2021-05-10 19:03:50 +00:00
|
|
|
MergeBehavior::Module,
|
2020-12-03 15:05:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-18 18:21:47 +00:00
|
|
|
#[test]
|
|
|
|
fn guess_empty() {
|
|
|
|
check_guess("", ImportGranularityGuess::Unknown);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_single() {
|
|
|
|
check_guess(r"use foo::{baz::{qux, quux}, bar};", ImportGranularityGuess::Crate);
|
|
|
|
check_guess(r"use foo::bar;", ImportGranularityGuess::Unknown);
|
|
|
|
check_guess(r"use foo::bar::{baz, qux};", ImportGranularityGuess::CrateOrModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_unknown() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::baz;
|
|
|
|
use oof::rab::xuq;
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::Unknown,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_item() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::baz;
|
|
|
|
use foo::bar::qux;
|
2021-06-08 20:14:30 +00:00
|
|
|
",
|
|
|
|
ImportGranularityGuess::Item,
|
|
|
|
);
|
2021-07-01 19:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_module_or_item() {
|
2021-06-08 20:14:30 +00:00
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::Bar;
|
2021-07-01 19:32:35 +00:00
|
|
|
use foo::qux;
|
2021-05-18 18:21:47 +00:00
|
|
|
",
|
2021-07-01 19:32:35 +00:00
|
|
|
ImportGranularityGuess::ModuleOrItem,
|
|
|
|
);
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::Bar;
|
|
|
|
use foo::bar;
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::ModuleOrItem,
|
2021-05-18 18:21:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_module() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::baz;
|
|
|
|
use foo::bar::{qux, quux};
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::Module,
|
|
|
|
);
|
|
|
|
// this is a rather odd case, technically this file isn't following any style properly.
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::baz;
|
|
|
|
use foo::{baz::{qux, quux}, bar};
|
2021-06-08 20:14:30 +00:00
|
|
|
",
|
|
|
|
ImportGranularityGuess::Module,
|
|
|
|
);
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::Bar;
|
|
|
|
use foo::baz::Baz;
|
|
|
|
use foo::{Foo, Qux};
|
2021-05-18 18:21:47 +00:00
|
|
|
",
|
|
|
|
ImportGranularityGuess::Module,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_crate_or_module() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::baz;
|
|
|
|
use oof::bar::{qux, quux};
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::CrateOrModule,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_crate() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use frob::bar::baz;
|
|
|
|
use foo::{baz::{qux, quux}, bar};
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::Crate,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn guess_skips_differing_vis() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::baz;
|
|
|
|
pub use foo::bar::qux;
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::Unknown,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-20 08:25:04 +00:00
|
|
|
#[test]
|
|
|
|
fn guess_skips_differing_attrs() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
pub use foo::bar::baz;
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub use foo::bar::qux;
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::Unknown,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-18 18:21:47 +00:00
|
|
|
#[test]
|
|
|
|
fn guess_grouping_matters() {
|
|
|
|
check_guess(
|
|
|
|
r"
|
|
|
|
use foo::bar::baz;
|
|
|
|
use oof::bar::baz;
|
|
|
|
use foo::bar::qux;
|
|
|
|
",
|
|
|
|
ImportGranularityGuess::Unknown,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-11 12:06:09 +00:00
|
|
|
#[test]
|
|
|
|
fn insert_with_renamed_import_simple_use() {
|
|
|
|
check_with_config(
|
|
|
|
"use self::foo::Foo",
|
|
|
|
r#"
|
|
|
|
use self::foo::Foo as _;
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use self::foo::Foo;
|
|
|
|
"#,
|
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Crate,
|
|
|
|
prefix_kind: hir::PrefixKind::BySelf,
|
|
|
|
enforce_granularity: true,
|
|
|
|
group: true,
|
|
|
|
skip_glob_imports: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_with_renamed_import_complex_use() {
|
|
|
|
check_with_config(
|
|
|
|
"use self::foo::Foo;",
|
|
|
|
r#"
|
|
|
|
use self::foo::{self, Foo as _, Bar};
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
use self::foo::{self, Foo, Bar};
|
|
|
|
"#,
|
|
|
|
&InsertUseConfig {
|
|
|
|
granularity: ImportGranularity::Crate,
|
|
|
|
prefix_kind: hir::PrefixKind::BySelf,
|
|
|
|
enforce_granularity: true,
|
|
|
|
group: true,
|
|
|
|
skip_glob_imports: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-18 21:11:56 +00:00
|
|
|
fn check_with_config(
|
2020-12-03 15:05:39 +00:00
|
|
|
path: &str,
|
|
|
|
ra_fixture_before: &str,
|
|
|
|
ra_fixture_after: &str,
|
2021-06-18 21:11:56 +00:00
|
|
|
config: &InsertUseConfig,
|
2020-12-03 15:05:39 +00:00
|
|
|
) {
|
2021-11-03 20:12:36 +00:00
|
|
|
let (db, file_id, pos) = if ra_fixture_before.contains(CURSOR_MARKER) {
|
|
|
|
let (db, file_id, range_or_offset) = RootDatabase::with_range_or_offset(ra_fixture_before);
|
|
|
|
(db, file_id, Some(range_or_offset))
|
2021-06-19 20:31:24 +00:00
|
|
|
} else {
|
2021-11-03 20:12:36 +00:00
|
|
|
let (db, file_id) = RootDatabase::with_single_file(ra_fixture_before);
|
|
|
|
(db, file_id, None)
|
2021-06-19 20:31:24 +00:00
|
|
|
};
|
2021-11-03 20:12:36 +00:00
|
|
|
let sema = &Semantics::new(&db);
|
|
|
|
let source_file = sema.parse(file_id);
|
|
|
|
let syntax = source_file.syntax().clone_for_update();
|
2021-06-19 20:31:24 +00:00
|
|
|
let file = pos
|
|
|
|
.and_then(|pos| syntax.token_at_offset(pos.expect_offset()).next()?.parent())
|
2021-11-03 20:12:36 +00:00
|
|
|
.and_then(|it| ImportScope::find_insert_use_container(&it, sema))
|
|
|
|
.or_else(|| ImportScope::from(syntax))
|
2021-06-19 20:31:24 +00:00
|
|
|
.unwrap();
|
2022-12-23 18:42:58 +00:00
|
|
|
let path = ast::SourceFile::parse(&format!("use {path};"))
|
2020-12-03 15:05:39 +00:00
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.find_map(ast::Path::cast)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-06-18 21:11:56 +00:00
|
|
|
insert_use(&file, path, config);
|
2021-06-19 20:31:24 +00:00
|
|
|
let result = file.as_syntax_node().ancestors().last().unwrap().to_string();
|
2021-11-03 20:12:36 +00:00
|
|
|
assert_eq_text!(&trim_indent(ra_fixture_after), &result);
|
2021-06-18 21:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check(
|
|
|
|
path: &str,
|
|
|
|
ra_fixture_before: &str,
|
|
|
|
ra_fixture_after: &str,
|
|
|
|
granularity: ImportGranularity,
|
|
|
|
) {
|
|
|
|
check_with_config(
|
2021-05-18 18:21:47 +00:00
|
|
|
path,
|
2021-06-18 21:11:56 +00:00
|
|
|
ra_fixture_before,
|
|
|
|
ra_fixture_after,
|
|
|
|
&InsertUseConfig {
|
2021-05-18 18:21:47 +00:00
|
|
|
granularity,
|
|
|
|
enforce_granularity: true,
|
|
|
|
prefix_kind: PrefixKind::Plain,
|
2021-06-19 20:31:24 +00:00
|
|
|
group: true,
|
2021-06-18 21:11:56 +00:00
|
|
|
skip_glob_imports: true,
|
2021-05-18 18:21:47 +00:00
|
|
|
},
|
2021-06-18 21:11:56 +00:00
|
|
|
)
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 19:03:50 +00:00
|
|
|
fn check_crate(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
2021-06-19 20:31:24 +00:00
|
|
|
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Crate)
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 19:03:50 +00:00
|
|
|
fn check_module(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
2021-06-19 20:31:24 +00:00
|
|
|
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Module)
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_none(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
2021-06-19 20:31:24 +00:00
|
|
|
check(path, ra_fixture_before, ra_fixture_after, ImportGranularity::Item)
|
2020-12-03 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 14:41:57 +00:00
|
|
|
fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {
|
2020-12-03 15:05:39 +00:00
|
|
|
let use0 = ast::SourceFile::parse(ra_fixture0)
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.find_map(ast::Use::cast)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let use1 = ast::SourceFile::parse(ra_fixture1)
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.find_map(ast::Use::cast)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let result = try_merge_imports(&use0, &use1, mb);
|
|
|
|
assert_eq!(result.map(|u| u.to_string()), None);
|
|
|
|
}
|
2021-05-18 18:21:47 +00:00
|
|
|
|
|
|
|
fn check_guess(ra_fixture: &str, expected: ImportGranularityGuess) {
|
|
|
|
let syntax = ast::SourceFile::parse(ra_fixture).tree().syntax().clone();
|
2021-11-03 20:12:36 +00:00
|
|
|
let file = ImportScope::from(syntax).unwrap();
|
2021-10-03 11:52:46 +00:00
|
|
|
assert_eq!(super::guess_granularity_from_scope(&file), expected);
|
2021-05-18 18:21:47 +00:00
|
|
|
}
|