mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-30 23:27:24 +00:00
Fold multiline calls
This commit is contained in:
parent
53e3a7aeb4
commit
8295dc42a0
3 changed files with 114 additions and 105 deletions
|
@ -118,8 +118,8 @@ pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
|
|||
}
|
||||
|
||||
/// Extracts ranges, marked with `<tag> </tag>` pairs from the `text`
|
||||
pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
|
||||
let open = format!("<{}>", tag);
|
||||
pub fn extract_tags(mut text: &str, tag: &str) -> (Vec<(TextRange, Option<String>)>, String) {
|
||||
let open = format!("<{}", tag);
|
||||
let close = format!("</{}>", tag);
|
||||
let mut ranges = Vec::new();
|
||||
let mut res = String::new();
|
||||
|
@ -134,22 +134,35 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
|
|||
res.push_str(&text[..i]);
|
||||
text = &text[i..];
|
||||
if text.starts_with(&open) {
|
||||
text = &text[open.len()..];
|
||||
let close_open = text.find('>').unwrap();
|
||||
let attr = text[open.len()..close_open].trim();
|
||||
let attr = if attr.is_empty() { None } else { Some(attr.to_string()) };
|
||||
text = &text[close_open + '>'.len_utf8()..];
|
||||
let from = TextSize::of(&res);
|
||||
stack.push(from);
|
||||
stack.push((from, attr));
|
||||
} else if text.starts_with(&close) {
|
||||
text = &text[close.len()..];
|
||||
let from = stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
|
||||
let (from, attr) =
|
||||
stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
|
||||
let to = TextSize::of(&res);
|
||||
ranges.push(TextRange::new(from, to));
|
||||
ranges.push((TextRange::new(from, to), attr));
|
||||
} else {
|
||||
res.push('<');
|
||||
text = &text['<'.len_utf8()..];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(stack.is_empty(), "unmatched <{}>", tag);
|
||||
ranges.sort_by_key(|r| (r.start(), r.end()));
|
||||
ranges.sort_by_key(|r| (r.0.start(), r.0.end()));
|
||||
(ranges, res)
|
||||
}
|
||||
#[test]
|
||||
fn test_extract_tags() {
|
||||
let (tags, text) = extract_tags(r#"<tag fn>fn <tag>main</tag>() {}</tag>"#, "tag");
|
||||
let actual = tags.into_iter().map(|(range, attr)| (&text[range], attr)).collect::<Vec<_>>();
|
||||
assert_eq!(actual, vec![("fn main() {}", Some("fn".into())), ("main", None),]);
|
||||
}
|
||||
|
||||
/// Inserts `<|>` marker into the `text` at `offset`.
|
||||
pub fn add_cursor(text: &str, offset: TextSize) -> String {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue