From 747d9e87848095374444e0d97772eb0237d9879a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 10 Jan 2022 13:38:00 -0800 Subject: [PATCH] Add files to project diagnostics view in order Co-Authored-By: Nathan Sobo --- crates/diagnostics/src/diagnostics.rs | 47 ++++++++++++++------------- crates/project/src/project.rs | 2 +- crates/project/src/worktree.rs | 4 +-- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index e70cd07f50..8c31673d41 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -1,7 +1,7 @@ pub mod items; use anyhow::Result; -use collections::{HashMap, HashSet}; +use collections::{HashMap, HashSet, BTreeSet}; use editor::{ context_header_renderer, diagnostic_block_renderer, diagnostic_header_renderer, display_map::{BlockDisposition, BlockId, BlockProperties}, @@ -15,7 +15,7 @@ use gpui::{ use language::{Bias, Buffer, DiagnosticEntry, Point, Selection, SelectionGoal}; use postage::watch; use project::{Project, ProjectPath, WorktreeId}; -use std::{cmp::Ordering, mem, ops::Range, path::Path, sync::Arc}; +use std::{cmp::Ordering, mem, ops::Range}; use util::TryFutureExt; use workspace::Workspace; @@ -48,8 +48,8 @@ struct ProjectDiagnosticsEditor { workspace: WeakViewHandle, editor: ViewHandle, excerpts: ModelHandle, - path_states: Vec<(Arc, Vec)>, - paths_to_update: HashMap>, + path_states: Vec<(ProjectPath, Vec)>, + paths_to_update: HashMap>, build_settings: BuildSettings, settings: watch::Receiver, } @@ -206,15 +206,21 @@ impl ProjectDiagnosticsEditor { } } - fn update_excerpts(&self, paths: HashSet, cx: &mut ViewContext) { + fn update_excerpts( + &self, + paths: BTreeSet, + cx: &mut ViewContext, + ) { let project = self.model.read(cx).project.clone(); cx.spawn(|this, mut cx| { async move { for path in paths { let buffer = project - .update(&mut cx, |project, cx| project.open_buffer(path, cx)) + .update(&mut cx, |project, cx| project.open_buffer(path.clone(), cx)) .await?; - this.update(&mut cx, |view, cx| view.populate_excerpts(buffer, cx)) + this.update(&mut cx, |view, cx| { + view.populate_excerpts(path, buffer, cx) + }) } Result::<_, anyhow::Error>::Ok(()) } @@ -223,23 +229,17 @@ impl ProjectDiagnosticsEditor { .detach(); } - fn populate_excerpts(&mut self, buffer: ModelHandle, cx: &mut ViewContext) { - let snapshot; - let path; - { - let buffer = buffer.read(cx); - snapshot = buffer.snapshot(); - if let Some(file) = buffer.file() { - path = file.path().clone(); - } else { - return; - } - } - + fn populate_excerpts( + &mut self, + path: ProjectPath, + buffer: ModelHandle, + cx: &mut ViewContext, + ) { let was_empty = self.path_states.is_empty(); + let snapshot = buffer.read(cx).snapshot(); let path_ix = match self .path_states - .binary_search_by_key(&path.as_ref(), |e| e.0.as_ref()) + .binary_search_by_key(&&path, |e| &e.0) { Ok(ix) => ix, Err(ix) => { @@ -445,11 +445,11 @@ impl ProjectDiagnosticsEditor { } self.editor.update(cx, |editor, cx| { - let groups = self.path_states.get(path_ix)?.1.as_slice(); - + let groups; let mut selections; let new_excerpt_ids_by_selection_id; if was_empty { + groups = self.path_states.first()?.1.as_slice(); new_excerpt_ids_by_selection_id = [(0, ExcerptId::min())].into_iter().collect(); selections = vec![Selection { id: 0, @@ -459,6 +459,7 @@ impl ProjectDiagnosticsEditor { goal: SelectionGoal::None, }]; } else { + groups = self.path_states.get(path_ix)?.1.as_slice(); new_excerpt_ids_by_selection_id = editor.refresh_selections(cx); selections = editor.local_selections::(cx); } diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 62ecf94b7e..5f0c966f6f 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -67,7 +67,7 @@ pub enum Event { DiagnosticsUpdated(ProjectPath), } -#[derive(Clone, Debug, Eq, PartialEq, Hash)] +#[derive(Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)] pub struct ProjectPath { pub worktree_id: WorktreeId, pub path: Arc, diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index 46caf8cd93..2406ea3510 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -57,7 +57,7 @@ enum ScanState { Err(Arc), } -#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash, PartialOrd, Ord)] pub struct WorktreeId(usize); pub enum Worktree { @@ -711,7 +711,7 @@ impl Worktree { let mut primary_diagnostic_group_ids = HashMap::default(); let mut sources_by_group_id = HashMap::default(); let mut supporting_diagnostic_severities = HashMap::default(); - for diagnostic in ¶ms.diagnostics { + for diagnostic in dbg!(¶ms.diagnostics) { let source = diagnostic.source.as_ref(); let code = diagnostic.code.as_ref().map(|code| match code { lsp::NumberOrString::Number(code) => code.to_string(),