Skip to content

Commit

Permalink
Diagnostics style 2 (#3483)
Browse files Browse the repository at this point in the history
[[PR Description]]

Merge past diagnostic multibuffer style work + some extras

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Dec 4, 2023
2 parents 4f40295 + 26c797c commit 4a5f703
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 56 deletions.
1 change: 1 addition & 0 deletions assets/icons/copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 49 additions & 23 deletions crates/diagnostics2/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,24 +774,39 @@ fn diagnostic_header_renderer(diagnostic: Diagnostic) -> RenderBlock {
Arc::new(move |_| {
h_stack()
.id("diagnostic header")
.gap_3()
.bg(gpui::red())
.map(|stack| {
let icon = if diagnostic.severity == DiagnosticSeverity::ERROR {
IconElement::new(Icon::XCircle).color(Color::Error)
} else {
IconElement::new(Icon::ExclamationTriangle).color(Color::Warning)
};

stack.child(div().pl_8().child(icon))
})
.when_some(diagnostic.source.as_ref(), |stack, source| {
stack.child(Label::new(format!("{source}:")).color(Color::Accent))
})
.child(HighlightedLabel::new(message.clone(), highlights.clone()))
.when_some(diagnostic.code.as_ref(), |stack, code| {
stack.child(Label::new(code.clone()))
})
.py_2()
.pl_10()
.pr_5()
.w_full()
.justify_between()
.gap_2()
.child(
h_stack()
.gap_3()
.map(|stack| {
let icon = if diagnostic.severity == DiagnosticSeverity::ERROR {
IconElement::new(Icon::XCircle).color(Color::Error)
} else {
IconElement::new(Icon::ExclamationTriangle).color(Color::Warning)
};
stack.child(icon)
})
.child(
h_stack()
.gap_1()
.child(HighlightedLabel::new(message.clone(), highlights.clone()))
.when_some(diagnostic.code.as_ref(), |stack, code| {
stack.child(Label::new(format!("({code})")).color(Color::Muted))
}),
),
)
.child(
h_stack()
.gap_1()
.when_some(diagnostic.source.as_ref(), |stack, source| {
stack.child(Label::new(format!("{source}")).color(Color::Muted))
}),
)
.into_any_element()
})
}
Expand All @@ -802,11 +817,22 @@ pub(crate) fn render_summary(summary: &DiagnosticSummary) -> AnyElement {
label.into_any_element()
} else {
h_stack()
.bg(gpui::red())
.child(IconElement::new(Icon::XCircle))
.child(Label::new(summary.error_count.to_string()))
.child(IconElement::new(Icon::ExclamationTriangle))
.child(Label::new(summary.warning_count.to_string()))
.gap_1()
.when(summary.error_count > 0, |then| {
then.child(
h_stack()
.gap_1()
.child(IconElement::new(Icon::XCircle).color(Color::Error))
.child(Label::new(summary.error_count.to_string())),
)
})
.when(summary.warning_count > 0, |then| {
then.child(
h_stack()
.child(IconElement::new(Icon::ExclamationTriangle).color(Color::Warning))
.child(Label::new(summary.warning_count.to_string())),
)
})
.into_any_element()
}
}
Expand Down
44 changes: 35 additions & 9 deletions crates/editor2/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ use text::{OffsetUtf16, Rope};
use theme::{
ActiveTheme, DiagnosticStyle, PlayerColor, SyntaxTheme, Theme, ThemeColors, ThemeSettings,
};
use ui::prelude::*;
use ui::{h_stack, v_stack, HighlightedLabel, IconButton, Popover, Tooltip};
use ui::{
h_stack, v_stack, ButtonSize, ButtonStyle, HighlightedLabel, Icon, IconButton, Popover, Tooltip,
};
use ui::{prelude::*, IconSize};
use util::{post_inc, RangeExt, ResultExt, TryFutureExt};
use workspace::{
item::{ItemEvent, ItemHandle},
Expand Down Expand Up @@ -9689,20 +9691,44 @@ pub fn diagnostic_block_renderer(diagnostic: Diagnostic, is_valid: bool) -> Rend
let message = diagnostic.message;
Arc::new(move |cx: &mut BlockContext| {
let message = message.clone();
let copy_id: SharedString = format!("copy-{}", cx.block_id.clone()).to_string().into();
// TODO: `cx.write_to_clipboard` is not implemented in tests.
// let write_to_clipboard = cx.write_to_clipboard(ClipboardItem::new(message.clone()));

// TODO: Nate: We should tint the background of the block with the severity color
// We need to extend the theme before we can do this
v_stack()
.id(cx.block_id)
.relative()
.size_full()
.bg(gpui::red())
.children(highlighted_lines.iter().map(|(line, highlights)| {
div()
let group_id = cx.block_id.to_string();

h_stack()
.group(group_id.clone())
.gap_2()
.absolute()
.left(cx.anchor_x)
.px_1p5()
.child(HighlightedLabel::new(line.clone(), highlights.clone()))
.ml(cx.anchor_x)
}))
.cursor_pointer()
.on_click(cx.listener(move |_, _, cx| {
cx.write_to_clipboard(ClipboardItem::new(message.clone()));
.child(
div()
.border()
.border_color(gpui::red())
.invisible()
.group_hover(group_id, |style| style.visible())
.child(
IconButton::new(copy_id.clone(), Icon::Copy)
.icon_color(Color::Muted)
.size(ButtonSize::Compact)
.style(ButtonStyle::Transparent)
// TODO: `cx.write_to_clipboard` is not implemented in tests.
// .on_click(cx.listener(move |_, _, cx| write_to_clipboard))
.tooltip(|cx| Tooltip::text("Copy diagnostic message", cx)),
),
)
}))
.tooltip(|cx| Tooltip::text("Copy diagnostic message", cx))
.into_any_element()
})
}
Expand Down
130 changes: 108 additions & 22 deletions crates/editor2/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ use std::{
};
use sum_tree::Bias;
use theme::{ActiveTheme, PlayerColor};
use ui::prelude::*;
use ui::{h_stack, IconButton, Tooltip};
use ui::{
h_stack, ButtonLike, ButtonStyle, Disclosure, IconButton, IconElement, IconSize, Label, Tooltip,
};
use ui::{prelude::*, Icon};
use util::ResultExt;
use workspace::item::Item;

Expand Down Expand Up @@ -2223,7 +2225,8 @@ impl EditorElement {
.as_ref()
.map(|project| project.read(cx).visible_worktrees(cx).count() > 1)
.unwrap_or_default();
let jump_icon = project::File::from_dyn(buffer.file()).map(|file| {

let jump_handler = project::File::from_dyn(buffer.file()).map(|file| {
let jump_path = ProjectPath {
worktree_id: file.worktree_id(cx),
path: file.path.clone(),
Expand All @@ -2234,11 +2237,11 @@ impl EditorElement {
.map_or(range.context.start, |primary| primary.start);
let jump_position = language::ToPoint::to_point(&jump_anchor, buffer);

IconButton::new(block_id, ui::Icon::ArrowUpRight)
.on_click(cx.listener_for(&self.editor, move |editor, e, cx| {
editor.jump(jump_path.clone(), jump_position, jump_anchor, cx);
}))
.tooltip(|cx| Tooltip::for_action("Jump to Buffer", &OpenExcerpts, cx))
let jump_handler = cx.listener_for(&self.editor, move |editor, e, cx| {
editor.jump(jump_path.clone(), jump_position, jump_anchor, cx);
});

jump_handler
});

let element = if *starts_new_buffer {
Expand All @@ -2253,25 +2256,108 @@ impl EditorElement {
.map(|p| SharedString::from(p.to_string_lossy().to_string() + "/"));
}

h_stack()
.id("path header block")
.size_full()
.bg(gpui::red())
.child(
filename
.map(SharedString::from)
.unwrap_or_else(|| "untitled".into()),
)
.children(parent_path)
.children(jump_icon) // .p_x(gutter_padding)
let is_open = true;

div().id("path header container").size_full().p_1p5().child(
h_stack()
.id("path header block")
.py_1p5()
.pl_3()
.pr_2()
.rounded_lg()
.shadow_md()
.border()
.border_color(cx.theme().colors().border)
.bg(cx.theme().colors().editor_subheader_background)
.justify_between()
.cursor_pointer()
.hover(|style| style.bg(cx.theme().colors().element_hover))
.on_click(cx.listener(|_editor, _event, _cx| {
// TODO: Implement collapsing path headers
todo!("Clicking path header")
}))
.child(
h_stack()
.gap_3()
// TODO: Add open/close state and toggle action
.child(
div().border().border_color(gpui::red()).child(
ButtonLike::new("path-header-disclosure-control")
.style(ButtonStyle::Subtle)
.child(IconElement::new(match is_open {
true => Icon::ChevronDown,
false => Icon::ChevronRight,
})),
),
)
.child(
h_stack()
.gap_2()
.child(Label::new(
filename
.map(SharedString::from)
.unwrap_or_else(|| "untitled".into()),
))
.when_some(parent_path, |then, path| {
then.child(Label::new(path).color(Color::Muted))
}),
),
)
.children(jump_handler.map(|jump_handler| {
IconButton::new(block_id, Icon::ArrowUpRight)
.style(ButtonStyle::Subtle)
.on_click(jump_handler)
.tooltip(|cx| {
Tooltip::for_action("Jump to Buffer", &OpenExcerpts, cx)
})
})), // .p_x(gutter_padding)
)
} else {
let text_style = style.text.clone();
h_stack()
.id("collapsed context")
.size_full()
.bg(gpui::red())
.child("⋯")
.children(jump_icon) // .p_x(gutter_padding)
.gap(gutter_padding)
.child(
h_stack()
.justify_end()
.flex_none()
.w(gutter_width - gutter_padding)
.h_full()
.text_buffer(cx)
.text_color(cx.theme().colors().editor_line_number)
.child("..."),
)
.map(|this| {
if let Some(jump_handler) = jump_handler {
this.child(
ButtonLike::new("jump to collapsed context")
.style(ButtonStyle::Transparent)
.full_width()
.on_click(jump_handler)
.tooltip(|cx| {
Tooltip::for_action(
"Jump to Buffer",
&OpenExcerpts,
cx,
)
})
.child(
div()
.h_px()
.w_full()
.bg(cx.theme().colors().border_variant)
.group_hover("", |style| {
style.bg(cx.theme().colors().border)
}),
),
)
} else {
this.child(div().size_full().bg(gpui::green()))
}
})
// .child("⋯")
// .children(jump_icon) // .p_x(gutter_padding)
};
element.into_any()
}
Expand Down
14 changes: 13 additions & 1 deletion crates/ui2/src/components/button/button.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gpui::AnyView;
use gpui::{AnyView, DefiniteLength};

use crate::prelude::*;
use crate::{
Expand Down Expand Up @@ -88,6 +88,18 @@ impl Clickable for Button {
}
}

impl FixedWidth for Button {
fn width(mut self, width: DefiniteLength) -> Self {
self.base = self.base.width(width);
self
}

fn full_width(mut self) -> Self {
self.base = self.base.full_width();
self
}
}

impl ButtonCommon for Button {
fn id(&self) -> &ElementId {
self.base.id()
Expand Down
18 changes: 18 additions & 0 deletions crates/ui2/src/components/button/button_like.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use gpui::{relative, DefiniteLength};
use gpui::{rems, transparent_black, AnyElement, AnyView, ClickEvent, Div, Hsla, Rems, Stateful};
use smallvec::SmallVec;

Expand Down Expand Up @@ -246,6 +247,7 @@ pub struct ButtonLike {
pub(super) style: ButtonStyle,
pub(super) disabled: bool,
pub(super) selected: bool,
pub(super) width: Option<DefiniteLength>,
size: ButtonSize,
tooltip: Option<Box<dyn Fn(&mut WindowContext) -> AnyView>>,
on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
Expand All @@ -259,6 +261,7 @@ impl ButtonLike {
style: ButtonStyle::default(),
disabled: false,
selected: false,
width: None,
size: ButtonSize::Default,
tooltip: None,
children: SmallVec::new(),
Expand Down Expand Up @@ -288,6 +291,18 @@ impl Clickable for ButtonLike {
}
}

impl FixedWidth for ButtonLike {
fn width(mut self, width: DefiniteLength) -> Self {
self.width = Some(width);
self
}

fn full_width(mut self) -> Self {
self.width = Some(relative(1.));
self
}
}

impl ButtonCommon for ButtonLike {
fn id(&self) -> &ElementId {
&self.id
Expand Down Expand Up @@ -321,7 +336,10 @@ impl RenderOnce for ButtonLike {
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
h_stack()
.id(self.id.clone())
.group("")
.flex_none()
.h(self.size.height())
.when_some(self.width, |this, width| this.w(width))
.rounded_md()
.gap_1()
.px_1()
Expand Down
Loading

0 comments on commit 4a5f703

Please sign in to comment.