Always use square brackets in marked_text_ranges

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-03-28 17:11:35 +02:00
parent bbfb63ff89
commit 45ecd8e0a6
2 changed files with 13 additions and 20 deletions

View file

@ -1081,10 +1081,7 @@ pub mod tests {
); );
language.set_theme(&theme); language.set_theme(&theme);
let (text, highlighted_ranges) = marked_text_ranges( let (text, highlighted_ranges) = marked_text_ranges(r#"const[] [a]: B = "c [d]""#);
r#"const{} <a>: B = "c [d]""#,
vec![('{', '}'), ('<', '>'), ('[', ']')],
);
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx)); let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx));
buffer.condition(&cx, |buf, _| !buf.is_parsing()).await; buffer.condition(&cx, |buf, _| !buf.is_parsing()).await;

View file

@ -77,22 +77,18 @@ pub fn marked_text(marked_text: &str) -> (String, Vec<usize>) {
(unmarked_text, markers.remove(&'|').unwrap_or_else(Vec::new)) (unmarked_text, markers.remove(&'|').unwrap_or_else(Vec::new))
} }
pub fn marked_text_ranges( pub fn marked_text_ranges(marked_text: &str) -> (String, Vec<Range<usize>>) {
marked_text: &str, let (unmarked_text, mut markers) = marked_text_by(marked_text, vec!['[', ']']);
range_markers: Vec<(char, char)>, let opens = markers.remove(&'[').unwrap_or_default();
) -> (String, Vec<Range<usize>>) { let closes = markers.remove(&']').unwrap_or_default();
let mut marker_chars = Vec::new(); assert_eq!(opens.len(), closes.len(), "marked ranges are unbalanced");
for (start, end) in range_markers.iter() {
marker_chars.push(*start); let ranges = opens
marker_chars.push(*end); .into_iter()
} .zip(closes)
let (unmarked_text, markers) = marked_text_by(marked_text, marker_chars); .map(|(open, close)| {
let ranges = range_markers assert!(close >= open, "marked ranges must be disjoint");
.iter() open..close
.map(|(start_marker, end_marker)| {
let start = markers.get(start_marker).unwrap()[0];
let end = markers.get(end_marker).unwrap()[0];
start..end
}) })
.collect(); .collect();
(unmarked_text, ranges) (unmarked_text, ranges)