Improve terminal rendering performance (#33345)
Closes #18263 Improvements: • **Batch text rendering** - Combine adjacent cells with identical styling into single text runs to reduce draw calls • **Throttle hyperlink searches** - Limit hyperlink detection to every 100ms or when mouse moves >5px to reduce CPU usage • **Pre-allocate collections** - Use `Vec::with_capacity()` for cells, runs, and regions to minimize reallocations • **Optimize background regions** - Merge adjacent background rectangles to reduce number of draw operations • **Cache selection text** - Only compute terminal selection string when selection exists Release Notes: - Improved terminal rendering performance. --------- Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
parent
bcac748c2b
commit
925464cfc6
9 changed files with 609 additions and 177 deletions
|
@ -1,17 +1,18 @@
|
|||
use crate::color_contrast;
|
||||
use editor::{CursorLayout, HighlightedRange, HighlightedRangeLine};
|
||||
use gpui::{
|
||||
AnyElement, App, AvailableSpace, Bounds, ContentMask, Context, DispatchPhase, Element,
|
||||
ElementId, Entity, FocusHandle, Font, FontStyle, FontWeight, GlobalElementId, HighlightStyle,
|
||||
Hitbox, Hsla, InputHandler, InteractiveElement, Interactivity, IntoElement, LayoutId, Length,
|
||||
ModifiersChangedEvent, MouseButton, MouseMoveEvent, Pixels, Point, ShapedLine,
|
||||
StatefulInteractiveElement, StrikethroughStyle, Styled, TextRun, TextStyle, UTF16Selection,
|
||||
UnderlineStyle, WeakEntity, WhiteSpace, Window, WindowTextSystem, div, fill, point, px,
|
||||
relative, size,
|
||||
AbsoluteLength, AnyElement, App, AvailableSpace, Bounds, ContentMask, Context, DispatchPhase,
|
||||
Element, ElementId, Entity, FocusHandle, Font, FontFeatures, FontStyle, FontWeight,
|
||||
GlobalElementId, HighlightStyle, Hitbox, Hsla, InputHandler, InteractiveElement, Interactivity,
|
||||
IntoElement, LayoutId, Length, ModifiersChangedEvent, MouseButton, MouseMoveEvent, Pixels,
|
||||
Point, ShapedLine, StatefulInteractiveElement, StrikethroughStyle, Styled, TextRun, TextStyle,
|
||||
UTF16Selection, UnderlineStyle, WeakEntity, WhiteSpace, Window, div, fill, point, px, relative,
|
||||
size,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use language::CursorShape;
|
||||
use settings::Settings;
|
||||
use std::time::Instant;
|
||||
use terminal::{
|
||||
IndexedCell, Terminal, TerminalBounds, TerminalContent,
|
||||
alacritty_terminal::{
|
||||
|
@ -38,7 +39,7 @@ use crate::{BlockContext, BlockProperties, ContentMode, TerminalMode, TerminalVi
|
|||
/// The information generated during layout that is necessary for painting.
|
||||
pub struct LayoutState {
|
||||
hitbox: Hitbox,
|
||||
cells: Vec<LayoutCell>,
|
||||
batched_text_runs: Vec<BatchedTextRun>,
|
||||
rects: Vec<LayoutRect>,
|
||||
relative_highlighted_ranges: Vec<(RangeInclusive<AlacPoint>, Hsla)>,
|
||||
cursor: Option<CursorLayout>,
|
||||
|
@ -76,37 +77,69 @@ impl DisplayCursor {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct LayoutCell {
|
||||
pub point: AlacPoint<i32, i32>,
|
||||
text: gpui::ShapedLine,
|
||||
/// A batched text run that combines multiple adjacent cells with the same style
|
||||
#[derive(Debug)]
|
||||
pub struct BatchedTextRun {
|
||||
pub start_point: AlacPoint<i32, i32>,
|
||||
pub text: String,
|
||||
pub cell_count: usize,
|
||||
pub style: TextRun,
|
||||
pub font_size: AbsoluteLength,
|
||||
}
|
||||
|
||||
impl LayoutCell {
|
||||
fn new(point: AlacPoint<i32, i32>, text: gpui::ShapedLine) -> LayoutCell {
|
||||
LayoutCell { point, text }
|
||||
impl BatchedTextRun {
|
||||
fn new_from_char(
|
||||
start_point: AlacPoint<i32, i32>,
|
||||
c: char,
|
||||
style: TextRun,
|
||||
font_size: AbsoluteLength,
|
||||
) -> Self {
|
||||
let mut text = String::with_capacity(100); // Pre-allocate for typical line length
|
||||
text.push(c);
|
||||
BatchedTextRun {
|
||||
start_point,
|
||||
text,
|
||||
cell_count: 1,
|
||||
style,
|
||||
font_size,
|
||||
}
|
||||
}
|
||||
|
||||
fn can_append(&self, other_style: &TextRun) -> bool {
|
||||
self.style.font == other_style.font
|
||||
&& self.style.color == other_style.color
|
||||
&& self.style.background_color == other_style.background_color
|
||||
&& self.style.underline == other_style.underline
|
||||
&& self.style.strikethrough == other_style.strikethrough
|
||||
}
|
||||
|
||||
fn append_char(&mut self, c: char) {
|
||||
self.text.push(c);
|
||||
self.cell_count += 1;
|
||||
self.style.len += c.len_utf8();
|
||||
}
|
||||
|
||||
pub fn paint(
|
||||
&self,
|
||||
origin: Point<Pixels>,
|
||||
dimensions: &TerminalBounds,
|
||||
_visible_bounds: Bounds<Pixels>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let pos = {
|
||||
let point = self.point;
|
||||
let pos = Point::new(
|
||||
(origin.x + self.start_point.column as f32 * dimensions.cell_width).floor(),
|
||||
origin.y + self.start_point.line as f32 * dimensions.line_height,
|
||||
);
|
||||
|
||||
Point::new(
|
||||
(origin.x + point.column as f32 * dimensions.cell_width).floor(),
|
||||
origin.y + point.line as f32 * dimensions.line_height,
|
||||
let _ = window
|
||||
.text_system()
|
||||
.shape_line(
|
||||
self.text.clone().into(),
|
||||
self.font_size.to_pixels(window.rem_size()),
|
||||
&[self.style.clone()],
|
||||
Some(dimensions.cell_width),
|
||||
)
|
||||
};
|
||||
|
||||
self.text
|
||||
.paint(pos, dimensions.line_height, window, cx)
|
||||
.ok();
|
||||
.paint(pos, dimensions.line_height, window, cx);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,14 +159,6 @@ impl LayoutRect {
|
|||
}
|
||||
}
|
||||
|
||||
fn extend(&self) -> Self {
|
||||
LayoutRect {
|
||||
point: self.point,
|
||||
num_of_cells: self.num_of_cells + 1,
|
||||
color: self.color,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn paint(&self, origin: Point<Pixels>, dimensions: &TerminalBounds, window: &mut Window) {
|
||||
let position = {
|
||||
let alac_point = self.point;
|
||||
|
@ -152,6 +177,87 @@ impl LayoutRect {
|
|||
}
|
||||
}
|
||||
|
||||
/// Represents a rectangular region with a specific background color
|
||||
#[derive(Debug, Clone)]
|
||||
struct BackgroundRegion {
|
||||
start_line: i32,
|
||||
start_col: i32,
|
||||
end_line: i32,
|
||||
end_col: i32,
|
||||
color: Hsla,
|
||||
}
|
||||
|
||||
impl BackgroundRegion {
|
||||
fn new(line: i32, col: i32, color: Hsla) -> Self {
|
||||
BackgroundRegion {
|
||||
start_line: line,
|
||||
start_col: col,
|
||||
end_line: line,
|
||||
end_col: col,
|
||||
color,
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if this region can be merged with another region
|
||||
fn can_merge_with(&self, other: &BackgroundRegion) -> bool {
|
||||
if self.color != other.color {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if regions are adjacent horizontally
|
||||
if self.start_line == other.start_line && self.end_line == other.end_line {
|
||||
return self.end_col + 1 == other.start_col || other.end_col + 1 == self.start_col;
|
||||
}
|
||||
|
||||
// Check if regions are adjacent vertically with same column span
|
||||
if self.start_col == other.start_col && self.end_col == other.end_col {
|
||||
return self.end_line + 1 == other.start_line || other.end_line + 1 == self.start_line;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// Merge this region with another region
|
||||
fn merge_with(&mut self, other: &BackgroundRegion) {
|
||||
self.start_line = self.start_line.min(other.start_line);
|
||||
self.start_col = self.start_col.min(other.start_col);
|
||||
self.end_line = self.end_line.max(other.end_line);
|
||||
self.end_col = self.end_col.max(other.end_col);
|
||||
}
|
||||
}
|
||||
|
||||
/// Merge background regions to minimize the number of rectangles
|
||||
fn merge_background_regions(regions: Vec<BackgroundRegion>) -> Vec<BackgroundRegion> {
|
||||
if regions.is_empty() {
|
||||
return regions;
|
||||
}
|
||||
|
||||
let mut merged = regions;
|
||||
let mut changed = true;
|
||||
|
||||
// Keep merging until no more merges are possible
|
||||
while changed {
|
||||
changed = false;
|
||||
let mut i = 0;
|
||||
|
||||
while i < merged.len() {
|
||||
let mut j = i + 1;
|
||||
while j < merged.len() {
|
||||
if merged[i].can_merge_with(&merged[j]) {
|
||||
let other = merged.remove(j);
|
||||
merged[i].merge_with(&other);
|
||||
changed = true;
|
||||
} else {
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
merged
|
||||
}
|
||||
|
||||
/// The GPUI element that paints the terminal.
|
||||
/// We need to keep a reference to the model for mouse events, do we need it for any other terminal stuff, or can we move that to connection?
|
||||
pub struct TerminalElement {
|
||||
|
@ -205,23 +311,37 @@ impl TerminalElement {
|
|||
grid: impl Iterator<Item = IndexedCell>,
|
||||
start_line_offset: i32,
|
||||
text_style: &TextStyle,
|
||||
text_system: &WindowTextSystem,
|
||||
hyperlink: Option<(HighlightStyle, &RangeInclusive<AlacPoint>)>,
|
||||
minimum_contrast: f32,
|
||||
window: &Window,
|
||||
cx: &App,
|
||||
) -> (Vec<LayoutCell>, Vec<LayoutRect>) {
|
||||
) -> (Vec<LayoutRect>, Vec<BatchedTextRun>) {
|
||||
let start_time = Instant::now();
|
||||
let theme = cx.theme();
|
||||
let mut cells = vec![];
|
||||
let mut rects = vec![];
|
||||
|
||||
let mut cur_rect: Option<LayoutRect> = None;
|
||||
let mut cur_alac_color = None;
|
||||
// Pre-allocate with estimated capacity to reduce reallocations
|
||||
let estimated_cells = grid.size_hint().0;
|
||||
let estimated_runs = estimated_cells / 10; // Estimate ~10 cells per run
|
||||
let estimated_regions = estimated_cells / 20; // Estimate ~20 cells per background region
|
||||
|
||||
let mut batched_runs = Vec::with_capacity(estimated_runs);
|
||||
let mut cell_count = 0;
|
||||
|
||||
// Collect background regions for efficient merging
|
||||
let mut background_regions: Vec<BackgroundRegion> = Vec::with_capacity(estimated_regions);
|
||||
let mut current_batch: Option<BatchedTextRun> = None;
|
||||
|
||||
// First pass: collect all cells and their backgrounds
|
||||
let linegroups = grid.into_iter().chunk_by(|i| i.point.line);
|
||||
for (line_index, (_, line)) in linegroups.into_iter().enumerate() {
|
||||
let alac_line = start_line_offset + line_index as i32;
|
||||
|
||||
// Flush any existing batch at line boundaries
|
||||
if let Some(batch) = current_batch.take() {
|
||||
batched_runs.push(batch);
|
||||
}
|
||||
|
||||
let mut previous_cell_had_extras = false;
|
||||
|
||||
for cell in line {
|
||||
let mut fg = cell.fg;
|
||||
let mut bg = cell.bg;
|
||||
|
@ -229,63 +349,43 @@ impl TerminalElement {
|
|||
mem::swap(&mut fg, &mut bg);
|
||||
}
|
||||
|
||||
//Expand background rect range
|
||||
{
|
||||
if matches!(bg, Named(NamedColor::Background)) {
|
||||
//Continue to next cell, resetting variables if necessary
|
||||
cur_alac_color = None;
|
||||
if let Some(rect) = cur_rect {
|
||||
rects.push(rect);
|
||||
cur_rect = None
|
||||
// Collect background regions (skip default background)
|
||||
if !matches!(bg, Named(NamedColor::Background)) {
|
||||
let color = convert_color(&bg, theme);
|
||||
let col = cell.point.column.0 as i32;
|
||||
|
||||
// Try to extend the last region if it's on the same line with the same color
|
||||
if let Some(last_region) = background_regions.last_mut() {
|
||||
if last_region.color == color
|
||||
&& last_region.start_line == alac_line
|
||||
&& last_region.end_line == alac_line
|
||||
&& last_region.end_col + 1 == col
|
||||
{
|
||||
last_region.end_col = col;
|
||||
} else {
|
||||
background_regions.push(BackgroundRegion::new(alac_line, col, color));
|
||||
}
|
||||
} else {
|
||||
match cur_alac_color {
|
||||
Some(cur_color) => {
|
||||
if bg == cur_color {
|
||||
// `cur_rect` can be None if it was moved to the `rects` vec after wrapping around
|
||||
// from one line to the next. The variables are all set correctly but there is no current
|
||||
// rect, so we create one if necessary.
|
||||
cur_rect = cur_rect.map_or_else(
|
||||
|| {
|
||||
Some(LayoutRect::new(
|
||||
AlacPoint::new(
|
||||
alac_line,
|
||||
cell.point.column.0 as i32,
|
||||
),
|
||||
1,
|
||||
convert_color(&bg, theme),
|
||||
))
|
||||
},
|
||||
|rect| Some(rect.extend()),
|
||||
);
|
||||
} else {
|
||||
cur_alac_color = Some(bg);
|
||||
if cur_rect.is_some() {
|
||||
rects.push(cur_rect.take().unwrap());
|
||||
}
|
||||
cur_rect = Some(LayoutRect::new(
|
||||
AlacPoint::new(alac_line, cell.point.column.0 as i32),
|
||||
1,
|
||||
convert_color(&bg, theme),
|
||||
));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
cur_alac_color = Some(bg);
|
||||
cur_rect = Some(LayoutRect::new(
|
||||
AlacPoint::new(alac_line, cell.point.column.0 as i32),
|
||||
1,
|
||||
convert_color(&bg, theme),
|
||||
));
|
||||
}
|
||||
}
|
||||
background_regions.push(BackgroundRegion::new(alac_line, col, color));
|
||||
}
|
||||
}
|
||||
// Skip wide character spacers - they're just placeholders for the second cell of wide characters
|
||||
if cell.flags.contains(Flags::WIDE_CHAR_SPACER) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip spaces that follow cells with extras (emoji variation sequences)
|
||||
if cell.c == ' ' && previous_cell_had_extras {
|
||||
previous_cell_had_extras = false;
|
||||
continue;
|
||||
}
|
||||
// Update tracking for next iteration
|
||||
previous_cell_had_extras = cell.extra.is_some();
|
||||
|
||||
//Layout current cell text
|
||||
{
|
||||
if !is_blank(&cell) {
|
||||
let cell_text = cell.c.to_string();
|
||||
cell_count += 1;
|
||||
let cell_style = TerminalElement::cell_style(
|
||||
&cell,
|
||||
fg,
|
||||
|
@ -296,25 +396,74 @@ impl TerminalElement {
|
|||
minimum_contrast,
|
||||
);
|
||||
|
||||
let layout_cell = text_system.shape_line(
|
||||
cell_text.into(),
|
||||
text_style.font_size.to_pixels(window.rem_size()),
|
||||
&[cell_style],
|
||||
);
|
||||
let cell_point = AlacPoint::new(alac_line, cell.point.column.0 as i32);
|
||||
|
||||
cells.push(LayoutCell::new(
|
||||
AlacPoint::new(alac_line, cell.point.column.0 as i32),
|
||||
layout_cell,
|
||||
))
|
||||
// Try to batch with existing run
|
||||
if let Some(ref mut batch) = current_batch {
|
||||
if batch.can_append(&cell_style)
|
||||
&& batch.start_point.line == cell_point.line
|
||||
&& batch.start_point.column + batch.cell_count as i32
|
||||
== cell_point.column
|
||||
{
|
||||
batch.append_char(cell.c);
|
||||
} else {
|
||||
// Flush current batch and start new one
|
||||
let old_batch = current_batch.take().unwrap();
|
||||
batched_runs.push(old_batch);
|
||||
current_batch = Some(BatchedTextRun::new_from_char(
|
||||
cell_point,
|
||||
cell.c,
|
||||
cell_style,
|
||||
text_style.font_size,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// Start new batch
|
||||
current_batch = Some(BatchedTextRun::new_from_char(
|
||||
cell_point,
|
||||
cell.c,
|
||||
cell_style,
|
||||
text_style.font_size,
|
||||
));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cur_rect.is_some() {
|
||||
rects.push(cur_rect.take().unwrap());
|
||||
// Flush any remaining batch
|
||||
if let Some(batch) = current_batch {
|
||||
batched_runs.push(batch);
|
||||
}
|
||||
|
||||
// Second pass: merge background regions and convert to layout rects
|
||||
let region_count = background_regions.len();
|
||||
let merged_regions = merge_background_regions(background_regions);
|
||||
let mut rects = Vec::with_capacity(merged_regions.len() * 2); // Estimate 2 rects per merged region
|
||||
|
||||
// Convert merged regions to layout rects
|
||||
// Since LayoutRect only supports single-line rectangles, we need to split multi-line regions
|
||||
for region in merged_regions {
|
||||
for line in region.start_line..=region.end_line {
|
||||
rects.push(LayoutRect::new(
|
||||
AlacPoint::new(line, region.start_col),
|
||||
(region.end_col - region.start_col + 1) as usize,
|
||||
region.color,
|
||||
));
|
||||
}
|
||||
}
|
||||
(cells, rects)
|
||||
|
||||
let layout_time = start_time.elapsed();
|
||||
log::debug!(
|
||||
"Terminal layout_grid: {} cells processed, {} batched runs created, {} rects (from {} merged regions), layout took {:?}",
|
||||
cell_count,
|
||||
batched_runs.len(),
|
||||
rects.len(),
|
||||
region_count,
|
||||
layout_time
|
||||
);
|
||||
|
||||
(rects, batched_runs)
|
||||
}
|
||||
|
||||
/// Computes the cursor position and expected block width, may return a zero width if x_for_index returns
|
||||
|
@ -708,7 +857,7 @@ impl Element for TerminalElement {
|
|||
let font_features = terminal_settings
|
||||
.font_features
|
||||
.as_ref()
|
||||
.unwrap_or(&settings.buffer_font.features)
|
||||
.unwrap_or(&FontFeatures::disable_ligatures())
|
||||
.clone();
|
||||
|
||||
let font_weight = terminal_settings.font_weight.unwrap_or_default();
|
||||
|
@ -857,19 +1006,22 @@ impl Element for TerminalElement {
|
|||
// then have that representation be converted to the appropriate highlight data structure
|
||||
|
||||
let content_mode = self.terminal_view.read(cx).content_mode(window, cx);
|
||||
let (cells, rects) = match content_mode {
|
||||
ContentMode::Scrollable => TerminalElement::layout_grid(
|
||||
cells.iter().cloned(),
|
||||
0,
|
||||
&text_style,
|
||||
window.text_system(),
|
||||
last_hovered_word
|
||||
.as_ref()
|
||||
.map(|last_hovered_word| (link_style, &last_hovered_word.word_match)),
|
||||
minimum_contrast,
|
||||
window,
|
||||
cx,
|
||||
),
|
||||
let (rects, batched_text_runs) = match content_mode {
|
||||
ContentMode::Scrollable => {
|
||||
// In scrollable mode, the terminal already provides cells
|
||||
// that are correctly positioned for the current viewport
|
||||
// based on its display_offset. We don't need additional filtering.
|
||||
TerminalElement::layout_grid(
|
||||
cells.iter().cloned(),
|
||||
0,
|
||||
&text_style,
|
||||
last_hovered_word.as_ref().map(|last_hovered_word| {
|
||||
(link_style, &last_hovered_word.word_match)
|
||||
}),
|
||||
minimum_contrast,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
ContentMode::Inline { .. } => {
|
||||
let intersection = window.content_mask().bounds.intersect(&bounds);
|
||||
let start_row = (intersection.top() - bounds.top()) / line_height_px;
|
||||
|
@ -884,12 +1036,10 @@ impl Element for TerminalElement {
|
|||
.cloned(),
|
||||
*line_range.start(),
|
||||
&text_style,
|
||||
window.text_system(),
|
||||
last_hovered_word.as_ref().map(|last_hovered_word| {
|
||||
(link_style, &last_hovered_word.word_match)
|
||||
}),
|
||||
minimum_contrast,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
|
@ -915,6 +1065,7 @@ impl Element for TerminalElement {
|
|||
underline: Default::default(),
|
||||
strikethrough: None,
|
||||
}],
|
||||
None,
|
||||
)
|
||||
};
|
||||
|
||||
|
@ -977,7 +1128,7 @@ impl Element for TerminalElement {
|
|||
|
||||
LayoutState {
|
||||
hitbox,
|
||||
cells,
|
||||
batched_text_runs,
|
||||
cursor,
|
||||
background_color,
|
||||
dimensions,
|
||||
|
@ -1005,6 +1156,7 @@ impl Element for TerminalElement {
|
|||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let paint_start = Instant::now();
|
||||
window.with_content_mask(Some(ContentMask { bounds }), |window| {
|
||||
let scroll_top = self.terminal_view.read(cx).scroll_top;
|
||||
|
||||
|
@ -1089,9 +1241,12 @@ impl Element for TerminalElement {
|
|||
}
|
||||
}
|
||||
|
||||
for cell in &layout.cells {
|
||||
cell.paint(origin, &layout.dimensions, bounds, window, cx);
|
||||
// Paint batched text runs instead of individual cells
|
||||
let text_paint_start = Instant::now();
|
||||
for batch in &layout.batched_text_runs {
|
||||
batch.paint(origin, &layout.dimensions, window, cx);
|
||||
}
|
||||
let text_paint_time = text_paint_start.elapsed();
|
||||
|
||||
if let Some(text_to_mark) = &marked_text_cloned {
|
||||
if !text_to_mark.is_empty() {
|
||||
|
@ -1115,6 +1270,7 @@ impl Element for TerminalElement {
|
|||
underline: ime_style.underline,
|
||||
strikethrough: None,
|
||||
}],
|
||||
None
|
||||
);
|
||||
shaped_line
|
||||
.paint(ime_position, layout.dimensions.line_height, window, cx)
|
||||
|
@ -1136,6 +1292,14 @@ impl Element for TerminalElement {
|
|||
if let Some(mut element) = hyperlink_tooltip {
|
||||
element.paint(window, cx);
|
||||
}
|
||||
let total_paint_time = paint_start.elapsed();
|
||||
log::debug!(
|
||||
"Terminal paint: {} text runs, {} rects, text paint took {:?}, total paint took {:?}",
|
||||
layout.batched_text_runs.len(),
|
||||
layout.rects.len(),
|
||||
text_paint_time,
|
||||
total_paint_time
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
@ -1290,7 +1454,7 @@ pub fn is_blank(cell: &IndexedCell) -> bool {
|
|||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
return true;
|
||||
}
|
||||
|
||||
fn to_highlighted_range_lines(
|
||||
|
@ -1409,6 +1573,7 @@ pub fn convert_color(fg: &terminal::alacritty_terminal::vte::ansi::Color, theme:
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use gpui::{AbsoluteLength, Hsla, font};
|
||||
|
||||
#[test]
|
||||
fn test_contrast_adjustment_logic() {
|
||||
|
@ -1523,4 +1688,203 @@ mod tests {
|
|||
new_contrast
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_batched_text_run_can_append() {
|
||||
let style1 = TextRun {
|
||||
len: 1,
|
||||
font: font("Helvetica"),
|
||||
color: Hsla::red(),
|
||||
background_color: None,
|
||||
underline: None,
|
||||
strikethrough: None,
|
||||
};
|
||||
|
||||
let style2 = TextRun {
|
||||
len: 1,
|
||||
font: font("Helvetica"),
|
||||
color: Hsla::red(),
|
||||
background_color: None,
|
||||
underline: None,
|
||||
strikethrough: None,
|
||||
};
|
||||
|
||||
let style3 = TextRun {
|
||||
len: 1,
|
||||
font: font("Helvetica"),
|
||||
color: Hsla::blue(), // Different color
|
||||
background_color: None,
|
||||
underline: None,
|
||||
strikethrough: None,
|
||||
};
|
||||
|
||||
let font_size = AbsoluteLength::Pixels(px(12.0));
|
||||
let batch =
|
||||
BatchedTextRun::new_from_char(AlacPoint::new(0, 0), 'a', style1.clone(), font_size);
|
||||
|
||||
// Should be able to append same style
|
||||
assert!(batch.can_append(&style2));
|
||||
|
||||
// Should not be able to append different style
|
||||
assert!(!batch.can_append(&style3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_batched_text_run_append() {
|
||||
let style = TextRun {
|
||||
len: 1,
|
||||
font: font("Helvetica"),
|
||||
color: Hsla::red(),
|
||||
background_color: None,
|
||||
underline: None,
|
||||
strikethrough: None,
|
||||
};
|
||||
|
||||
let font_size = AbsoluteLength::Pixels(px(12.0));
|
||||
let mut batch = BatchedTextRun::new_from_char(AlacPoint::new(0, 0), 'a', style, font_size);
|
||||
|
||||
assert_eq!(batch.text, "a");
|
||||
assert_eq!(batch.cell_count, 1);
|
||||
assert_eq!(batch.style.len, 1);
|
||||
|
||||
batch.append_char('b');
|
||||
|
||||
assert_eq!(batch.text, "ab");
|
||||
assert_eq!(batch.cell_count, 2);
|
||||
assert_eq!(batch.style.len, 2);
|
||||
|
||||
batch.append_char('c');
|
||||
|
||||
assert_eq!(batch.text, "abc");
|
||||
assert_eq!(batch.cell_count, 3);
|
||||
assert_eq!(batch.style.len, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_batched_text_run_append_char() {
|
||||
let style = TextRun {
|
||||
len: 1,
|
||||
font: font("Helvetica"),
|
||||
color: Hsla::red(),
|
||||
background_color: None,
|
||||
underline: None,
|
||||
strikethrough: None,
|
||||
};
|
||||
|
||||
let font_size = AbsoluteLength::Pixels(px(12.0));
|
||||
let mut batch = BatchedTextRun::new_from_char(AlacPoint::new(0, 0), 'x', style, font_size);
|
||||
|
||||
assert_eq!(batch.text, "x");
|
||||
assert_eq!(batch.cell_count, 1);
|
||||
assert_eq!(batch.style.len, 1);
|
||||
|
||||
batch.append_char('y');
|
||||
|
||||
assert_eq!(batch.text, "xy");
|
||||
assert_eq!(batch.cell_count, 2);
|
||||
assert_eq!(batch.style.len, 2);
|
||||
|
||||
// Test with multi-byte character
|
||||
batch.append_char('😀');
|
||||
|
||||
assert_eq!(batch.text, "xy😀");
|
||||
assert_eq!(batch.cell_count, 3);
|
||||
assert_eq!(batch.style.len, 6); // 1 + 1 + 4 bytes for emoji
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_background_region_can_merge() {
|
||||
let color1 = Hsla::red();
|
||||
let color2 = Hsla::blue();
|
||||
|
||||
// Test horizontal merging
|
||||
let mut region1 = BackgroundRegion::new(0, 0, color1);
|
||||
region1.end_col = 5;
|
||||
let region2 = BackgroundRegion::new(0, 6, color1);
|
||||
assert!(region1.can_merge_with(®ion2));
|
||||
|
||||
// Test vertical merging with same column span
|
||||
let mut region3 = BackgroundRegion::new(0, 0, color1);
|
||||
region3.end_col = 5;
|
||||
let mut region4 = BackgroundRegion::new(1, 0, color1);
|
||||
region4.end_col = 5;
|
||||
assert!(region3.can_merge_with(®ion4));
|
||||
|
||||
// Test cannot merge different colors
|
||||
let region5 = BackgroundRegion::new(0, 0, color1);
|
||||
let region6 = BackgroundRegion::new(0, 1, color2);
|
||||
assert!(!region5.can_merge_with(®ion6));
|
||||
|
||||
// Test cannot merge non-adjacent regions
|
||||
let region7 = BackgroundRegion::new(0, 0, color1);
|
||||
let region8 = BackgroundRegion::new(0, 2, color1);
|
||||
assert!(!region7.can_merge_with(®ion8));
|
||||
|
||||
// Test cannot merge vertical regions with different column spans
|
||||
let mut region9 = BackgroundRegion::new(0, 0, color1);
|
||||
region9.end_col = 5;
|
||||
let mut region10 = BackgroundRegion::new(1, 0, color1);
|
||||
region10.end_col = 6;
|
||||
assert!(!region9.can_merge_with(®ion10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_background_region_merge() {
|
||||
let color = Hsla::red();
|
||||
|
||||
// Test horizontal merge
|
||||
let mut region1 = BackgroundRegion::new(0, 0, color);
|
||||
region1.end_col = 5;
|
||||
let mut region2 = BackgroundRegion::new(0, 6, color);
|
||||
region2.end_col = 10;
|
||||
region1.merge_with(®ion2);
|
||||
assert_eq!(region1.start_col, 0);
|
||||
assert_eq!(region1.end_col, 10);
|
||||
assert_eq!(region1.start_line, 0);
|
||||
assert_eq!(region1.end_line, 0);
|
||||
|
||||
// Test vertical merge
|
||||
let mut region3 = BackgroundRegion::new(0, 0, color);
|
||||
region3.end_col = 5;
|
||||
let mut region4 = BackgroundRegion::new(1, 0, color);
|
||||
region4.end_col = 5;
|
||||
region3.merge_with(®ion4);
|
||||
assert_eq!(region3.start_col, 0);
|
||||
assert_eq!(region3.end_col, 5);
|
||||
assert_eq!(region3.start_line, 0);
|
||||
assert_eq!(region3.end_line, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_merge_background_regions() {
|
||||
let color = Hsla::red();
|
||||
|
||||
// Test merging multiple adjacent regions
|
||||
let regions = vec![
|
||||
BackgroundRegion::new(0, 0, color),
|
||||
BackgroundRegion::new(0, 1, color),
|
||||
BackgroundRegion::new(0, 2, color),
|
||||
BackgroundRegion::new(1, 0, color),
|
||||
BackgroundRegion::new(1, 1, color),
|
||||
BackgroundRegion::new(1, 2, color),
|
||||
];
|
||||
|
||||
let merged = merge_background_regions(regions);
|
||||
assert_eq!(merged.len(), 1);
|
||||
assert_eq!(merged[0].start_line, 0);
|
||||
assert_eq!(merged[0].end_line, 1);
|
||||
assert_eq!(merged[0].start_col, 0);
|
||||
assert_eq!(merged[0].end_col, 2);
|
||||
|
||||
// Test with non-mergeable regions
|
||||
let color2 = Hsla::blue();
|
||||
let regions2 = vec![
|
||||
BackgroundRegion::new(0, 0, color),
|
||||
BackgroundRegion::new(0, 2, color), // Gap at column 1
|
||||
BackgroundRegion::new(1, 0, color2), // Different color
|
||||
];
|
||||
|
||||
let merged2 = merge_background_regions(regions2);
|
||||
assert_eq!(merged2.len(), 3);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue