mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Nicer API
This commit is contained in:
parent
3486b47e5c
commit
fdf86aee18
4 changed files with 80 additions and 78 deletions
|
@ -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, parse_fixture, FixtureEntry, CURSOR_MARKER};
|
use test_utils::{extract_offset, Fixture, CURSOR_MARKER};
|
||||||
use vfs::{file_set::FileSet, VfsPath};
|
use vfs::{file_set::FileSet, VfsPath};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -107,9 +107,9 @@ fn with_files(
|
||||||
db: &mut dyn SourceDatabaseExt,
|
db: &mut dyn SourceDatabaseExt,
|
||||||
fixture: &str,
|
fixture: &str,
|
||||||
) -> (Option<FilePosition>, Vec<FileId>) {
|
) -> (Option<FilePosition>, Vec<FileId>) {
|
||||||
let mut files = Vec::new();
|
let fixture = Fixture::parse(fixture);
|
||||||
let fixture = parse_fixture(fixture);
|
|
||||||
|
|
||||||
|
let mut files = Vec::new();
|
||||||
let mut crate_graph = CrateGraph::default();
|
let mut crate_graph = CrateGraph::default();
|
||||||
let mut crates = FxHashMap::default();
|
let mut crates = FxHashMap::default();
|
||||||
let mut crate_deps = Vec::new();
|
let mut crate_deps = Vec::new();
|
||||||
|
@ -201,8 +201,8 @@ struct FileMeta {
|
||||||
env: Env,
|
env: Env,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&FixtureEntry> for ParsedMeta {
|
impl From<&Fixture> for ParsedMeta {
|
||||||
fn from(f: &FixtureEntry) -> Self {
|
fn from(f: &Fixture) -> Self {
|
||||||
Self::File(FileMeta {
|
Self::File(FileMeta {
|
||||||
path: f.path.to_owned(),
|
path: f.path.to_owned(),
|
||||||
krate: f.crate_name.to_owned(),
|
krate: f.crate_name.to_owned(),
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::{str::FromStr, sync::Arc};
|
||||||
|
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath};
|
use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath};
|
||||||
use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER};
|
use test_utils::{extract_offset, extract_range, Fixture, CURSOR_MARKER};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
|
Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange,
|
||||||
|
@ -12,7 +12,7 @@ use crate::{
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum MockFileData {
|
enum MockFileData {
|
||||||
Plain { path: String, content: String },
|
Plain { path: String, content: String },
|
||||||
Fixture(FixtureEntry),
|
Fixture(Fixture),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockFileData {
|
impl MockFileData {
|
||||||
|
@ -60,8 +60,8 @@ impl MockFileData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<FixtureEntry> for MockFileData {
|
impl From<Fixture> for MockFileData {
|
||||||
fn from(fixture: FixtureEntry) -> Self {
|
fn from(fixture: Fixture) -> Self {
|
||||||
Self::Fixture(fixture)
|
Self::Fixture(fixture)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ impl MockAnalysis {
|
||||||
/// ```
|
/// ```
|
||||||
pub fn with_files(fixture: &str) -> MockAnalysis {
|
pub fn with_files(fixture: &str) -> MockAnalysis {
|
||||||
let mut res = MockAnalysis::new();
|
let mut res = MockAnalysis::new();
|
||||||
for entry in parse_fixture(fixture) {
|
for entry in Fixture::parse(fixture) {
|
||||||
res.add_file_fixture(entry);
|
res.add_file_fixture(entry);
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
|
@ -100,7 +100,7 @@ impl MockAnalysis {
|
||||||
pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
|
pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
|
||||||
let mut position = None;
|
let mut position = None;
|
||||||
let mut res = MockAnalysis::new();
|
let mut res = MockAnalysis::new();
|
||||||
for entry in parse_fixture(fixture) {
|
for entry in Fixture::parse(fixture) {
|
||||||
if entry.text.contains(CURSOR_MARKER) {
|
if entry.text.contains(CURSOR_MARKER) {
|
||||||
assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
|
assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
|
||||||
position = Some(res.add_file_fixture_with_position(entry));
|
position = Some(res.add_file_fixture_with_position(entry));
|
||||||
|
@ -112,13 +112,13 @@ impl MockAnalysis {
|
||||||
(res, position)
|
(res, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_file_fixture(&mut self, fixture: FixtureEntry) -> FileId {
|
pub fn add_file_fixture(&mut self, fixture: Fixture) -> FileId {
|
||||||
let file_id = self.next_id();
|
let file_id = self.next_id();
|
||||||
self.files.push(MockFileData::from(fixture));
|
self.files.push(MockFileData::from(fixture));
|
||||||
file_id
|
file_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_file_fixture_with_position(&mut self, mut fixture: FixtureEntry) -> FilePosition {
|
pub fn add_file_fixture_with_position(&mut self, mut fixture: Fixture) -> FilePosition {
|
||||||
let (offset, text) = extract_offset(&fixture.text);
|
let (offset, text) = extract_offset(&fixture.text);
|
||||||
fixture.text = text;
|
fixture.text = text;
|
||||||
let file_id = self.next_id();
|
let file_id = self.next_id();
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustc_hash::FxHashMap;
|
||||||
use stdx::split1;
|
use stdx::split1;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct FixtureEntry {
|
pub struct Fixture {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub text: String,
|
pub text: String,
|
||||||
pub crate_name: Option<String>,
|
pub crate_name: Option<String>,
|
||||||
|
@ -13,19 +13,20 @@ pub struct FixtureEntry {
|
||||||
pub env: FxHashMap<String, String>,
|
pub env: FxHashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses text which looks like this:
|
impl Fixture {
|
||||||
///
|
/// Parses text which looks like this:
|
||||||
/// ```not_rust
|
///
|
||||||
/// //- some meta
|
/// ```not_rust
|
||||||
/// line 1
|
/// //- some meta
|
||||||
/// line 2
|
/// line 1
|
||||||
/// // - other meta
|
/// line 2
|
||||||
/// ```
|
/// // - other meta
|
||||||
pub fn parse_fixture(ra_fixture: &str) -> Vec<FixtureEntry> {
|
/// ```
|
||||||
let fixture = indent_first_line(ra_fixture);
|
pub fn parse(ra_fixture: &str) -> Vec<Fixture> {
|
||||||
let margin = fixture_margin(&fixture);
|
let fixture = indent_first_line(ra_fixture);
|
||||||
|
let margin = fixture_margin(&fixture);
|
||||||
|
|
||||||
let mut lines = fixture
|
let mut lines = fixture
|
||||||
.split('\n') // don't use `.lines` to not drop `\r\n`
|
.split('\n') // don't use `.lines` to not drop `\r\n`
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(ix, line)| {
|
.filter_map(|(ix, line)| {
|
||||||
|
@ -48,58 +49,59 @@ The offending line: {:?}"#,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut res: Vec<FixtureEntry> = Vec::new();
|
let mut res: Vec<Fixture> = Vec::new();
|
||||||
for line in lines.by_ref() {
|
for line in lines.by_ref() {
|
||||||
if line.starts_with("//-") {
|
if line.starts_with("//-") {
|
||||||
let meta = line["//-".len()..].trim().to_string();
|
let meta = line["//-".len()..].trim().to_string();
|
||||||
let meta = parse_meta(&meta);
|
let meta = Fixture::parse_single(&meta);
|
||||||
res.push(meta)
|
res.push(meta)
|
||||||
} else if let Some(entry) = res.last_mut() {
|
} else if let Some(entry) = res.last_mut() {
|
||||||
entry.text.push_str(line);
|
entry.text.push_str(line);
|
||||||
entry.text.push('\n');
|
entry.text.push('\n');
|
||||||
}
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
|
|
||||||
fn parse_meta(meta: &str) -> FixtureEntry {
|
|
||||||
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let path = components[0].to_string();
|
|
||||||
assert!(path.starts_with("/"));
|
|
||||||
|
|
||||||
let mut krate = None;
|
|
||||||
let mut deps = Vec::new();
|
|
||||||
let mut edition = None;
|
|
||||||
let mut cfg = CfgOptions::default();
|
|
||||||
let mut env = FxHashMap::default();
|
|
||||||
for component in components[1..].iter() {
|
|
||||||
let (key, value) = split1(component, ':').unwrap();
|
|
||||||
match key {
|
|
||||||
"crate" => krate = Some(value.to_string()),
|
|
||||||
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
|
|
||||||
"edition" => edition = Some(value.to_string()),
|
|
||||||
"cfg" => {
|
|
||||||
for key in value.split(',') {
|
|
||||||
match split1(key, '=') {
|
|
||||||
None => cfg.insert_atom(key.into()),
|
|
||||||
Some((k, v)) => cfg.insert_key_value(k.into(), v.into()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
"env" => {
|
|
||||||
for key in value.split(',') {
|
|
||||||
if let Some((k, v)) = split1(key, '=') {
|
|
||||||
env.insert(k.into(), v.into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => panic!("bad component: {:?}", component),
|
|
||||||
}
|
}
|
||||||
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
FixtureEntry { path, text: String::new(), crate_name: krate, deps, edition, cfg, env }
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
|
||||||
|
fn parse_single(meta: &str) -> Fixture {
|
||||||
|
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let path = components[0].to_string();
|
||||||
|
assert!(path.starts_with("/"));
|
||||||
|
|
||||||
|
let mut krate = None;
|
||||||
|
let mut deps = Vec::new();
|
||||||
|
let mut edition = None;
|
||||||
|
let mut cfg = CfgOptions::default();
|
||||||
|
let mut env = FxHashMap::default();
|
||||||
|
for component in components[1..].iter() {
|
||||||
|
let (key, value) = split1(component, ':').unwrap();
|
||||||
|
match key {
|
||||||
|
"crate" => krate = Some(value.to_string()),
|
||||||
|
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
|
||||||
|
"edition" => edition = Some(value.to_string()),
|
||||||
|
"cfg" => {
|
||||||
|
for key in value.split(',') {
|
||||||
|
match split1(key, '=') {
|
||||||
|
None => cfg.insert_atom(key.into()),
|
||||||
|
Some((k, v)) => cfg.insert_key_value(k.into(), v.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"env" => {
|
||||||
|
for key in value.split(',') {
|
||||||
|
if let Some((k, v)) = split1(key, '=') {
|
||||||
|
env.insert(k.into(), v.into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => panic!("bad component: {:?}", component),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Fixture { path, text: String::new(), crate_name: krate, deps, edition, cfg, env }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
|
/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
|
||||||
|
@ -170,8 +172,8 @@ fn parse_fixture_can_handle_dedented_first_line() {
|
||||||
struct Bar;
|
struct Bar;
|
||||||
";
|
";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_fixture(fixture),
|
Fixture::parse(fixture),
|
||||||
parse_fixture(
|
Fixture::parse(
|
||||||
"//- /lib.rs
|
"//- /lib.rs
|
||||||
mod foo;
|
mod foo;
|
||||||
//- /foo.rs
|
//- /foo.rs
|
||||||
|
@ -183,7 +185,7 @@ struct Bar;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_fixture_gets_full_meta() {
|
fn parse_fixture_gets_full_meta() {
|
||||||
let parsed = parse_fixture(
|
let parsed = Fixture::parse(
|
||||||
r"
|
r"
|
||||||
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
|
||||||
mod m;
|
mod m;
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub use difference::Changeset as __Changeset;
|
||||||
pub use ra_cfg::CfgOptions;
|
pub use ra_cfg::CfgOptions;
|
||||||
pub use rustc_hash::FxHashMap;
|
pub use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
pub use crate::fixture::{parse_fixture, FixtureEntry};
|
pub use crate::fixture::Fixture;
|
||||||
|
|
||||||
pub const CURSOR_MARKER: &str = "<|>";
|
pub const CURSOR_MARKER: &str = "<|>";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue