Rework SuggestionMap to take highlight style when retrieving chunks

This commit is contained in:
Antonio Scandurra
2023-03-28 09:42:01 -07:00
committed by Mikayla Maki
parent 093e0a30e9
commit 6715e5247c
9 changed files with 95 additions and 73 deletions
+5 -2
View File
@@ -4,7 +4,7 @@ use super::{
};
use crate::{Anchor, ExcerptId, ExcerptRange, ToPoint as _};
use collections::{Bound, HashMap, HashSet};
use gpui::{ElementBox, RenderContext};
use gpui::{fonts::HighlightStyle, ElementBox, RenderContext};
use language::{BufferSnapshot, Chunk, Patch, Point};
use parking_lot::Mutex;
use std::{
@@ -572,7 +572,7 @@ impl<'a> BlockMapWriter<'a> {
impl BlockSnapshot {
#[cfg(test)]
pub fn text(&self) -> String {
self.chunks(0..self.transforms.summary().output_rows, false, None)
self.chunks(0..self.transforms.summary().output_rows, false, None, None)
.map(|chunk| chunk.text)
.collect()
}
@@ -582,6 +582,7 @@ impl BlockSnapshot {
rows: Range<u32>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
suggestion_highlight: Option<HighlightStyle>,
) -> BlockChunks<'a> {
let max_output_row = cmp::min(rows.end, self.transforms.summary().output_rows);
let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
@@ -614,6 +615,7 @@ impl BlockSnapshot {
input_start..input_end,
language_aware,
text_highlights,
suggestion_highlight,
),
input_chunk: Default::default(),
transforms: cursor,
@@ -1498,6 +1500,7 @@ mod tests {
start_row as u32..blocks_snapshot.max_point().row + 1,
false,
None,
None,
)
.map(|chunk| chunk.text)
.collect::<String>();
+13 -11
View File
@@ -60,7 +60,6 @@ impl SuggestionPoint {
pub struct Suggestion<T> {
pub position: T,
pub text: Rope,
pub highlight_style: HighlightStyle,
}
pub struct SuggestionMap(Mutex<SuggestionSnapshot>);
@@ -93,7 +92,6 @@ impl SuggestionMap {
Suggestion {
position: fold_offset,
text: new_suggestion.text,
highlight_style: new_suggestion.highlight_style,
}
});
@@ -369,7 +367,7 @@ impl SuggestionSnapshot {
pub fn chars_at(&self, start: SuggestionPoint) -> impl '_ + Iterator<Item = char> {
let start = self.to_offset(start);
self.chunks(start..self.len(), false, None)
self.chunks(start..self.len(), false, None, None)
.flat_map(|chunk| chunk.text.chars())
}
@@ -378,6 +376,7 @@ impl SuggestionSnapshot {
range: Range<SuggestionOffset>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
suggestion_highlight: Option<HighlightStyle>,
) -> SuggestionChunks<'a> {
if let Some(suggestion) = self.suggestion.as_ref() {
let suggestion_range =
@@ -421,7 +420,7 @@ impl SuggestionSnapshot {
prefix_chunks,
suggestion_chunks,
suffix_chunks,
highlight_style: suggestion.highlight_style,
highlight_style: suggestion_highlight,
}
} else {
SuggestionChunks {
@@ -432,7 +431,7 @@ impl SuggestionSnapshot {
)),
suggestion_chunks: None,
suffix_chunks: None,
highlight_style: Default::default(),
highlight_style: None,
}
}
}
@@ -467,7 +466,7 @@ impl SuggestionSnapshot {
#[cfg(test)]
pub fn text(&self) -> String {
self.chunks(Default::default()..self.len(), false, None)
self.chunks(Default::default()..self.len(), false, None, None)
.map(|chunk| chunk.text)
.collect()
}
@@ -477,7 +476,7 @@ pub struct SuggestionChunks<'a> {
prefix_chunks: Option<FoldChunks<'a>>,
suggestion_chunks: Option<text::Chunks<'a>>,
suffix_chunks: Option<FoldChunks<'a>>,
highlight_style: HighlightStyle,
highlight_style: Option<HighlightStyle>,
}
impl<'a> Iterator for SuggestionChunks<'a> {
@@ -497,7 +496,7 @@ impl<'a> Iterator for SuggestionChunks<'a> {
return Some(Chunk {
text: chunk,
syntax_highlight_id: None,
highlight_style: Some(self.highlight_style),
highlight_style: self.highlight_style,
diagnostic_severity: None,
is_unnecessary: false,
});
@@ -563,7 +562,6 @@ mod tests {
Some(Suggestion {
position: 3,
text: "123\n456".into(),
highlight_style: Default::default(),
}),
fold_snapshot,
Default::default(),
@@ -692,7 +690,12 @@ mod tests {
start = expected_text.clip_offset(start, Bias::Right);
let actual_text = suggestion_snapshot
.chunks(SuggestionOffset(start)..SuggestionOffset(end), false, None)
.chunks(
SuggestionOffset(start)..SuggestionOffset(end),
false,
None,
None,
)
.map(|chunk| chunk.text)
.collect::<String>();
assert_eq!(
@@ -816,7 +819,6 @@ mod tests {
.collect::<String>()
.as_str()
.into(),
highlight_style: Default::default(),
})
};
+14 -4
View File
@@ -3,6 +3,7 @@ use super::{
TextHighlights,
};
use crate::MultiBufferSnapshot;
use gpui::fonts::HighlightStyle;
use language::{Chunk, Point};
use parking_lot::Mutex;
use std::{cmp, mem, num::NonZeroU32, ops::Range};
@@ -47,6 +48,7 @@ impl TabMap {
suggestion_edit.old.end..old_max_offset,
false,
None,
None,
) {
let patterns: &[_] = &['\t', '\n'];
if let Some(ix) = chunk.text.find(patterns) {
@@ -126,6 +128,7 @@ impl TabSnapshot {
TabPoint::new(row, 0)..TabPoint::new(row + 1, 0),
false,
None,
None,
)
.map(|chunk| chunk.text.len() as u32)
.sum::<u32>()
@@ -153,7 +156,7 @@ impl TabSnapshot {
self.max_point()
};
for c in self
.chunks(range.start..line_end, false, None)
.chunks(range.start..line_end, false, None, None)
.flat_map(|chunk| chunk.text.chars())
{
if c == '\n' {
@@ -167,7 +170,12 @@ impl TabSnapshot {
last_line_chars = first_line_chars;
} else {
for _ in self
.chunks(TabPoint::new(range.end.row(), 0)..range.end, false, None)
.chunks(
TabPoint::new(range.end.row(), 0)..range.end,
false,
None,
None,
)
.flat_map(|chunk| chunk.text.chars())
{
last_line_chars += 1;
@@ -188,6 +196,7 @@ impl TabSnapshot {
range: Range<TabPoint>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
suggestion_highlight: Option<HighlightStyle>,
) -> TabChunks<'a> {
let (input_start, expanded_char_column, to_next_stop) =
self.to_suggestion_point(range.start, Bias::Left);
@@ -206,6 +215,7 @@ impl TabSnapshot {
input_start..input_end,
language_aware,
text_highlights,
suggestion_highlight,
),
column: expanded_char_column,
output_position: range.start.0,
@@ -225,7 +235,7 @@ impl TabSnapshot {
#[cfg(test)]
pub fn text(&self) -> String {
self.chunks(TabPoint::zero()..self.max_point(), false, None)
self.chunks(TabPoint::zero()..self.max_point(), false, None, None)
.map(|chunk| chunk.text)
.collect()
}
@@ -574,7 +584,7 @@ mod tests {
assert_eq!(
expected_text,
tabs_snapshot
.chunks(start..end, false, None)
.chunks(start..end, false, None, None)
.map(|c| c.text)
.collect::<String>(),
"chunks({:?}..{:?})",
+8 -4
View File
@@ -5,8 +5,9 @@ use super::{
};
use crate::MultiBufferSnapshot;
use gpui::{
fonts::FontId, text_layout::LineWrapper, Entity, ModelContext, ModelHandle, MutableAppContext,
Task,
fonts::{FontId, HighlightStyle},
text_layout::LineWrapper,
Entity, ModelContext, ModelHandle, MutableAppContext, Task,
};
use language::{Chunk, Point};
use lazy_static::lazy_static;
@@ -444,6 +445,7 @@ impl WrapSnapshot {
TabPoint::new(edit.new_rows.start, 0)..new_tab_snapshot.max_point(),
false,
None,
None,
);
let mut edit_transforms = Vec::<Transform>::new();
for _ in edit.new_rows.start..edit.new_rows.end {
@@ -573,6 +575,7 @@ impl WrapSnapshot {
rows: Range<u32>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
suggestion_highlight: Option<HighlightStyle>,
) -> WrapChunks<'a> {
let output_start = WrapPoint::new(rows.start, 0);
let output_end = WrapPoint::new(rows.end, 0);
@@ -590,6 +593,7 @@ impl WrapSnapshot {
input_start..input_end,
language_aware,
text_highlights,
suggestion_highlight,
),
input_chunk: Default::default(),
output_position: output_start,
@@ -1315,7 +1319,7 @@ mod tests {
}
pub fn text_chunks(&self, wrap_row: u32) -> impl Iterator<Item = &str> {
self.chunks(wrap_row..self.max_point().row() + 1, false, None)
self.chunks(wrap_row..self.max_point().row() + 1, false, None, None)
.map(|h| h.text)
}
@@ -1339,7 +1343,7 @@ mod tests {
}
let actual_text = self
.chunks(start_row..end_row, true, None)
.chunks(start_row..end_row, true, None, None)
.map(|c| c.text)
.collect::<String>();
assert_eq!(