From ff4bdb31142171ff373d88eae80fec18acab9e12 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 23 Mar 2022 18:20:55 -0700 Subject: [PATCH 1/4] Fix incorrect highlighting when an empty range is highlighted via the DisplayMap Co-Authored-By: Keith Simmons --- crates/editor/src/display_map.rs | 130 +++++++++++++++++++--- crates/editor/src/display_map/fold_map.rs | 2 +- crates/editor/src/editor.rs | 12 +- crates/editor/src/test.rs | 23 ++++ 4 files changed, 149 insertions(+), 18 deletions(-) diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index 6f31541137..1f825e175c 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -489,13 +489,17 @@ impl ToDisplayPoint for Anchor { #[cfg(test)] mod tests { use super::*; - use crate::movement; + use crate::{ + movement, + test::{marked_text, marked_text_ranges}, + }; use gpui::{color::Color, elements::*, test::observe, MutableAppContext}; use language::{Buffer, Language, LanguageConfig, RandomCharIter, SelectionGoal}; use rand::{prelude::*, Rng}; use smol::stream::StreamExt; use std::{env, sync::Arc}; use theme::SyntaxTheme; + use unindent::Unindent; use util::test::sample_text; use Bias::*; @@ -929,7 +933,7 @@ mod tests { let map = cx .add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, 1, 1, cx)); assert_eq!( - cx.update(|cx| chunks(0..5, &map, &theme, cx)), + cx.update(|cx| syntax_chunks(0..5, &map, &theme, cx)), vec![ ("fn ".to_string(), None), ("outer".to_string(), Some(Color::blue())), @@ -940,7 +944,7 @@ mod tests { ] ); assert_eq!( - cx.update(|cx| chunks(3..5, &map, &theme, cx)), + cx.update(|cx| syntax_chunks(3..5, &map, &theme, cx)), vec![ (" fn ".to_string(), Some(Color::red())), ("inner".to_string(), Some(Color::blue())), @@ -952,7 +956,7 @@ mod tests { map.fold(vec![Point::new(0, 6)..Point::new(3, 2)], cx) }); assert_eq!( - cx.update(|cx| chunks(0..2, &map, &theme, cx)), + cx.update(|cx| syntax_chunks(0..2, &map, &theme, cx)), vec![ ("fn ".to_string(), None), ("out".to_string(), Some(Color::blue())), @@ -1018,7 +1022,7 @@ mod tests { DisplayMap::new(buffer, tab_size, font_id, font_size, Some(40.0), 1, 1, cx) }); assert_eq!( - cx.update(|cx| chunks(0..5, &map, &theme, cx)), + cx.update(|cx| syntax_chunks(0..5, &map, &theme, cx)), [ ("fn \n".to_string(), None), ("oute\nr".to_string(), Some(Color::blue())), @@ -1026,7 +1030,7 @@ mod tests { ] ); assert_eq!( - cx.update(|cx| chunks(3..5, &map, &theme, cx)), + cx.update(|cx| syntax_chunks(3..5, &map, &theme, cx)), [("{}\n\n".to_string(), None)] ); @@ -1034,7 +1038,7 @@ mod tests { map.fold(vec![Point::new(0, 6)..Point::new(3, 2)], cx) }); assert_eq!( - cx.update(|cx| chunks(1..4, &map, &theme, cx)), + cx.update(|cx| syntax_chunks(1..4, &map, &theme, cx)), [ ("out".to_string(), Some(Color::blue())), ("…\n".to_string(), None), @@ -1044,6 +1048,89 @@ mod tests { ); } + #[gpui::test] + async fn test_chunks_with_text_highlights(cx: &mut gpui::TestAppContext) { + cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX); + + let theme = SyntaxTheme::new(vec![ + ("operator".to_string(), Color::red().into()), + ("string".to_string(), Color::green().into()), + ]); + let language = Arc::new( + Language::new( + LanguageConfig { + name: "Test".into(), + path_suffixes: vec![".test".to_string()], + ..Default::default() + }, + Some(tree_sitter_rust::language()), + ) + .with_highlights_query( + r#" + ":" @operator + (string_literal) @string + "#, + ) + .unwrap(), + ); + language.set_theme(&theme); + + let (text, highlighted_ranges) = marked_text_ranges( + r#"const{} : B = "c [d]""#, + vec![('{', '}'), ('<', '>'), ('[', ']')], + ); + + let buffer = cx.add_model(|cx| Buffer::new(0, text, cx).with_language(language, cx)); + buffer.condition(&cx, |buf, _| !buf.is_parsing()).await; + + let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx)); + let buffer_snapshot = buffer.read_with(cx, |buffer, cx| buffer.snapshot(cx)); + + let font_cache = cx.font_cache(); + let tab_size = 4; + let family_id = font_cache.load_family(&["Courier"]).unwrap(); + let font_id = font_cache + .select_font(family_id, &Default::default()) + .unwrap(); + let font_size = 16.0; + let map = cx + .add_model(|cx| DisplayMap::new(buffer, tab_size, font_id, font_size, None, 1, 1, cx)); + + enum MyType {} + + let style = HighlightStyle { + color: Some(Color::blue()), + ..Default::default() + }; + + map.update(cx, |map, _cx| { + map.highlight_text( + TypeId::of::(), + highlighted_ranges + .into_iter() + .map(|range| { + buffer_snapshot.anchor_before(range.start) + ..buffer_snapshot.anchor_before(range.end) + }) + .collect(), + style, + ); + }); + + assert_eq!( + cx.update(|cx| chunks(0..10, &map, &theme, cx)), + [ + ("const ".to_string(), None, None), + ("a".to_string(), None, Some(Color::blue())), + (":".to_string(), Some(Color::red()), None), + (" B = ".to_string(), None, None), + ("\"c ".to_string(), Some(Color::green()), None), + ("d".to_string(), Some(Color::green()), Some(Color::blue())), + ("\"".to_string(), Some(Color::green()), None), + ] + ); + } + #[gpui::test] fn test_clip_point(cx: &mut gpui::MutableAppContext) { use Bias::{Left, Right}; @@ -1170,27 +1257,38 @@ mod tests { ) } - fn chunks<'a>( + fn syntax_chunks<'a>( rows: Range, map: &ModelHandle, theme: &'a SyntaxTheme, cx: &mut MutableAppContext, ) -> Vec<(String, Option)> { + chunks(rows, map, theme, cx) + .into_iter() + .map(|(text, color, _)| (text, color)) + .collect() + } + + fn chunks<'a>( + rows: Range, + map: &ModelHandle, + theme: &'a SyntaxTheme, + cx: &mut MutableAppContext, + ) -> Vec<(String, Option, Option)> { let snapshot = map.update(cx, |map, cx| map.snapshot(cx)); - let mut chunks: Vec<(String, Option)> = Vec::new(); + let mut chunks: Vec<(String, Option, Option)> = Vec::new(); for chunk in snapshot.chunks(rows, true) { - let color = chunk + let syntax_color = chunk .syntax_highlight_id .and_then(|id| id.style(theme)?.color); - if let Some((last_chunk, last_color)) = chunks.last_mut() { - if color == *last_color { + let highlight_color = chunk.highlight_style.and_then(|style| style.color); + if let Some((last_chunk, last_syntax_color, last_highlight_color)) = chunks.last_mut() { + if syntax_color == *last_syntax_color && highlight_color == *last_highlight_color { last_chunk.push_str(chunk.text); - } else { - chunks.push((chunk.text.to_string(), color)); + continue; } - } else { - chunks.push((chunk.text.to_string(), color)); } + chunks.push((chunk.text.to_string(), syntax_color, highlight_color)); } chunks } diff --git a/crates/editor/src/display_map/fold_map.rs b/crates/editor/src/display_map/fold_map.rs index ac4477263d..7f744afa40 100644 --- a/crates/editor/src/display_map/fold_map.rs +++ b/crates/editor/src/display_map/fold_map.rs @@ -1156,7 +1156,7 @@ impl Ord for HighlightEndpoint { fn cmp(&self, other: &Self) -> Ordering { self.offset .cmp(&other.offset) - .then_with(|| self.is_start.cmp(&other.is_start)) + .then_with(|| other.is_start.cmp(&self.is_start)) } } diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 6722d2b1fb..ba95d33d39 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4,6 +4,8 @@ pub mod items; pub mod movement; mod multi_buffer; +pub mod repro; + #[cfg(test)] mod test; @@ -4404,7 +4406,15 @@ impl Editor { .into_iter() .flat_map(|(_, ranges)| ranges), ) - .collect(); + .collect::>(); + + let snapshot = this.buffer.read(cx).snapshot(cx); + let point_ranges = ranges + .iter() + .map(|range| range.to_point(&snapshot)) + .collect::>(); + dbg!(point_ranges); + this.highlight_text::( ranges, HighlightStyle { diff --git a/crates/editor/src/test.rs b/crates/editor/src/test.rs index 4908016ef5..566ca9b2db 100644 --- a/crates/editor/src/test.rs +++ b/crates/editor/src/test.rs @@ -1,3 +1,5 @@ +use std::ops::Range; + use collections::HashMap; #[cfg(test)] @@ -31,3 +33,24 @@ pub fn marked_text(marked_text: &str) -> (String, Vec) { let (unmarked_text, mut markers) = marked_text_by(marked_text, vec!['|']); (unmarked_text, markers.remove(&'|').unwrap_or_else(Vec::new)) } + +pub fn marked_text_ranges( + marked_text: &str, + range_markers: Vec<(char, char)>, +) -> (String, Vec>) { + 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 + }) + .collect(); + (unmarked_text, ranges) +} From f6805eb80255c9dbd7af057291ff923f32282e27 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Thu, 24 Mar 2022 10:22:47 -0700 Subject: [PATCH 2/4] Make rename highlights work across multibuffer excerpts Co-authored-by: Antonio Scandurra Co-authored-by: Nathan Sobo --- crates/diagnostics/src/diagnostics.rs | 3 +- crates/editor/src/editor.rs | 47 ++++++++++++++++-------- crates/editor/src/multi_buffer.rs | 36 +++++++++++++++--- crates/editor/src/multi_buffer/anchor.rs | 4 +- crates/editor/src/repro.rs | 9 +++++ crates/language/src/diagnostic_set.rs | 8 ++-- crates/language/src/tests.rs | 2 +- crates/text/src/anchor.rs | 20 +++++++++- crates/text/src/text.rs | 26 ++++++------- 9 files changed, 110 insertions(+), 45 deletions(-) create mode 100644 crates/editor/src/repro.rs diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index a0182902f1..69a4d1235f 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -278,7 +278,8 @@ impl ProjectDiagnosticsEditor { prev_excerpt_id = excerpt_id.clone(); first_excerpt_id.get_or_insert_with(|| prev_excerpt_id.clone()); group_state.excerpts.push(excerpt_id.clone()); - let header_position = (excerpt_id.clone(), language::Anchor::min()); + let header_position = + (excerpt_id.clone(), language::Anchor::build_min()); if is_first_excerpt_for_group { is_first_excerpt_for_group = false; diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index ba95d33d39..634851bd82 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -2399,7 +2399,7 @@ impl Editor { ) -> Result<()> { let replica_id = this.read_with(&cx, |this, cx| this.replica_id(cx)); - // If the code action's edits are all contained within this editor, then + // If the project transaction's edits are all contained within this editor, then // avoid opening a new editor to display them. let mut entries = transaction.0.iter(); if let Some((buffer, transaction)) = entries.next() { @@ -2521,7 +2521,6 @@ impl Editor { } let buffer_id = cursor_position.buffer_id; - let excerpt_id = cursor_position.excerpt_id.clone(); let style = this.style(cx); let read_background = style.document_highlight_read_background; let write_background = style.document_highlight_write_background; @@ -2533,22 +2532,39 @@ impl Editor { return; } + let cursor_buffer_snapshot = cursor_buffer.read(cx); let mut write_ranges = Vec::new(); let mut read_ranges = Vec::new(); for highlight in highlights { - let range = Anchor { - buffer_id, - excerpt_id: excerpt_id.clone(), - text_anchor: highlight.range.start, - }..Anchor { - buffer_id, - excerpt_id: excerpt_id.clone(), - text_anchor: highlight.range.end, - }; - if highlight.kind == lsp::DocumentHighlightKind::WRITE { - write_ranges.push(range); - } else { - read_ranges.push(range); + for (excerpt_id, excerpt_range) in + buffer.excerpts_for_buffer(&cursor_buffer, cx) + { + let start = highlight + .range + .start + .max(&excerpt_range.start, cursor_buffer_snapshot); + let end = highlight + .range + .end + .min(&excerpt_range.end, cursor_buffer_snapshot); + if start.cmp(&end, cursor_buffer_snapshot).unwrap().is_ge() { + continue; + } + + let range = Anchor { + buffer_id, + excerpt_id: excerpt_id.clone(), + text_anchor: start, + }..Anchor { + buffer_id, + excerpt_id, + text_anchor: end, + }; + if highlight.kind == lsp::DocumentHighlightKind::WRITE { + write_ranges.push(range); + } else { + read_ranges.push(range); + } } } @@ -4413,7 +4429,6 @@ impl Editor { .iter() .map(|range| range.to_point(&snapshot)) .collect::>(); - dbg!(point_ranges); this.highlight_text::( ranges, diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index 594e3fafb7..398f7da99e 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -211,7 +211,11 @@ impl MultiBuffer { pub fn singleton(buffer: ModelHandle, cx: &mut ModelContext) -> Self { let mut this = Self::new(buffer.read(cx).replica_id()); this.singleton = true; - this.push_excerpts(buffer, [text::Anchor::min()..text::Anchor::max()], cx); + this.push_excerpts( + buffer, + [text::Anchor::build_min()..text::Anchor::build_max()], + cx, + ); this.snapshot.borrow_mut().singleton = true; this } @@ -814,11 +818,30 @@ impl MultiBuffer { cx.notify(); } - pub fn excerpt_ids_for_buffer(&self, buffer: &ModelHandle) -> Vec { - self.buffers - .borrow() + pub fn excerpts_for_buffer( + &self, + buffer: &ModelHandle, + cx: &AppContext, + ) -> Vec<(ExcerptId, Range)> { + let mut excerpts = Vec::new(); + let snapshot = self.read(cx); + let buffers = self.buffers.borrow(); + let mut cursor = snapshot.excerpts.cursor::>(); + for excerpt_id in buffers .get(&buffer.id()) - .map_or(Vec::new(), |state| state.excerpts.clone()) + .map(|state| &state.excerpts) + .into_iter() + .flatten() + { + cursor.seek_forward(&Some(excerpt_id), Bias::Left, &()); + if let Some(excerpt) = cursor.item() { + if excerpt.id == *excerpt_id { + excerpts.push((excerpt.id.clone(), excerpt.range.clone())); + } + } + } + + excerpts } pub fn excerpt_ids(&self) -> Vec { @@ -3070,7 +3093,8 @@ mod tests { ); let snapshot = multibuffer.update(cx, |multibuffer, cx| { - let buffer_2_excerpt_id = multibuffer.excerpt_ids_for_buffer(&buffer_2)[0].clone(); + let (buffer_2_excerpt_id, _) = + multibuffer.excerpts_for_buffer(&buffer_2, cx)[0].clone(); multibuffer.remove_excerpts(&[buffer_2_excerpt_id], cx); multibuffer.snapshot(cx) }); diff --git a/crates/editor/src/multi_buffer/anchor.rs b/crates/editor/src/multi_buffer/anchor.rs index 33147ce285..847a38f500 100644 --- a/crates/editor/src/multi_buffer/anchor.rs +++ b/crates/editor/src/multi_buffer/anchor.rs @@ -19,7 +19,7 @@ impl Anchor { Self { buffer_id: None, excerpt_id: ExcerptId::min(), - text_anchor: text::Anchor::min(), + text_anchor: text::Anchor::build_min(), } } @@ -27,7 +27,7 @@ impl Anchor { Self { buffer_id: None, excerpt_id: ExcerptId::max(), - text_anchor: text::Anchor::max(), + text_anchor: text::Anchor::build_max(), } } diff --git a/crates/editor/src/repro.rs b/crates/editor/src/repro.rs new file mode 100644 index 0000000000..0dfa9ecbfc --- /dev/null +++ b/crates/editor/src/repro.rs @@ -0,0 +1,9 @@ +fn test(complicated: i32, test: i32) { + complicated; + test; + // 1 + // 2 + // 3 + complicated; + test; +} diff --git a/crates/language/src/diagnostic_set.rs b/crates/language/src/diagnostic_set.rs index e25551ee3a..017f117bbe 100644 --- a/crates/language/src/diagnostic_set.rs +++ b/crates/language/src/diagnostic_set.rs @@ -187,10 +187,10 @@ impl DiagnosticEntry { impl Default for Summary { fn default() -> Self { Self { - start: Anchor::min(), - end: Anchor::max(), - min_start: Anchor::max(), - max_end: Anchor::min(), + start: Anchor::build_min(), + end: Anchor::build_max(), + min_start: Anchor::build_max(), + max_end: Anchor::build_min(), count: 0, } } diff --git a/crates/language/src/tests.rs b/crates/language/src/tests.rs index d36771c44f..3dbe8508a5 100644 --- a/crates/language/src/tests.rs +++ b/crates/language/src/tests.rs @@ -789,7 +789,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) { for buffer in &buffers { let buffer = buffer.read(cx).snapshot(); let actual_remote_selections = buffer - .remote_selections_in_range(Anchor::min()..Anchor::max()) + .remote_selections_in_range(Anchor::build_min()..Anchor::build_max()) .map(|(replica_id, selections)| (replica_id, selections.collect::>())) .collect::>(); let expected_remote_selections = active_selections diff --git a/crates/text/src/anchor.rs b/crates/text/src/anchor.rs index 28da998d67..c7de6bc882 100644 --- a/crates/text/src/anchor.rs +++ b/crates/text/src/anchor.rs @@ -12,7 +12,7 @@ pub struct Anchor { } impl Anchor { - pub fn min() -> Self { + pub fn build_min() -> Self { Self { timestamp: clock::Local::MIN, offset: usize::MIN, @@ -20,7 +20,7 @@ impl Anchor { } } - pub fn max() -> Self { + pub fn build_max() -> Self { Self { timestamp: clock::Local::MAX, offset: usize::MAX, @@ -42,6 +42,22 @@ impl Anchor { .then_with(|| self.bias.cmp(&other.bias))) } + pub fn min(&self, other: &Self, buffer: &BufferSnapshot) -> Self { + if self.cmp(other, buffer).unwrap().is_le() { + self.clone() + } else { + other.clone() + } + } + + pub fn max(&self, other: &Self, buffer: &BufferSnapshot) -> Self { + if self.cmp(other, buffer).unwrap().is_ge() { + self.clone() + } else { + other.clone() + } + } + pub fn bias(&self, bias: Bias, buffer: &BufferSnapshot) -> Anchor { if bias == Bias::Left { self.bias_left(buffer) diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index 338a5c0ad1..8cb9629d07 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -1318,8 +1318,8 @@ impl Buffer { let mut futures = Vec::new(); for anchor in anchors { if !self.version.observed(anchor.timestamp) - && *anchor != Anchor::max() - && *anchor != Anchor::min() + && *anchor != Anchor::build_max() + && *anchor != Anchor::build_min() { let (tx, rx) = oneshot::channel(); self.edit_id_resolvers @@ -1638,9 +1638,9 @@ impl BufferSnapshot { let mut position = D::default(); anchors.map(move |anchor| { - if *anchor == Anchor::min() { + if *anchor == Anchor::build_min() { return D::default(); - } else if *anchor == Anchor::max() { + } else if *anchor == Anchor::build_max() { return D::from_text_summary(&self.visible_text.summary()); } @@ -1680,9 +1680,9 @@ impl BufferSnapshot { where D: TextDimension, { - if *anchor == Anchor::min() { + if *anchor == Anchor::build_min() { D::default() - } else if *anchor == Anchor::max() { + } else if *anchor == Anchor::build_max() { D::from_text_summary(&self.visible_text.summary()) } else { let anchor_key = InsertionFragmentKey { @@ -1718,9 +1718,9 @@ impl BufferSnapshot { } fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator { - if *anchor == Anchor::min() { + if *anchor == Anchor::build_min() { &locator::MIN - } else if *anchor == Anchor::max() { + } else if *anchor == Anchor::build_max() { &locator::MAX } else { let anchor_key = InsertionFragmentKey { @@ -1758,9 +1758,9 @@ impl BufferSnapshot { pub fn anchor_at(&self, position: T, bias: Bias) -> Anchor { let offset = position.to_offset(self); if bias == Bias::Left && offset == 0 { - Anchor::min() + Anchor::build_min() } else if bias == Bias::Right && offset == self.len() { - Anchor::max() + Anchor::build_max() } else { let mut fragment_cursor = self.fragments.cursor::(); fragment_cursor.seek(&offset, bias, &None); @@ -1775,8 +1775,8 @@ impl BufferSnapshot { } pub fn can_resolve(&self, anchor: &Anchor) -> bool { - *anchor == Anchor::min() - || *anchor == Anchor::max() + *anchor == Anchor::build_min() + || *anchor == Anchor::build_max() || self.version.observed(anchor.timestamp) } @@ -1799,7 +1799,7 @@ impl BufferSnapshot { where D: TextDimension + Ord, { - self.edits_since_in_range(since, Anchor::min()..Anchor::max()) + self.edits_since_in_range(since, Anchor::build_min()..Anchor::build_max()) } pub fn edited_ranges_for_transaction<'a, D>( From ab631cf6c3fd5a4cfb0de996072eaa299c44b0e3 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Thu, 24 Mar 2022 10:35:30 -0700 Subject: [PATCH 3/4] Change language::anchor::min() to a constant Co-authored-by: Antonio Scandurra Co-authored-by: Nathan Sobo --- crates/diagnostics/src/diagnostics.rs | 3 +-- crates/editor/src/display_map.rs | 3 +-- crates/editor/src/editor.rs | 10 +-------- crates/editor/src/multi_buffer.rs | 6 +----- crates/editor/src/multi_buffer/anchor.rs | 4 ++-- crates/editor/src/repro.rs | 9 -------- crates/language/src/diagnostic_set.rs | 8 ++++---- crates/language/src/tests.rs | 2 +- crates/text/src/anchor.rs | 24 +++++++++------------- crates/text/src/text.rs | 26 +++++++++++------------- 10 files changed, 33 insertions(+), 62 deletions(-) delete mode 100644 crates/editor/src/repro.rs diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 69a4d1235f..61a2e6c9c1 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -278,8 +278,7 @@ impl ProjectDiagnosticsEditor { prev_excerpt_id = excerpt_id.clone(); first_excerpt_id.get_or_insert_with(|| prev_excerpt_id.clone()); group_state.excerpts.push(excerpt_id.clone()); - let header_position = - (excerpt_id.clone(), language::Anchor::build_min()); + let header_position = (excerpt_id.clone(), language::Anchor::MIN); if is_first_excerpt_for_group { is_first_excerpt_for_group = false; diff --git a/crates/editor/src/display_map.rs b/crates/editor/src/display_map.rs index 1f825e175c..aee964ec56 100644 --- a/crates/editor/src/display_map.rs +++ b/crates/editor/src/display_map.rs @@ -491,7 +491,7 @@ mod tests { use super::*; use crate::{ movement, - test::{marked_text, marked_text_ranges}, + test::{marked_text_ranges}, }; use gpui::{color::Color, elements::*, test::observe, MutableAppContext}; use language::{Buffer, Language, LanguageConfig, RandomCharIter, SelectionGoal}; @@ -499,7 +499,6 @@ mod tests { use smol::stream::StreamExt; use std::{env, sync::Arc}; use theme::SyntaxTheme; - use unindent::Unindent; use util::test::sample_text; use Bias::*; diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 634851bd82..3fbdf7def1 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4,8 +4,6 @@ pub mod items; pub mod movement; mod multi_buffer; -pub mod repro; - #[cfg(test)] mod test; @@ -4422,13 +4420,7 @@ impl Editor { .into_iter() .flat_map(|(_, ranges)| ranges), ) - .collect::>(); - - let snapshot = this.buffer.read(cx).snapshot(cx); - let point_ranges = ranges - .iter() - .map(|range| range.to_point(&snapshot)) - .collect::>(); + .collect(); this.highlight_text::( ranges, diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index 398f7da99e..de735dafa7 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -211,11 +211,7 @@ impl MultiBuffer { pub fn singleton(buffer: ModelHandle, cx: &mut ModelContext) -> Self { let mut this = Self::new(buffer.read(cx).replica_id()); this.singleton = true; - this.push_excerpts( - buffer, - [text::Anchor::build_min()..text::Anchor::build_max()], - cx, - ); + this.push_excerpts(buffer, [text::Anchor::MIN..text::Anchor::MAX], cx); this.snapshot.borrow_mut().singleton = true; this } diff --git a/crates/editor/src/multi_buffer/anchor.rs b/crates/editor/src/multi_buffer/anchor.rs index 847a38f500..aeaf184c60 100644 --- a/crates/editor/src/multi_buffer/anchor.rs +++ b/crates/editor/src/multi_buffer/anchor.rs @@ -19,7 +19,7 @@ impl Anchor { Self { buffer_id: None, excerpt_id: ExcerptId::min(), - text_anchor: text::Anchor::build_min(), + text_anchor: text::Anchor::MIN, } } @@ -27,7 +27,7 @@ impl Anchor { Self { buffer_id: None, excerpt_id: ExcerptId::max(), - text_anchor: text::Anchor::build_max(), + text_anchor: text::Anchor::MAX, } } diff --git a/crates/editor/src/repro.rs b/crates/editor/src/repro.rs deleted file mode 100644 index 0dfa9ecbfc..0000000000 --- a/crates/editor/src/repro.rs +++ /dev/null @@ -1,9 +0,0 @@ -fn test(complicated: i32, test: i32) { - complicated; - test; - // 1 - // 2 - // 3 - complicated; - test; -} diff --git a/crates/language/src/diagnostic_set.rs b/crates/language/src/diagnostic_set.rs index 017f117bbe..28b03b5aa0 100644 --- a/crates/language/src/diagnostic_set.rs +++ b/crates/language/src/diagnostic_set.rs @@ -187,10 +187,10 @@ impl DiagnosticEntry { impl Default for Summary { fn default() -> Self { Self { - start: Anchor::build_min(), - end: Anchor::build_max(), - min_start: Anchor::build_max(), - max_end: Anchor::build_min(), + start: Anchor::MIN, + end: Anchor::MAX, + min_start: Anchor::MAX, + max_end: Anchor::MIN, count: 0, } } diff --git a/crates/language/src/tests.rs b/crates/language/src/tests.rs index 3dbe8508a5..7eff3b0d97 100644 --- a/crates/language/src/tests.rs +++ b/crates/language/src/tests.rs @@ -789,7 +789,7 @@ fn test_random_collaboration(cx: &mut MutableAppContext, mut rng: StdRng) { for buffer in &buffers { let buffer = buffer.read(cx).snapshot(); let actual_remote_selections = buffer - .remote_selections_in_range(Anchor::build_min()..Anchor::build_max()) + .remote_selections_in_range(Anchor::MIN..Anchor::MAX) .map(|(replica_id, selections)| (replica_id, selections.collect::>())) .collect::>(); let expected_remote_selections = active_selections diff --git a/crates/text/src/anchor.rs b/crates/text/src/anchor.rs index c7de6bc882..5438ecd51f 100644 --- a/crates/text/src/anchor.rs +++ b/crates/text/src/anchor.rs @@ -12,21 +12,17 @@ pub struct Anchor { } impl Anchor { - pub fn build_min() -> Self { - Self { - timestamp: clock::Local::MIN, - offset: usize::MIN, - bias: Bias::Left, - } - } + pub const MIN: Self = Self { + timestamp: clock::Local::MIN, + offset: usize::MIN, + bias: Bias::Left, + }; - pub fn build_max() -> Self { - Self { - timestamp: clock::Local::MAX, - offset: usize::MAX, - bias: Bias::Right, - } - } + pub const MAX: Self = Self { + timestamp: clock::Local::MAX, + offset: usize::MAX, + bias: Bias::Right, + }; pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Result { let fragment_id_comparison = if self.timestamp == other.timestamp { diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index 8cb9629d07..b811d08c04 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -1318,8 +1318,8 @@ impl Buffer { let mut futures = Vec::new(); for anchor in anchors { if !self.version.observed(anchor.timestamp) - && *anchor != Anchor::build_max() - && *anchor != Anchor::build_min() + && *anchor != Anchor::MAX + && *anchor != Anchor::MIN { let (tx, rx) = oneshot::channel(); self.edit_id_resolvers @@ -1638,9 +1638,9 @@ impl BufferSnapshot { let mut position = D::default(); anchors.map(move |anchor| { - if *anchor == Anchor::build_min() { + if *anchor == Anchor::MIN { return D::default(); - } else if *anchor == Anchor::build_max() { + } else if *anchor == Anchor::MAX { return D::from_text_summary(&self.visible_text.summary()); } @@ -1680,9 +1680,9 @@ impl BufferSnapshot { where D: TextDimension, { - if *anchor == Anchor::build_min() { + if *anchor == Anchor::MIN { D::default() - } else if *anchor == Anchor::build_max() { + } else if *anchor == Anchor::MAX { D::from_text_summary(&self.visible_text.summary()) } else { let anchor_key = InsertionFragmentKey { @@ -1718,9 +1718,9 @@ impl BufferSnapshot { } fn fragment_id_for_anchor(&self, anchor: &Anchor) -> &Locator { - if *anchor == Anchor::build_min() { + if *anchor == Anchor::MIN { &locator::MIN - } else if *anchor == Anchor::build_max() { + } else if *anchor == Anchor::MAX { &locator::MAX } else { let anchor_key = InsertionFragmentKey { @@ -1758,9 +1758,9 @@ impl BufferSnapshot { pub fn anchor_at(&self, position: T, bias: Bias) -> Anchor { let offset = position.to_offset(self); if bias == Bias::Left && offset == 0 { - Anchor::build_min() + Anchor::MIN } else if bias == Bias::Right && offset == self.len() { - Anchor::build_max() + Anchor::MAX } else { let mut fragment_cursor = self.fragments.cursor::(); fragment_cursor.seek(&offset, bias, &None); @@ -1775,9 +1775,7 @@ impl BufferSnapshot { } pub fn can_resolve(&self, anchor: &Anchor) -> bool { - *anchor == Anchor::build_min() - || *anchor == Anchor::build_max() - || self.version.observed(anchor.timestamp) + *anchor == Anchor::MIN || *anchor == Anchor::MAX || self.version.observed(anchor.timestamp) } pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize { @@ -1799,7 +1797,7 @@ impl BufferSnapshot { where D: TextDimension + Ord, { - self.edits_since_in_range(since, Anchor::build_min()..Anchor::build_max()) + self.edits_since_in_range(since, Anchor::MIN..Anchor::MAX) } pub fn edited_ranges_for_transaction<'a, D>( From 92c7b5d6ef576151afc3964c8cde872daceed8d9 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Thu, 24 Mar 2022 11:48:31 -0700 Subject: [PATCH 4/4] Remove result from anchor cmp functions Co-authored-by: Nathan Sobo --- crates/diagnostics/src/diagnostics.rs | 3 +- crates/editor/src/display_map/block_map.rs | 2 +- crates/editor/src/display_map/fold_map.rs | 29 ++++++-------- crates/editor/src/editor.rs | 25 ++++-------- crates/editor/src/multi_buffer.rs | 45 +++++----------------- crates/editor/src/multi_buffer/anchor.rs | 19 +++++---- crates/language/src/buffer.rs | 12 +----- crates/language/src/diagnostic_set.rs | 16 +++----- crates/search/src/search.rs | 8 ++-- crates/text/src/anchor.rs | 14 +++---- crates/text/src/tests.rs | 36 +++++------------ 11 files changed, 68 insertions(+), 141 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index 61a2e6c9c1..56de434cf4 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -367,8 +367,7 @@ impl ProjectDiagnosticsEditor { range_a .start .cmp(&range_b.start, &snapshot) - .unwrap() - .then_with(|| range_a.end.cmp(&range_b.end, &snapshot).unwrap()) + .then_with(|| range_a.end.cmp(&range_b.end, &snapshot)) }); if path_state.diagnostic_groups.is_empty() { diff --git a/crates/editor/src/display_map/block_map.rs b/crates/editor/src/display_map/block_map.rs index 489381daef..9d1b7a7588 100644 --- a/crates/editor/src/display_map/block_map.rs +++ b/crates/editor/src/display_map/block_map.rs @@ -499,7 +499,7 @@ impl<'a> BlockMapWriter<'a> { let block_ix = match self .0 .blocks - .binary_search_by(|probe| probe.position.cmp(&position, &buffer).unwrap()) + .binary_search_by(|probe| probe.position.cmp(&position, &buffer)) { Ok(ix) | Err(ix) => ix, }; diff --git a/crates/editor/src/display_map/fold_map.rs b/crates/editor/src/display_map/fold_map.rs index 7f744afa40..4b07854c3e 100644 --- a/crates/editor/src/display_map/fold_map.rs +++ b/crates/editor/src/display_map/fold_map.rs @@ -256,7 +256,7 @@ impl FoldMap { let mut folds = self.folds.iter().peekable(); while let Some(fold) = folds.next() { if let Some(next_fold) = folds.peek() { - let comparison = fold.0.cmp(&next_fold.0, &self.buffer.lock()).unwrap(); + let comparison = fold.0.cmp(&next_fold.0, &self.buffer.lock()); assert!(comparison.is_le()); } } @@ -699,10 +699,7 @@ impl FoldSnapshot { let ranges = &highlights.1; let start_ix = match ranges.binary_search_by(|probe| { - let cmp = probe - .end - .cmp(&transform_start, &self.buffer_snapshot()) - .unwrap(); + let cmp = probe.end.cmp(&transform_start, &self.buffer_snapshot()); if cmp.is_gt() { Ordering::Greater } else { @@ -715,7 +712,6 @@ impl FoldSnapshot { if range .start .cmp(&transform_end, &self.buffer_snapshot) - .unwrap() .is_ge() { break; @@ -820,8 +816,8 @@ where let start = buffer.anchor_before(range.start.to_offset(buffer)); let end = buffer.anchor_after(range.end.to_offset(buffer)); let mut cursor = folds.filter::<_, usize>(move |summary| { - let start_cmp = start.cmp(&summary.max_end, buffer).unwrap(); - let end_cmp = end.cmp(&summary.min_start, buffer).unwrap(); + let start_cmp = start.cmp(&summary.max_end, buffer); + let end_cmp = end.cmp(&summary.min_start, buffer); if inclusive { start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal @@ -962,19 +958,19 @@ impl sum_tree::Summary for FoldSummary { type Context = MultiBufferSnapshot; fn add_summary(&mut self, other: &Self, buffer: &MultiBufferSnapshot) { - if other.min_start.cmp(&self.min_start, buffer).unwrap() == Ordering::Less { + if other.min_start.cmp(&self.min_start, buffer) == Ordering::Less { self.min_start = other.min_start.clone(); } - if other.max_end.cmp(&self.max_end, buffer).unwrap() == Ordering::Greater { + if other.max_end.cmp(&self.max_end, buffer) == Ordering::Greater { self.max_end = other.max_end.clone(); } #[cfg(debug_assertions)] { - let start_comparison = self.start.cmp(&other.start, buffer).unwrap(); + let start_comparison = self.start.cmp(&other.start, buffer); assert!(start_comparison <= Ordering::Equal); if start_comparison == Ordering::Equal { - assert!(self.end.cmp(&other.end, buffer).unwrap() >= Ordering::Equal); + assert!(self.end.cmp(&other.end, buffer) >= Ordering::Equal); } } @@ -993,7 +989,7 @@ impl<'a> sum_tree::Dimension<'a, FoldSummary> for Fold { impl<'a> sum_tree::SeekTarget<'a, FoldSummary, Fold> for Fold { fn cmp(&self, other: &Self, buffer: &MultiBufferSnapshot) -> Ordering { - self.0.cmp(&other.0, buffer).unwrap() + self.0.cmp(&other.0, buffer) } } @@ -1600,9 +1596,8 @@ mod tests { .filter(|fold| { let start = buffer_snapshot.anchor_before(start); let end = buffer_snapshot.anchor_after(end); - start.cmp(&fold.0.end, &buffer_snapshot).unwrap() == Ordering::Less - && end.cmp(&fold.0.start, &buffer_snapshot).unwrap() - == Ordering::Greater + start.cmp(&fold.0.end, &buffer_snapshot) == Ordering::Less + && end.cmp(&fold.0.start, &buffer_snapshot) == Ordering::Greater }) .map(|fold| fold.0) .collect::>(); @@ -1680,7 +1675,7 @@ mod tests { let buffer = self.buffer.lock().clone(); let mut folds = self.folds.items(&buffer); // Ensure sorting doesn't change how folds get merged and displayed. - folds.sort_by(|a, b| a.0.cmp(&b.0, &buffer).unwrap()); + folds.sort_by(|a, b| a.0.cmp(&b.0, &buffer)); let mut fold_ranges = folds .iter() .map(|fold| fold.0.start.to_offset(&buffer)..fold.0.end.to_offset(&buffer)) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 3fbdf7def1..78b7f9a9f9 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -2545,7 +2545,7 @@ impl Editor { .range .end .min(&excerpt_range.end, cursor_buffer_snapshot); - if start.cmp(&end, cursor_buffer_snapshot).unwrap().is_ge() { + if start.cmp(&end, cursor_buffer_snapshot).is_ge() { continue; } @@ -2672,8 +2672,7 @@ impl Editor { }) }) .collect::>(); - tabstop_ranges - .sort_unstable_by(|a, b| a.start.cmp(&b.start, snapshot).unwrap()); + tabstop_ranges.sort_unstable_by(|a, b| a.start.cmp(&b.start, snapshot)); tabstop_ranges }) .collect::>() @@ -4700,13 +4699,13 @@ impl Editor { let start_ix = match self .selections - .binary_search_by(|probe| probe.end.cmp(&range.start, &buffer).unwrap()) + .binary_search_by(|probe| probe.end.cmp(&range.start, &buffer)) { Ok(ix) | Err(ix) => ix, }; let end_ix = match self .selections - .binary_search_by(|probe| probe.start.cmp(&range.end, &buffer).unwrap()) + .binary_search_by(|probe| probe.start.cmp(&range.end, &buffer)) { Ok(ix) => ix + 1, Err(ix) => ix, @@ -4928,8 +4927,7 @@ impl Editor { selections.sort_by(|a, b| { a.start .cmp(&b.start, &*buffer) - .unwrap() - .then_with(|| b.end.cmp(&a.end, &*buffer).unwrap()) + .then_with(|| b.end.cmp(&a.end, &*buffer)) }); // Merge overlapping selections @@ -4938,24 +4936,17 @@ impl Editor { if selections[i - 1] .end .cmp(&selections[i].start, &*buffer) - .unwrap() .is_ge() { let removed = selections.remove(i); if removed .start .cmp(&selections[i - 1].start, &*buffer) - .unwrap() .is_lt() { selections[i - 1].start = removed.start; } - if removed - .end - .cmp(&selections[i - 1].end, &*buffer) - .unwrap() - .is_gt() - { + if removed.end.cmp(&selections[i - 1].end, &*buffer).is_gt() { selections[i - 1].end = removed.end; } } else { @@ -5429,7 +5420,7 @@ impl Editor { let buffer = &display_snapshot.buffer_snapshot; for (color, ranges) in self.background_highlights.values() { let start_ix = match ranges.binary_search_by(|probe| { - let cmp = probe.end.cmp(&search_range.start, &buffer).unwrap(); + let cmp = probe.end.cmp(&search_range.start, &buffer); if cmp.is_gt() { Ordering::Greater } else { @@ -5439,7 +5430,7 @@ impl Editor { Ok(i) | Err(i) => i, }; for range in &ranges[start_ix..] { - if range.start.cmp(&search_range.end, &buffer).unwrap().is_ge() { + if range.start.cmp(&search_range.end, &buffer).is_ge() { break; } let start = range diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index de735dafa7..af98f3d589 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -522,24 +522,14 @@ impl MultiBuffer { self.buffers.borrow()[&buffer_id] .buffer .update(cx, |buffer, cx| { - selections.sort_unstable_by(|a, b| a.start.cmp(&b.start, buffer).unwrap()); + selections.sort_unstable_by(|a, b| a.start.cmp(&b.start, buffer)); let mut selections = selections.into_iter().peekable(); let merged_selections = Arc::from_iter(iter::from_fn(|| { let mut selection = selections.next()?; while let Some(next_selection) = selections.peek() { - if selection - .end - .cmp(&next_selection.start, buffer) - .unwrap() - .is_ge() - { + if selection.end.cmp(&next_selection.start, buffer).is_ge() { let next_selection = selections.next().unwrap(); - if next_selection - .end - .cmp(&selection.end, buffer) - .unwrap() - .is_ge() - { + if next_selection.end.cmp(&selection.end, buffer).is_ge() { selection.end = next_selection.end; } } else { @@ -1936,11 +1926,7 @@ impl MultiBufferSnapshot { .range .start .bias(anchor.text_anchor.bias, &excerpt.buffer); - if text_anchor - .cmp(&excerpt.range.end, &excerpt.buffer) - .unwrap() - .is_gt() - { + if text_anchor.cmp(&excerpt.range.end, &excerpt.buffer).is_gt() { text_anchor = excerpt.range.end.clone(); } Anchor { @@ -1955,7 +1941,6 @@ impl MultiBufferSnapshot { .bias(anchor.text_anchor.bias, &excerpt.buffer); if text_anchor .cmp(&excerpt.range.start, &excerpt.buffer) - .unwrap() .is_lt() { text_anchor = excerpt.range.start.clone(); @@ -1975,7 +1960,7 @@ impl MultiBufferSnapshot { result.push((anchor_ix, anchor, kept_position)); } } - result.sort_unstable_by(|a, b| a.1.cmp(&b.1, self).unwrap()); + result.sort_unstable_by(|a, b| a.1.cmp(&b.1, self)); result } @@ -2322,10 +2307,10 @@ impl MultiBufferSnapshot { excerpt_id: excerpt.id.clone(), text_anchor: selection.end.clone(), }; - if range.start.cmp(&start, self).unwrap().is_gt() { + if range.start.cmp(&start, self).is_gt() { start = range.start.clone(); } - if range.end.cmp(&end, self).unwrap().is_lt() { + if range.end.cmp(&end, self).is_lt() { end = range.end.clone(); } @@ -2549,17 +2534,9 @@ impl Excerpt { } fn clip_anchor(&self, text_anchor: text::Anchor) -> text::Anchor { - if text_anchor - .cmp(&self.range.start, &self.buffer) - .unwrap() - .is_lt() - { + if text_anchor.cmp(&self.range.start, &self.buffer).is_lt() { self.range.start.clone() - } else if text_anchor - .cmp(&self.range.end, &self.buffer) - .unwrap() - .is_gt() - { + } else if text_anchor.cmp(&self.range.end, &self.buffer).is_gt() { self.range.end.clone() } else { text_anchor @@ -2572,13 +2549,11 @@ impl Excerpt { .range .start .cmp(&anchor.text_anchor, &self.buffer) - .unwrap() .is_le() && self .range .end .cmp(&anchor.text_anchor, &self.buffer) - .unwrap() .is_ge() } } @@ -3385,7 +3360,7 @@ mod tests { let bias = if rng.gen() { Bias::Left } else { Bias::Right }; log::info!("Creating anchor at {} with bias {:?}", offset, bias); anchors.push(multibuffer.anchor_at(offset, bias)); - anchors.sort_by(|a, b| a.cmp(&b, &multibuffer).unwrap()); + anchors.sort_by(|a, b| a.cmp(&b, &multibuffer)); } 40..=44 if !anchors.is_empty() => { let multibuffer = multibuffer.read(cx).read(cx); diff --git a/crates/editor/src/multi_buffer/anchor.rs b/crates/editor/src/multi_buffer/anchor.rs index aeaf184c60..df080f074c 100644 --- a/crates/editor/src/multi_buffer/anchor.rs +++ b/crates/editor/src/multi_buffer/anchor.rs @@ -1,5 +1,4 @@ use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint}; -use anyhow::Result; use std::{ cmp::Ordering, ops::{Range, Sub}, @@ -35,18 +34,18 @@ impl Anchor { &self.excerpt_id } - pub fn cmp<'a>(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Result { + pub fn cmp<'a>(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering { let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id); if excerpt_id_cmp.is_eq() { if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() { - Ok(Ordering::Equal) + Ordering::Equal } else if let Some(excerpt) = snapshot.excerpt(&self.excerpt_id) { self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer) } else { - Ok(Ordering::Equal) + Ordering::Equal } } else { - Ok(excerpt_id_cmp) + excerpt_id_cmp } } @@ -97,17 +96,17 @@ impl ToPoint for Anchor { } pub trait AnchorRangeExt { - fn cmp(&self, b: &Range, buffer: &MultiBufferSnapshot) -> Result; + fn cmp(&self, b: &Range, buffer: &MultiBufferSnapshot) -> Ordering; fn to_offset(&self, content: &MultiBufferSnapshot) -> Range; fn to_point(&self, content: &MultiBufferSnapshot) -> Range; } impl AnchorRangeExt for Range { - fn cmp(&self, other: &Range, buffer: &MultiBufferSnapshot) -> Result { - Ok(match self.start.cmp(&other.start, buffer)? { - Ordering::Equal => other.end.cmp(&self.end, buffer)?, + fn cmp(&self, other: &Range, buffer: &MultiBufferSnapshot) -> Ordering { + match self.start.cmp(&other.start, buffer) { + Ordering::Equal => other.end.cmp(&self.end, buffer), ord @ _ => ord, - }) + } } fn to_offset(&self, content: &MultiBufferSnapshot) -> Range { diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index f32028002a..4c8341cf76 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -1813,20 +1813,12 @@ impl BufferSnapshot { }) .map(move |(replica_id, set)| { let start_ix = match set.selections.binary_search_by(|probe| { - probe - .end - .cmp(&range.start, self) - .unwrap() - .then(Ordering::Greater) + probe.end.cmp(&range.start, self).then(Ordering::Greater) }) { Ok(ix) | Err(ix) => ix, }; let end_ix = match set.selections.binary_search_by(|probe| { - probe - .start - .cmp(&range.end, self) - .unwrap() - .then(Ordering::Less) + probe.start.cmp(&range.end, self).then(Ordering::Less) }) { Ok(ix) | Err(ix) => ix, }; diff --git a/crates/language/src/diagnostic_set.rs b/crates/language/src/diagnostic_set.rs index 28b03b5aa0..490789a8c8 100644 --- a/crates/language/src/diagnostic_set.rs +++ b/crates/language/src/diagnostic_set.rs @@ -81,8 +81,8 @@ impl DiagnosticSet { let range = buffer.anchor_before(range.start)..buffer.anchor_at(range.end, end_bias); let mut cursor = self.diagnostics.filter::<_, ()>({ move |summary: &Summary| { - let start_cmp = range.start.cmp(&summary.max_end, buffer).unwrap(); - let end_cmp = range.end.cmp(&summary.min_start, buffer).unwrap(); + let start_cmp = range.start.cmp(&summary.max_end, buffer); + let end_cmp = range.end.cmp(&summary.min_start, buffer); if inclusive { start_cmp <= Ordering::Equal && end_cmp >= Ordering::Equal } else { @@ -123,7 +123,7 @@ impl DiagnosticSet { let start_ix = output.len(); output.extend(groups.into_values().filter_map(|mut entries| { - entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer).unwrap()); + entries.sort_unstable_by(|a, b| a.range.start.cmp(&b.range.start, buffer)); entries .iter() .position(|entry| entry.diagnostic.is_primary) @@ -137,7 +137,6 @@ impl DiagnosticSet { .range .start .cmp(&b.entries[b.primary_ix].range.start, buffer) - .unwrap() }); } @@ -200,15 +199,10 @@ impl sum_tree::Summary for Summary { type Context = text::BufferSnapshot; fn add_summary(&mut self, other: &Self, buffer: &Self::Context) { - if other - .min_start - .cmp(&self.min_start, buffer) - .unwrap() - .is_lt() - { + if other.min_start.cmp(&self.min_start, buffer).is_lt() { self.min_start = other.min_start.clone(); } - if other.max_end.cmp(&self.max_end, buffer).unwrap().is_gt() { + if other.max_end.cmp(&self.max_end, buffer).is_gt() { self.max_end = other.max_end.clone(); } self.start = other.start.clone(); diff --git a/crates/search/src/search.rs b/crates/search/src/search.rs index a1e335a035..9fb4cda8e9 100644 --- a/crates/search/src/search.rs +++ b/crates/search/src/search.rs @@ -39,9 +39,9 @@ pub(crate) fn active_match_index( None } else { match ranges.binary_search_by(|probe| { - if probe.end.cmp(&cursor, &*buffer).unwrap().is_lt() { + if probe.end.cmp(&cursor, &*buffer).is_lt() { Ordering::Less - } else if probe.start.cmp(&cursor, &*buffer).unwrap().is_gt() { + } else if probe.start.cmp(&cursor, &*buffer).is_gt() { Ordering::Greater } else { Ordering::Equal @@ -59,7 +59,7 @@ pub(crate) fn match_index_for_direction( direction: Direction, buffer: &MultiBufferSnapshot, ) -> usize { - if ranges[index].start.cmp(&cursor, &buffer).unwrap().is_gt() { + if ranges[index].start.cmp(&cursor, &buffer).is_gt() { if direction == Direction::Prev { if index == 0 { index = ranges.len() - 1; @@ -67,7 +67,7 @@ pub(crate) fn match_index_for_direction( index -= 1; } } - } else if ranges[index].end.cmp(&cursor, &buffer).unwrap().is_lt() { + } else if ranges[index].end.cmp(&cursor, &buffer).is_lt() { if direction == Direction::Next { index = 0; } diff --git a/crates/text/src/anchor.rs b/crates/text/src/anchor.rs index 5438ecd51f..e642aa45d3 100644 --- a/crates/text/src/anchor.rs +++ b/crates/text/src/anchor.rs @@ -24,7 +24,7 @@ impl Anchor { bias: Bias::Right, }; - pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Result { + pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Ordering { let fragment_id_comparison = if self.timestamp == other.timestamp { Ordering::Equal } else { @@ -33,13 +33,13 @@ impl Anchor { .cmp(&buffer.fragment_id_for_anchor(other)) }; - Ok(fragment_id_comparison + fragment_id_comparison .then_with(|| self.offset.cmp(&other.offset)) - .then_with(|| self.bias.cmp(&other.bias))) + .then_with(|| self.bias.cmp(&other.bias)) } pub fn min(&self, other: &Self, buffer: &BufferSnapshot) -> Self { - if self.cmp(other, buffer).unwrap().is_le() { + if self.cmp(other, buffer).is_le() { self.clone() } else { other.clone() @@ -47,7 +47,7 @@ impl Anchor { } pub fn max(&self, other: &Self, buffer: &BufferSnapshot) -> Self { - if self.cmp(other, buffer).unwrap().is_ge() { + if self.cmp(other, buffer).is_ge() { self.clone() } else { other.clone() @@ -117,8 +117,8 @@ pub trait AnchorRangeExt { impl AnchorRangeExt for Range { fn cmp(&self, other: &Range, buffer: &BufferSnapshot) -> Result { - Ok(match self.start.cmp(&other.start, buffer)? { - Ordering::Equal => other.end.cmp(&self.end, buffer)?, + Ok(match self.start.cmp(&other.start, buffer) { + Ordering::Equal => other.end.cmp(&self.end, buffer), ord @ _ => ord, }) } diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index 05cf0af6ec..7961dccd56 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -340,59 +340,41 @@ fn test_anchors() { let anchor_at_offset_2 = buffer.anchor_before(2); assert_eq!( - anchor_at_offset_0 - .cmp(&anchor_at_offset_0, &buffer) - .unwrap(), + anchor_at_offset_0.cmp(&anchor_at_offset_0, &buffer), Ordering::Equal ); assert_eq!( - anchor_at_offset_1 - .cmp(&anchor_at_offset_1, &buffer) - .unwrap(), + anchor_at_offset_1.cmp(&anchor_at_offset_1, &buffer), Ordering::Equal ); assert_eq!( - anchor_at_offset_2 - .cmp(&anchor_at_offset_2, &buffer) - .unwrap(), + anchor_at_offset_2.cmp(&anchor_at_offset_2, &buffer), Ordering::Equal ); assert_eq!( - anchor_at_offset_0 - .cmp(&anchor_at_offset_1, &buffer) - .unwrap(), + anchor_at_offset_0.cmp(&anchor_at_offset_1, &buffer), Ordering::Less ); assert_eq!( - anchor_at_offset_1 - .cmp(&anchor_at_offset_2, &buffer) - .unwrap(), + anchor_at_offset_1.cmp(&anchor_at_offset_2, &buffer), Ordering::Less ); assert_eq!( - anchor_at_offset_0 - .cmp(&anchor_at_offset_2, &buffer) - .unwrap(), + anchor_at_offset_0.cmp(&anchor_at_offset_2, &buffer), Ordering::Less ); assert_eq!( - anchor_at_offset_1 - .cmp(&anchor_at_offset_0, &buffer) - .unwrap(), + anchor_at_offset_1.cmp(&anchor_at_offset_0, &buffer), Ordering::Greater ); assert_eq!( - anchor_at_offset_2 - .cmp(&anchor_at_offset_1, &buffer) - .unwrap(), + anchor_at_offset_2.cmp(&anchor_at_offset_1, &buffer), Ordering::Greater ); assert_eq!( - anchor_at_offset_2 - .cmp(&anchor_at_offset_0, &buffer) - .unwrap(), + anchor_at_offset_2.cmp(&anchor_at_offset_0, &buffer), Ordering::Greater ); }