Show diagnostic codes (#29296)
Closes #28135 Closes #4388 Closes #28136 Release Notes: - diagnostics: Show the diagnostic code if available --------- Co-authored-by: Neo Nie <nihgwu@live.com> Co-authored-by: Zed AI <ai+claude-3.7@zed.dev>
This commit is contained in:
co-authored by
Neo Nie
Zed AI
parent
8836c6fb42
commit
9d10489607
+27
-13
@@ -394,10 +394,32 @@ pub trait DiagnosticRenderer {
|
||||
editor: WeakEntity<Editor>,
|
||||
cx: &mut App,
|
||||
) -> Vec<BlockProperties<Anchor>>;
|
||||
|
||||
fn render_hover(
|
||||
&self,
|
||||
diagnostic_group: Vec<DiagnosticEntry<Point>>,
|
||||
range: Range<Point>,
|
||||
buffer_id: BufferId,
|
||||
cx: &mut App,
|
||||
) -> Option<Entity<markdown::Markdown>>;
|
||||
|
||||
fn open_link(
|
||||
&self,
|
||||
editor: &mut Editor,
|
||||
link: SharedString,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Editor>,
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) struct GlobalDiagnosticRenderer(pub Arc<dyn DiagnosticRenderer>);
|
||||
|
||||
impl GlobalDiagnosticRenderer {
|
||||
fn global(cx: &App) -> Option<Arc<dyn DiagnosticRenderer>> {
|
||||
cx.try_global::<Self>().map(|g| g.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl gpui::Global for GlobalDiagnosticRenderer {}
|
||||
pub fn set_diagnostic_renderer(renderer: impl DiagnosticRenderer + 'static, cx: &mut App) {
|
||||
cx.set_global(GlobalDiagnosticRenderer(Arc::new(renderer)));
|
||||
@@ -867,7 +889,7 @@ pub struct Editor {
|
||||
read_only: bool,
|
||||
leader_peer_id: Option<PeerId>,
|
||||
remote_id: Option<ViewId>,
|
||||
hover_state: HoverState,
|
||||
pub hover_state: HoverState,
|
||||
pending_mouse_down: Option<Rc<RefCell<Option<MouseDownEvent>>>>,
|
||||
gutter_hovered: bool,
|
||||
hovered_link_state: Option<HoveredLinkState>,
|
||||
@@ -14788,25 +14810,17 @@ impl Editor {
|
||||
}
|
||||
self.dismiss_diagnostics(cx);
|
||||
let snapshot = self.snapshot(window, cx);
|
||||
let Some(diagnostic_renderer) = cx
|
||||
.try_global::<GlobalDiagnosticRenderer>()
|
||||
.map(|g| g.0.clone())
|
||||
else {
|
||||
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||
let Some(renderer) = GlobalDiagnosticRenderer::global(cx) else {
|
||||
return;
|
||||
};
|
||||
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||
|
||||
let diagnostic_group = buffer
|
||||
.diagnostic_group(buffer_id, diagnostic.diagnostic.group_id)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let blocks = diagnostic_renderer.render_group(
|
||||
diagnostic_group,
|
||||
buffer_id,
|
||||
snapshot,
|
||||
cx.weak_entity(),
|
||||
cx,
|
||||
);
|
||||
let blocks =
|
||||
renderer.render_group(diagnostic_group, buffer_id, snapshot, cx.weak_entity(), cx);
|
||||
|
||||
let blocks = self.display_map.update(cx, |display_map, cx| {
|
||||
display_map.insert_blocks(blocks, cx).into_iter().collect()
|
||||
|
||||
@@ -12855,46 +12855,6 @@ async fn go_to_prev_overlapping_diagnostic(executor: BackgroundExecutor, cx: &mu
|
||||
"});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_diagnostics_with_links(cx: &mut TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
let mut cx = EditorTestContext::new(cx).await;
|
||||
|
||||
cx.set_state(indoc! {"
|
||||
fn func(abˇc def: i32) -> u32 {
|
||||
}
|
||||
"});
|
||||
let lsp_store =
|
||||
cx.update_editor(|editor, _, cx| editor.project.as_ref().unwrap().read(cx).lsp_store());
|
||||
|
||||
cx.update(|_, cx| {
|
||||
lsp_store.update(cx, |lsp_store, cx| {
|
||||
lsp_store.update_diagnostics(
|
||||
LanguageServerId(0),
|
||||
lsp::PublishDiagnosticsParams {
|
||||
uri: lsp::Url::from_file_path(path!("/root/file")).unwrap(),
|
||||
version: None,
|
||||
diagnostics: vec![lsp::Diagnostic {
|
||||
range: lsp::Range::new(lsp::Position::new(0, 8), lsp::Position::new(0, 12)),
|
||||
severity: Some(lsp::DiagnosticSeverity::ERROR),
|
||||
message: "we've had problems with <https://link.one>, and <https://link.two> is broken".to_string(),
|
||||
..Default::default()
|
||||
}],
|
||||
},
|
||||
&[],
|
||||
cx,
|
||||
)
|
||||
})
|
||||
}).unwrap();
|
||||
cx.run_until_parked();
|
||||
cx.update_editor(|editor, window, cx| {
|
||||
hover_popover::hover(editor, &Default::default(), window, cx)
|
||||
});
|
||||
cx.run_until_parked();
|
||||
cx.update_editor(|editor, _, _| assert!(editor.hover_state.diagnostic_popover.is_some()))
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_go_to_hunk(executor: BackgroundExecutor, cx: &mut TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
@@ -71,7 +71,7 @@ pub enum HoverLink {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct InlayHighlight {
|
||||
pub struct InlayHighlight {
|
||||
pub inlay: InlayId,
|
||||
pub inlay_position: Anchor,
|
||||
pub range: Range<usize>,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
ActiveDiagnostic, Anchor, AnchorRangeExt, DisplayPoint, DisplayRow, Editor, EditorSettings,
|
||||
EditorSnapshot, Hover,
|
||||
EditorSnapshot, GlobalDiagnosticRenderer, Hover,
|
||||
display_map::{InlayOffset, ToDisplayPoint, invisibles::is_invisible},
|
||||
hover_links::{InlayHighlight, RangeInEditor},
|
||||
scroll::{Autoscroll, ScrollAmount},
|
||||
@@ -15,7 +15,7 @@ use itertools::Itertools;
|
||||
use language::{DiagnosticEntry, Language, LanguageRegistry};
|
||||
use lsp::DiagnosticSeverity;
|
||||
use markdown::{Markdown, MarkdownElement, MarkdownStyle};
|
||||
use multi_buffer::{MultiOrSingleBufferOffsetRange, ToOffset};
|
||||
use multi_buffer::{MultiOrSingleBufferOffsetRange, ToOffset, ToPoint};
|
||||
use project::{HoverBlock, HoverBlockKind, InlayHintLabelPart};
|
||||
use settings::Settings;
|
||||
use std::{borrow::Cow, cell::RefCell};
|
||||
@@ -81,7 +81,7 @@ pub fn show_keyboard_hover(
|
||||
.as_ref()
|
||||
.and_then(|d| {
|
||||
if *d.keyboard_grace.borrow() {
|
||||
d.anchor
|
||||
Some(d.anchor)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -283,6 +283,7 @@ fn show_hover(
|
||||
None
|
||||
};
|
||||
|
||||
let renderer = GlobalDiagnosticRenderer::global(cx);
|
||||
let task = cx.spawn_in(window, async move |this, cx| {
|
||||
async move {
|
||||
// If we need to delay, delay a set amount initially before making the lsp request
|
||||
@@ -313,28 +314,35 @@ fn show_hover(
|
||||
} else {
|
||||
snapshot
|
||||
.buffer_snapshot
|
||||
.diagnostics_in_range::<usize>(offset..offset)
|
||||
.filter(|diagnostic| Some(diagnostic.diagnostic.group_id) != active_group_id)
|
||||
.diagnostics_with_buffer_ids_in_range::<usize>(offset..offset)
|
||||
.filter(|(_, diagnostic)| {
|
||||
Some(diagnostic.diagnostic.group_id) != active_group_id
|
||||
})
|
||||
// Find the entry with the most specific range
|
||||
.min_by_key(|entry| entry.range.len())
|
||||
.min_by_key(|(_, entry)| entry.range.len())
|
||||
};
|
||||
|
||||
let diagnostic_popover = if let Some(local_diagnostic) = local_diagnostic {
|
||||
let text = match local_diagnostic.diagnostic.source {
|
||||
Some(ref source) => {
|
||||
format!("{source}: {}", local_diagnostic.diagnostic.message)
|
||||
}
|
||||
None => local_diagnostic.diagnostic.message.clone(),
|
||||
};
|
||||
let local_diagnostic = DiagnosticEntry {
|
||||
diagnostic: local_diagnostic.diagnostic,
|
||||
range: snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_before(local_diagnostic.range.start)
|
||||
..snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_after(local_diagnostic.range.end),
|
||||
};
|
||||
let diagnostic_popover = if let Some((buffer_id, local_diagnostic)) = local_diagnostic {
|
||||
let group = snapshot
|
||||
.buffer_snapshot
|
||||
.diagnostic_group(buffer_id, local_diagnostic.diagnostic.group_id)
|
||||
.collect::<Vec<_>>();
|
||||
let point_range = local_diagnostic
|
||||
.range
|
||||
.start
|
||||
.to_point(&snapshot.buffer_snapshot)
|
||||
..local_diagnostic
|
||||
.range
|
||||
.end
|
||||
.to_point(&snapshot.buffer_snapshot);
|
||||
let markdown = cx.update(|_, cx| {
|
||||
renderer
|
||||
.as_ref()
|
||||
.and_then(|renderer| {
|
||||
renderer.render_hover(group, point_range, buffer_id, cx)
|
||||
})
|
||||
.ok_or_else(|| anyhow::anyhow!("no rendered diagnostic"))
|
||||
})??;
|
||||
|
||||
let (background_color, border_color) = cx.update(|_, cx| {
|
||||
let status_colors = cx.theme().status();
|
||||
@@ -359,28 +367,26 @@ fn show_hover(
|
||||
}
|
||||
})?;
|
||||
|
||||
let parsed_content = cx
|
||||
.new(|cx| Markdown::new_text(SharedString::new(text), cx))
|
||||
.ok();
|
||||
let subscription =
|
||||
this.update(cx, |_, cx| cx.observe(&markdown, |_, _, cx| cx.notify()))?;
|
||||
|
||||
let subscription = this
|
||||
.update(cx, |_, cx| {
|
||||
if let Some(parsed_content) = &parsed_content {
|
||||
Some(cx.observe(parsed_content, |_, _, cx| cx.notify()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.ok()
|
||||
.flatten();
|
||||
let local_diagnostic = DiagnosticEntry {
|
||||
diagnostic: local_diagnostic.diagnostic,
|
||||
range: snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_before(local_diagnostic.range.start)
|
||||
..snapshot
|
||||
.buffer_snapshot
|
||||
.anchor_after(local_diagnostic.range.end),
|
||||
};
|
||||
|
||||
Some(DiagnosticPopover {
|
||||
local_diagnostic,
|
||||
parsed_content,
|
||||
markdown,
|
||||
border_color,
|
||||
background_color,
|
||||
keyboard_grace: Rc::new(RefCell::new(ignore_timeout)),
|
||||
anchor: Some(anchor),
|
||||
anchor,
|
||||
_subscription: subscription,
|
||||
})
|
||||
} else {
|
||||
@@ -719,7 +725,7 @@ pub fn open_markdown_url(link: SharedString, window: &mut Window, cx: &mut App)
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct HoverState {
|
||||
pub(crate) info_popovers: Vec<InfoPopover>,
|
||||
pub info_popovers: Vec<InfoPopover>,
|
||||
pub diagnostic_popover: Option<DiagnosticPopover>,
|
||||
pub triggered_from: Option<Anchor>,
|
||||
pub info_task: Option<Task<Option<()>>>,
|
||||
@@ -789,23 +795,25 @@ impl HoverState {
|
||||
}
|
||||
}
|
||||
if let Some(diagnostic_popover) = &self.diagnostic_popover {
|
||||
if let Some(markdown_view) = &diagnostic_popover.parsed_content {
|
||||
if markdown_view.focus_handle(cx).is_focused(window) {
|
||||
hover_popover_is_focused = true;
|
||||
}
|
||||
if diagnostic_popover
|
||||
.markdown
|
||||
.focus_handle(cx)
|
||||
.is_focused(window)
|
||||
{
|
||||
hover_popover_is_focused = true;
|
||||
}
|
||||
}
|
||||
hover_popover_is_focused
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct InfoPopover {
|
||||
pub(crate) symbol_range: RangeInEditor,
|
||||
pub(crate) parsed_content: Option<Entity<Markdown>>,
|
||||
pub(crate) scroll_handle: ScrollHandle,
|
||||
pub(crate) scrollbar_state: ScrollbarState,
|
||||
pub(crate) keyboard_grace: Rc<RefCell<bool>>,
|
||||
pub(crate) anchor: Option<Anchor>,
|
||||
pub struct InfoPopover {
|
||||
pub symbol_range: RangeInEditor,
|
||||
pub parsed_content: Option<Entity<Markdown>>,
|
||||
pub scroll_handle: ScrollHandle,
|
||||
pub scrollbar_state: ScrollbarState,
|
||||
pub keyboard_grace: Rc<RefCell<bool>>,
|
||||
pub anchor: Option<Anchor>,
|
||||
_subscription: Option<Subscription>,
|
||||
}
|
||||
|
||||
@@ -897,12 +905,12 @@ impl InfoPopover {
|
||||
|
||||
pub struct DiagnosticPopover {
|
||||
pub(crate) local_diagnostic: DiagnosticEntry<Anchor>,
|
||||
parsed_content: Option<Entity<Markdown>>,
|
||||
markdown: Entity<Markdown>,
|
||||
border_color: Hsla,
|
||||
background_color: Hsla,
|
||||
pub keyboard_grace: Rc<RefCell<bool>>,
|
||||
pub anchor: Option<Anchor>,
|
||||
_subscription: Option<Subscription>,
|
||||
pub anchor: Anchor,
|
||||
_subscription: Subscription,
|
||||
}
|
||||
|
||||
impl DiagnosticPopover {
|
||||
@@ -913,6 +921,7 @@ impl DiagnosticPopover {
|
||||
cx: &mut Context<Editor>,
|
||||
) -> AnyElement {
|
||||
let keyboard_grace = Rc::clone(&self.keyboard_grace);
|
||||
let this = cx.entity().downgrade();
|
||||
div()
|
||||
.id("diagnostic")
|
||||
.block()
|
||||
@@ -935,51 +944,29 @@ impl DiagnosticPopover {
|
||||
*keyboard_grace = false;
|
||||
cx.stop_propagation();
|
||||
})
|
||||
.when_some(self.parsed_content.clone(), |this, markdown| {
|
||||
this.child(
|
||||
div()
|
||||
.py_1()
|
||||
.px_2()
|
||||
.child(
|
||||
MarkdownElement::new(markdown, {
|
||||
let settings = ThemeSettings::get_global(cx);
|
||||
let mut base_text_style = window.text_style();
|
||||
base_text_style.refine(&TextStyleRefinement {
|
||||
font_family: Some(settings.ui_font.family.clone()),
|
||||
font_fallbacks: settings.ui_font.fallbacks.clone(),
|
||||
font_size: Some(settings.ui_font_size(cx).into()),
|
||||
color: Some(cx.theme().colors().editor_foreground),
|
||||
background_color: Some(gpui::transparent_black()),
|
||||
..Default::default()
|
||||
});
|
||||
MarkdownStyle {
|
||||
base_text_style,
|
||||
selection_background_color: {
|
||||
cx.theme().players().local().selection
|
||||
},
|
||||
link: TextStyleRefinement {
|
||||
underline: Some(gpui::UnderlineStyle {
|
||||
thickness: px(1.),
|
||||
color: Some(cx.theme().colors().editor_foreground),
|
||||
wavy: false,
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
})
|
||||
.code_block_renderer(markdown::CodeBlockRenderer::Default {
|
||||
copy_button: false,
|
||||
border: false,
|
||||
})
|
||||
.on_url_click(open_markdown_url),
|
||||
.child(
|
||||
div()
|
||||
.py_1()
|
||||
.px_2()
|
||||
.child(
|
||||
MarkdownElement::new(
|
||||
self.markdown.clone(),
|
||||
hover_markdown_style(window, cx),
|
||||
)
|
||||
.bg(self.background_color)
|
||||
.border_1()
|
||||
.border_color(self.border_color)
|
||||
.rounded_lg(),
|
||||
)
|
||||
})
|
||||
.on_url_click(move |link, window, cx| {
|
||||
if let Some(renderer) = GlobalDiagnosticRenderer::global(cx) {
|
||||
this.update(cx, |this, cx| {
|
||||
renderer.as_ref().open_link(this, link, window, cx);
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
}),
|
||||
)
|
||||
.bg(self.background_color)
|
||||
.border_1()
|
||||
.border_color(self.border_color)
|
||||
.rounded_lg(),
|
||||
)
|
||||
.into_any_element()
|
||||
}
|
||||
}
|
||||
@@ -998,8 +985,7 @@ mod tests {
|
||||
use collections::BTreeSet;
|
||||
use gpui::App;
|
||||
use indoc::indoc;
|
||||
use language::{Diagnostic, DiagnosticSet, language_settings::InlayHintSettings};
|
||||
use lsp::LanguageServerId;
|
||||
use language::language_settings::InlayHintSettings;
|
||||
use markdown::parser::MarkdownEvent;
|
||||
use smol::stream::StreamExt;
|
||||
use std::sync::atomic;
|
||||
@@ -1484,76 +1470,6 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_hover_diagnostic_and_info_popovers(cx: &mut gpui::TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
let mut cx = EditorLspTestContext::new_rust(
|
||||
lsp::ServerCapabilities {
|
||||
hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
|
||||
..Default::default()
|
||||
},
|
||||
cx,
|
||||
)
|
||||
.await;
|
||||
|
||||
// Hover with just diagnostic, pops DiagnosticPopover immediately and then
|
||||
// info popover once request completes
|
||||
cx.set_state(indoc! {"
|
||||
fn teˇst() { println!(); }
|
||||
"});
|
||||
|
||||
// Send diagnostic to client
|
||||
let range = cx.text_anchor_range(indoc! {"
|
||||
fn «test»() { println!(); }
|
||||
"});
|
||||
cx.update_buffer(|buffer, cx| {
|
||||
let snapshot = buffer.text_snapshot();
|
||||
let set = DiagnosticSet::from_sorted_entries(
|
||||
vec![DiagnosticEntry {
|
||||
range,
|
||||
diagnostic: Diagnostic {
|
||||
message: "A test diagnostic message.".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
}],
|
||||
&snapshot,
|
||||
);
|
||||
buffer.update_diagnostics(LanguageServerId(0), set, cx);
|
||||
});
|
||||
|
||||
// Hover pops diagnostic immediately
|
||||
cx.update_editor(|editor, window, cx| hover(editor, &Hover, window, cx));
|
||||
cx.background_executor.run_until_parked();
|
||||
|
||||
cx.editor(|Editor { hover_state, .. }, _, _| {
|
||||
assert!(
|
||||
hover_state.diagnostic_popover.is_some() && hover_state.info_popovers.is_empty()
|
||||
)
|
||||
});
|
||||
|
||||
// Info Popover shows after request responded to
|
||||
let range = cx.lsp_range(indoc! {"
|
||||
fn «test»() { println!(); }
|
||||
"});
|
||||
cx.set_request_handler::<lsp::request::HoverRequest, _, _>(move |_, _, _| async move {
|
||||
Ok(Some(lsp::Hover {
|
||||
contents: lsp::HoverContents::Markup(lsp::MarkupContent {
|
||||
kind: lsp::MarkupKind::Markdown,
|
||||
value: "some new docs".to_string(),
|
||||
}),
|
||||
range: Some(range),
|
||||
}))
|
||||
});
|
||||
cx.background_executor
|
||||
.advance_clock(Duration::from_millis(get_hover_popover_delay(&cx) + 100));
|
||||
|
||||
cx.background_executor.run_until_parked();
|
||||
cx.editor(|Editor { hover_state, .. }, _, _| {
|
||||
hover_state.diagnostic_popover.is_some() && hover_state.info_task.is_some()
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
// https://github.com/zed-industries/zed/issues/15498
|
||||
async fn test_info_hover_with_hrs(cx: &mut gpui::TestAppContext) {
|
||||
|
||||
Reference in New Issue
Block a user