mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-27 04:44:30 +00:00
Always use square brackets in marked_text_ranges
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
bbfb63ff89
commit
45ecd8e0a6
2 changed files with 13 additions and 20 deletions
|
@ -1081,10 +1081,7 @@ pub mod tests {
|
|||
);
|
||||
language.set_theme(&theme);
|
||||
|
||||
let (text, highlighted_ranges) = marked_text_ranges(
|
||||
r#"const{} <a>: B = "c [d]""#,
|
||||
vec![('{', '}'), ('<', '>'), ('[', ']')],
|
||||
);
|
||||
let (text, highlighted_ranges) = marked_text_ranges(r#"const[] [a]: B = "c [d]""#);
|
||||
|
||||
let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx));
|
||||
buffer.condition(&cx, |buf, _| !buf.is_parsing()).await;
|
||||
|
|
|
@ -77,22 +77,18 @@ pub fn marked_text(marked_text: &str) -> (String, Vec<usize>) {
|
|||
(unmarked_text, markers.remove(&'|').unwrap_or_else(Vec::new))
|
||||
}
|
||||
|
||||
pub fn marked_text_ranges(
|
||||
marked_text: &str,
|
||||
range_markers: Vec<(char, char)>,
|
||||
) -> (String, Vec<Range<usize>>) {
|
||||
let mut marker_chars = Vec::new();
|
||||
for (start, end) in range_markers.iter() {
|
||||
marker_chars.push(*start);
|
||||
marker_chars.push(*end);
|
||||
}
|
||||
let (unmarked_text, markers) = marked_text_by(marked_text, marker_chars);
|
||||
let ranges = range_markers
|
||||
.iter()
|
||||
.map(|(start_marker, end_marker)| {
|
||||
let start = markers.get(start_marker).unwrap()[0];
|
||||
let end = markers.get(end_marker).unwrap()[0];
|
||||
start..end
|
||||
pub fn marked_text_ranges(marked_text: &str) -> (String, Vec<Range<usize>>) {
|
||||
let (unmarked_text, mut markers) = marked_text_by(marked_text, vec!['[', ']']);
|
||||
let opens = markers.remove(&'[').unwrap_or_default();
|
||||
let closes = markers.remove(&']').unwrap_or_default();
|
||||
assert_eq!(opens.len(), closes.len(), "marked ranges are unbalanced");
|
||||
|
||||
let ranges = opens
|
||||
.into_iter()
|
||||
.zip(closes)
|
||||
.map(|(open, close)| {
|
||||
assert!(close >= open, "marked ranges must be disjoint");
|
||||
open..close
|
||||
})
|
||||
.collect();
|
||||
(unmarked_text, ranges)
|
||||
|
|
Loading…
Reference in a new issue