Move layout metrics into theme and apply audit cleanups
This commit is contained in:
@@ -11,7 +11,7 @@ use gpui::{
|
||||
actions, canvas, div, uniform_list,
|
||||
};
|
||||
use gpui_native_core::scrollbar::{Scrollbar, ScrollbarHandle, ScrollbarVisibility};
|
||||
use gpui_native_theme::{ActiveTheme as _, ColorTokens, Ramps};
|
||||
use gpui_native_theme::{ActiveTheme as _, ColorTokens, MONO_ADVANCE_RATIO, Ramps};
|
||||
|
||||
use crate::highlight::{HighlightedCode, Language, Role, highlight};
|
||||
use crate::utils::{gutter_cell, run_style};
|
||||
@@ -132,11 +132,6 @@ impl CodeViewState {
|
||||
&self.source
|
||||
}
|
||||
|
||||
/// The language the source highlights as.
|
||||
pub fn language(&self) -> Language {
|
||||
self.language
|
||||
}
|
||||
|
||||
/// The highlighted lines.
|
||||
pub fn code(&self) -> &HighlightedCode {
|
||||
&self.code
|
||||
@@ -209,13 +204,7 @@ impl CodeViewState {
|
||||
(!selected.is_empty()).then(|| selected.join("\n"))
|
||||
}
|
||||
|
||||
/// Drops the selection.
|
||||
pub fn clear_selection(&mut self) {
|
||||
self.selection = None;
|
||||
self.selecting = false;
|
||||
}
|
||||
|
||||
/// Overrides the accessibility label of the view.
|
||||
// Overrides the accessibility label of the view.
|
||||
pub fn accessibility_label<L>(mut self, label: L) -> Self
|
||||
where
|
||||
L: Into<SharedString>,
|
||||
@@ -235,8 +224,7 @@ impl CodeViewState {
|
||||
text_x: Pixels,
|
||||
) -> SelectionPoint {
|
||||
let row = (((position.y - bounds.origin.y - scroll_y) / line_height).floor() as isize)
|
||||
.clamp(0, self.code.lines.len() as isize - 1)
|
||||
.max(0) as usize;
|
||||
.clamp(0, self.code.lines.len() as isize - 1) as usize;
|
||||
|
||||
let chars = self.code.lines[row]
|
||||
.spans
|
||||
@@ -244,9 +232,8 @@ impl CodeViewState {
|
||||
.map(|span| span.text.chars().count())
|
||||
.sum::<usize>();
|
||||
|
||||
let col = (((position.x - text_x) / char_width).round() as isize)
|
||||
.clamp(0, chars as isize)
|
||||
.max(0) as usize;
|
||||
let col = (((position.x - text_x) / char_width).round() as isize).clamp(0, chars as isize)
|
||||
as usize;
|
||||
|
||||
SelectionPoint { row, col }
|
||||
}
|
||||
@@ -265,7 +252,7 @@ impl Render for CodeViewState {
|
||||
let mono_font = theme.mono_font;
|
||||
let text_size = theme.sizes.small.text_size;
|
||||
let line_height = text_size * 1.5;
|
||||
let char_width = text_size * 0.62;
|
||||
let char_width = text_size * MONO_ADVANCE_RATIO;
|
||||
let root_font_size = theme.root_font_size;
|
||||
|
||||
let id = self.id.clone();
|
||||
|
||||
@@ -13,7 +13,7 @@ use gpui::{
|
||||
point, uniform_list,
|
||||
};
|
||||
use gpui_native_core::scrollbar::{Scrollbar, ScrollbarHandle, ScrollbarVisibility};
|
||||
use gpui_native_theme::ActiveTheme as _;
|
||||
use gpui_native_theme::{ActiveTheme as _, MONO_ADVANCE_RATIO};
|
||||
use similar::{ChangeTag, TextDiff};
|
||||
|
||||
use std::ops::Range;
|
||||
@@ -399,7 +399,7 @@ fn render_row(
|
||||
let ramps = theme.ramps;
|
||||
let text_size = theme.sizes.small.text_size;
|
||||
let line_height = text_size * 1.5;
|
||||
let char_width = text_size * 0.62;
|
||||
let char_width = text_size * MONO_ADVANCE_RATIO;
|
||||
|
||||
let frame = div()
|
||||
.debug_selector(move || format!("diff-row-{row_ix}"))
|
||||
|
||||
@@ -101,8 +101,6 @@ pub enum InlineStyle {
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct MarkdownTableCell {
|
||||
pub spans: Vec<InlineSpan>,
|
||||
/// 1-based column position, so renderers can align to the table's columns.
|
||||
pub column: usize,
|
||||
}
|
||||
|
||||
/// A table row; the header row precedes the body rows.
|
||||
@@ -293,24 +291,6 @@ struct ListFrame {
|
||||
task: Option<bool>,
|
||||
}
|
||||
|
||||
/// The table being assembled, when the parser is inside one.
|
||||
enum TableBuilder {
|
||||
None,
|
||||
Body { rows: Vec<MarkdownTableRow> },
|
||||
}
|
||||
|
||||
/// One open code block, accumulating its raw text.
|
||||
#[derive(Default)]
|
||||
struct CodeBuilder {
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl CodeBuilder {
|
||||
fn push(&mut self, text: &str) {
|
||||
self.text.push_str(text);
|
||||
}
|
||||
}
|
||||
|
||||
/// Accumulates inline spans across inline events.
|
||||
#[derive(Default)]
|
||||
struct InlineBuilder {
|
||||
@@ -403,8 +383,8 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
// Whether each open list item already flushed content rows.
|
||||
let mut items: Vec<bool> = Vec::new();
|
||||
let mut quote_depth = 0usize;
|
||||
let mut table = TableBuilder::None;
|
||||
let mut code: Option<CodeBuilder> = None;
|
||||
let mut table: Option<Vec<MarkdownTableRow>> = None;
|
||||
let mut code: Option<String> = None;
|
||||
// The fence info string arrives on Start, its text follows.
|
||||
let mut code_language: Option<SharedString> = None;
|
||||
|
||||
@@ -469,7 +449,7 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
&mut blocks,
|
||||
quote_depth,
|
||||
);
|
||||
code = Some(CodeBuilder::default());
|
||||
code = Some(String::new());
|
||||
if let CodeBlockKind::Fenced(info) = kind {
|
||||
let info = info.trim();
|
||||
code_language =
|
||||
@@ -484,10 +464,10 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
&mut blocks,
|
||||
quote_depth,
|
||||
);
|
||||
table = TableBuilder::Body { rows: Vec::new() }
|
||||
table = Some(Vec::new())
|
||||
}
|
||||
Tag::TableHead => {
|
||||
if let TableBuilder::Body { rows } = &mut table {
|
||||
if let Some(rows) = &mut table {
|
||||
rows.push(MarkdownTableRow {
|
||||
cells: Vec::new(),
|
||||
header: true,
|
||||
@@ -495,7 +475,7 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
}
|
||||
}
|
||||
Tag::TableRow => {
|
||||
if let TableBuilder::Body { rows } = &mut table {
|
||||
if let Some(rows) = &mut table {
|
||||
rows.push(MarkdownTableRow {
|
||||
cells: Vec::new(),
|
||||
header: false,
|
||||
@@ -583,7 +563,7 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
}
|
||||
TagEnd::CodeBlock => {
|
||||
let built = code.take().unwrap_or_default();
|
||||
let text = built.text.trim_end_matches('\n');
|
||||
let text = built.trim_end_matches('\n');
|
||||
let language = code_language.take();
|
||||
let lines = match highlight_code
|
||||
.then(|| language.as_deref().and_then(grammar_language))
|
||||
@@ -600,8 +580,7 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
});
|
||||
}
|
||||
TagEnd::Table => {
|
||||
let built = std::mem::replace(&mut table, TableBuilder::None);
|
||||
if let TableBuilder::Body { rows } = built {
|
||||
if let Some(rows) = table.take() {
|
||||
blocks.push(MarkdownBlock::Table {
|
||||
table: MarkdownTable { rows },
|
||||
indent: lists.len(),
|
||||
@@ -611,16 +590,13 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
}
|
||||
TagEnd::TableCell => {
|
||||
let spans = inline.finish();
|
||||
let TableBuilder::Body { rows } = &mut table else {
|
||||
let Some(rows) = &mut table else {
|
||||
continue;
|
||||
};
|
||||
let Some(row) = rows.last_mut() else {
|
||||
continue;
|
||||
};
|
||||
row.cells.push(MarkdownTableCell {
|
||||
spans,
|
||||
column: row.cells.len() + 1,
|
||||
});
|
||||
row.cells.push(MarkdownTableCell { spans });
|
||||
}
|
||||
TagEnd::Emphasis
|
||||
| TagEnd::Strong
|
||||
@@ -630,8 +606,8 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
_ => {}
|
||||
},
|
||||
Event::Text(text) => {
|
||||
if let Some(built) = code.as_mut() {
|
||||
built.push(&text);
|
||||
if let Some(buffer) = code.as_mut() {
|
||||
buffer.push_str(&text);
|
||||
} else {
|
||||
inline.push_text(&text);
|
||||
}
|
||||
@@ -660,9 +636,9 @@ fn parse(source: &str, highlight_code: bool) -> Vec<MarkdownBlock> {
|
||||
frame.task = Some(checked);
|
||||
}
|
||||
}
|
||||
Event::FootnoteReference(label) => inline.push_text(&label),
|
||||
// HTML and math render nothing in this view.
|
||||
Event::InlineHtml(_)
|
||||
// Footnotes are disabled in the parser options.
|
||||
Event::FootnoteReference(_)
|
||||
| Event::InlineHtml(_)
|
||||
| Event::Html(_)
|
||||
| Event::InlineMath(_)
|
||||
| Event::DisplayMath(_) => {}
|
||||
|
||||
@@ -305,14 +305,6 @@ fn tables_parse_header_first_with_column_positions() {
|
||||
.map(|cell| spans_text(&cell.spans))
|
||||
.collect();
|
||||
assert_eq!(header, vec!["a", "b"]);
|
||||
assert_eq!(
|
||||
table.rows[0]
|
||||
.cells
|
||||
.iter()
|
||||
.map(|cell| cell.column)
|
||||
.collect::<Vec<_>>(),
|
||||
vec![1, 2]
|
||||
);
|
||||
assert_eq!(spans_text(&table.rows[2].cells[1].spans), "4");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user