TODO section 28, lane C (the file-size rule in CLAUDE.md).
server.rs (2 559 production + 6 293 test lines) becomes server/{mod,
dispatch, client, index, notification, progress, diagnostics, batch,
formatting, large_file, semantic_tokens}.rs with the tests in
server/tests/ (16 files, one per former banner section, 15 shared
helpers hoisted into the harness); completion.rs (1 716 + 2 463)
becomes completion/{mod, member, module, tiers, word, site,
harness}.rs with each provider's tests beside it. answer_request (393)
and handle_notification (197) stay whole as the dispatch tables they
are; perf.rs stays whole because its one sentence has no and. Every
import elsewhere in the crate resolves through pub(crate) re-exports,
so main.rs, inline.rs, instantiate.rs, hover.rs, nav.rs and state.rs
are byte-identical. Proof: per-binary counts identical (1 435 / 85 / 3;
workspace 3 215 / 48), e2e and the vsix smoke green, a live LSP session
against opentitan answering byte-identically from this build and the
base, and a line-multiset + signature census (360/360, 188/188) done
by the dev and redone from scratch by QA.
docs/features/decomposition-server/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
273 lines
12 KiB
Rust
273 lines
12 KiB
Rust
//! The indexing bar and the per-file log a rescan runs under.
|
|
//!
|
|
//! `begin_indexing` puts the bar up and `end_indexing` takes it down with
|
|
//! the count; between them `IndexWatch` — which holds the connection rather
|
|
//! than the `Server`, because the workspace is borrowed mutably for the
|
|
//! whole of the rebuild it watches — turns each `IndexEvent` into a report
|
|
//! and a log line, thinned past `MAX_REPORTS` files so a 40 000-file
|
|
//! workspace does not send 40 000 notifications per save. Its own file
|
|
//! because it is the one part of a rescan that is about *saying* rather than
|
|
//! *doing*, and `index.rs` reads better without it.
|
|
|
|
use std::path::Path;
|
|
|
|
use lsp_server::Connection;
|
|
use lsp_types::notification::{LogMessage, Notification as _, Progress};
|
|
use lsp_types::request::{Request as _, WorkDoneProgressCreate};
|
|
use lsp_types::{
|
|
LogMessageParams, MessageType, ProgressParams, ProgressParamsValue, ProgressToken,
|
|
WorkDoneProgress, WorkDoneProgressBegin, WorkDoneProgressCreateParams, WorkDoneProgressEnd,
|
|
WorkDoneProgressReport,
|
|
};
|
|
|
|
use super::client::notify_on;
|
|
use super::Server;
|
|
use crate::settings::ClientSettings;
|
|
use crate::state::IndexEvent;
|
|
|
|
/// What the workspace-rescan progress bar calls itself. One title for every
|
|
/// rescan — startup, save, watched file, changed scope — because they are the
|
|
/// same work and a user reading the status bar wants to know that.
|
|
const INDEXING_TITLE: &str = "Indexing Verilog workspace";
|
|
|
|
impl Server {
|
|
/// Put a progress bar up for the rescan that is about to run, and report
|
|
/// the token it must be taken down with. `None` for a client that cannot
|
|
/// show one — the spec forbids creating progress it never asked for.
|
|
///
|
|
/// The `begin` goes out immediately after the `create`, without waiting
|
|
/// for its answer: the rescan blocks this thread the moment this returns,
|
|
/// so waiting would mean the bar appears only once the work it describes
|
|
/// is over. The spec allows it, and a client that errors the `create`
|
|
/// ignores the `$/progress` that follows.
|
|
///
|
|
/// The `begin` carries no percentage: the first one that means anything
|
|
/// is the first file's, and it is `IndexWatch` that sends it. Every
|
|
/// percentage from there on is measured — `n` files of a work list whose
|
|
/// length was known before the first parse — never invented.
|
|
pub(super) fn begin_indexing(&mut self) -> Option<ProgressToken> {
|
|
if !self.abilities.work_done_progress {
|
|
return None;
|
|
}
|
|
let token = ProgressToken::String(format!("lucisv-index-{}", self.indexings));
|
|
self.indexings += 1;
|
|
let params = WorkDoneProgressCreateParams { token: token.clone() };
|
|
self.send_request(WorkDoneProgressCreate::METHOD, params);
|
|
self.progress(
|
|
token.clone(),
|
|
WorkDoneProgress::Begin(WorkDoneProgressBegin {
|
|
title: INDEXING_TITLE.to_string(),
|
|
// Nothing already running in this server can be cancelled: the
|
|
// loop is one thread and a rescan runs to completion — the
|
|
// `$/cancelRequest` support only answers requests still
|
|
// queued. Saying so is what keeps a cancel button the user
|
|
// could press off the bar.
|
|
cancellable: Some(false),
|
|
..Default::default()
|
|
}),
|
|
);
|
|
Some(token)
|
|
}
|
|
|
|
/// Take the bar down, naming what the rescan found. The count is the
|
|
/// index's own file list, which is the only number this costs nothing to
|
|
/// know. Called the moment the rescan returns, before the scope warnings
|
|
/// and the republishes — the bar names the scan, and what follows it is
|
|
/// per-open-buffer work the user already pays on every keystroke.
|
|
///
|
|
/// `skipped` is how many files the size cap kept out, and it is named here
|
|
/// whether or not the per-file log is on: the log is the *detail*, and a
|
|
/// user who turned it off should still learn that part of their workspace
|
|
/// is not in the index. `cached` is how many were restored from the
|
|
/// persistent index cache rather than parsed, and is named for the
|
|
/// opposite reason — a scan that finished suspiciously fast should say
|
|
/// why (`docs/features/index-persistence/README.md`).
|
|
pub(super) fn end_indexing(&self, token: Option<ProgressToken>, skipped: usize, cached: usize) {
|
|
let Some(token) = token else { return };
|
|
let files = self.workspace.index().files().len();
|
|
let plural = if files == 1 { "" } else { "s" };
|
|
let mut message = format!("{files} file{plural} indexed");
|
|
if cached > 0 {
|
|
message.push_str(&format!(", {cached} from cache"));
|
|
}
|
|
if skipped > 0 {
|
|
message.push_str(&format!(", {skipped} skipped"));
|
|
}
|
|
self.progress(token, WorkDoneProgress::End(WorkDoneProgressEnd { message: Some(message) }));
|
|
}
|
|
|
|
/// One `$/progress` notification for a token the client already knows.
|
|
fn progress(&self, token: ProgressToken, value: WorkDoneProgress) {
|
|
progress_on(&self.connection, &token, value);
|
|
}
|
|
}
|
|
|
|
/// How many `$/progress` reports one rescan may send.
|
|
///
|
|
/// One per file is right for the workspaces people actually have — the 860-file
|
|
/// corpus is 860 small notifications across two thirds of a second — but a
|
|
/// 40,000-file filelist would be 40,000 of them on every save. Past this many
|
|
/// files the reports thin out to every ⌈total/1000⌉-th, which still moves the
|
|
/// bar by a tenth of a percent at a time: under a pixel.
|
|
pub(super) const MAX_REPORTS: usize = 1000;
|
|
|
|
/// Whether the file numbered `done` (from 0) of `total` gets a `report`.
|
|
///
|
|
/// The last file always reports, so a rescan's final bar position is always
|
|
/// `total/total` at 100%. Without that, a 10,000-file scan would stop
|
|
/// reporting at 10,000 exactly — but a 10,001-file one would stop at 10,000
|
|
/// and leave the bar a file short of the work it had finished.
|
|
pub(super) fn reports(done: usize, total: usize) -> bool {
|
|
let step = total.div_ceil(MAX_REPORTS).max(1);
|
|
(done + 1).is_multiple_of(step) || done + 1 == total
|
|
}
|
|
|
|
/// Watches one index rebuild: a `$/progress` report and a log line per file,
|
|
/// and a count of the files the size cap skipped.
|
|
///
|
|
/// Holds the connection rather than the `Server`, because the workspace is
|
|
/// borrowed mutably for the whole of the rebuild being watched. Everything it
|
|
/// sends is a notification, so nothing here can fail in a way a caller could
|
|
/// act on.
|
|
pub(super) struct IndexWatch<'a> {
|
|
connection: &'a Connection,
|
|
/// The bar to report against — `None` for a client that cannot show one,
|
|
/// which is the same gate `begin_indexing` applied.
|
|
bar: Option<&'a ProgressToken>,
|
|
/// `lucisv.index.logFiles`: whether the per-file lines are sent at all.
|
|
log_files: bool,
|
|
}
|
|
|
|
impl<'a> IndexWatch<'a> {
|
|
pub(super) fn new(
|
|
connection: &'a Connection,
|
|
bar: Option<&'a ProgressToken>,
|
|
settings: &ClientSettings,
|
|
) -> IndexWatch<'a> {
|
|
IndexWatch { connection, bar, log_files: settings.log_files }
|
|
}
|
|
|
|
/// Report one thing the rebuild just did.
|
|
pub(super) fn see(&mut self, event: IndexEvent) {
|
|
match event {
|
|
IndexEvent::Parsing { done, total, path } => {
|
|
self.say(format!("indexing: {}", path.display()));
|
|
self.tick(done, total, path);
|
|
}
|
|
// A different word for a file that cost nothing, and the same
|
|
// tick: a cached file is a file of the work list, and the bar
|
|
// flying through them is exactly what the cache bought.
|
|
IndexEvent::Cached { done, total, path } => {
|
|
self.say(format!("cached: {}", path.display()));
|
|
self.tick(done, total, path);
|
|
}
|
|
// A third word, and no count in the end message: an open buffer's
|
|
// own analysis costs nothing either, but one file out of a
|
|
// workspace is not why a scan finished fast, and a bar that grew a
|
|
// clause on every session with a file open would say less, not
|
|
// more. The per-file log still names it, which is where a user
|
|
// asking "did it re-parse my huge buffer?" looks.
|
|
IndexEvent::Analyzed { done, total, path } => {
|
|
self.say(format!("analyzed: {}", path.display()));
|
|
self.tick(done, total, path);
|
|
}
|
|
IndexEvent::Skipped { path, size, cap } => {
|
|
self.say(format!(
|
|
"skipped ({} > {} cap): {}",
|
|
human_size(size),
|
|
human_size(cap),
|
|
path.display()
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Move the bar to `done + 1` of `total`, if this file is one of the ones
|
|
/// that reports.
|
|
///
|
|
/// The message names the file rather than its path: the bar is a strip of
|
|
/// status bar a few dozen characters wide, and the path is in the log for
|
|
/// anyone who wants it. The percentage is `(done + 1) / total`, which
|
|
/// only ever counts up.
|
|
fn tick(&self, done: usize, total: usize, path: &Path) {
|
|
let Some(token) = self.bar else { return };
|
|
if !reports(done, total) {
|
|
return;
|
|
}
|
|
let name = path.file_name().unwrap_or(path.as_os_str()).to_string_lossy();
|
|
let report = WorkDoneProgressReport {
|
|
cancellable: Some(false),
|
|
message: Some(format!("{}/{total} {name}", done + 1)),
|
|
// `total` cannot be 0 here — this is called from inside the loop
|
|
// over the files — but a percentage is not worth a division that
|
|
// has to be argued about.
|
|
percentage: Some(((done + 1) * 100 / total.max(1)) as u32),
|
|
};
|
|
progress_on(self.connection, token, WorkDoneProgress::Report(report));
|
|
}
|
|
|
|
/// One line in the client's output channel, if the user left the file log
|
|
/// on.
|
|
///
|
|
/// `MessageType::LOG`, unprefixed: these are hundreds of lines per rescan
|
|
/// in a channel that is already this server's, and "lucisv: " on every one
|
|
/// of them would be noise plus traffic. The scope warnings, which are
|
|
/// rare and also shown in a popup, keep their prefix.
|
|
fn say(&self, message: String) {
|
|
if !self.log_files {
|
|
return;
|
|
}
|
|
let params = LogMessageParams { typ: MessageType::LOG, message };
|
|
notify_on(self.connection, LogMessage::METHOD, params);
|
|
}
|
|
}
|
|
|
|
/// A byte count in the largest unit that keeps it readable.
|
|
///
|
|
/// The cap is written in bytes and can be set to anything, so a fixed unit is
|
|
/// wrong at one end or the other: "0.0 MiB > 0.0 MiB cap" says nothing about a
|
|
/// 4 KiB file under a 1 KiB cap, and "4194304 B" says nothing quickly about a
|
|
/// netlist.
|
|
fn human_size(bytes: u64) -> String {
|
|
const KIB: f64 = 1024.0;
|
|
const MIB: f64 = KIB * 1024.0;
|
|
// The unit is chosen by what the ROUNDED number reads as, not the raw
|
|
// size: 1 048 575 bytes is "1.0 MiB", never "1024.0 KiB" (QA log,
|
|
// 2026-08-27 — the same class of bug the e2e's "0.0 MiB > 0.0 MiB"
|
|
// caught at the opposite edge).
|
|
let size = bytes as f64;
|
|
if size < KIB {
|
|
format!("{bytes} B")
|
|
} else if (size / KIB).round() < KIB {
|
|
format!("{:.1} KiB", size / KIB)
|
|
} else {
|
|
format!("{:.1} MiB", size / MIB)
|
|
}
|
|
}
|
|
|
|
/// One `$/progress` notification, on a connection rather than through a
|
|
/// `Server` — what the index watcher has while the workspace is borrowed.
|
|
fn progress_on(connection: &Connection, token: &ProgressToken, value: WorkDoneProgress) {
|
|
let params =
|
|
ProgressParams { token: token.clone(), value: ProgressParamsValue::WorkDone(value) };
|
|
notify_on(connection, Progress::METHOD, params);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod human_size_tests {
|
|
use super::human_size;
|
|
|
|
#[test]
|
|
fn the_unit_is_chosen_by_what_the_rounded_number_reads_as() {
|
|
// QA finding, 2026-08-27: a byte short of a MiB must not print as
|
|
// "1024.0 KiB". The unit flips when the smaller unit's rounded
|
|
// number would read as the next unit.
|
|
assert_eq!(human_size(1_048_575), "1.0 MiB");
|
|
assert_eq!(human_size(1_048_576), "1.0 MiB");
|
|
assert_eq!(human_size(1023), "1023 B");
|
|
assert_eq!(human_size(1024), "1.0 KiB");
|
|
assert_eq!(human_size(4_096), "4.0 KiB");
|
|
assert_eq!(human_size(2_411_724), "2.3 MiB");
|
|
}
|
|
}
|