Unify text and inlay highlights

This commit is contained in:
Kirill Bulatov
2023-08-25 14:26:17 +03:00
parent 420f8b7b15
commit 12ffbe54fb
9 changed files with 76 additions and 163 deletions
+1 -5
View File
@@ -1,6 +1,6 @@
use super::{
wrap_map::{self, WrapEdit, WrapPoint, WrapSnapshot},
InlayHighlights, TextHighlights,
TextHighlights,
};
use crate::{Anchor, Editor, ExcerptId, ExcerptRange, ToPoint as _};
use collections::{Bound, HashMap, HashSet};
@@ -579,7 +579,6 @@ impl BlockSnapshot {
None,
None,
None,
None,
)
.map(|chunk| chunk.text)
.collect()
@@ -590,7 +589,6 @@ impl BlockSnapshot {
rows: Range<u32>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
inlay_highlights: Option<&'a InlayHighlights>,
inlay_highlight_style: Option<HighlightStyle>,
suggestion_highlight_style: Option<HighlightStyle>,
) -> BlockChunks<'a> {
@@ -625,7 +623,6 @@ impl BlockSnapshot {
input_start..input_end,
language_aware,
text_highlights,
inlay_highlights,
inlay_highlight_style,
suggestion_highlight_style,
),
@@ -1507,7 +1504,6 @@ mod tests {
None,
None,
None,
None,
)
.map(|chunk| chunk.text)
.collect::<String>();
+5 -14
View File
@@ -1,6 +1,6 @@
use super::{
inlay_map::{InlayBufferRows, InlayChunks, InlayEdit, InlayOffset, InlayPoint, InlaySnapshot},
InlayHighlights, TextHighlights,
TextHighlights,
};
use crate::{Anchor, AnchorRangeExt, MultiBufferSnapshot, ToOffset};
use gpui::{color::Color, fonts::HighlightStyle};
@@ -475,7 +475,7 @@ pub struct FoldSnapshot {
impl FoldSnapshot {
#[cfg(test)]
pub fn text(&self) -> String {
self.chunks(FoldOffset(0)..self.len(), false, None, None, None, None)
self.chunks(FoldOffset(0)..self.len(), false, None, None, None)
.map(|c| c.text)
.collect()
}
@@ -652,7 +652,6 @@ impl FoldSnapshot {
range: Range<FoldOffset>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
inlay_highlights: Option<&'a InlayHighlights>,
inlay_highlight_style: Option<HighlightStyle>,
suggestion_highlight_style: Option<HighlightStyle>,
) -> FoldChunks<'a> {
@@ -676,7 +675,6 @@ impl FoldSnapshot {
inlay_start..inlay_end,
language_aware,
text_highlights,
inlay_highlights,
inlay_highlight_style,
suggestion_highlight_style,
),
@@ -689,15 +687,8 @@ impl FoldSnapshot {
}
pub fn chars_at(&self, start: FoldPoint) -> impl '_ + Iterator<Item = char> {
self.chunks(
start.to_offset(self)..self.len(),
false,
None,
None,
None,
None,
)
.flat_map(|chunk| chunk.text.chars())
self.chunks(start.to_offset(self)..self.len(), false, None, None, None)
.flat_map(|chunk| chunk.text.chars())
}
#[cfg(test)]
@@ -1505,7 +1496,7 @@ mod tests {
let text = &expected_text[start.0..end.0];
assert_eq!(
snapshot
.chunks(start..end, false, None, None, None, None)
.chunks(start..end, false, None, None, None)
.map(|c| c.text)
.collect::<String>(),
text,
+27 -86
View File
@@ -1,8 +1,9 @@
use crate::{
link_go_to_definition::DocumentRange,
multi_buffer::{MultiBufferChunks, MultiBufferRows},
Anchor, InlayId, MultiBufferSnapshot, ToOffset,
};
use collections::{BTreeMap, BTreeSet, HashSet};
use collections::{BTreeMap, BTreeSet};
use gpui::fonts::HighlightStyle;
use language::{Chunk, Edit, Point, TextSummary};
use std::{
@@ -15,7 +16,7 @@ use std::{
use sum_tree::{Bias, Cursor, SumTree};
use text::{Patch, Rope};
use super::{InlayHighlights, TextHighlights};
use super::TextHighlights;
pub struct InlayMap {
snapshot: InlaySnapshot,
@@ -244,7 +245,6 @@ impl<'a> Iterator for InlayChunks<'a> {
return None;
}
// TODO kb highlights are not displayed still
let mut next_highlight_endpoint = InlayOffset(usize::MAX);
while let Some(endpoint) = self.highlight_endpoints.peek().copied() {
if endpoint.offset <= self.output_offset {
@@ -993,7 +993,6 @@ impl InlaySnapshot {
range: Range<InlayOffset>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
inlay_highlights: Option<&'a InlayHighlights>,
inlay_highlight_style: Option<HighlightStyle>,
suggestion_highlight_style: Option<HighlightStyle>,
) -> InlayChunks<'a> {
@@ -1002,15 +1001,14 @@ impl InlaySnapshot {
let empty_text_highlights = TextHighlights::default();
let text_highlights = text_highlights.unwrap_or_else(|| &empty_text_highlights);
let empty_inlay_highlights = InlayHighlights::default();
let inlay_highlights = inlay_highlights.unwrap_or_else(|| &empty_inlay_highlights);
let mut highlight_endpoints = Vec::new();
if !text_highlights.is_empty() || !inlay_highlights.is_empty() {
if !text_highlights.is_empty() {
while cursor.start().0 < range.end {
let transform_start = self
.buffer
.anchor_after(self.to_buffer_offset(cmp::max(range.start, cursor.start().0)));
let transform_start = self.to_inlay_offset(transform_start.to_offset(&self.buffer));
let transform_end = {
let overshoot = InlayOffset(range.end.0 - cursor.start().0 .0);
@@ -1019,15 +1017,17 @@ impl InlaySnapshot {
cursor.start().0 + overshoot,
)))
};
let transform_end = self.to_inlay_offset(transform_end.to_offset(&self.buffer));
let mut covered_tags = HashSet::default();
for (tag, text_highlights) in text_highlights.iter() {
covered_tags.insert(*tag);
let style = text_highlights.0;
let ranges = &text_highlights.1;
let start_ix = match ranges.binary_search_by(|probe| {
let cmp = probe.end.cmp(&transform_start, &self.buffer);
let cmp = self
.document_to_inlay_range(probe)
.end
.cmp(&transform_start);
if cmp.is_gt() {
cmp::Ordering::Greater
} else {
@@ -1037,46 +1037,24 @@ impl InlaySnapshot {
Ok(i) | Err(i) => i,
};
for range in &ranges[start_ix..] {
if range.start.cmp(&transform_end, &self.buffer).is_ge() {
let range = self.document_to_inlay_range(range);
if range.start.cmp(&transform_end).is_ge() {
break;
}
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.start.to_offset(&self.buffer)),
offset: range.start,
is_start: true,
tag: *tag,
style,
});
highlight_endpoints.push(HighlightEndpoint {
offset: self.to_inlay_offset(range.end.to_offset(&self.buffer)),
offset: range.end,
is_start: false,
tag: *tag,
style,
});
}
if let Some(inlay_highlights) = inlay_highlights.get(tag) {
self.push_inlay_highlight_range(
inlay_highlights,
transform_start,
transform_end,
&mut highlight_endpoints,
tag,
);
}
}
for (tag, inlay_highlights) in inlay_highlights
.iter()
.filter(|(tag, _)| !covered_tags.contains(tag))
{
self.push_inlay_highlight_range(
inlay_highlights,
transform_start,
transform_end,
&mut highlight_endpoints,
tag,
);
}
cursor.next(&());
@@ -1104,60 +1082,23 @@ impl InlaySnapshot {
}
}
fn push_inlay_highlight_range(
&self,
inlay_highlights: &std::sync::Arc<(
HighlightStyle,
Vec<crate::link_go_to_definition::InlayRange>,
)>,
transform_start: Anchor,
transform_end: Anchor,
highlight_endpoints: &mut Vec<HighlightEndpoint>,
tag: &Option<TypeId>,
) {
let style = inlay_highlights.0;
let ranges = &inlay_highlights.1;
let start_ix = match ranges
.binary_search_by(|probe| probe.inlay_position.cmp(&transform_start, &self.buffer))
{
Ok(i) | Err(i) => i,
};
for range in &ranges[start_ix..] {
if range
.inlay_position
.cmp(&transform_end, &self.buffer)
.is_ge()
{
break;
fn document_to_inlay_range(&self, range: &DocumentRange) -> Range<InlayOffset> {
match range {
DocumentRange::Text(text_range) => {
self.to_inlay_offset(text_range.start.to_offset(&self.buffer))
..self.to_inlay_offset(text_range.end.to_offset(&self.buffer))
}
DocumentRange::Inlay(inlay_range) => {
inlay_range.highlight_start..inlay_range.highlight_end
}
highlight_endpoints.push(HighlightEndpoint {
offset: range.highlight_start,
is_start: true,
tag: *tag,
style,
});
highlight_endpoints.push(HighlightEndpoint {
offset: range.highlight_end,
is_start: false,
tag: *tag,
style,
});
}
}
#[cfg(test)]
pub fn text(&self) -> String {
self.chunks(
Default::default()..self.len(),
false,
None,
None,
None,
None,
)
.map(|chunk| chunk.text)
.collect()
self.chunks(Default::default()..self.len(), false, None, None, None)
.map(|chunk| chunk.text)
.collect()
}
fn check_invariants(&self) {
@@ -1651,6 +1592,8 @@ mod tests {
.map(|range| {
buffer_snapshot.anchor_before(range.start)..buffer_snapshot.anchor_after(range.end)
})
// TODO add inlay highlight tests
.map(DocumentRange::Text)
.collect::<Vec<_>>();
highlights.insert(
@@ -1731,8 +1674,6 @@ mod tests {
InlayOffset(start)..InlayOffset(end),
false,
Some(&highlights),
// TODO kb add tests
None,
None,
None,
)
+8 -21
View File
@@ -1,6 +1,6 @@
use super::{
fold_map::{self, FoldChunks, FoldEdit, FoldPoint, FoldSnapshot},
InlayHighlights, TextHighlights,
TextHighlights,
};
use crate::MultiBufferSnapshot;
use gpui::fonts::HighlightStyle;
@@ -71,7 +71,6 @@ impl TabMap {
None,
None,
None,
None,
) {
for (ix, _) in chunk.text.match_indices('\t') {
let offset_from_edit = offset_from_edit + (ix as u32);
@@ -184,7 +183,7 @@ impl TabSnapshot {
self.max_point()
};
for c in self
.chunks(range.start..line_end, false, None, None, None, None)
.chunks(range.start..line_end, false, None, None, None)
.flat_map(|chunk| chunk.text.chars())
{
if c == '\n' {
@@ -204,7 +203,6 @@ impl TabSnapshot {
None,
None,
None,
None,
)
.flat_map(|chunk| chunk.text.chars())
{
@@ -225,9 +223,8 @@ impl TabSnapshot {
&'a self,
range: Range<TabPoint>,
language_aware: bool,
// TODO kb extract into one struct
// TODO kb extract into one struct?
text_highlights: Option<&'a TextHighlights>,
inlay_highlights: Option<&'a InlayHighlights>,
inlay_highlight_style: Option<HighlightStyle>,
suggestion_highlight_style: Option<HighlightStyle>,
) -> TabChunks<'a> {
@@ -250,7 +247,6 @@ impl TabSnapshot {
input_start..input_end,
language_aware,
text_highlights,
inlay_highlights,
inlay_highlight_style,
suggestion_highlight_style,
),
@@ -275,16 +271,9 @@ impl TabSnapshot {
#[cfg(test)]
pub fn text(&self) -> String {
self.chunks(
TabPoint::zero()..self.max_point(),
false,
None,
None,
None,
None,
)
.map(|chunk| chunk.text)
.collect()
self.chunks(TabPoint::zero()..self.max_point(), false, None, None, None)
.map(|chunk| chunk.text)
.collect()
}
pub fn max_point(&self) -> TabPoint {
@@ -612,7 +601,6 @@ mod tests {
None,
None,
None,
None,
)
.map(|c| c.text)
.collect::<String>(),
@@ -687,8 +675,7 @@ mod tests {
let mut chunks = Vec::new();
let mut was_tab = false;
let mut text = String::new();
for chunk in snapshot.chunks(start..snapshot.max_point(), false, None, None, None, None)
{
for chunk in snapshot.chunks(start..snapshot.max_point(), false, None, None, None) {
if chunk.is_tab != was_tab {
if !text.is_empty() {
chunks.push((mem::take(&mut text), was_tab));
@@ -757,7 +744,7 @@ mod tests {
let expected_summary = TextSummary::from(expected_text.as_str());
assert_eq!(
tabs_snapshot
.chunks(start..end, false, None, None, None, None)
.chunks(start..end, false, None, None, None)
.map(|c| c.text)
.collect::<String>(),
expected_text,
+2 -6
View File
@@ -1,7 +1,7 @@
use super::{
fold_map::FoldBufferRows,
tab_map::{self, TabEdit, TabPoint, TabSnapshot},
InlayHighlights, TextHighlights,
TextHighlights,
};
use crate::MultiBufferSnapshot;
use gpui::{
@@ -447,7 +447,6 @@ impl WrapSnapshot {
None,
None,
None,
None,
);
let mut edit_transforms = Vec::<Transform>::new();
for _ in edit.new_rows.start..edit.new_rows.end {
@@ -577,7 +576,6 @@ impl WrapSnapshot {
rows: Range<u32>,
language_aware: bool,
text_highlights: Option<&'a TextHighlights>,
inlay_highlights: Option<&'a InlayHighlights>,
inlay_highlight_style: Option<HighlightStyle>,
suggestion_highlight_style: Option<HighlightStyle>,
) -> WrapChunks<'a> {
@@ -597,7 +595,6 @@ impl WrapSnapshot {
input_start..input_end,
language_aware,
text_highlights,
inlay_highlights,
inlay_highlight_style,
suggestion_highlight_style,
),
@@ -1329,7 +1326,6 @@ mod tests {
None,
None,
None,
None,
)
.map(|h| h.text)
}
@@ -1354,7 +1350,7 @@ mod tests {
}
let actual_text = self
.chunks(start_row..end_row, true, None, None, None, None)
.chunks(start_row..end_row, true, None, None, None)
.map(|c| c.text)
.collect::<String>();
assert_eq!(