Skip to content

Commit

Permalink
broken: implement bracket coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed Dec 11, 2024
1 parent 3a33deb commit 50ab0e1
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 62 deletions.
18 changes: 10 additions & 8 deletions crates/editor/src/display_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use inlay_map::{InlayMap, InlaySnapshot};
pub use inlay_map::{InlayOffset, InlayPoint};
use invisibles::{is_invisible, replacement};
use language::{
language_settings::language_settings, ChunkRenderer, OffsetUtf16, Point,
language_settings::language_settings, ChunkKind, ChunkRenderer, OffsetUtf16, Point,
Subscription as BufferSubscription,
};
use lsp::DiagnosticSeverity;
Expand Down Expand Up @@ -547,10 +547,11 @@ pub enum ChunkReplacement {
Str(SharedString),
}

#[derive(Default)]
pub struct HighlightedChunk<'a> {
pub text: &'a str,
pub style: Option<HighlightStyle>,
pub is_tab: bool,
pub kind: ChunkKind,
pub replacement: Option<ChunkReplacement>,
}

Expand All @@ -562,8 +563,9 @@ impl<'a> HighlightedChunk<'a> {
let mut chars = self.text.chars().peekable();
let mut text = self.text;
let style = self.style;
let is_tab = self.is_tab;
let renderer = self.replacement;
let kind = self.kind;

iter::from_fn(move || {
let mut prefix_len = 0;
while let Some(&ch) = chars.peek() {
Expand All @@ -578,7 +580,7 @@ impl<'a> HighlightedChunk<'a> {
return Some(HighlightedChunk {
text: prefix,
style,
is_tab,
kind,
replacement: renderer.clone(),
});
}
Expand All @@ -604,7 +606,7 @@ impl<'a> HighlightedChunk<'a> {
return Some(HighlightedChunk {
text: prefix,
style: Some(invisible_style),
is_tab: false,
kind: ChunkKind::Other,
replacement: Some(ChunkReplacement::Str(replacement.into())),
});
} else {
Expand All @@ -627,7 +629,7 @@ impl<'a> HighlightedChunk<'a> {
return Some(HighlightedChunk {
text: prefix,
style: Some(invisible_style),
is_tab: false,
kind: ChunkKind::Other,
replacement: renderer.clone(),
});
}
Expand All @@ -639,7 +641,7 @@ impl<'a> HighlightedChunk<'a> {
Some(HighlightedChunk {
text: remainder,
style,
is_tab,
kind,
replacement: renderer.clone(),
})
} else {
Expand Down Expand Up @@ -902,7 +904,7 @@ impl DisplaySnapshot {
HighlightedChunk {
text: chunk.text,
style: highlight_style,
is_tab: chunk.is_tab,
kind: chunk.kind,
replacement: chunk.renderer.map(ChunkReplacement::Renderer),
}
.highlight_invisibles(editor_style)
Expand Down
12 changes: 6 additions & 6 deletions crates/editor/src/display_map/tab_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
fold_map::{self, FoldChunks, FoldEdit, FoldPoint, FoldSnapshot},
Highlights,
};
use language::{Chunk, Point};
use language::{Chunk, ChunkKind, Point};
use multi_buffer::MultiBufferSnapshot;
use std::{cmp, mem, num::NonZeroU32, ops::Range};
use sum_tree::Bias;
Expand Down Expand Up @@ -265,7 +265,7 @@ impl TabSnapshot {
tab_size: self.tab_size,
chunk: Chunk {
text: &SPACES[0..(to_next_stop as usize)],
is_tab: true,
kind: ChunkKind::Tab,
..Default::default()
},
inside_leading_tab: to_next_stop > 0,
Expand Down Expand Up @@ -522,7 +522,7 @@ impl<'a> TabChunks<'a> {
self.max_output_position = range.end.0;
self.chunk = Chunk {
text: &SPACES[0..(to_next_stop as usize)],
is_tab: true,
kind: ChunkKind::Tab,
..Default::default()
};
self.inside_leading_tab = to_next_stop > 0;
Expand Down Expand Up @@ -574,7 +574,7 @@ impl<'a> Iterator for TabChunks<'a> {
self.output_position = next_output_position;
return Some(Chunk {
text: &SPACES[..len as usize],
is_tab: true,
kind: ChunkKind::Tab,
..self.chunk.clone()
});
}
Expand Down Expand Up @@ -718,11 +718,11 @@ mod tests {
let mut text = String::new();
for chunk in snapshot.chunks(start..snapshot.max_point(), false, Highlights::default())
{
if chunk.is_tab != was_tab {
if chunk.kind.is_tab() != was_tab {
if !text.is_empty() {
chunks.push((mem::take(&mut text), was_tab));
}
was_tab = chunk.is_tab;
was_tab = chunk.kind.is_tab();
}
text.push_str(chunk.text);
}
Expand Down
27 changes: 19 additions & 8 deletions crates/editor/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use language::{
IndentGuideBackgroundColoring, IndentGuideColoring, IndentGuideSettings,
ShowWhitespaceSetting,
},
ChunkRendererContext,
ChunkKind, ChunkRendererContext,
};
use lsp::DiagnosticSeverity;
use multi_buffer::{
Expand Down Expand Up @@ -4606,12 +4606,11 @@ impl LineWithInvisibles {

let ellipsis = SharedString::from("⋯");

for highlighted_chunk in chunks.chain([HighlightedChunk {
let last_chunk = HighlightedChunk {
text: "\n",
style: None,
is_tab: false,
replacement: None,
}]) {
..HighlightedChunk::default()
};
for highlighted_chunk in chunks.chain([last_chunk]) {
if let Some(replacement) = highlighted_chunk.replacement {
if !line.is_empty() {
let shaped_line = cx
Expand Down Expand Up @@ -4734,10 +4733,22 @@ impl LineWithInvisibles {
line_exceeded_max_len = true;
}

let mut color = text_style.color;
let accents = cx.theme().accents();
// update text color if chunk is a bracket, and bracket coloring is enabled
if let ChunkKind::Bracket { depth } = highlighted_chunk.kind {
// TODO 1: we can't remote negative depth because we can't parse
// files all the way from the beginning, find another approach
// TODO 2: only apply if the bracket coloring setting is enabled
if depth > 0 {
color = accents.color_for_index(depth as u32);
}
}

styles.push(TextRun {
len: line_chunk.len(),
font: text_style.font(),
color: text_style.color,
color,
background_color: text_style.background_color,
underline: text_style.underline,
strikethrough: text_style.strikethrough,
Expand All @@ -4747,7 +4758,7 @@ impl LineWithInvisibles {
// Line wrap pads its contents with fake whitespaces,
// avoid printing them
let is_soft_wrapped = is_row_soft_wrapped(row);
if highlighted_chunk.is_tab {
if highlighted_chunk.kind.is_tab() {
if non_whitespace_added || !is_soft_wrapped {
invisibles.push(Invisible::Tab {
line_start_offset: line.len(),
Expand Down
Loading

0 comments on commit 50ab0e1

Please sign in to comment.