Port to gpui2

This commit is contained in:
Kirill Bulatov 2023-11-30 15:46:35 +02:00
parent 69bfd47cf9
commit 091d93533d
5 changed files with 127 additions and 37 deletions

View file

@ -3688,7 +3688,7 @@ async fn test_collaborating_with_diagnostics(
project_b.read_with(cx_b, |project, cx| { project_b.read_with(cx_b, |project, cx| {
assert_eq!( assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(), project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
&[( &[(
ProjectPath { ProjectPath {
worktree_id, worktree_id,
@ -3708,14 +3708,14 @@ async fn test_collaborating_with_diagnostics(
let project_c = client_c.build_remote_project(project_id, cx_c).await; let project_c = client_c.build_remote_project(project_id, cx_c).await;
let project_c_diagnostic_summaries = let project_c_diagnostic_summaries =
Rc::new(RefCell::new(project_c.read_with(cx_c, |project, cx| { Rc::new(RefCell::new(project_c.read_with(cx_c, |project, cx| {
project.diagnostic_summaries(cx).collect::<Vec<_>>() project.diagnostic_summaries(false, cx).collect::<Vec<_>>()
}))); })));
project_c.update(cx_c, |_, cx| { project_c.update(cx_c, |_, cx| {
let summaries = project_c_diagnostic_summaries.clone(); let summaries = project_c_diagnostic_summaries.clone();
cx.subscribe(&project_c, { cx.subscribe(&project_c, {
move |p, _, event, cx| { move |p, _, event, cx| {
if let project::Event::DiskBasedDiagnosticsFinished { .. } = event { if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
*summaries.borrow_mut() = p.diagnostic_summaries(cx).collect(); *summaries.borrow_mut() = p.diagnostic_summaries(false, cx).collect();
} }
} }
}) })
@ -3766,7 +3766,7 @@ async fn test_collaborating_with_diagnostics(
project_b.read_with(cx_b, |project, cx| { project_b.read_with(cx_b, |project, cx| {
assert_eq!( assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(), project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[( [(
ProjectPath { ProjectPath {
worktree_id, worktree_id,
@ -3783,7 +3783,7 @@ async fn test_collaborating_with_diagnostics(
project_c.read_with(cx_c, |project, cx| { project_c.read_with(cx_c, |project, cx| {
assert_eq!( assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(), project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[( [(
ProjectPath { ProjectPath {
worktree_id, worktree_id,
@ -3844,15 +3844,24 @@ async fn test_collaborating_with_diagnostics(
executor.run_until_parked(); executor.run_until_parked();
project_a.read_with(cx_a, |project, cx| { project_a.read_with(cx_a, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), []) assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
}); });
project_b.read_with(cx_b, |project, cx| { project_b.read_with(cx_b, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), []) assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
}); });
project_c.read_with(cx_c, |project, cx| { project_c.read_with(cx_c, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), []) assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
}); });
} }

View file

@ -165,7 +165,7 @@ impl ProjectDiagnosticsEditor {
}); });
let project = project_handle.read(cx); let project = project_handle.read(cx);
let summary = project.diagnostic_summary(cx); let summary = project.diagnostic_summary(false, cx);
let mut this = Self { let mut this = Self {
project: project_handle, project: project_handle,
summary, summary,
@ -252,7 +252,7 @@ impl ProjectDiagnosticsEditor {
let mut new_summaries: HashMap<LanguageServerId, HashSet<ProjectPath>> = self let mut new_summaries: HashMap<LanguageServerId, HashSet<ProjectPath>> = self
.project .project
.read(cx) .read(cx)
.diagnostic_summaries(cx) .diagnostic_summaries(false, cx)
.fold(HashMap::default(), |mut summaries, (path, server_id, _)| { .fold(HashMap::default(), |mut summaries, (path, server_id, _)| {
summaries.entry(server_id).or_default().insert(path); summaries.entry(server_id).or_default().insert(path);
summaries summaries
@ -332,7 +332,7 @@ impl ProjectDiagnosticsEditor {
.context("rechecking diagnostics for paths")?; .context("rechecking diagnostics for paths")?;
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.summary = this.project.read(cx).diagnostic_summary(cx); this.summary = this.project.read(cx).diagnostic_summary(false, cx);
cx.emit(ItemEvent::UpdateTab); cx.emit(ItemEvent::UpdateTab);
cx.emit(ItemEvent::UpdateBreadcrumbs); cx.emit(ItemEvent::UpdateBreadcrumbs);
})?; })?;

View file

@ -77,13 +77,13 @@ impl DiagnosticIndicator {
project::Event::DiskBasedDiagnosticsFinished { language_server_id } project::Event::DiskBasedDiagnosticsFinished { language_server_id }
| project::Event::LanguageServerRemoved(language_server_id) => { | project::Event::LanguageServerRemoved(language_server_id) => {
this.summary = project.read(cx).diagnostic_summary(cx); this.summary = project.read(cx).diagnostic_summary(false, cx);
this.in_progress_checks.remove(language_server_id); this.in_progress_checks.remove(language_server_id);
cx.notify(); cx.notify();
} }
project::Event::DiagnosticsUpdated { .. } => { project::Event::DiagnosticsUpdated { .. } => {
this.summary = project.read(cx).diagnostic_summary(cx); this.summary = project.read(cx).diagnostic_summary(false, cx);
cx.notify(); cx.notify();
} }
@ -92,7 +92,7 @@ impl DiagnosticIndicator {
.detach(); .detach();
Self { Self {
summary: project.read(cx).diagnostic_summary(cx), summary: project.read(cx).diagnostic_summary(false, cx),
in_progress_checks: project in_progress_checks: project
.read(cx) .read(cx)
.language_servers_running_disk_based_diagnostics() .language_servers_running_disk_based_diagnostics()

View file

@ -6606,9 +6606,15 @@ impl Project {
}) })
} }
pub fn diagnostic_summary(&self, cx: &AppContext) -> DiagnosticSummary { pub fn diagnostic_summary(&self, include_ignored: bool, cx: &AppContext) -> DiagnosticSummary {
let mut summary = DiagnosticSummary::default(); let mut summary = DiagnosticSummary::default();
for (_, _, path_summary) in self.diagnostic_summaries(cx) { for (_, _, path_summary) in
self.diagnostic_summaries(include_ignored, cx)
.filter(|(path, _, _)| {
let worktree = self.entry_for_path(&path, cx).map(|entry| entry.is_ignored);
include_ignored || worktree == Some(false)
})
{
summary.error_count += path_summary.error_count; summary.error_count += path_summary.error_count;
summary.warning_count += path_summary.warning_count; summary.warning_count += path_summary.warning_count;
} }
@ -6617,17 +6623,23 @@ impl Project {
pub fn diagnostic_summaries<'a>( pub fn diagnostic_summaries<'a>(
&'a self, &'a self,
include_ignored: bool,
cx: &'a AppContext, cx: &'a AppContext,
) -> impl Iterator<Item = (ProjectPath, LanguageServerId, DiagnosticSummary)> + 'a { ) -> impl Iterator<Item = (ProjectPath, LanguageServerId, DiagnosticSummary)> + 'a {
self.visible_worktrees(cx).flat_map(move |worktree| { self.visible_worktrees(cx)
let worktree = worktree.read(cx); .flat_map(move |worktree| {
let worktree_id = worktree.id(); let worktree = worktree.read(cx);
worktree let worktree_id = worktree.id();
.diagnostic_summaries() worktree
.map(move |(path, server_id, summary)| { .diagnostic_summaries()
(ProjectPath { worktree_id, path }, server_id, summary) .map(move |(path, server_id, summary)| {
}) (ProjectPath { worktree_id, path }, server_id, summary)
}) })
})
.filter(move |(path, _, _)| {
let worktree = self.entry_for_path(&path, cx).map(|entry| entry.is_ignored);
include_ignored || worktree == Some(false)
})
} }
pub fn disk_based_diagnostics_started( pub fn disk_based_diagnostics_started(

View file

@ -823,7 +823,7 @@ async fn test_single_file_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
} }
#[gpui::test] #[gpui::test]
async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) { async fn test_omitted_diagnostics(cx: &mut gpui::TestAppContext) {
init_test(cx); init_test(cx);
let fs = FakeFs::new(cx.executor()); let fs = FakeFs::new(cx.executor());
@ -831,7 +831,12 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
"/root", "/root",
json!({ json!({
"dir": { "dir": {
".git": {
"HEAD": "ref: refs/heads/main",
},
".gitignore": "b.rs",
"a.rs": "let a = 1;", "a.rs": "let a = 1;",
"b.rs": "let b = 2;",
}, },
"other.rs": "let b = c;" "other.rs": "let b = c;"
}), }),
@ -839,6 +844,13 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
.await; .await;
let project = Project::test(fs, ["/root/dir".as_ref()], cx).await; let project = Project::test(fs, ["/root/dir".as_ref()], cx).await;
let (worktree, _) = project
.update(cx, |project, cx| {
project.find_or_create_local_worktree("/root/dir", true, cx)
})
.await
.unwrap();
let main_worktree_id = worktree.read_with(cx, |tree, _| tree.id());
let (worktree, _) = project let (worktree, _) = project
.update(cx, |project, cx| { .update(cx, |project, cx| {
@ -846,12 +858,30 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
}) })
.await .await
.unwrap(); .unwrap();
let worktree_id = worktree.update(cx, |tree, _| tree.id()); let other_worktree_id = worktree.update(cx, |tree, _| tree.id());
let server_id = LanguageServerId(0);
project.update(cx, |project, cx| { project.update(cx, |project, cx| {
project project
.update_diagnostics( .update_diagnostics(
LanguageServerId(0), server_id,
lsp::PublishDiagnosticsParams {
uri: Url::from_file_path("/root/dir/b.rs").unwrap(),
version: None,
diagnostics: vec![lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 5)),
severity: Some(lsp::DiagnosticSeverity::ERROR),
message: "unused variable 'b'".to_string(),
..Default::default()
}],
},
&[],
cx,
)
.unwrap();
project
.update_diagnostics(
server_id,
lsp::PublishDiagnosticsParams { lsp::PublishDiagnosticsParams {
uri: Url::from_file_path("/root/other.rs").unwrap(), uri: Url::from_file_path("/root/other.rs").unwrap(),
version: None, version: None,
@ -868,11 +898,34 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
.unwrap(); .unwrap();
}); });
let buffer = project let main_ignored_buffer = project
.update(cx, |project, cx| project.open_buffer((worktree_id, ""), cx)) .update(cx, |project, cx| {
project.open_buffer((main_worktree_id, "b.rs"), cx)
})
.await .await
.unwrap(); .unwrap();
buffer.update(cx, |buffer, _| { main_ignored_buffer.update(cx, |buffer, _| {
let chunks = chunks_with_diagnostics(buffer, 0..buffer.len());
assert_eq!(
chunks
.iter()
.map(|(s, d)| (s.as_str(), *d))
.collect::<Vec<_>>(),
&[
("let ", None),
("b", Some(DiagnosticSeverity::ERROR)),
(" = 2;", None),
],
"Gigitnored buffers should still get in-buffer diagnostics",
);
});
let other_buffer = project
.update(cx, |project, cx| {
project.open_buffer((other_worktree_id, ""), cx)
})
.await
.unwrap();
other_buffer.update(cx, |buffer, _| {
let chunks = chunks_with_diagnostics(buffer, 0..buffer.len()); let chunks = chunks_with_diagnostics(buffer, 0..buffer.len());
assert_eq!( assert_eq!(
chunks chunks
@ -883,13 +936,29 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
("let b = ", None), ("let b = ", None),
("c", Some(DiagnosticSeverity::ERROR)), ("c", Some(DiagnosticSeverity::ERROR)),
(";", None), (";", None),
] ],
"Buffers from hidden projects should still get in-buffer diagnostics"
); );
}); });
project.update(cx, |project, cx| { project.update(cx, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).next(), None); assert_eq!(project.diagnostic_summaries(false, cx).next(), None);
assert_eq!(project.diagnostic_summary(cx).error_count, 0); assert_eq!(
project.diagnostic_summaries(true, cx).collect::<Vec<_>>(),
vec![(
ProjectPath {
worktree_id: main_worktree_id,
path: Arc::from(Path::new("b.rs")),
},
server_id,
DiagnosticSummary {
error_count: 1,
warning_count: 0,
}
)]
);
assert_eq!(project.diagnostic_summary(false, cx).error_count, 0);
assert_eq!(project.diagnostic_summary(true, cx).error_count, 1);
}); });
} }
@ -1162,7 +1231,7 @@ async fn test_restarting_server_with_diagnostics_published(cx: &mut gpui::TestAp
}); });
project.update(cx, |project, cx| { project.update(cx, |project, cx| {
assert_eq!( assert_eq!(
project.diagnostic_summary(cx), project.diagnostic_summary(false, cx),
DiagnosticSummary { DiagnosticSummary {
error_count: 1, error_count: 1,
warning_count: 0, warning_count: 0,
@ -1188,7 +1257,7 @@ async fn test_restarting_server_with_diagnostics_published(cx: &mut gpui::TestAp
}); });
project.update(cx, |project, cx| { project.update(cx, |project, cx| {
assert_eq!( assert_eq!(
project.diagnostic_summary(cx), project.diagnostic_summary(false, cx),
DiagnosticSummary { DiagnosticSummary {
error_count: 0, error_count: 0,
warning_count: 0, warning_count: 0,
@ -1777,7 +1846,7 @@ async fn test_diagnostics_from_multiple_language_servers(cx: &mut gpui::TestAppC
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
project.diagnostic_summary(cx), project.diagnostic_summary(false, cx),
DiagnosticSummary { DiagnosticSummary {
error_count: 2, error_count: 2,
warning_count: 0, warning_count: 0,