Format messages within backticks using bold instead of with a background

This commit is contained in:
Antonio Scandurra 2022-01-25 15:46:33 +01:00
parent 7e55353de8
commit 65d4c33c0e
4 changed files with 45 additions and 70 deletions

View file

@ -697,34 +697,7 @@ fn diagnostic_header_renderer(
diagnostic: Diagnostic, diagnostic: Diagnostic,
build_settings: BuildSettings, build_settings: BuildSettings,
) -> RenderBlock { ) -> RenderBlock {
enum Run { let (message, highlights) = highlight_diagnostic_message(&diagnostic.message);
Text(Range<usize>),
Code(Range<usize>),
}
let mut prev_ix = 0;
let mut inside_block = false;
let mut runs = Vec::new();
for (backtick_ix, _) in diagnostic.message.match_indices('`') {
if backtick_ix > prev_ix {
if inside_block {
runs.push(Run::Code(prev_ix..backtick_ix));
} else {
runs.push(Run::Text(prev_ix..backtick_ix));
}
}
inside_block = !inside_block;
prev_ix = backtick_ix + 1;
}
if prev_ix < diagnostic.message.len() {
if inside_block {
runs.push(Run::Code(prev_ix..diagnostic.message.len()));
} else {
runs.push(Run::Text(prev_ix..diagnostic.message.len()));
}
}
Arc::new(move |cx| { Arc::new(move |cx| {
let settings = build_settings(cx); let settings = build_settings(cx);
let style = &settings.style.diagnostic_header; let style = &settings.style.diagnostic_header;
@ -745,28 +718,14 @@ fn diagnostic_header_renderer(
.with_style(style.icon.container) .with_style(style.icon.container)
.boxed(), .boxed(),
) )
.with_children(runs.iter().map(|run| { .with_child(
let container_style; Label::new(message.clone(), style.message.label.clone())
let text_style; .with_highlights(highlights.clone())
let range;
match run {
Run::Text(run_range) => {
container_style = Default::default();
text_style = style.text.clone();
range = run_range.clone();
}
Run::Code(run_range) => {
container_style = style.highlighted_text.container;
text_style = style.highlighted_text.text.clone();
range = run_range.clone();
}
}
Label::new(diagnostic.message[range].to_string(), text_style)
.contained() .contained()
.with_style(container_style) .with_style(style.message.container)
.aligned() .aligned()
.boxed() .boxed(),
})) )
.with_children(diagnostic.code.clone().map(|code| { .with_children(diagnostic.code.clone().map(|code| {
Label::new(code, style.code.text.clone()) Label::new(code, style.code.text.clone())
.contained() .contained()
@ -782,6 +741,28 @@ fn diagnostic_header_renderer(
}) })
} }
fn highlight_diagnostic_message(message: &str) -> (String, Vec<usize>) {
let mut message_without_backticks = String::new();
let mut prev_offset = 0;
let mut inside_block = false;
let mut highlights = Vec::new();
for (match_ix, (offset, _)) in message
.match_indices('`')
.chain([(message.len(), "")])
.enumerate()
{
message_without_backticks.push_str(&message[prev_offset..offset]);
if inside_block {
highlights.extend(prev_offset - match_ix..offset - match_ix);
}
inside_block = !inside_block;
prev_offset = offset + 1;
}
(message_without_backticks, highlights)
}
fn context_header_renderer(build_settings: BuildSettings) -> RenderBlock { fn context_header_renderer(build_settings: BuildSettings) -> RenderBlock {
Arc::new(move |cx| { Arc::new(move |cx| {
let settings = build_settings(cx); let settings = build_settings(cx);

View file

@ -3803,6 +3803,8 @@ impl Deref for EditorSnapshot {
impl EditorSettings { impl EditorSettings {
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub fn test(cx: &AppContext) -> Self { pub fn test(cx: &AppContext) -> Self {
use theme::{ContainedLabel, ContainedText, DiagnosticHeader, DiagnosticPathHeader};
Self { Self {
tab_size: 4, tab_size: 4,
soft_wrap: SoftWrap::None, soft_wrap: SoftWrap::None,
@ -3835,25 +3837,24 @@ impl EditorSettings {
selection: Default::default(), selection: Default::default(),
guest_selections: Default::default(), guest_selections: Default::default(),
syntax: Default::default(), syntax: Default::default(),
diagnostic_path_header: theme::DiagnosticPathHeader { diagnostic_path_header: DiagnosticPathHeader {
container: Default::default(), container: Default::default(),
filename: theme::ContainedText { filename: ContainedText {
container: Default::default(), container: Default::default(),
text: text.clone(), text: text.clone(),
}, },
path: theme::ContainedText { path: ContainedText {
container: Default::default(), container: Default::default(),
text: text.clone(), text: text.clone(),
}, },
}, },
diagnostic_header: theme::DiagnosticHeader { diagnostic_header: DiagnosticHeader {
container: Default::default(), container: Default::default(),
text: text.clone(), message: ContainedLabel {
highlighted_text: theme::ContainedText {
container: Default::default(), container: Default::default(),
text: text.clone(), label: text.clone().into(),
}, },
code: theme::ContainedText { code: ContainedText {
container: Default::default(), container: Default::default(),
text: text.clone(), text: text.clone(),
}, },

View file

@ -221,7 +221,7 @@ pub struct ContainedText {
pub text: TextStyle, pub text: TextStyle,
} }
#[derive(Deserialize, Default)] #[derive(Clone, Deserialize, Default)]
pub struct ContainedLabel { pub struct ContainedLabel {
#[serde(flatten)] #[serde(flatten)]
pub container: ContainerStyle, pub container: ContainerStyle,
@ -275,8 +275,7 @@ pub struct DiagnosticPathHeader {
pub struct DiagnosticHeader { pub struct DiagnosticHeader {
#[serde(flatten)] #[serde(flatten)]
pub container: ContainerStyle, pub container: ContainerStyle,
pub text: TextStyle, pub message: ContainedLabel,
pub highlighted_text: ContainedText,
pub code: ContainedText, pub code: ContainedText,
pub icon: DiagnosticHeaderIcon, pub icon: DiagnosticHeaderIcon,
} }
@ -356,10 +355,9 @@ impl InputEditorStyle {
}, },
diagnostic_header: DiagnosticHeader { diagnostic_header: DiagnosticHeader {
container: Default::default(), container: Default::default(),
text: self.text.clone(), message: ContainedLabel {
highlighted_text: ContainedText {
container: Default::default(), container: Default::default(),
text: self.text.clone(), label: self.text.clone().into(),
}, },
code: ContainedText { code: ContainedText {
container: Default::default(), container: Default::default(),

View file

@ -263,17 +263,12 @@ path = { extends = "$text.2", size = 14, margin.left = 12 }
[editor.diagnostic_header] [editor.diagnostic_header]
background = "$state.active_line" background = "$state.active_line"
border = { width = 1, top = true, bottom = true, color = "$border.0" } border = { width = 1, top = true, bottom = true, color = "$border.0" }
text = { extends = "$text.1", size = 14 }
code = { extends = "$text.2", size = 14, margin.left = 10 } code = { extends = "$text.2", size = 14, margin.left = 10 }
icon = { width = 10, margin.right = 8 } icon = { width = 10, margin.right = 8 }
[editor.diagnostic_header.highlighted_text] [editor.diagnostic_header.message]
extends = "$editor.diagnostic_header.text" text = { extends = "$text.1", size = 14 }
color = "$text.0.color" highlight_text = { extends = "$text.0", size = 14, weight = "bold" }
background = "#ffffff1f"
padding.left = 3
padding.right = 3
corner_radius = 3
[editor.error_diagnostic] [editor.error_diagnostic]
text = "$status.bad" text = "$status.bad"