Skip to content

Commit

Permalink
Lazy progress reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jun 17, 2023
1 parent 64a8887 commit b5e0452
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 70 deletions.
104 changes: 58 additions & 46 deletions crates/rust-analyzer/src/cli/analysis_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use profile::{Bytes, StopWatch};
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSource};
use rayon::prelude::*;
use rustc_hash::FxHashSet;
use stdx::format_to;
use syntax::{AstNode, SyntaxNode};
use vfs::{AbsPathBuf, Vfs, VfsPath};

Expand Down Expand Up @@ -241,8 +240,10 @@ impl flags::AnalysisStats {
continue;
}
all += 1;
let Err(e) = db.layout_of_adt(hir_def::AdtId::from(a).into(), Substitution::empty(Interner), a.krate(db).into()) else {
continue;
let Err(e)
= db.layout_of_adt(hir_def::AdtId::from(a).into(), Substitution::empty(Interner), a.krate(db).into())
else {
continue
};
if verbosity.is_spammy() {
let full_name = a
Expand Down Expand Up @@ -363,7 +364,7 @@ impl flags::AnalysisStats {
for &body_id in bodies {
let name = body_id.name(db).unwrap_or_else(Name::missing);
let module = body_id.module(db);
let full_name = || {
let full_name = move || {
module
.krate()
.display_name(db)
Expand All @@ -375,7 +376,7 @@ impl flags::AnalysisStats {
.into_iter()
.filter_map(|it| it.name(db))
.rev()
.chain(Some(name.clone()))
.chain(Some(body_id.name(db).unwrap_or_else(Name::missing)))
.map(|it| it.display(db).to_string()),
)
.join("::")
Expand All @@ -385,26 +386,31 @@ impl flags::AnalysisStats {
continue;
}
}
let mut msg = format!("processing: {}", full_name());
if verbosity.is_verbose() {
let source = match body_id {
DefWithBody::Function(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Static(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Const(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Variant(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::InTypeConst(_) => unimplemented!(),
};
if let Some(src) = source {
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.text_range();
format_to!(msg, " ({} {:?})", path, syntax_range);
let msg = move || {
if verbosity.is_verbose() {
let source = match body_id {
DefWithBody::Function(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Static(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Const(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Variant(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::InTypeConst(_) => unimplemented!(),
};
if let Some(src) = source {
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.text_range();
format!("processing: {} ({} {:?})", full_name(), path, syntax_range)
} else {
format!("processing: {}", full_name())
}
} else {
format!("processing: {}", full_name())
}
}
};
if verbosity.is_spammy() {
bar.println(msg.to_string());
bar.println(msg());
}
bar.set_message(&msg);
bar.set_message(msg);
let (body, sm) = db.body_with_source_map(body_id.into());
let inference_result = db.infer(body_id.into());

Expand Down Expand Up @@ -641,16 +647,15 @@ impl flags::AnalysisStats {
) {
let mut bar = match verbosity {
Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(),
_ if self.parallel || self.output.is_some() => ProgressReport::hidden(),
_ if self.output.is_some() => ProgressReport::hidden(),
_ => ProgressReport::new(bodies.len() as u64),
};

let mut sw = self.stop_watch();
bar.tick();
for &body_id in bodies {
let name = body_id.name(db).unwrap_or_else(Name::missing);
let module = body_id.module(db);
let full_name = || {
let full_name = move || {
module
.krate()
.display_name(db)
Expand All @@ -662,38 +667,45 @@ impl flags::AnalysisStats {
.into_iter()
.filter_map(|it| it.name(db))
.rev()
.chain(Some(name.clone()))
.chain(Some(body_id.name(db).unwrap_or_else(Name::missing)))
.map(|it| it.display(db).to_string()),
)
.join("::")
};
if let Some(only_name) = self.only.as_deref() {
if name.display(db).to_string() != only_name && full_name() != only_name {
if body_id.name(db).unwrap_or_else(Name::missing).display(db).to_string()
!= only_name
&& full_name() != only_name
{
continue;
}
}
let mut msg = format!("processing: {}", full_name());
if verbosity.is_verbose() {
let source = match body_id {
DefWithBody::Function(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Static(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Const(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Variant(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::InTypeConst(_) => unimplemented!(),
};
if let Some(src) = source {
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.text_range();
format_to!(msg, " ({} {:?})", path, syntax_range);
let msg = move || {
if verbosity.is_verbose() {
let source = match body_id {
DefWithBody::Function(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Static(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Const(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::Variant(it) => it.source(db).map(|it| it.syntax().cloned()),
DefWithBody::InTypeConst(_) => unimplemented!(),
};
if let Some(src) = source {
let original_file = src.file_id.original_file(db);
let path = vfs.file_path(original_file);
let syntax_range = src.value.text_range();
format!("processing: {} ({} {:?})", full_name(), path, syntax_range)
} else {
format!("processing: {}", full_name())
}
} else {
format!("processing: {}", full_name())
}
}
};
if verbosity.is_spammy() {
bar.println(msg.to_string());
bar.println(msg());
}
bar.set_message(&msg);
let (body, sm) = db.body_with_source_map(body_id.into());
// endregion:patterns
bar.set_message(msg);
db.body_with_source_map(body_id.into());
bar.inc(1);
}

Expand Down
41 changes: 17 additions & 24 deletions crates/rust-analyzer/src/cli/progress_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,29 @@
use std::io::{self, Write};

/// A Simple ASCII Progress Bar
pub(crate) struct ProgressReport {
pub(crate) struct ProgressReport<'a> {
curr: f32,
text: String,
hidden: bool,

len: u64,
pos: u64,
msg: String,
msg: Option<Box<dyn Fn() -> String + 'a>>,
}

impl ProgressReport {
pub(crate) fn new(len: u64) -> ProgressReport {
ProgressReport {
curr: 0.0,
text: String::new(),
hidden: false,
len,
pos: 0,
msg: String::new(),
}
impl<'a> ProgressReport<'a> {
pub(crate) fn new(len: u64) -> ProgressReport<'a> {
ProgressReport { curr: 0.0, text: String::new(), hidden: false, len, pos: 0, msg: None }
}

pub(crate) fn hidden() -> ProgressReport {
ProgressReport {
curr: 0.0,
text: String::new(),
hidden: true,
len: 0,
pos: 0,
msg: String::new(),
}
pub(crate) fn hidden() -> ProgressReport<'a> {
ProgressReport { curr: 0.0, text: String::new(), hidden: true, len: 0, pos: 0, msg: None }
}

pub(crate) fn set_message(&mut self, msg: &str) {
self.msg = msg.to_string();
pub(crate) fn set_message(&mut self, msg: impl Fn() -> String + 'a) {
if !self.hidden {
self.msg = Some(Box::new(msg));
}
self.tick();
}

Expand Down Expand Up @@ -67,7 +55,12 @@ impl ProgressReport {
return;
}
let percent = (self.curr * 100.0) as u32;
let text = format!("{}/{} {percent:3>}% {}", self.pos, self.len, self.msg);
let text = format!(
"{}/{} {percent:3>}% {}",
self.pos,
self.len,
self.msg.as_ref().map_or_else(|| String::new(), |it| it())
);
self.update_text(&text);
}

Expand Down

0 comments on commit b5e0452

Please sign in to comment.