mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-31 15:47:31 +00:00
extract fixture parsing
This commit is contained in:
parent
b58ca6b1a6
commit
64ce895ef0
4 changed files with 51 additions and 22 deletions
|
@ -87,3 +87,45 @@ pub fn add_cursor(text: &str, offset: TextUnit) -> String {
|
|||
res.push_str(&text[offset..]);
|
||||
res
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FixtureEntry {
|
||||
pub meta: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
/// Parses text wich looks like this:
|
||||
///
|
||||
/// ```notrust
|
||||
/// //- some meta
|
||||
/// line 1
|
||||
/// line 2
|
||||
/// // - other meta
|
||||
/// ```
|
||||
pub fn parse_fixture(fixture: &str) -> Vec<FixtureEntry> {
|
||||
let mut res = Vec::new();
|
||||
let mut buf = String::new();
|
||||
let mut meta: Option<&str> = None;
|
||||
|
||||
macro_rules! flush {
|
||||
() => {
|
||||
if let Some(meta) = meta {
|
||||
res.push(FixtureEntry { meta: meta.to_string(), text: buf.clone() });
|
||||
buf.clear();
|
||||
}
|
||||
};
|
||||
};
|
||||
for line in fixture.lines() {
|
||||
if line.starts_with("//-") {
|
||||
flush!();
|
||||
buf.clear();
|
||||
meta = Some(line["//-".len()..].trim());
|
||||
continue;
|
||||
}
|
||||
buf.push_str(line);
|
||||
buf.push('\n');
|
||||
}
|
||||
flush!();
|
||||
res
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue