chore: audit components

This commit is contained in:
2026-11-10 15:06:16 +07:00
parent af2177c6c8
commit 03369f501e
92 changed files with 2744 additions and 1947 deletions
+62 -103
View File
@@ -1,54 +1,71 @@
//! Shared helpers for the editor's views: gutter cells, role colors, code
//! line text, language mapping, change washes and diff row building.
//! Shared helpers for the editor's views: gutter cells, role colors and run
//! styles, code line text, language mapping and change washes.
#[cfg(any(feature = "diff_view", feature = "markdown_view"))]
#[cfg(feature = "markdown_view")]
use std::ops::Range;
#[cfg(feature = "markdown_view")]
use crate::highlight::{Language, Line};
#[cfg(feature = "markdown_view")]
use gpui::StyledText;
#[cfg(feature = "code_view")]
use gpui::{App, HighlightStyle, Hsla, IntoElement, ParentElement as _, Styled as _, div};
#[cfg(feature = "code_view")]
use gpui_native_theme::{ActiveTheme as _, ColorTokens, Ramps};
#[cfg(feature = "diff_view")]
use similar::{Change, ChangeTag};
use gpui_native_theme::{WASH_ALPHA, WASH_ALPHA_GUTTER, WASH_ALPHA_STRONG};
#[cfg(feature = "diff_view")]
use crate::diff_view::{DiffRow, DiffRowKind};
#[cfg(feature = "diff_view")]
use crate::highlight::HighlightedCode;
#[cfg(feature = "code_view")]
use crate::highlight::Role;
#[cfg(feature = "markdown_view")]
use crate::highlight::{Language, Line};
/// Maps a highlight role to the theme's semantic palette.
pub(crate) fn role_color(role: Role, colors: ColorTokens, ramps: Ramps) -> Hsla {
match role {
Role::Text => colors.foreground,
Role::Keyword => ramps.primary.base,
Role::String | Role::Escape => ramps.success.base,
Role::Comment | Role::Punctuation => colors.muted_foreground,
Role::Function | Role::Property => ramps.info.base,
Role::Type => ramps.warning.base,
Role::Number | Role::Constant => ramps.secondary.base,
Role::Operator => colors.foreground,
Role::Tag => ramps.destructive.base,
#[cfg(feature = "code_view")]
impl Role {
/// Maps the role to the theme's semantic palette.
pub(crate) fn color(self, colors: ColorTokens, ramps: Ramps) -> Hsla {
match self {
Role::Text => colors.foreground,
Role::Keyword => ramps.primary.base,
Role::String | Role::Escape => ramps.success.base,
Role::Comment | Role::Punctuation => colors.muted_foreground,
Role::Function | Role::Property => ramps.info.base,
Role::Type => ramps.warning.base,
Role::Number | Role::Constant => ramps.secondary.base,
Role::Operator => colors.foreground,
Role::Tag => ramps.destructive.base,
}
}
/// Builds the highlight style for one run of a code view row.
pub(crate) fn run_style(
self,
selected: bool,
colors: ColorTokens,
ramps: Ramps,
) -> HighlightStyle {
HighlightStyle {
color: Some(self.color(colors, ramps)),
background_color: selected.then_some(colors.selection),
..Default::default()
}
}
}
/// Builds the highlight style for one run of a code view row.
pub(crate) fn run_style(
role: Role,
selected: bool,
colors: ColorTokens,
ramps: Ramps,
) -> HighlightStyle {
HighlightStyle {
color: Some(role_color(role, colors, ramps)),
background_color: selected.then_some(colors.selection),
..Default::default()
#[cfg(feature = "markdown_view")]
impl Language {
/// Maps a fence info string to a bundled grammar.
pub(crate) fn from_fence_info(info: &str) -> Option<Language> {
match info.to_ascii_lowercase().as_str() {
"rust" | "rs" => Some(Language::Rust),
"typescript" | "ts" => Some(Language::TypeScript),
"javascript" | "js" => Some(Language::JavaScript),
"python" | "py" => Some(Language::Python),
_ => None,
}
}
}
/// A gutter cell sized to the whole row.
#[cfg(feature = "code_view")]
pub(crate) fn gutter_cell<E>(cx: &App, color: Hsla, content: E) -> gpui::Div
where
E: IntoElement,
@@ -68,75 +85,29 @@ where
.child(content)
}
/// Builds a diff line row from a `similar` change.
#[cfg(feature = "diff_view")]
pub(crate) fn line_row(
change: &Change<&str>,
code: &HighlightedCode,
changed_ranges: Vec<Range<usize>>,
) -> DiffRow {
let content = match change.tag() {
ChangeTag::Equal => change
.new_index()
.and_then(|ix| code.lines.get(ix))
.cloned()
.unwrap_or_default(),
ChangeTag::Delete => change
.old_index()
.and_then(|ix| code.lines.get(ix))
.cloned()
.unwrap_or_default(),
ChangeTag::Insert => change
.new_index()
.and_then(|ix| code.lines.get(ix))
.cloned()
.unwrap_or_default(),
};
let (old_number, new_number) = match change.tag() {
ChangeTag::Equal => (
change.old_index().map(|ix| ix + 1),
change.new_index().map(|ix| ix + 1),
),
ChangeTag::Delete => (change.old_index().map(|ix| ix + 1), None),
ChangeTag::Insert => (None, change.new_index().map(|ix| ix + 1)),
};
DiffRow {
kind: match change.tag() {
ChangeTag::Equal => DiffRowKind::Context,
ChangeTag::Delete => DiffRowKind::Removed,
ChangeTag::Insert => DiffRowKind::Added,
},
content,
old_number,
new_number,
changed_ranges,
header: Default::default(),
hidden_lines: 0,
}
}
/// A change wash: the ramp color at the alpha rows tint with.
#[cfg(feature = "diff_view")]
pub(crate) fn wash(color: Hsla) -> Hsla {
Hsla {
a: color.a * 0.14,
a: color.a * WASH_ALPHA,
..color
}
}
/// The stronger wash under number gutters and intra-line word changes.
#[cfg(feature = "diff_view")]
pub(crate) fn wash_strong(color: Hsla) -> Hsla {
Hsla {
a: color.a * 0.34,
a: color.a * WASH_ALPHA_STRONG,
..color
}
}
/// The subtle wash that sets the number gutter off from the code.
#[cfg(feature = "diff_view")]
pub(crate) fn wash_gutter(colors: &ColorTokens) -> Hsla {
Hsla {
a: colors.muted.a * 0.18,
a: colors.muted.a * WASH_ALPHA_GUTTER,
..colors.muted
}
}
@@ -144,34 +115,22 @@ pub(crate) fn wash_gutter(colors: &ColorTokens) -> Hsla {
/// One highlighted code line as styled text, roles resolved from the theme.
#[cfg(feature = "markdown_view")]
pub(crate) fn code_line(line: &Line, colors: ColorTokens, ramps: Ramps) -> StyledText {
let mut text = String::new();
let mut highlights: Vec<(Range<usize>, HighlightStyle)> = Vec::new();
let mut offset = 0usize;
for span in &line.spans {
let start = text.len();
text.push_str(&span.text);
let end = offset + span.text.len();
if span.role != Role::Text {
highlights.push((
start..text.len(),
offset..end,
HighlightStyle {
color: Some(role_color(span.role, colors, ramps)),
color: Some(span.role.color(colors, ramps)),
..Default::default()
},
));
}
offset = end;
}
StyledText::new(text).with_highlights(highlights)
}
/// Maps a fence info string to a bundled grammar.
#[cfg(feature = "markdown_view")]
pub(crate) fn grammar_language(info: &str) -> Option<Language> {
match info.to_ascii_lowercase().as_str() {
"rust" | "rs" => Some(Language::Rust),
"typescript" | "ts" => Some(Language::TypeScript),
"javascript" | "js" => Some(Language::JavaScript),
"python" | "py" => Some(Language::Python),
_ => None,
}
StyledText::new(line.text()).with_highlights(highlights)
}