mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Centralize fixture parsing for assists
This commit is contained in:
parent
44cf263edf
commit
e5101ae150
8 changed files with 65 additions and 60 deletions
|
@ -986,6 +986,7 @@ fn main() {
|
||||||
",
|
",
|
||||||
r"
|
r"
|
||||||
|
|
||||||
|
|
||||||
pub(crate) fn bar() {
|
pub(crate) fn bar() {
|
||||||
${0:todo!()}
|
${0:todo!()}
|
||||||
}",
|
}",
|
||||||
|
|
|
@ -763,9 +763,9 @@ mod tests {
|
||||||
fn foo(opt: Option<i32>) {
|
fn foo(opt: Option<i32>) {
|
||||||
match opt<|> {
|
match opt<|> {
|
||||||
}
|
}
|
||||||
}"#;
|
}
|
||||||
let before =
|
"#;
|
||||||
&format!("//- /main.rs crate:main deps:core\n{}{}", before, FamousDefs::FIXTURE);
|
let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
|
||||||
|
|
||||||
check_assist(
|
check_assist(
|
||||||
fill_match_arms,
|
fill_match_arms,
|
||||||
|
|
|
@ -226,27 +226,31 @@ fn foo() {
|
||||||
mark::check!(test_introduce_var_last_expr);
|
mark::check!(test_introduce_var_last_expr);
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
introduce_variable,
|
||||||
"
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
bar(<|>1 + 1<|>)
|
bar(<|>1 + 1<|>)
|
||||||
}",
|
}
|
||||||
"
|
"#,
|
||||||
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let $0var_name = 1 + 1;
|
let $0var_name = 1 + 1;
|
||||||
bar(var_name)
|
bar(var_name)
|
||||||
}",
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
check_assist(
|
check_assist(
|
||||||
introduce_variable,
|
introduce_variable,
|
||||||
"
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
<|>bar(1 + 1)<|>
|
<|>bar(1 + 1)<|>
|
||||||
}",
|
}
|
||||||
"
|
"#,
|
||||||
|
r#"
|
||||||
fn foo() {
|
fn foo() {
|
||||||
let $0var_name = bar(1 + 1);
|
let $0var_name = bar(1 + 1);
|
||||||
var_name
|
var_name
|
||||||
}",
|
}
|
||||||
|
"#,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -158,16 +158,16 @@ mod test {
|
||||||
check_assist(
|
check_assist(
|
||||||
make_raw_string,
|
make_raw_string,
|
||||||
r#"
|
r#"
|
||||||
fn f() {
|
fn f() {
|
||||||
let s = <|>"random\nstring";
|
let s = <|>"random\nstring";
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
r##"
|
r##"
|
||||||
fn f() {
|
fn f() {
|
||||||
let s = r#"random
|
let s = r#"random
|
||||||
string"#;
|
string"#;
|
||||||
}
|
}
|
||||||
"##,
|
"##,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,16 +193,16 @@ string"#;
|
||||||
check_assist(
|
check_assist(
|
||||||
make_raw_string,
|
make_raw_string,
|
||||||
r###"
|
r###"
|
||||||
fn f() {
|
fn f() {
|
||||||
let s = <|>"#random##\nstring";
|
let s = <|>"#random##\nstring";
|
||||||
}
|
}
|
||||||
"###,
|
"###,
|
||||||
r####"
|
r####"
|
||||||
fn f() {
|
fn f() {
|
||||||
let s = r#"#random##
|
let s = r#"#random##
|
||||||
string"#;
|
string"#;
|
||||||
}
|
}
|
||||||
"####,
|
"####,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,16 +211,16 @@ string"#;
|
||||||
check_assist(
|
check_assist(
|
||||||
make_raw_string,
|
make_raw_string,
|
||||||
r###"
|
r###"
|
||||||
fn f() {
|
fn f() {
|
||||||
let s = <|>"#random\"##\nstring";
|
let s = <|>"#random\"##\nstring";
|
||||||
}
|
}
|
||||||
"###,
|
"###,
|
||||||
r####"
|
r####"
|
||||||
fn f() {
|
fn f() {
|
||||||
let s = r###"#random"##
|
let s = r###"#random"##
|
||||||
string"###;
|
string"###;
|
||||||
}
|
}
|
||||||
"####,
|
"####,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,18 +4,18 @@ use hir::Semantics;
|
||||||
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
|
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
|
||||||
use ra_ide_db::RootDatabase;
|
use ra_ide_db::RootDatabase;
|
||||||
use ra_syntax::TextRange;
|
use ra_syntax::TextRange;
|
||||||
use test_utils::{
|
use test_utils::{assert_eq_text, extract_offset, extract_range, extract_range_or_offset};
|
||||||
assert_eq_text, extract_offset, extract_range, extract_range_or_offset, RangeOrOffset,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists};
|
use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists};
|
||||||
|
use stdx::trim_indent;
|
||||||
|
|
||||||
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
||||||
RootDatabase::with_single_file(text)
|
RootDatabase::with_single_file(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
|
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||||
check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after));
|
let ra_fixture_after = trim_indent(ra_fixture_after);
|
||||||
|
check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after));
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: instead of having a separate function here, maybe use
|
// FIXME: instead of having a separate function here, maybe use
|
||||||
|
@ -65,19 +65,8 @@ enum ExpectedResult<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check(handler: Handler, before: &str, expected: ExpectedResult) {
|
fn check(handler: Handler, before: &str, expected: ExpectedResult) {
|
||||||
let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") {
|
let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before);
|
||||||
let (db, position) = RootDatabase::with_position(before);
|
let text_without_caret = db.file_text(file_with_caret_id).as_ref().to_owned();
|
||||||
(
|
|
||||||
db.file_text(position.file_id).as_ref().to_owned(),
|
|
||||||
position.file_id,
|
|
||||||
RangeOrOffset::Offset(position.offset),
|
|
||||||
db,
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
let (range_or_offset, text_without_caret) = extract_range_or_offset(before);
|
|
||||||
let (db, file_id) = with_single_file(&text_without_caret);
|
|
||||||
(text_without_caret, file_id, range_or_offset, db)
|
|
||||||
};
|
|
||||||
|
|
||||||
let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
|
let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
|
||||||
|
|
||||||
|
|
|
@ -198,8 +198,7 @@ pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>,
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
impl FamousDefs<'_, '_> {
|
impl FamousDefs<'_, '_> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) const FIXTURE: &'static str = r#"
|
pub(crate) const FIXTURE: &'static str = r#"//- /libcore.rs crate:core
|
||||||
//- /libcore.rs crate:core
|
|
||||||
pub mod convert {
|
pub mod convert {
|
||||||
pub trait From<T> {
|
pub trait From<T> {
|
||||||
fn from(T) -> Self;
|
fn from(T) -> Self;
|
||||||
|
|
|
@ -61,7 +61,7 @@ use std::{str::FromStr, sync::Arc};
|
||||||
|
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use test_utils::{extract_offset, Fixture, CURSOR_MARKER};
|
use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER};
|
||||||
use vfs::{file_set::FileSet, VfsPath};
|
use vfs::{file_set::FileSet, VfsPath};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -86,9 +86,19 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
|
fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
|
||||||
|
let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
|
||||||
|
let offset = match range_or_offset {
|
||||||
|
RangeOrOffset::Range(_) => panic!(),
|
||||||
|
RangeOrOffset::Offset(it) => it,
|
||||||
|
};
|
||||||
|
(db, FilePosition { file_id, offset })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) {
|
||||||
let mut db = Self::default();
|
let mut db = Self::default();
|
||||||
let (pos, _) = with_files(&mut db, ra_fixture);
|
let (pos, _) = with_files(&mut db, ra_fixture);
|
||||||
(db, pos.unwrap())
|
let (file_id, range_or_offset) = pos.unwrap();
|
||||||
|
(db, file_id, range_or_offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_crate(&self) -> CrateId {
|
fn test_crate(&self) -> CrateId {
|
||||||
|
@ -151,7 +161,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
|
||||||
fn with_files(
|
fn with_files(
|
||||||
db: &mut dyn SourceDatabaseExt,
|
db: &mut dyn SourceDatabaseExt,
|
||||||
fixture: &str,
|
fixture: &str,
|
||||||
) -> (Option<FilePosition>, Vec<FileId>) {
|
) -> (Option<(FileId, RangeOrOffset)>, Vec<FileId>) {
|
||||||
let fixture = Fixture::parse(fixture);
|
let fixture = Fixture::parse(fixture);
|
||||||
|
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
|
@ -193,9 +203,9 @@ fn with_files(
|
||||||
}
|
}
|
||||||
|
|
||||||
let text = if entry.text.contains(CURSOR_MARKER) {
|
let text = if entry.text.contains(CURSOR_MARKER) {
|
||||||
let (offset, text) = extract_offset(&entry.text);
|
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
|
||||||
assert!(file_position.is_none());
|
assert!(file_position.is_none());
|
||||||
file_position = Some(FilePosition { file_id, offset });
|
file_position = Some((file_id, range_or_offset));
|
||||||
text.to_string()
|
text.to_string()
|
||||||
} else {
|
} else {
|
||||||
entry.text.to_string()
|
entry.text.to_string()
|
||||||
|
|
|
@ -30,7 +30,9 @@ impl Fixture {
|
||||||
|
|
||||||
let mut res: Vec<Fixture> = Vec::new();
|
let mut res: Vec<Fixture> = Vec::new();
|
||||||
|
|
||||||
for (ix, line) in lines_with_ends(&fixture).enumerate() {
|
let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") };
|
||||||
|
|
||||||
|
for (ix, line) in default.into_iter().chain(lines_with_ends(&fixture)).enumerate() {
|
||||||
if line.contains("//-") {
|
if line.contains("//-") {
|
||||||
assert!(
|
assert!(
|
||||||
line.starts_with("//-"),
|
line.starts_with("//-"),
|
||||||
|
|
Loading…
Reference in a new issue