Support highlighting in blocks
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
co-authored by
Nathan Sobo
parent
0f1eb3dd2e
commit
0a6293bcda
@@ -15,6 +15,7 @@ use std::{
|
||||
},
|
||||
};
|
||||
use sum_tree::SumTree;
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
pub struct BlockMap {
|
||||
buffer: ModelHandle<Buffer>,
|
||||
@@ -459,12 +460,12 @@ impl<'a> BlockMapWriter<'a> {
|
||||
impl BlockSnapshot {
|
||||
#[cfg(test)]
|
||||
fn text(&mut self) -> String {
|
||||
self.chunks(0..self.transforms.summary().output_rows, false)
|
||||
self.chunks(0..self.transforms.summary().output_rows, None)
|
||||
.map(|chunk| chunk.text)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn chunks(&self, rows: Range<u32>, highlights: bool) -> Chunks {
|
||||
pub fn chunks<'a>(&'a self, rows: Range<u32>, theme: Option<&'a SyntaxTheme>) -> Chunks<'a> {
|
||||
let max_output_row = cmp::min(rows.end, self.transforms.summary().output_rows);
|
||||
let mut cursor = self.transforms.cursor::<(BlockRow, WrapRow)>();
|
||||
let input_end = {
|
||||
@@ -492,9 +493,7 @@ impl BlockSnapshot {
|
||||
cursor.start().1 .0 + overshoot
|
||||
};
|
||||
Chunks {
|
||||
input_chunks: self
|
||||
.wrap_snapshot
|
||||
.chunks(input_start..input_end, highlights),
|
||||
input_chunks: self.wrap_snapshot.chunks(input_start..input_end, theme),
|
||||
input_chunk: Default::default(),
|
||||
block_chunks: None,
|
||||
transforms: cursor,
|
||||
@@ -785,9 +784,9 @@ impl<'a> Iterator for BlockChunks<'a> {
|
||||
|
||||
let chunk = self.chunk?;
|
||||
let mut chunk_len = chunk.len();
|
||||
// let mut highlight_style = None;
|
||||
if let Some((run_len, _)) = self.runs.peek() {
|
||||
// highlight_style = Some(style.clone());
|
||||
let mut highlight_style = None;
|
||||
if let Some((run_len, style)) = self.runs.peek() {
|
||||
highlight_style = Some(style.clone());
|
||||
let run_end_in_chunk = self.run_start + run_len - self.offset;
|
||||
if run_end_in_chunk <= chunk_len {
|
||||
chunk_len = run_end_in_chunk;
|
||||
@@ -806,7 +805,7 @@ impl<'a> Iterator for BlockChunks<'a> {
|
||||
|
||||
Some(Chunk {
|
||||
text: chunk,
|
||||
highlight_id: Default::default(),
|
||||
highlight_style,
|
||||
diagnostic: None,
|
||||
})
|
||||
}
|
||||
@@ -1314,7 +1313,7 @@ mod tests {
|
||||
for start_row in 0..expected_row_count {
|
||||
let expected_text = expected_lines[start_row..].join("\n");
|
||||
let actual_text = blocks_snapshot
|
||||
.chunks(start_row as u32..expected_row_count as u32, false)
|
||||
.chunks(start_row as u32..expected_row_count as u32, None)
|
||||
.map(|chunk| chunk.text)
|
||||
.collect::<String>();
|
||||
assert_eq!(
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use gpui::{AppContext, ModelHandle};
|
||||
use language::{
|
||||
Anchor, AnchorRangeExt, Buffer, Chunk, HighlightId, Point, PointUtf16, TextSummary, ToOffset,
|
||||
};
|
||||
use language::{Anchor, AnchorRangeExt, Buffer, Chunk, Point, PointUtf16, TextSummary, ToOffset};
|
||||
use parking_lot::Mutex;
|
||||
use std::{
|
||||
cmp::{self, Ordering},
|
||||
@@ -10,6 +8,7 @@ use std::{
|
||||
sync::atomic::{AtomicUsize, Ordering::SeqCst},
|
||||
};
|
||||
use sum_tree::{Bias, Cursor, FilterCursor, SumTree};
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
pub trait ToFoldPoint {
|
||||
fn to_fold_point(&self, snapshot: &Snapshot, bias: Bias) -> FoldPoint;
|
||||
@@ -498,7 +497,7 @@ pub struct Snapshot {
|
||||
impl Snapshot {
|
||||
#[cfg(test)]
|
||||
pub fn text(&self) -> String {
|
||||
self.chunks(FoldOffset(0)..self.len(), false)
|
||||
self.chunks(FoldOffset(0)..self.len(), None)
|
||||
.map(|c| c.text)
|
||||
.collect()
|
||||
}
|
||||
@@ -630,11 +629,15 @@ impl Snapshot {
|
||||
|
||||
pub fn chars_at(&self, start: FoldPoint) -> impl '_ + Iterator<Item = char> {
|
||||
let start = start.to_offset(self);
|
||||
self.chunks(start..self.len(), false)
|
||||
self.chunks(start..self.len(), None)
|
||||
.flat_map(|chunk| chunk.text.chars())
|
||||
}
|
||||
|
||||
pub fn chunks(&self, range: Range<FoldOffset>, enable_highlights: bool) -> Chunks {
|
||||
pub fn chunks<'a>(
|
||||
&'a self,
|
||||
range: Range<FoldOffset>,
|
||||
theme: Option<&'a SyntaxTheme>,
|
||||
) -> Chunks<'a> {
|
||||
let mut transform_cursor = self.transforms.cursor::<(FoldOffset, usize)>();
|
||||
|
||||
transform_cursor.seek(&range.end, Bias::Right, &());
|
||||
@@ -647,9 +650,7 @@ impl Snapshot {
|
||||
|
||||
Chunks {
|
||||
transform_cursor,
|
||||
buffer_chunks: self
|
||||
.buffer_snapshot
|
||||
.chunks(buffer_start..buffer_end, enable_highlights),
|
||||
buffer_chunks: self.buffer_snapshot.chunks(buffer_start..buffer_end, theme),
|
||||
buffer_chunk: None,
|
||||
buffer_offset: buffer_start,
|
||||
output_offset: range.start.0,
|
||||
@@ -974,7 +975,7 @@ impl<'a> Iterator for Chunks<'a> {
|
||||
self.output_offset += output_text.len();
|
||||
return Some(Chunk {
|
||||
text: output_text,
|
||||
highlight_id: HighlightId::default(),
|
||||
highlight_style: None,
|
||||
diagnostic: None,
|
||||
});
|
||||
}
|
||||
@@ -1384,7 +1385,7 @@ mod tests {
|
||||
log::info!("slicing {:?}..{:?} (text: {:?})", start, end, text);
|
||||
assert_eq!(
|
||||
snapshot
|
||||
.chunks(start..end, false)
|
||||
.chunks(start..end, None)
|
||||
.map(|c| c.text)
|
||||
.collect::<String>(),
|
||||
text,
|
||||
|
||||
@@ -4,6 +4,7 @@ use language::{rope, Chunk};
|
||||
use parking_lot::Mutex;
|
||||
use std::{cmp, mem, ops::Range};
|
||||
use sum_tree::Bias;
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
pub struct TabMap(Mutex<Snapshot>);
|
||||
|
||||
@@ -33,7 +34,7 @@ impl TabMap {
|
||||
let mut delta = 0;
|
||||
for chunk in old_snapshot
|
||||
.fold_snapshot
|
||||
.chunks(fold_edit.old_bytes.end..max_offset, false)
|
||||
.chunks(fold_edit.old_bytes.end..max_offset, None)
|
||||
{
|
||||
let patterns: &[_] = &['\t', '\n'];
|
||||
if let Some(ix) = chunk.text.find(patterns) {
|
||||
@@ -116,7 +117,7 @@ impl Snapshot {
|
||||
self.max_point()
|
||||
};
|
||||
for c in self
|
||||
.chunks(range.start..line_end, false)
|
||||
.chunks(range.start..line_end, None)
|
||||
.flat_map(|chunk| chunk.text.chars())
|
||||
{
|
||||
if c == '\n' {
|
||||
@@ -130,7 +131,7 @@ impl Snapshot {
|
||||
last_line_chars = first_line_chars;
|
||||
} else {
|
||||
for _ in self
|
||||
.chunks(TabPoint::new(range.end.row(), 0)..range.end, false)
|
||||
.chunks(TabPoint::new(range.end.row(), 0)..range.end, None)
|
||||
.flat_map(|chunk| chunk.text.chars())
|
||||
{
|
||||
last_line_chars += 1;
|
||||
@@ -150,7 +151,11 @@ impl Snapshot {
|
||||
self.fold_snapshot.version
|
||||
}
|
||||
|
||||
pub fn chunks(&self, range: Range<TabPoint>, highlights: bool) -> Chunks {
|
||||
pub fn chunks<'a>(
|
||||
&'a self,
|
||||
range: Range<TabPoint>,
|
||||
theme: Option<&'a SyntaxTheme>,
|
||||
) -> Chunks<'a> {
|
||||
let (input_start, expanded_char_column, to_next_stop) =
|
||||
self.to_fold_point(range.start, Bias::Left);
|
||||
let input_start = input_start.to_offset(&self.fold_snapshot);
|
||||
@@ -165,9 +170,7 @@ impl Snapshot {
|
||||
};
|
||||
|
||||
Chunks {
|
||||
fold_chunks: self
|
||||
.fold_snapshot
|
||||
.chunks(input_start..input_end, highlights),
|
||||
fold_chunks: self.fold_snapshot.chunks(input_start..input_end, theme),
|
||||
column: expanded_char_column,
|
||||
output_position: range.start.0,
|
||||
max_output_position: range.end.0,
|
||||
@@ -186,7 +189,7 @@ impl Snapshot {
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn text(&self) -> String {
|
||||
self.chunks(TabPoint::zero()..self.max_point(), false)
|
||||
self.chunks(TabPoint::zero()..self.max_point(), None)
|
||||
.map(|chunk| chunk.text)
|
||||
.collect()
|
||||
}
|
||||
@@ -502,7 +505,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
expected_text,
|
||||
tabs_snapshot
|
||||
.chunks(start..end, false)
|
||||
.chunks(start..end, None)
|
||||
.map(|c| c.text)
|
||||
.collect::<String>()
|
||||
);
|
||||
|
||||
@@ -12,6 +12,7 @@ use lazy_static::lazy_static;
|
||||
use smol::future::yield_now;
|
||||
use std::{collections::VecDeque, mem, ops::Range, time::Duration};
|
||||
use sum_tree::{Bias, Cursor, SumTree};
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
pub use super::tab_map::TextSummary;
|
||||
pub type Edit = buffer::Edit<u32>;
|
||||
@@ -427,7 +428,7 @@ impl Snapshot {
|
||||
let mut remaining = None;
|
||||
let mut chunks = new_tab_snapshot.chunks(
|
||||
TabPoint::new(edit.new_rows.start, 0)..new_tab_snapshot.max_point(),
|
||||
false,
|
||||
None,
|
||||
);
|
||||
let mut edit_transforms = Vec::<Transform>::new();
|
||||
for _ in edit.new_rows.start..edit.new_rows.end {
|
||||
@@ -553,11 +554,11 @@ impl Snapshot {
|
||||
}
|
||||
|
||||
pub fn text_chunks(&self, wrap_row: u32) -> impl Iterator<Item = &str> {
|
||||
self.chunks(wrap_row..self.max_point().row() + 1, false)
|
||||
self.chunks(wrap_row..self.max_point().row() + 1, None)
|
||||
.map(|h| h.text)
|
||||
}
|
||||
|
||||
pub fn chunks(&self, rows: Range<u32>, highlights: bool) -> Chunks {
|
||||
pub fn chunks<'a>(&'a self, rows: Range<u32>, theme: Option<&'a SyntaxTheme>) -> Chunks<'a> {
|
||||
let output_start = WrapPoint::new(rows.start, 0);
|
||||
let output_end = WrapPoint::new(rows.end, 0);
|
||||
let mut transforms = self.transforms.cursor::<(WrapPoint, TabPoint)>();
|
||||
@@ -570,7 +571,7 @@ impl Snapshot {
|
||||
.to_tab_point(output_end)
|
||||
.min(self.tab_snapshot.max_point());
|
||||
Chunks {
|
||||
input_chunks: self.tab_snapshot.chunks(input_start..input_end, highlights),
|
||||
input_chunks: self.tab_snapshot.chunks(input_start..input_end, theme),
|
||||
input_chunk: Default::default(),
|
||||
output_position: output_start,
|
||||
max_output_row: rows.end,
|
||||
@@ -1233,7 +1234,7 @@ mod tests {
|
||||
}
|
||||
|
||||
let actual_text = self
|
||||
.chunks(start_row..end_row, false)
|
||||
.chunks(start_row..end_row, None)
|
||||
.map(|c| c.text)
|
||||
.collect::<String>();
|
||||
assert_eq!(
|
||||
|
||||
Reference in New Issue
Block a user