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:
parent
8836c6fb42
commit
9d10489607
13 changed files with 517 additions and 327 deletions
|
@ -28,6 +28,7 @@ impl DiagnosticRenderer {
|
||||||
diagnostic_group: Vec<DiagnosticEntry<Point>>,
|
diagnostic_group: Vec<DiagnosticEntry<Point>>,
|
||||||
buffer_id: BufferId,
|
buffer_id: BufferId,
|
||||||
diagnostics_editor: Option<WeakEntity<ProjectDiagnosticsEditor>>,
|
diagnostics_editor: Option<WeakEntity<ProjectDiagnosticsEditor>>,
|
||||||
|
merge_same_row: bool,
|
||||||
cx: &mut App,
|
cx: &mut App,
|
||||||
) -> Vec<DiagnosticBlock> {
|
) -> Vec<DiagnosticBlock> {
|
||||||
let Some(primary_ix) = diagnostic_group
|
let Some(primary_ix) = diagnostic_group
|
||||||
|
@ -45,7 +46,7 @@ impl DiagnosticRenderer {
|
||||||
if entry.diagnostic.is_primary {
|
if entry.diagnostic.is_primary {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if entry.range.start.row == primary.range.start.row {
|
if entry.range.start.row == primary.range.start.row && merge_same_row {
|
||||||
same_row.push(entry)
|
same_row.push(entry)
|
||||||
} else if entry.range.start.row.abs_diff(primary.range.start.row) < 5 {
|
} else if entry.range.start.row.abs_diff(primary.range.start.row) < 5 {
|
||||||
close.push(entry)
|
close.push(entry)
|
||||||
|
@ -54,28 +55,48 @@ impl DiagnosticRenderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut markdown =
|
let mut markdown = String::new();
|
||||||
Markdown::escape(&if let Some(source) = primary.diagnostic.source.as_ref() {
|
let diagnostic = &primary.diagnostic;
|
||||||
format!("{}: {}", source, primary.diagnostic.message)
|
markdown.push_str(&Markdown::escape(&diagnostic.message));
|
||||||
} else {
|
|
||||||
primary.diagnostic.message
|
|
||||||
})
|
|
||||||
.to_string();
|
|
||||||
for entry in same_row {
|
for entry in same_row {
|
||||||
markdown.push_str("\n- hint: ");
|
markdown.push_str("\n- hint: ");
|
||||||
markdown.push_str(&Markdown::escape(&entry.diagnostic.message))
|
markdown.push_str(&Markdown::escape(&entry.diagnostic.message))
|
||||||
}
|
}
|
||||||
|
if diagnostic.source.is_some() || diagnostic.code.is_some() {
|
||||||
|
markdown.push_str(" (");
|
||||||
|
}
|
||||||
|
if let Some(source) = diagnostic.source.as_ref() {
|
||||||
|
markdown.push_str(&Markdown::escape(&source));
|
||||||
|
}
|
||||||
|
if diagnostic.source.is_some() && diagnostic.code.is_some() {
|
||||||
|
markdown.push(' ');
|
||||||
|
}
|
||||||
|
if let Some(code) = diagnostic.code.as_ref() {
|
||||||
|
if let Some(description) = diagnostic.code_description.as_ref() {
|
||||||
|
markdown.push('[');
|
||||||
|
markdown.push_str(&Markdown::escape(&code.to_string()));
|
||||||
|
markdown.push_str("](");
|
||||||
|
markdown.push_str(&Markdown::escape(description.as_ref()));
|
||||||
|
markdown.push(')');
|
||||||
|
} else {
|
||||||
|
markdown.push_str(&Markdown::escape(&code.to_string()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if diagnostic.source.is_some() || diagnostic.code.is_some() {
|
||||||
|
markdown.push(')');
|
||||||
|
}
|
||||||
|
|
||||||
for (ix, entry) in &distant {
|
for (ix, entry) in &distant {
|
||||||
markdown.push_str("\n- hint: [");
|
markdown.push_str("\n- hint: [");
|
||||||
markdown.push_str(&Markdown::escape(&entry.diagnostic.message));
|
markdown.push_str(&Markdown::escape(&entry.diagnostic.message));
|
||||||
markdown.push_str(&format!("](file://#diagnostic-{group_id}-{ix})\n",))
|
markdown.push_str(&format!(
|
||||||
|
"](file://#diagnostic-{buffer_id}-{group_id}-{ix})\n",
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut results = vec![DiagnosticBlock {
|
let mut results = vec![DiagnosticBlock {
|
||||||
initial_range: primary.range,
|
initial_range: primary.range,
|
||||||
severity: primary.diagnostic.severity,
|
severity: primary.diagnostic.severity,
|
||||||
buffer_id,
|
|
||||||
diagnostics_editor: diagnostics_editor.clone(),
|
diagnostics_editor: diagnostics_editor.clone(),
|
||||||
markdown: cx.new(|cx| Markdown::new(markdown.into(), None, None, cx)),
|
markdown: cx.new(|cx| Markdown::new(markdown.into(), None, None, cx)),
|
||||||
}];
|
}];
|
||||||
|
@ -91,7 +112,6 @@ impl DiagnosticRenderer {
|
||||||
results.push(DiagnosticBlock {
|
results.push(DiagnosticBlock {
|
||||||
initial_range: entry.range,
|
initial_range: entry.range,
|
||||||
severity: entry.diagnostic.severity,
|
severity: entry.diagnostic.severity,
|
||||||
buffer_id,
|
|
||||||
diagnostics_editor: diagnostics_editor.clone(),
|
diagnostics_editor: diagnostics_editor.clone(),
|
||||||
markdown: cx.new(|cx| Markdown::new(markdown.into(), None, None, cx)),
|
markdown: cx.new(|cx| Markdown::new(markdown.into(), None, None, cx)),
|
||||||
});
|
});
|
||||||
|
@ -105,15 +125,12 @@ impl DiagnosticRenderer {
|
||||||
};
|
};
|
||||||
let mut markdown = Markdown::escape(&markdown).to_string();
|
let mut markdown = Markdown::escape(&markdown).to_string();
|
||||||
markdown.push_str(&format!(
|
markdown.push_str(&format!(
|
||||||
" ([back](file://#diagnostic-{group_id}-{primary_ix}))"
|
" ([back](file://#diagnostic-{buffer_id}-{group_id}-{primary_ix}))"
|
||||||
));
|
));
|
||||||
// problem: group-id changes...
|
|
||||||
// - only an issue in diagnostics because caching
|
|
||||||
|
|
||||||
results.push(DiagnosticBlock {
|
results.push(DiagnosticBlock {
|
||||||
initial_range: entry.range,
|
initial_range: entry.range,
|
||||||
severity: entry.diagnostic.severity,
|
severity: entry.diagnostic.severity,
|
||||||
buffer_id,
|
|
||||||
diagnostics_editor: diagnostics_editor.clone(),
|
diagnostics_editor: diagnostics_editor.clone(),
|
||||||
markdown: cx.new(|cx| Markdown::new(markdown.into(), None, None, cx)),
|
markdown: cx.new(|cx| Markdown::new(markdown.into(), None, None, cx)),
|
||||||
});
|
});
|
||||||
|
@ -132,7 +149,7 @@ impl editor::DiagnosticRenderer for DiagnosticRenderer {
|
||||||
editor: WeakEntity<Editor>,
|
editor: WeakEntity<Editor>,
|
||||||
cx: &mut App,
|
cx: &mut App,
|
||||||
) -> Vec<BlockProperties<Anchor>> {
|
) -> Vec<BlockProperties<Anchor>> {
|
||||||
let blocks = Self::diagnostic_blocks_for_group(diagnostic_group, buffer_id, None, cx);
|
let blocks = Self::diagnostic_blocks_for_group(diagnostic_group, buffer_id, None, true, cx);
|
||||||
blocks
|
blocks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|block| {
|
.map(|block| {
|
||||||
|
@ -151,13 +168,40 @@ impl editor::DiagnosticRenderer for DiagnosticRenderer {
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_hover(
|
||||||
|
&self,
|
||||||
|
diagnostic_group: Vec<DiagnosticEntry<Point>>,
|
||||||
|
range: Range<Point>,
|
||||||
|
buffer_id: BufferId,
|
||||||
|
cx: &mut App,
|
||||||
|
) -> Option<Entity<Markdown>> {
|
||||||
|
let blocks =
|
||||||
|
Self::diagnostic_blocks_for_group(diagnostic_group, buffer_id, None, false, cx);
|
||||||
|
blocks.into_iter().find_map(|block| {
|
||||||
|
if block.initial_range == range {
|
||||||
|
Some(block.markdown)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_link(
|
||||||
|
&self,
|
||||||
|
editor: &mut Editor,
|
||||||
|
link: SharedString,
|
||||||
|
window: &mut Window,
|
||||||
|
cx: &mut Context<Editor>,
|
||||||
|
) {
|
||||||
|
DiagnosticBlock::open_link(editor, &None, link, window, cx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct DiagnosticBlock {
|
pub(crate) struct DiagnosticBlock {
|
||||||
pub(crate) initial_range: Range<Point>,
|
pub(crate) initial_range: Range<Point>,
|
||||||
pub(crate) severity: DiagnosticSeverity,
|
pub(crate) severity: DiagnosticSeverity,
|
||||||
pub(crate) buffer_id: BufferId,
|
|
||||||
pub(crate) markdown: Entity<Markdown>,
|
pub(crate) markdown: Entity<Markdown>,
|
||||||
pub(crate) diagnostics_editor: Option<WeakEntity<ProjectDiagnosticsEditor>>,
|
pub(crate) diagnostics_editor: Option<WeakEntity<ProjectDiagnosticsEditor>>,
|
||||||
}
|
}
|
||||||
|
@ -181,7 +225,6 @@ impl DiagnosticBlock {
|
||||||
let settings = ThemeSettings::get_global(cx);
|
let settings = ThemeSettings::get_global(cx);
|
||||||
let editor_line_height = (settings.line_height() * settings.buffer_font_size(cx)).round();
|
let editor_line_height = (settings.line_height() * settings.buffer_font_size(cx)).round();
|
||||||
let line_height = editor_line_height;
|
let line_height = editor_line_height;
|
||||||
let buffer_id = self.buffer_id;
|
|
||||||
let diagnostics_editor = self.diagnostics_editor.clone();
|
let diagnostics_editor = self.diagnostics_editor.clone();
|
||||||
|
|
||||||
div()
|
div()
|
||||||
|
@ -195,14 +238,11 @@ impl DiagnosticBlock {
|
||||||
MarkdownElement::new(self.markdown.clone(), hover_markdown_style(bcx.window, cx))
|
MarkdownElement::new(self.markdown.clone(), hover_markdown_style(bcx.window, cx))
|
||||||
.on_url_click({
|
.on_url_click({
|
||||||
move |link, window, cx| {
|
move |link, window, cx| {
|
||||||
Self::open_link(
|
editor
|
||||||
editor.clone(),
|
.update(cx, |editor, cx| {
|
||||||
&diagnostics_editor,
|
Self::open_link(editor, &diagnostics_editor, link, window, cx)
|
||||||
link,
|
})
|
||||||
window,
|
.ok();
|
||||||
buffer_id,
|
|
||||||
cx,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -210,24 +250,22 @@ impl DiagnosticBlock {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_link(
|
pub fn open_link(
|
||||||
editor: WeakEntity<Editor>,
|
editor: &mut Editor,
|
||||||
diagnostics_editor: &Option<WeakEntity<ProjectDiagnosticsEditor>>,
|
diagnostics_editor: &Option<WeakEntity<ProjectDiagnosticsEditor>>,
|
||||||
link: SharedString,
|
link: SharedString,
|
||||||
window: &mut Window,
|
window: &mut Window,
|
||||||
buffer_id: BufferId,
|
cx: &mut Context<Editor>,
|
||||||
cx: &mut App,
|
|
||||||
) {
|
) {
|
||||||
editor
|
|
||||||
.update(cx, |editor, cx| {
|
|
||||||
let Some(diagnostic_link) = link.strip_prefix("file://#diagnostic-") else {
|
let Some(diagnostic_link) = link.strip_prefix("file://#diagnostic-") else {
|
||||||
editor::hover_popover::open_markdown_url(link, window, cx);
|
editor::hover_popover::open_markdown_url(link, window, cx);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let Some((group_id, ix)) = maybe!({
|
let Some((buffer_id, group_id, ix)) = maybe!({
|
||||||
let (group_id, ix) = diagnostic_link.split_once('-')?;
|
let mut parts = diagnostic_link.split('-');
|
||||||
let group_id: usize = group_id.parse().ok()?;
|
let buffer_id: u64 = parts.next()?.parse().ok()?;
|
||||||
let ix: usize = ix.parse().ok()?;
|
let group_id: usize = parts.next()?.parse().ok()?;
|
||||||
Some((group_id, ix))
|
let ix: usize = parts.next()?.parse().ok()?;
|
||||||
|
Some((BufferId::new(buffer_id).ok()?, group_id, ix))
|
||||||
}) else {
|
}) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
@ -259,11 +297,7 @@ impl DiagnosticBlock {
|
||||||
if range.context.overlaps(&diagnostic.range, &snapshot) {
|
if range.context.overlaps(&diagnostic.range, &snapshot) {
|
||||||
Self::jump_to(
|
Self::jump_to(
|
||||||
editor,
|
editor,
|
||||||
Anchor::range_in_buffer(
|
Anchor::range_in_buffer(excerpt_id, buffer_id, diagnostic.range),
|
||||||
excerpt_id,
|
|
||||||
buffer_id,
|
|
||||||
diagnostic.range,
|
|
||||||
),
|
|
||||||
window,
|
window,
|
||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
|
@ -281,8 +315,6 @@ impl DiagnosticBlock {
|
||||||
Self::jump_to(editor, diagnostic.range, window, cx)
|
Self::jump_to(editor, diagnostic.range, window, cx)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})
|
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn jump_to<T: ToOffset>(
|
fn jump_to<T: ToOffset>(
|
||||||
|
|
|
@ -416,6 +416,7 @@ impl ProjectDiagnosticsEditor {
|
||||||
group,
|
group,
|
||||||
buffer_snapshot.remote_id(),
|
buffer_snapshot.remote_id(),
|
||||||
Some(this.clone()),
|
Some(this.clone()),
|
||||||
|
true,
|
||||||
cx,
|
cx,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
use collections::{HashMap, HashSet};
|
use collections::{HashMap, HashSet};
|
||||||
use editor::{
|
use editor::{
|
||||||
DisplayPoint, InlayId,
|
DisplayPoint, EditorSettings, InlayId,
|
||||||
actions::{GoToDiagnostic, GoToPreviousDiagnostic, MoveToBeginning},
|
actions::{GoToDiagnostic, GoToPreviousDiagnostic, Hover, MoveToBeginning},
|
||||||
display_map::{DisplayRow, Inlay},
|
display_map::{DisplayRow, Inlay},
|
||||||
test::{editor_content_with_blocks, editor_test_context::EditorTestContext},
|
test::{
|
||||||
|
editor_content_with_blocks, editor_lsp_test_context::EditorLspTestContext,
|
||||||
|
editor_test_context::EditorTestContext,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use gpui::{TestAppContext, VisualTestContext};
|
use gpui::{TestAppContext, VisualTestContext};
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
@ -134,11 +137,13 @@ async fn test_diagnostics(cx: &mut TestAppContext) {
|
||||||
// comment 1
|
// comment 1
|
||||||
// comment 2
|
// comment 2
|
||||||
c(y);
|
c(y);
|
||||||
§ use of moved value value used here after move
|
§ use of moved value
|
||||||
|
§ value used here after move
|
||||||
§ hint: move occurs because `y` has type `Vec<char>`, which does not
|
§ hint: move occurs because `y` has type `Vec<char>`, which does not
|
||||||
§ implement the `Copy` trait
|
§ implement the `Copy` trait
|
||||||
d(x);
|
d(x);
|
||||||
§ use of moved value value used here after move
|
§ use of moved value
|
||||||
|
§ value used here after move
|
||||||
§ hint: move occurs because `x` has type `Vec<char>`, which does not
|
§ hint: move occurs because `x` has type `Vec<char>`, which does not
|
||||||
§ implement the `Copy` trait
|
§ implement the `Copy` trait
|
||||||
§ hint: value moved here
|
§ hint: value moved here
|
||||||
|
@ -168,7 +173,7 @@ async fn test_diagnostics(cx: &mut TestAppContext) {
|
||||||
lsp::Position::new(0, 15),
|
lsp::Position::new(0, 15),
|
||||||
),
|
),
|
||||||
severity: Some(lsp::DiagnosticSeverity::ERROR),
|
severity: Some(lsp::DiagnosticSeverity::ERROR),
|
||||||
message: "mismatched types\nexpected `usize`, found `char`".to_string(),
|
message: "mismatched types expected `usize`, found `char`".to_string(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}],
|
}],
|
||||||
version: None,
|
version: None,
|
||||||
|
@ -206,11 +211,13 @@ async fn test_diagnostics(cx: &mut TestAppContext) {
|
||||||
// comment 1
|
// comment 1
|
||||||
// comment 2
|
// comment 2
|
||||||
c(y);
|
c(y);
|
||||||
§ use of moved value value used here after move
|
§ use of moved value
|
||||||
|
§ value used here after move
|
||||||
§ hint: move occurs because `y` has type `Vec<char>`, which does not
|
§ hint: move occurs because `y` has type `Vec<char>`, which does not
|
||||||
§ implement the `Copy` trait
|
§ implement the `Copy` trait
|
||||||
d(x);
|
d(x);
|
||||||
§ use of moved value value used here after move
|
§ use of moved value
|
||||||
|
§ value used here after move
|
||||||
§ hint: move occurs because `x` has type `Vec<char>`, which does not
|
§ hint: move occurs because `x` has type `Vec<char>`, which does not
|
||||||
§ implement the `Copy` trait
|
§ implement the `Copy` trait
|
||||||
§ hint: value moved here
|
§ hint: value moved here
|
||||||
|
@ -241,7 +248,7 @@ async fn test_diagnostics(cx: &mut TestAppContext) {
|
||||||
lsp::Position::new(0, 15),
|
lsp::Position::new(0, 15),
|
||||||
),
|
),
|
||||||
severity: Some(lsp::DiagnosticSeverity::ERROR),
|
severity: Some(lsp::DiagnosticSeverity::ERROR),
|
||||||
message: "mismatched types\nexpected `usize`, found `char`".to_string(),
|
message: "mismatched types expected `usize`, found `char`".to_string(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
lsp::Diagnostic {
|
lsp::Diagnostic {
|
||||||
|
@ -289,11 +296,13 @@ async fn test_diagnostics(cx: &mut TestAppContext) {
|
||||||
// comment 1
|
// comment 1
|
||||||
// comment 2
|
// comment 2
|
||||||
c(y);
|
c(y);
|
||||||
§ use of moved value value used here after move
|
§ use of moved value
|
||||||
|
§ value used here after move
|
||||||
§ hint: move occurs because `y` has type `Vec<char>`, which does not
|
§ hint: move occurs because `y` has type `Vec<char>`, which does not
|
||||||
§ implement the `Copy` trait
|
§ implement the `Copy` trait
|
||||||
d(x);
|
d(x);
|
||||||
§ use of moved value value used here after move
|
§ use of moved value
|
||||||
|
§ value used here after move
|
||||||
§ hint: move occurs because `x` has type `Vec<char>`, which does not
|
§ hint: move occurs because `x` has type `Vec<char>`, which does not
|
||||||
§ implement the `Copy` trait
|
§ implement the `Copy` trait
|
||||||
§ hint: value moved here
|
§ hint: value moved here
|
||||||
|
@ -1192,8 +1201,219 @@ async fn cycle_through_same_place_diagnostics(cx: &mut TestAppContext) {
|
||||||
"});
|
"});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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| {
|
||||||
|
editor::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_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.lsp_range(indoc! {"
|
||||||
|
fn «test»() { println!(); }
|
||||||
|
"});
|
||||||
|
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/dir/file.rs")).unwrap(),
|
||||||
|
version: None,
|
||||||
|
diagnostics: vec![lsp::Diagnostic {
|
||||||
|
range,
|
||||||
|
severity: Some(lsp::DiagnosticSeverity::ERROR),
|
||||||
|
message: "A test diagnostic message.".to_string(),
|
||||||
|
..Default::default()
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
&[],
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
cx.run_until_parked();
|
||||||
|
|
||||||
|
// Hover pops diagnostic immediately
|
||||||
|
cx.update_editor(|editor, window, cx| editor::hover_popover::hover(editor, &Hover, window, cx));
|
||||||
|
cx.background_executor.run_until_parked();
|
||||||
|
|
||||||
|
cx.editor(|Editor { hover_state, .. }, _, _| {
|
||||||
|
assert!(hover_state.diagnostic_popover.is_some());
|
||||||
|
assert!(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),
|
||||||
|
}))
|
||||||
|
});
|
||||||
|
let delay = cx.update(|_, cx| EditorSettings::get_global(cx).hover_popover_delay + 1);
|
||||||
|
cx.background_executor
|
||||||
|
.advance_clock(Duration::from_millis(delay));
|
||||||
|
|
||||||
|
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]
|
||||||
|
async fn test_diagnostics_with_code(cx: &mut TestAppContext) {
|
||||||
|
init_test(cx);
|
||||||
|
|
||||||
|
let fs = FakeFs::new(cx.executor());
|
||||||
|
fs.insert_tree(
|
||||||
|
path!("/root"),
|
||||||
|
json!({
|
||||||
|
"main.js": "
|
||||||
|
function test() {
|
||||||
|
const x = 10;
|
||||||
|
const y = 20;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
test();
|
||||||
|
"
|
||||||
|
.unindent(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let language_server_id = LanguageServerId(0);
|
||||||
|
let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
|
||||||
|
let lsp_store = project.read_with(cx, |project, _| project.lsp_store());
|
||||||
|
let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
|
||||||
|
let cx = &mut VisualTestContext::from_window(*window, cx);
|
||||||
|
let workspace = window.root(cx).unwrap();
|
||||||
|
let uri = lsp::Url::from_file_path(path!("/root/main.js")).unwrap();
|
||||||
|
|
||||||
|
// Create diagnostics with code fields
|
||||||
|
lsp_store.update(cx, |lsp_store, cx| {
|
||||||
|
lsp_store
|
||||||
|
.update_diagnostics(
|
||||||
|
language_server_id,
|
||||||
|
lsp::PublishDiagnosticsParams {
|
||||||
|
uri: uri.clone(),
|
||||||
|
diagnostics: vec![
|
||||||
|
lsp::Diagnostic {
|
||||||
|
range: lsp::Range::new(
|
||||||
|
lsp::Position::new(1, 4),
|
||||||
|
lsp::Position::new(1, 14),
|
||||||
|
),
|
||||||
|
severity: Some(lsp::DiagnosticSeverity::WARNING),
|
||||||
|
code: Some(lsp::NumberOrString::String("no-unused-vars".to_string())),
|
||||||
|
source: Some("eslint".to_string()),
|
||||||
|
message: "'x' is assigned a value but never used".to_string(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
lsp::Diagnostic {
|
||||||
|
range: lsp::Range::new(
|
||||||
|
lsp::Position::new(2, 4),
|
||||||
|
lsp::Position::new(2, 14),
|
||||||
|
),
|
||||||
|
severity: Some(lsp::DiagnosticSeverity::WARNING),
|
||||||
|
code: Some(lsp::NumberOrString::String("no-unused-vars".to_string())),
|
||||||
|
source: Some("eslint".to_string()),
|
||||||
|
message: "'y' is assigned a value but never used".to_string(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
],
|
||||||
|
version: None,
|
||||||
|
},
|
||||||
|
&[],
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Open the project diagnostics view
|
||||||
|
let diagnostics = window.build_entity(cx, |window, cx| {
|
||||||
|
ProjectDiagnosticsEditor::new(true, project.clone(), workspace.downgrade(), window, cx)
|
||||||
|
});
|
||||||
|
let editor = diagnostics.update(cx, |diagnostics, _| diagnostics.editor.clone());
|
||||||
|
|
||||||
|
diagnostics
|
||||||
|
.next_notification(DIAGNOSTICS_UPDATE_DELAY + Duration::from_millis(10), cx)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Verify that the diagnostic codes are displayed correctly
|
||||||
|
pretty_assertions::assert_eq!(
|
||||||
|
editor_content_with_blocks(&editor, cx),
|
||||||
|
indoc::indoc! {
|
||||||
|
"§ main.js
|
||||||
|
§ -----
|
||||||
|
function test() {
|
||||||
|
const x = 10; § 'x' is assigned a value but never used (eslint no-unused-vars)
|
||||||
|
const y = 20; § 'y' is assigned a value but never used (eslint no-unused-vars)
|
||||||
|
return 1;
|
||||||
|
}"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn init_test(cx: &mut TestAppContext) {
|
fn init_test(cx: &mut TestAppContext) {
|
||||||
cx.update(|cx| {
|
cx.update(|cx| {
|
||||||
|
env_logger::try_init().ok();
|
||||||
let settings = SettingsStore::test(cx);
|
let settings = SettingsStore::test(cx);
|
||||||
cx.set_global(settings);
|
cx.set_global(settings);
|
||||||
theme::init(theme::LoadThemes::JustBase, cx);
|
theme::init(theme::LoadThemes::JustBase, cx);
|
||||||
|
|
|
@ -394,10 +394,32 @@ pub trait DiagnosticRenderer {
|
||||||
editor: WeakEntity<Editor>,
|
editor: WeakEntity<Editor>,
|
||||||
cx: &mut App,
|
cx: &mut App,
|
||||||
) -> Vec<BlockProperties<Anchor>>;
|
) -> 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>);
|
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 {}
|
impl gpui::Global for GlobalDiagnosticRenderer {}
|
||||||
pub fn set_diagnostic_renderer(renderer: impl DiagnosticRenderer + 'static, cx: &mut App) {
|
pub fn set_diagnostic_renderer(renderer: impl DiagnosticRenderer + 'static, cx: &mut App) {
|
||||||
cx.set_global(GlobalDiagnosticRenderer(Arc::new(renderer)));
|
cx.set_global(GlobalDiagnosticRenderer(Arc::new(renderer)));
|
||||||
|
@ -867,7 +889,7 @@ pub struct Editor {
|
||||||
read_only: bool,
|
read_only: bool,
|
||||||
leader_peer_id: Option<PeerId>,
|
leader_peer_id: Option<PeerId>,
|
||||||
remote_id: Option<ViewId>,
|
remote_id: Option<ViewId>,
|
||||||
hover_state: HoverState,
|
pub hover_state: HoverState,
|
||||||
pending_mouse_down: Option<Rc<RefCell<Option<MouseDownEvent>>>>,
|
pending_mouse_down: Option<Rc<RefCell<Option<MouseDownEvent>>>>,
|
||||||
gutter_hovered: bool,
|
gutter_hovered: bool,
|
||||||
hovered_link_state: Option<HoveredLinkState>,
|
hovered_link_state: Option<HoveredLinkState>,
|
||||||
|
@ -14788,25 +14810,17 @@ impl Editor {
|
||||||
}
|
}
|
||||||
self.dismiss_diagnostics(cx);
|
self.dismiss_diagnostics(cx);
|
||||||
let snapshot = self.snapshot(window, cx);
|
let snapshot = self.snapshot(window, cx);
|
||||||
let Some(diagnostic_renderer) = cx
|
let buffer = self.buffer.read(cx).snapshot(cx);
|
||||||
.try_global::<GlobalDiagnosticRenderer>()
|
let Some(renderer) = GlobalDiagnosticRenderer::global(cx) else {
|
||||||
.map(|g| g.0.clone())
|
|
||||||
else {
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let buffer = self.buffer.read(cx).snapshot(cx);
|
|
||||||
|
|
||||||
let diagnostic_group = buffer
|
let diagnostic_group = buffer
|
||||||
.diagnostic_group(buffer_id, diagnostic.diagnostic.group_id)
|
.diagnostic_group(buffer_id, diagnostic.diagnostic.group_id)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let blocks = diagnostic_renderer.render_group(
|
let blocks =
|
||||||
diagnostic_group,
|
renderer.render_group(diagnostic_group, buffer_id, snapshot, cx.weak_entity(), cx);
|
||||||
buffer_id,
|
|
||||||
snapshot,
|
|
||||||
cx.weak_entity(),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
|
|
||||||
let blocks = self.display_map.update(cx, |display_map, cx| {
|
let blocks = self.display_map.update(cx, |display_map, cx| {
|
||||||
display_map.insert_blocks(blocks, cx).into_iter().collect()
|
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]
|
#[gpui::test]
|
||||||
async fn test_go_to_hunk(executor: BackgroundExecutor, cx: &mut TestAppContext) {
|
async fn test_go_to_hunk(executor: BackgroundExecutor, cx: &mut TestAppContext) {
|
||||||
init_test(cx, |_| {});
|
init_test(cx, |_| {});
|
||||||
|
|
|
@ -71,7 +71,7 @@ pub enum HoverLink {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub(crate) struct InlayHighlight {
|
pub struct InlayHighlight {
|
||||||
pub inlay: InlayId,
|
pub inlay: InlayId,
|
||||||
pub inlay_position: Anchor,
|
pub inlay_position: Anchor,
|
||||||
pub range: Range<usize>,
|
pub range: Range<usize>,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
ActiveDiagnostic, Anchor, AnchorRangeExt, DisplayPoint, DisplayRow, Editor, EditorSettings,
|
ActiveDiagnostic, Anchor, AnchorRangeExt, DisplayPoint, DisplayRow, Editor, EditorSettings,
|
||||||
EditorSnapshot, Hover,
|
EditorSnapshot, GlobalDiagnosticRenderer, Hover,
|
||||||
display_map::{InlayOffset, ToDisplayPoint, invisibles::is_invisible},
|
display_map::{InlayOffset, ToDisplayPoint, invisibles::is_invisible},
|
||||||
hover_links::{InlayHighlight, RangeInEditor},
|
hover_links::{InlayHighlight, RangeInEditor},
|
||||||
scroll::{Autoscroll, ScrollAmount},
|
scroll::{Autoscroll, ScrollAmount},
|
||||||
|
@ -15,7 +15,7 @@ use itertools::Itertools;
|
||||||
use language::{DiagnosticEntry, Language, LanguageRegistry};
|
use language::{DiagnosticEntry, Language, LanguageRegistry};
|
||||||
use lsp::DiagnosticSeverity;
|
use lsp::DiagnosticSeverity;
|
||||||
use markdown::{Markdown, MarkdownElement, MarkdownStyle};
|
use markdown::{Markdown, MarkdownElement, MarkdownStyle};
|
||||||
use multi_buffer::{MultiOrSingleBufferOffsetRange, ToOffset};
|
use multi_buffer::{MultiOrSingleBufferOffsetRange, ToOffset, ToPoint};
|
||||||
use project::{HoverBlock, HoverBlockKind, InlayHintLabelPart};
|
use project::{HoverBlock, HoverBlockKind, InlayHintLabelPart};
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use std::{borrow::Cow, cell::RefCell};
|
use std::{borrow::Cow, cell::RefCell};
|
||||||
|
@ -81,7 +81,7 @@ pub fn show_keyboard_hover(
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|d| {
|
.and_then(|d| {
|
||||||
if *d.keyboard_grace.borrow() {
|
if *d.keyboard_grace.borrow() {
|
||||||
d.anchor
|
Some(d.anchor)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -283,6 +283,7 @@ fn show_hover(
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let renderer = GlobalDiagnosticRenderer::global(cx);
|
||||||
let task = cx.spawn_in(window, async move |this, cx| {
|
let task = cx.spawn_in(window, async move |this, cx| {
|
||||||
async move {
|
async move {
|
||||||
// If we need to delay, delay a set amount initially before making the lsp request
|
// If we need to delay, delay a set amount initially before making the lsp request
|
||||||
|
@ -313,28 +314,35 @@ fn show_hover(
|
||||||
} else {
|
} else {
|
||||||
snapshot
|
snapshot
|
||||||
.buffer_snapshot
|
.buffer_snapshot
|
||||||
.diagnostics_in_range::<usize>(offset..offset)
|
.diagnostics_with_buffer_ids_in_range::<usize>(offset..offset)
|
||||||
.filter(|diagnostic| Some(diagnostic.diagnostic.group_id) != active_group_id)
|
.filter(|(_, diagnostic)| {
|
||||||
|
Some(diagnostic.diagnostic.group_id) != active_group_id
|
||||||
|
})
|
||||||
// Find the entry with the most specific range
|
// 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 diagnostic_popover = if let Some((buffer_id, local_diagnostic)) = local_diagnostic {
|
||||||
let text = match local_diagnostic.diagnostic.source {
|
let group = snapshot
|
||||||
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
|
.buffer_snapshot
|
||||||
.anchor_before(local_diagnostic.range.start)
|
.diagnostic_group(buffer_id, local_diagnostic.diagnostic.group_id)
|
||||||
..snapshot
|
.collect::<Vec<_>>();
|
||||||
.buffer_snapshot
|
let point_range = local_diagnostic
|
||||||
.anchor_after(local_diagnostic.range.end),
|
.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 (background_color, border_color) = cx.update(|_, cx| {
|
||||||
let status_colors = cx.theme().status();
|
let status_colors = cx.theme().status();
|
||||||
|
@ -359,28 +367,26 @@ fn show_hover(
|
||||||
}
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let parsed_content = cx
|
let subscription =
|
||||||
.new(|cx| Markdown::new_text(SharedString::new(text), cx))
|
this.update(cx, |_, cx| cx.observe(&markdown, |_, _, cx| cx.notify()))?;
|
||||||
.ok();
|
|
||||||
|
|
||||||
let subscription = this
|
let local_diagnostic = DiagnosticEntry {
|
||||||
.update(cx, |_, cx| {
|
diagnostic: local_diagnostic.diagnostic,
|
||||||
if let Some(parsed_content) = &parsed_content {
|
range: snapshot
|
||||||
Some(cx.observe(parsed_content, |_, _, cx| cx.notify()))
|
.buffer_snapshot
|
||||||
} else {
|
.anchor_before(local_diagnostic.range.start)
|
||||||
None
|
..snapshot
|
||||||
}
|
.buffer_snapshot
|
||||||
})
|
.anchor_after(local_diagnostic.range.end),
|
||||||
.ok()
|
};
|
||||||
.flatten();
|
|
||||||
|
|
||||||
Some(DiagnosticPopover {
|
Some(DiagnosticPopover {
|
||||||
local_diagnostic,
|
local_diagnostic,
|
||||||
parsed_content,
|
markdown,
|
||||||
border_color,
|
border_color,
|
||||||
background_color,
|
background_color,
|
||||||
keyboard_grace: Rc::new(RefCell::new(ignore_timeout)),
|
keyboard_grace: Rc::new(RefCell::new(ignore_timeout)),
|
||||||
anchor: Some(anchor),
|
anchor,
|
||||||
_subscription: subscription,
|
_subscription: subscription,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -719,7 +725,7 @@ pub fn open_markdown_url(link: SharedString, window: &mut Window, cx: &mut App)
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct HoverState {
|
pub struct HoverState {
|
||||||
pub(crate) info_popovers: Vec<InfoPopover>,
|
pub info_popovers: Vec<InfoPopover>,
|
||||||
pub diagnostic_popover: Option<DiagnosticPopover>,
|
pub diagnostic_popover: Option<DiagnosticPopover>,
|
||||||
pub triggered_from: Option<Anchor>,
|
pub triggered_from: Option<Anchor>,
|
||||||
pub info_task: Option<Task<Option<()>>>,
|
pub info_task: Option<Task<Option<()>>>,
|
||||||
|
@ -789,23 +795,25 @@ impl HoverState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(diagnostic_popover) = &self.diagnostic_popover {
|
if let Some(diagnostic_popover) = &self.diagnostic_popover {
|
||||||
if let Some(markdown_view) = &diagnostic_popover.parsed_content {
|
if diagnostic_popover
|
||||||
if markdown_view.focus_handle(cx).is_focused(window) {
|
.markdown
|
||||||
|
.focus_handle(cx)
|
||||||
|
.is_focused(window)
|
||||||
|
{
|
||||||
hover_popover_is_focused = true;
|
hover_popover_is_focused = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
hover_popover_is_focused
|
hover_popover_is_focused
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct InfoPopover {
|
pub struct InfoPopover {
|
||||||
pub(crate) symbol_range: RangeInEditor,
|
pub symbol_range: RangeInEditor,
|
||||||
pub(crate) parsed_content: Option<Entity<Markdown>>,
|
pub parsed_content: Option<Entity<Markdown>>,
|
||||||
pub(crate) scroll_handle: ScrollHandle,
|
pub scroll_handle: ScrollHandle,
|
||||||
pub(crate) scrollbar_state: ScrollbarState,
|
pub scrollbar_state: ScrollbarState,
|
||||||
pub(crate) keyboard_grace: Rc<RefCell<bool>>,
|
pub keyboard_grace: Rc<RefCell<bool>>,
|
||||||
pub(crate) anchor: Option<Anchor>,
|
pub anchor: Option<Anchor>,
|
||||||
_subscription: Option<Subscription>,
|
_subscription: Option<Subscription>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -897,12 +905,12 @@ impl InfoPopover {
|
||||||
|
|
||||||
pub struct DiagnosticPopover {
|
pub struct DiagnosticPopover {
|
||||||
pub(crate) local_diagnostic: DiagnosticEntry<Anchor>,
|
pub(crate) local_diagnostic: DiagnosticEntry<Anchor>,
|
||||||
parsed_content: Option<Entity<Markdown>>,
|
markdown: Entity<Markdown>,
|
||||||
border_color: Hsla,
|
border_color: Hsla,
|
||||||
background_color: Hsla,
|
background_color: Hsla,
|
||||||
pub keyboard_grace: Rc<RefCell<bool>>,
|
pub keyboard_grace: Rc<RefCell<bool>>,
|
||||||
pub anchor: Option<Anchor>,
|
pub anchor: Anchor,
|
||||||
_subscription: Option<Subscription>,
|
_subscription: Subscription,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DiagnosticPopover {
|
impl DiagnosticPopover {
|
||||||
|
@ -913,6 +921,7 @@ impl DiagnosticPopover {
|
||||||
cx: &mut Context<Editor>,
|
cx: &mut Context<Editor>,
|
||||||
) -> AnyElement {
|
) -> AnyElement {
|
||||||
let keyboard_grace = Rc::clone(&self.keyboard_grace);
|
let keyboard_grace = Rc::clone(&self.keyboard_grace);
|
||||||
|
let this = cx.entity().downgrade();
|
||||||
div()
|
div()
|
||||||
.id("diagnostic")
|
.id("diagnostic")
|
||||||
.block()
|
.block()
|
||||||
|
@ -935,51 +944,29 @@ impl DiagnosticPopover {
|
||||||
*keyboard_grace = false;
|
*keyboard_grace = false;
|
||||||
cx.stop_propagation();
|
cx.stop_propagation();
|
||||||
})
|
})
|
||||||
.when_some(self.parsed_content.clone(), |this, markdown| {
|
.child(
|
||||||
this.child(
|
|
||||||
div()
|
div()
|
||||||
.py_1()
|
.py_1()
|
||||||
.px_2()
|
.px_2()
|
||||||
.child(
|
.child(
|
||||||
MarkdownElement::new(markdown, {
|
MarkdownElement::new(
|
||||||
let settings = ThemeSettings::get_global(cx);
|
self.markdown.clone(),
|
||||||
let mut base_text_style = window.text_style();
|
hover_markdown_style(window, cx),
|
||||||
base_text_style.refine(&TextStyleRefinement {
|
)
|
||||||
font_family: Some(settings.ui_font.family.clone()),
|
.on_url_click(move |link, window, cx| {
|
||||||
font_fallbacks: settings.ui_font.fallbacks.clone(),
|
if let Some(renderer) = GlobalDiagnosticRenderer::global(cx) {
|
||||||
font_size: Some(settings.ui_font_size(cx).into()),
|
this.update(cx, |this, cx| {
|
||||||
color: Some(cx.theme().colors().editor_foreground),
|
renderer.as_ref().open_link(this, link, window, cx);
|
||||||
background_color: Some(gpui::transparent_black()),
|
})
|
||||||
..Default::default()
|
.ok();
|
||||||
});
|
|
||||||
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),
|
|
||||||
)
|
)
|
||||||
.bg(self.background_color)
|
.bg(self.background_color)
|
||||||
.border_1()
|
.border_1()
|
||||||
.border_color(self.border_color)
|
.border_color(self.border_color)
|
||||||
.rounded_lg(),
|
.rounded_lg(),
|
||||||
)
|
)
|
||||||
})
|
|
||||||
.into_any_element()
|
.into_any_element()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -998,8 +985,7 @@ mod tests {
|
||||||
use collections::BTreeSet;
|
use collections::BTreeSet;
|
||||||
use gpui::App;
|
use gpui::App;
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
use language::{Diagnostic, DiagnosticSet, language_settings::InlayHintSettings};
|
use language::language_settings::InlayHintSettings;
|
||||||
use lsp::LanguageServerId;
|
|
||||||
use markdown::parser::MarkdownEvent;
|
use markdown::parser::MarkdownEvent;
|
||||||
use smol::stream::StreamExt;
|
use smol::stream::StreamExt;
|
||||||
use std::sync::atomic;
|
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]
|
#[gpui::test]
|
||||||
// https://github.com/zed-industries/zed/issues/15498
|
// https://github.com/zed-industries/zed/issues/15498
|
||||||
async fn test_info_hover_with_hrs(cx: &mut gpui::TestAppContext) {
|
async fn test_info_hover_with_hrs(cx: &mut gpui::TestAppContext) {
|
||||||
|
|
|
@ -208,6 +208,7 @@ pub struct Diagnostic {
|
||||||
pub source: Option<String>,
|
pub source: Option<String>,
|
||||||
/// A machine-readable code that identifies this diagnostic.
|
/// A machine-readable code that identifies this diagnostic.
|
||||||
pub code: Option<NumberOrString>,
|
pub code: Option<NumberOrString>,
|
||||||
|
pub code_description: Option<lsp::Url>,
|
||||||
/// Whether this diagnostic is a hint, warning, or error.
|
/// Whether this diagnostic is a hint, warning, or error.
|
||||||
pub severity: DiagnosticSeverity,
|
pub severity: DiagnosticSeverity,
|
||||||
/// The human-readable message associated with this diagnostic.
|
/// The human-readable message associated with this diagnostic.
|
||||||
|
@ -4612,6 +4613,7 @@ impl Default for Diagnostic {
|
||||||
Self {
|
Self {
|
||||||
source: Default::default(),
|
source: Default::default(),
|
||||||
code: None,
|
code: None,
|
||||||
|
code_description: None,
|
||||||
severity: DiagnosticSeverity::ERROR,
|
severity: DiagnosticSeverity::ERROR,
|
||||||
message: Default::default(),
|
message: Default::default(),
|
||||||
group_id: 0,
|
group_id: 0,
|
||||||
|
|
|
@ -213,6 +213,11 @@ pub fn serialize_diagnostics<'a>(
|
||||||
group_id: entry.diagnostic.group_id as u64,
|
group_id: entry.diagnostic.group_id as u64,
|
||||||
is_primary: entry.diagnostic.is_primary,
|
is_primary: entry.diagnostic.is_primary,
|
||||||
code: entry.diagnostic.code.as_ref().map(|s| s.to_string()),
|
code: entry.diagnostic.code.as_ref().map(|s| s.to_string()),
|
||||||
|
code_description: entry
|
||||||
|
.diagnostic
|
||||||
|
.code_description
|
||||||
|
.as_ref()
|
||||||
|
.map(|s| s.to_string()),
|
||||||
is_disk_based: entry.diagnostic.is_disk_based,
|
is_disk_based: entry.diagnostic.is_disk_based,
|
||||||
is_unnecessary: entry.diagnostic.is_unnecessary,
|
is_unnecessary: entry.diagnostic.is_unnecessary,
|
||||||
data: entry.diagnostic.data.as_ref().map(|data| data.to_string()),
|
data: entry.diagnostic.data.as_ref().map(|data| data.to_string()),
|
||||||
|
@ -419,6 +424,9 @@ pub fn deserialize_diagnostics(
|
||||||
message: diagnostic.message,
|
message: diagnostic.message,
|
||||||
group_id: diagnostic.group_id as usize,
|
group_id: diagnostic.group_id as usize,
|
||||||
code: diagnostic.code.map(lsp::NumberOrString::from_string),
|
code: diagnostic.code.map(lsp::NumberOrString::from_string),
|
||||||
|
code_description: diagnostic
|
||||||
|
.code_description
|
||||||
|
.and_then(|s| lsp::Url::parse(&s).ok()),
|
||||||
is_primary: diagnostic.is_primary,
|
is_primary: diagnostic.is_primary,
|
||||||
is_disk_based: diagnostic.is_disk_based,
|
is_disk_based: diagnostic.is_disk_based,
|
||||||
is_unnecessary: diagnostic.is_unnecessary,
|
is_unnecessary: diagnostic.is_unnecessary,
|
||||||
|
|
|
@ -215,11 +215,16 @@ impl Markdown {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn escape(s: &str) -> Cow<str> {
|
pub fn escape(s: &str) -> Cow<str> {
|
||||||
let count = s.bytes().filter(|c| c.is_ascii_punctuation()).count();
|
let count = s
|
||||||
|
.bytes()
|
||||||
|
.filter(|c| *c == b'\n' || c.is_ascii_punctuation())
|
||||||
|
.count();
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
let mut output = String::with_capacity(s.len() + count);
|
let mut output = String::with_capacity(s.len() + count);
|
||||||
for c in s.chars() {
|
for c in s.chars() {
|
||||||
if c.is_ascii_punctuation() {
|
if c == '\n' {
|
||||||
|
output.push('\n')
|
||||||
|
} else if c.is_ascii_punctuation() {
|
||||||
output.push('\\')
|
output.push('\\')
|
||||||
}
|
}
|
||||||
output.push(c)
|
output.push(c)
|
||||||
|
|
|
@ -5950,6 +5950,29 @@ impl MultiBufferSnapshot {
|
||||||
.map(|(range, diagnostic, _)| DiagnosticEntry { diagnostic, range })
|
.map(|(range, diagnostic, _)| DiagnosticEntry { diagnostic, range })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn diagnostics_with_buffer_ids_in_range<'a, T>(
|
||||||
|
&'a self,
|
||||||
|
range: Range<T>,
|
||||||
|
) -> impl Iterator<Item = (BufferId, DiagnosticEntry<T>)> + 'a
|
||||||
|
where
|
||||||
|
T: 'a
|
||||||
|
+ text::ToOffset
|
||||||
|
+ text::FromAnchor
|
||||||
|
+ TextDimension
|
||||||
|
+ Ord
|
||||||
|
+ Sub<T, Output = T>
|
||||||
|
+ fmt::Debug,
|
||||||
|
{
|
||||||
|
self.lift_buffer_metadata(range, move |buffer, buffer_range| {
|
||||||
|
Some(
|
||||||
|
buffer
|
||||||
|
.diagnostics_in_range(buffer_range.start..buffer_range.end, false)
|
||||||
|
.map(|entry| (entry.range, entry.diagnostic)),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.map(|(range, diagnostic, b)| (b.buffer_id, DiagnosticEntry { diagnostic, range }))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn syntax_ancestor<T: ToOffset>(
|
pub fn syntax_ancestor<T: ToOffset>(
|
||||||
&self,
|
&self,
|
||||||
range: Range<T>,
|
range: Range<T>,
|
||||||
|
|
|
@ -8613,6 +8613,10 @@ impl LspStore {
|
||||||
diagnostic: Diagnostic {
|
diagnostic: Diagnostic {
|
||||||
source: diagnostic.source.clone(),
|
source: diagnostic.source.clone(),
|
||||||
code: diagnostic.code.clone(),
|
code: diagnostic.code.clone(),
|
||||||
|
code_description: diagnostic
|
||||||
|
.code_description
|
||||||
|
.as_ref()
|
||||||
|
.map(|d| d.href.clone()),
|
||||||
severity: diagnostic.severity.unwrap_or(DiagnosticSeverity::ERROR),
|
severity: diagnostic.severity.unwrap_or(DiagnosticSeverity::ERROR),
|
||||||
message: diagnostic.message.trim().to_string(),
|
message: diagnostic.message.trim().to_string(),
|
||||||
group_id,
|
group_id,
|
||||||
|
@ -8631,6 +8635,10 @@ impl LspStore {
|
||||||
diagnostic: Diagnostic {
|
diagnostic: Diagnostic {
|
||||||
source: diagnostic.source.clone(),
|
source: diagnostic.source.clone(),
|
||||||
code: diagnostic.code.clone(),
|
code: diagnostic.code.clone(),
|
||||||
|
code_description: diagnostic
|
||||||
|
.code_description
|
||||||
|
.as_ref()
|
||||||
|
.map(|c| c.href.clone()),
|
||||||
severity: DiagnosticSeverity::INFORMATION,
|
severity: DiagnosticSeverity::INFORMATION,
|
||||||
message: info.message.trim().to_string(),
|
message: info.message.trim().to_string(),
|
||||||
group_id,
|
group_id,
|
||||||
|
|
|
@ -270,6 +270,7 @@ message Diagnostic {
|
||||||
Hint = 4;
|
Hint = 4;
|
||||||
}
|
}
|
||||||
optional string data = 12;
|
optional string data = 12;
|
||||||
|
optional string code_description = 13;
|
||||||
}
|
}
|
||||||
|
|
||||||
message SearchQuery {
|
message SearchQuery {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue