Search rendering and basic regex stuff complete

This commit is contained in:
Mikayla Maki 2022-08-31 16:17:17 -07:00
parent 8e7d9cf22e
commit 63d9d29762
3 changed files with 155 additions and 185 deletions

View file

@ -2,7 +2,6 @@ use alacritty_terminal::{
ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor},
grid::Dimensions,
index::Point,
selection::SelectionRange,
term::{
cell::{Cell, Flags},
TermMode,
@ -27,7 +26,7 @@ use settings::Settings;
use theme::TerminalStyle;
use util::ResultExt;
use std::fmt::Debug;
use std::{fmt::Debug, ops::RangeInclusive};
use std::{
mem,
ops::{Deref, Range},
@ -43,12 +42,12 @@ use crate::{
pub struct LayoutState {
cells: Vec<LayoutCell>,
rects: Vec<LayoutRect>,
selections: Vec<RelativeHighlightedRange>,
relative_highlighted_ranges: Vec<(RangeInclusive<Point>, Color)>,
cursor: Option<Cursor>,
background_color: Color,
selection_color: Color,
size: TerminalSize,
mode: TermMode,
display_offset: usize,
}
#[derive(Debug)]
@ -166,30 +165,6 @@ impl LayoutRect {
}
}
#[derive(Clone, Debug, Default)]
struct RelativeHighlightedRange {
line_index: usize,
range: Range<usize>,
}
impl RelativeHighlightedRange {
fn new(line_index: usize, range: Range<usize>) -> Self {
RelativeHighlightedRange { line_index, range }
}
fn to_highlighted_range_line(
&self,
origin: Vector2F,
layout: &LayoutState,
) -> HighlightedRangeLine {
let start_x = origin.x() + self.range.start as f32 * layout.size.cell_width;
let end_x =
origin.x() + self.range.end as f32 * layout.size.cell_width + layout.size.cell_width;
HighlightedRangeLine { start_x, end_x }
}
}
///The GPUI element that paints the terminal.
///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection?
pub struct TerminalElement {
@ -217,6 +192,8 @@ impl TerminalElement {
}
}
//Vec<Range<Point>> -> Clip out the parts of the ranges
fn layout_grid(
grid: Vec<IndexedCell>,
text_style: &TextStyle,
@ -224,41 +201,22 @@ impl TerminalElement {
text_layout_cache: &TextLayoutCache,
font_cache: &FontCache,
modal: bool,
selection_range: Option<SelectionRange>,
) -> (
Vec<LayoutCell>,
Vec<LayoutRect>,
Vec<RelativeHighlightedRange>,
) {
) -> (Vec<LayoutCell>, Vec<LayoutRect>) {
let mut cells = vec![];
let mut rects = vec![];
let mut highlight_ranges = vec![];
let mut cur_rect: Option<LayoutRect> = None;
let mut cur_alac_color = None;
let mut highlighted_range = None;
let linegroups = grid.into_iter().group_by(|i| i.point.line);
for (line_index, (_, line)) in linegroups.into_iter().enumerate() {
for (x_index, cell) in line.enumerate() {
for cell in line {
let mut fg = cell.fg;
let mut bg = cell.bg;
if cell.flags.contains(Flags::INVERSE) {
mem::swap(&mut fg, &mut bg);
}
//Increase selection range
{
if selection_range
.map(|range| range.contains(cell.point))
.unwrap_or(false)
{
let mut range = highlighted_range.take().unwrap_or(x_index..x_index);
range.end = range.end.max(x_index);
highlighted_range = Some(range);
}
}
//Expand background rect range
{
if matches!(bg, Named(NamedColor::Background)) {
@ -324,18 +282,11 @@ impl TerminalElement {
};
}
if highlighted_range.is_some() {
highlight_ranges.push(RelativeHighlightedRange::new(
line_index,
highlighted_range.take().unwrap(),
))
}
if cur_rect.is_some() {
rects.push(cur_rect.take().unwrap());
}
}
(cells, rects, highlight_ranges)
(cells, rects)
}
// Compute the cursor position and expected block width, may return a zero width if x_for_index returns
@ -612,6 +563,7 @@ impl Element for TerminalElement {
let terminal_theme = settings.theme.terminal.clone(); //TODO: Try to minimize this clone.
let text_style = TerminalElement::make_text_style(font_cache, settings);
let selection_color = settings.theme.editor.selection.selection;
let match_color = settings.theme.search.match_background;
let dimensions = {
let line_height = font_cache.line_height(text_style.font_size);
let cell_width = font_cache.em_advance(text_style.font_id, text_style.font_size);
@ -624,13 +576,13 @@ impl Element for TerminalElement {
terminal_theme.colors.background
};
let (cells, selection, cursor, display_offset, cursor_text, searcher, mode) = self
let (cells, selection, cursor, display_offset, cursor_text, search_matches, mode) = self
.terminal
.upgrade(cx)
.unwrap()
.update(cx.app, |terminal, mcx| {
.update(cx.app, |terminal, cx| {
terminal.set_size(dimensions);
terminal.render_lock(mcx, |content, cursor_text, searcher| {
terminal.render_lock(cx, |content, cursor_text, search_matches| {
let mut cells = vec![];
cells.extend(
content
@ -653,20 +605,30 @@ impl Element for TerminalElement {
content.cursor,
content.display_offset,
cursor_text,
searcher,
search_matches.clone(),
content.mode,
)
})
});
let (cells, rects, selections) = TerminalElement::layout_grid(
// searches, highlights to a single range representations
let mut relative_highlighted_ranges = Vec::new();
if let Some(selection) = selection {
relative_highlighted_ranges.push((selection.start..=selection.end, selection_color));
}
for search_match in search_matches {
relative_highlighted_ranges.push((search_match, match_color))
}
// then have that representation be converted to the appropriate highlight data structure
let (cells, rects) = TerminalElement::layout_grid(
cells,
&text_style,
&terminal_theme,
cx.text_layout_cache,
cx.font_cache(),
self.modal,
selection,
);
//Layout cursor. Rectangle is used for IME, so we should lay it out even
@ -729,11 +691,11 @@ impl Element for TerminalElement {
cells,
cursor,
background_color,
selection_color,
size: dimensions,
rects,
selections,
relative_highlighted_ranges,
mode,
display_offset,
},
)
}
@ -768,30 +730,23 @@ impl Element for TerminalElement {
}
});
//Draw Selection
//Draw Highlighted Backgrounds
cx.paint_layer(clip_bounds, |cx| {
let start_y = layout.selections.get(0).map(|highlight| {
origin.y() + highlight.line_index as f32 * layout.size.line_height
});
if let Some(y) = start_y {
let range_lines = layout
.selections
.iter()
.map(|relative_highlight| {
relative_highlight.to_highlighted_range_line(origin, layout)
})
.collect::<Vec<HighlightedRangeLine>>();
let hr = HighlightedRange {
start_y: y, //Need to change this
line_height: layout.size.line_height,
lines: range_lines,
color: layout.selection_color,
//Copied from editor. TODO: move to theme or something
corner_radius: 0.15 * layout.size.line_height,
};
hr.paint(bounds, cx.scene);
for (relative_highlighted_range, color) in layout.relative_highlighted_ranges.iter()
{
if let Some((start_y, highlighted_range_lines)) =
to_highlighted_range_lines(relative_highlighted_range, layout, origin)
{
let hr = HighlightedRange {
start_y, //Need to change this
line_height: layout.size.line_height,
lines: highlighted_range_lines,
color: color.clone(),
//Copied from editor. TODO: move to theme or something
corner_radius: 0.15 * layout.size.line_height,
};
hr.paint(bounds, cx.scene);
}
}
});
@ -894,3 +849,65 @@ impl Element for TerminalElement {
Some(layout.cursor.as_ref()?.bounding_rect(origin))
}
}
fn to_highlighted_range_lines(
range: &RangeInclusive<Point>,
layout: &LayoutState,
origin: Vector2F,
) -> Option<(f32, Vec<HighlightedRangeLine>)> {
// Step 1. Normalize the points to be viewport relative.
//When display_offset = 1, here's how the grid is arranged:
//--- Viewport top
//-2,0 -2,1...
//-1,0 -1,1...
//--------- Terminal Top
// 0,0 0,1...
// 1,0 1,1...
//--- Viewport Bottom
// 2,0 2,1...
//--------- Terminal Bottom
// Normalize to viewport relative, from terminal relative.
// lines are i32s, which are negative above the top left corner of the terminal
// If the user has scrolled, we use the display_offset to tell us which offset
// of the grid data we should be looking at. But for the rendering step, we don't
// want negatives. We want things relative to the 'viewport' (the area of the grid
// which is currently shown according to the display offset)
let unclamped_start = Point::new(
range.start().line + layout.display_offset,
range.start().column,
);
let unclamped_end = Point::new(range.end().line + layout.display_offset, range.end().column);
// Step 2. Clamp range to viewport, and return None if it doesn't overlap
if unclamped_end.line.0 < 0 || unclamped_start.line.0 > layout.size.num_lines() as i32 {
return None;
}
let clamped_start_line = unclamped_start.line.0.max(0) as usize;
let clamped_end_line = unclamped_end.line.0.min(layout.size.num_lines() as i32) as usize;
//Convert the start of the range to pixels
let start_y = origin.y() + clamped_start_line as f32 * layout.size.line_height;
// Step 3. Expand ranges that cross lines into a collection of single-line ranges.
// (also convert to pixels)
let mut highlighted_range_lines = Vec::new();
for line in clamped_start_line..=clamped_end_line {
let mut line_start = 0;
let mut line_end = layout.size.columns();
if line == clamped_start_line {
line_start = unclamped_start.column.0 as usize;
}
if line == clamped_end_line {
line_end = unclamped_end.column.0 as usize + 1; //+1 for inclusive
}
highlighted_range_lines.push(HighlightedRangeLine {
start_x: origin.x() + line_start as f32 * layout.size.cell_width,
end_x: origin.x() + line_end as f32 * layout.size.cell_width,
});
}
Some((start_y, highlighted_range_lines))
}