Get diagnostics view almost building in the zed2 world

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Julia 2023-11-14 19:25:55 -05:00
parent 96f0257fb3
commit f4eb219c75
22 changed files with 2251 additions and 91 deletions

View file

@ -0,0 +1,43 @@
[package]
name = "diagnostics2"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
path = "src/diagnostics.rs"
doctest = false
[dependencies]
collections = { path = "../collections" }
editor = { package = "editor2", path = "../editor2" }
gpui = { package = "gpui2", path = "../gpui2" }
ui = { package = "ui2", path = "../ui2" }
language = { package = "language2", path = "../language2" }
lsp = { package = "lsp2", path = "../lsp2" }
project = { package = "project2", path = "../project2" }
settings = { package = "settings2", path = "../settings2" }
theme = { package = "theme2", path = "../theme2" }
util = { path = "../util" }
workspace = { package = "workspace2", path = "../workspace2" }
log.workspace = true
anyhow.workspace = true
futures.workspace = true
schemars.workspace = true
serde.workspace = true
serde_derive.workspace = true
smallvec.workspace = true
postage.workspace = true
[dev-dependencies]
client = { package = "client2", path = "../client2", features = ["test-support"] }
editor = { package = "editor2", path = "../editor2", features = ["test-support"] }
language = { package = "language2", path = "../language2", features = ["test-support"] }
lsp = { package = "lsp2", path = "../lsp2", features = ["test-support"] }
gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] }
workspace = { package = "workspace2", path = "../workspace2", features = ["test-support"] }
theme = { package = "theme2", path = "../theme2", features = ["test-support"] }
serde_json.workspace = true
unindent.workspace = true

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,251 @@
use collections::HashSet;
use editor::{Editor, GoToDiagnostic};
use gpui::{
div, serde_json, AppContext, CursorStyle, Div, Entity, EventEmitter, MouseButton, Render,
Styled, Subscription, View, ViewContext, WeakView,
};
use language::Diagnostic;
use lsp::LanguageServerId;
use workspace::{item::ItemHandle, StatusItemView, ToolbarItemEvent, Workspace};
use crate::ProjectDiagnosticsEditor;
// todo!()
// pub fn init(cx: &mut AppContext) {
// cx.add_action(DiagnosticIndicator::go_to_next_diagnostic);
// }
pub struct DiagnosticIndicator {
summary: project::DiagnosticSummary,
active_editor: Option<WeakView<Editor>>,
workspace: WeakView<Workspace>,
current_diagnostic: Option<Diagnostic>,
in_progress_checks: HashSet<LanguageServerId>,
_observe_active_editor: Option<Subscription>,
}
impl Render for DiagnosticIndicator {
type Element = Div<Self>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
div().size_full().bg(gpui::red())
}
}
impl DiagnosticIndicator {
pub fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
let project = workspace.project();
cx.subscribe(project, |this, project, event, cx| match event {
project::Event::DiskBasedDiagnosticsStarted { language_server_id } => {
this.in_progress_checks.insert(*language_server_id);
cx.notify();
}
project::Event::DiskBasedDiagnosticsFinished { language_server_id }
| project::Event::LanguageServerRemoved(language_server_id) => {
this.summary = project.read(cx).diagnostic_summary(cx);
this.in_progress_checks.remove(language_server_id);
cx.notify();
}
project::Event::DiagnosticsUpdated { .. } => {
this.summary = project.read(cx).diagnostic_summary(cx);
cx.notify();
}
_ => {}
})
.detach();
Self {
summary: project.read(cx).diagnostic_summary(cx),
in_progress_checks: project
.read(cx)
.language_servers_running_disk_based_diagnostics()
.collect(),
active_editor: None,
workspace: workspace.weak_handle(),
current_diagnostic: None,
_observe_active_editor: None,
}
}
fn go_to_next_diagnostic(&mut self, _: &GoToDiagnostic, cx: &mut ViewContext<Self>) {
if let Some(editor) = self.active_editor.as_ref().and_then(|e| e.upgrade()) {
editor.update(cx, |editor, cx| {
editor.go_to_diagnostic_impl(editor::Direction::Next, cx);
})
}
}
fn update(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
let editor = editor.read(cx);
let buffer = editor.buffer().read(cx);
let cursor_position = editor.selections.newest::<usize>(cx).head();
let new_diagnostic = buffer
.snapshot(cx)
.diagnostics_in_range::<_, usize>(cursor_position..cursor_position, false)
.filter(|entry| !entry.range.is_empty())
.min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
.map(|entry| entry.diagnostic);
if new_diagnostic != self.current_diagnostic {
self.current_diagnostic = new_diagnostic;
cx.notify();
}
}
}
// todo: is this nessesary anymore?
impl EventEmitter<ToolbarItemEvent> for DiagnosticIndicator {}
// impl View for DiagnosticIndicator {
// fn ui_name() -> &'static str {
// "DiagnosticIndicator"
// }
// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
// enum Summary {}
// enum Message {}
// let tooltip_style = theme::current(cx).tooltip.clone();
// let in_progress = !self.in_progress_checks.is_empty();
// let mut element = Flex::row().with_child(
// MouseEventHandler::new::<Summary, _>(0, cx, |state, cx| {
// let theme = theme::current(cx);
// let style = theme
// .workspace
// .status_bar
// .diagnostic_summary
// .style_for(state);
// let mut summary_row = Flex::row();
// if self.summary.error_count > 0 {
// summary_row.add_child(
// Svg::new("icons/error.svg")
// .with_color(style.icon_color_error)
// .constrained()
// .with_width(style.icon_width)
// .aligned()
// .contained()
// .with_margin_right(style.icon_spacing),
// );
// summary_row.add_child(
// Label::new(self.summary.error_count.to_string(), style.text.clone())
// .aligned(),
// );
// }
// if self.summary.warning_count > 0 {
// summary_row.add_child(
// Svg::new("icons/warning.svg")
// .with_color(style.icon_color_warning)
// .constrained()
// .with_width(style.icon_width)
// .aligned()
// .contained()
// .with_margin_right(style.icon_spacing)
// .with_margin_left(if self.summary.error_count > 0 {
// style.summary_spacing
// } else {
// 0.
// }),
// );
// summary_row.add_child(
// Label::new(self.summary.warning_count.to_string(), style.text.clone())
// .aligned(),
// );
// }
// if self.summary.error_count == 0 && self.summary.warning_count == 0 {
// summary_row.add_child(
// Svg::new("icons/check_circle.svg")
// .with_color(style.icon_color_ok)
// .constrained()
// .with_width(style.icon_width)
// .aligned()
// .into_any_named("ok-icon"),
// );
// }
// summary_row
// .constrained()
// .with_height(style.height)
// .contained()
// .with_style(if self.summary.error_count > 0 {
// style.container_error
// } else if self.summary.warning_count > 0 {
// style.container_warning
// } else {
// style.container_ok
// })
// })
// .with_cursor_style(CursorStyle::PointingHand)
// .on_click(MouseButton::Left, |_, this, cx| {
// if let Some(workspace) = this.workspace.upgrade(cx) {
// workspace.update(cx, |workspace, cx| {
// ProjectDiagnosticsEditor::deploy(workspace, &Default::default(), cx)
// })
// }
// })
// .with_tooltip::<Summary>(
// 0,
// "Project Diagnostics",
// Some(Box::new(crate::Deploy)),
// tooltip_style,
// cx,
// )
// .aligned()
// .into_any(),
// );
// let style = &theme::current(cx).workspace.status_bar;
// let item_spacing = style.item_spacing;
// if in_progress {
// element.add_child(
// Label::new("Checking…", style.diagnostic_message.default.text.clone())
// .aligned()
// .contained()
// .with_margin_left(item_spacing),
// );
// } else if let Some(diagnostic) = &self.current_diagnostic {
// let message_style = style.diagnostic_message.clone();
// element.add_child(
// MouseEventHandler::new::<Message, _>(1, cx, |state, _| {
// Label::new(
// diagnostic.message.split('\n').next().unwrap().to_string(),
// message_style.style_for(state).text.clone(),
// )
// .aligned()
// .contained()
// .with_margin_left(item_spacing)
// })
// .with_cursor_style(CursorStyle::PointingHand)
// .on_click(MouseButton::Left, |_, this, cx| {
// this.go_to_next_diagnostic(&Default::default(), cx)
// }),
// );
// }
// element.into_any_named("diagnostic indicator")
// }
// fn debug_json(&self, _: &gpui::AppContext) -> serde_json::Value {
// serde_json::json!({ "summary": self.summary })
// }
// }
impl StatusItemView for DiagnosticIndicator {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut ViewContext<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
self.active_editor = Some(editor.downgrade());
self._observe_active_editor = Some(cx.observe(&editor, Self::update));
self.update(editor, cx);
} else {
self.active_editor = None;
self.current_diagnostic = None;
self._observe_active_editor = None;
}
cx.notify();
}
}

View file

@ -0,0 +1,28 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Debug)]
pub struct ProjectDiagnosticsSettings {
pub include_warnings: bool,
}
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, Debug)]
pub struct ProjectDiagnosticsSettingsContent {
include_warnings: Option<bool>,
}
impl settings::Settings for ProjectDiagnosticsSettings {
const KEY: Option<&'static str> = Some("diagnostics");
type FileContent = ProjectDiagnosticsSettingsContent;
fn load(
default_value: &Self::FileContent,
user_values: &[&Self::FileContent],
_cx: &mut gpui::AppContext,
) -> anyhow::Result<Self>
where
Self: Sized,
{
Self::load_via_json_merge(default_value, user_values)
}
}

View file

@ -0,0 +1,123 @@
use crate::{ProjectDiagnosticsEditor, ToggleWarnings};
use gpui::{
div, Action, CursorStyle, Div, Entity, EventEmitter, MouseButton, ParentComponent, Render,
View, ViewContext, WeakView,
};
use ui::{Icon, IconButton, StyledExt};
use workspace::{item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};
pub struct ToolbarControls {
editor: Option<WeakView<ProjectDiagnosticsEditor>>,
}
impl Render for ToolbarControls {
type Element = Div<Self>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
div()
.h_flex()
.child(IconButton::new("toggle-warnings", Icon::Warning).on_click(|view, cx| todo!()))
}
}
impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
// impl View for ToolbarControls {
// fn ui_name() -> &'static str {
// "ToolbarControls"
// }
// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
// let include_warnings = self
// .editor
// .as_ref()
// .and_then(|editor| editor.upgrade(cx))
// .map(|editor| editor.read(cx).include_warnings)
// .unwrap_or(false);
// let tooltip = if include_warnings {
// "Exclude Warnings".into()
// } else {
// "Include Warnings".into()
// };
// Flex::row()
// .with_child(render_toggle_button(
// 0,
// "icons/warning.svg",
// include_warnings,
// (tooltip, Some(Box::new(ToggleWarnings))),
// cx,
// move |this, cx| {
// if let Some(editor) = this.editor.and_then(|editor| editor.upgrade(cx)) {
// editor.update(cx, |editor, cx| {
// editor.toggle_warnings(&Default::default(), cx)
// });
// }
// },
// ))
// .into_any()
// }
// }
impl ToolbarItemView for ToolbarControls {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemHandle>,
_: &mut ViewContext<Self>,
) -> ToolbarItemLocation {
if let Some(pane_item) = active_pane_item.as_ref() {
if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
self.editor = Some(editor.downgrade());
ToolbarItemLocation::PrimaryRight { flex: None }
} else {
ToolbarItemLocation::Hidden
}
} else {
ToolbarItemLocation::Hidden
}
}
}
impl ToolbarControls {
pub fn new() -> Self {
ToolbarControls { editor: None }
}
}
// fn render_toggle_button<
// F: 'static + Fn(&mut ToolbarControls, &mut EventContext<ToolbarControls>),
// >(
// index: usize,
// icon: &'static str,
// toggled: bool,
// tooltip: (String, Option<Box<dyn Action>>),
// cx: &mut ViewContext<ToolbarControls>,
// on_click: F,
// ) -> AnyElement<ToolbarControls> {
// enum Button {}
// let theme = theme::current(cx);
// let (tooltip_text, action) = tooltip;
// MouseEventHandler::new::<Button, _>(index, cx, |mouse_state, _| {
// let style = theme
// .workspace
// .toolbar
// .toggleable_tool
// .in_state(toggled)
// .style_for(mouse_state);
// Svg::new(icon)
// .with_color(style.color)
// .constrained()
// .with_width(style.icon_width)
// .aligned()
// .constrained()
// .with_width(style.button_width)
// .with_height(style.button_width)
// .contained()
// .with_style(style.container)
// })
// .with_cursor_style(CursorStyle::PointingHand)
// .on_click(MouseButton::Left, move |_, view, cx| on_click(view, cx))
// .with_tooltip::<Button>(index, tooltip_text, action, theme.tooltip.clone(), cx)
// .into_any_named("quick action bar button")
// }