410 lines
20 KiB
Rust
410 lines
20 KiB
Rust
//! The rescans: how the workspace index is rebuilt, and what a rebuild owes
|
|
//! every open buffer afterwards.
|
|
//!
|
|
//! Four entries — `reindex` (the whole scope), `reindex_one` (a save),
|
|
//! `reindex_widened` (a `didOpen` that grew the scope) and `reindex_for` (a
|
|
//! file operation, filtered) — over one loop, `scan_until_settled`, which is
|
|
//! the whole of the coalescing rule: a rescan asked for mid-scan is one more
|
|
//! scan when this one ends, never a scan inside a scan. `scan_once` is the
|
|
//! scan that answers the client's messages between two of its files
|
|
//! (`docs/features/scan-interleave/README.md`); the bar and the log it runs
|
|
//! under are `progress.rs`'s.
|
|
|
|
use std::path::Path;
|
|
|
|
use lsp_types::ProgressToken;
|
|
|
|
use super::batch::opened_in;
|
|
use super::client::{notify_on, plural};
|
|
use super::progress::IndexWatch;
|
|
use super::{Server, INTERLEAVE_ABOVE};
|
|
use crate::state::{IndexEvent, Scan, Workspace};
|
|
use crate::{design_hierarchy, file_ops};
|
|
|
|
impl Server {
|
|
/// Rescan the workspace and republish every open buffer against the new
|
|
/// index. Every caller changed what the index should contain — a save, a
|
|
/// new `lucisv.exclude`/`lucisv.filelist`, a watched file changing on disk,
|
|
/// or a file the client renamed or deleted.
|
|
pub(super) fn reindex(&mut self) {
|
|
// A rescan asked for by a message this scan answered is *this* scan's
|
|
// successor, not a scan inside it — and one asked for before the
|
|
// startup scan has begun is that scan itself. See
|
|
// `scan_until_settled` and [`Server::scan_owed`].
|
|
if self.scan_owed() {
|
|
self.rescan_wanted = true;
|
|
return;
|
|
}
|
|
let warnings = self.scan_until_settled(true);
|
|
if self.stopped {
|
|
return;
|
|
}
|
|
// A rescan can change any module's instantiation count, so every lens
|
|
// a client is holding may now read wrong. Sent after the bar comes
|
|
// down, for the same reason the bar comes down before the republishes:
|
|
// the bar names the scan, and everything after it is work done on the
|
|
// scan's result.
|
|
self.refresh_code_lenses();
|
|
self.hierarchy_index_changed();
|
|
// Every open buffer's diagnostics are computed against the index, so a
|
|
// pulling client's cached answers are all stale from here.
|
|
self.generation += 1;
|
|
self.report_scope(warnings);
|
|
for uri in self.workspace.open_uris() {
|
|
self.publish(&uri);
|
|
}
|
|
}
|
|
|
|
/// [`Server::reindex`] for a trigger that named exactly **one** file, when
|
|
/// that file is one the index already holds. Reports whether it ran; a
|
|
/// `false` means the caller owes a whole rebuild.
|
|
///
|
|
/// Everything a rebuild does *after* the scan happens here too, for the
|
|
/// same reasons: the code-lens refresh (the file that moved may have
|
|
/// changed an instantiation count in any buffer), the diagnostics
|
|
/// generation bump (every open buffer's diagnostics are computed against
|
|
/// the index), and a republish of every open buffer. The republish stays
|
|
/// wholesale rather than narrowing to the buffers that could have moved:
|
|
/// a lint reads the index, so any buffer's diagnostics may have changed,
|
|
/// and narrowing it correctly needs a dependency graph this server does not
|
|
/// have. Worth doing later; wrong to guess at now.
|
|
///
|
|
/// Three things a whole rebuild does that this deliberately does not:
|
|
///
|
|
/// * **no progress bar.** The bar exists because a whole scan takes
|
|
/// perceptible seconds on a large workspace; one file is one parse, and a
|
|
/// bar that appears and vanishes inside a frame is noise, not progress.
|
|
/// * **no per-file log line.** Same reason, and the log's purpose — "which
|
|
/// files is this scan reading?" — is a question about a scan.
|
|
/// * **no scope warnings.** They come from *resolving* the scope, and the
|
|
/// scope was not re-resolved: nothing about which files exist changed, so
|
|
/// repeating a filelist's complaints would be repeating stale news.
|
|
pub(super) fn reindex_one(&mut self, path: &Path) -> bool {
|
|
// Mid-scan the cheap path is not available and is not needed: the scan
|
|
// holds the text this file had when it started and will index *that*,
|
|
// so patching the half-built index here would be overwritten by older
|
|
// bytes a moment later. One more scan when this one ends is the whole
|
|
// answer, and it is the same flag every other mid-scan trigger sets.
|
|
// `true` because the caller's fallback — a whole rebuild, right now —
|
|
// is exactly what must not happen. Same for the startup's
|
|
// configuration wait, whose scan reads this file from disk anyway.
|
|
if self.scan_owed() {
|
|
self.rescan_wanted = true;
|
|
return true;
|
|
}
|
|
let started = self.timer();
|
|
if !self.workspace.update_one(path) {
|
|
// No line: nothing was reindexed, and the caller is about to
|
|
// rebuild the whole index — which times itself.
|
|
return false;
|
|
}
|
|
self.refresh_code_lenses();
|
|
self.hierarchy_index_changed();
|
|
self.generation += 1;
|
|
for uri in self.workspace.open_uris() {
|
|
self.publish(&uri);
|
|
}
|
|
self.timed(started, |took| format!("reindex_one({}) took {took}", path.display()));
|
|
true
|
|
}
|
|
|
|
/// [`Server::reindex`] for a `didOpen` that only ever **added** folders to
|
|
/// the scope: index those folders and fold them in, leaving every file
|
|
/// already indexed exactly as it is.
|
|
///
|
|
/// Everything after the scan is what a whole rescan does, for the same
|
|
/// reasons — the widen changes which names exist, so lenses, the
|
|
/// diagnostics generation and every open buffer's publish all move with it.
|
|
///
|
|
/// **Mid-scan this defers to one whole rescan rather than widening.** A
|
|
/// scan already running holds a work list taken from the *old* scope, and
|
|
/// folding a widen into it would mean two answers to "which files exist"
|
|
/// inside one scan; the follow-up scan the flag asks for reads the
|
|
/// persistent store for everything the running scan already parsed, so it
|
|
/// is the cheap kind. This is the same flag every other mid-scan trigger
|
|
/// sets, and it is what keeps the coalescing rule one rule
|
|
/// (`docs/features/scan-interleave/README.md`).
|
|
pub(super) fn reindex_widened(&mut self, added: Vec<std::path::PathBuf>) {
|
|
// Before the startup scan has begun there is nothing to widen *into*:
|
|
// that scan resolves the scope afresh, the buffer this `didOpen` just
|
|
// installed is already in it, and `open_first` puts the buffer at the
|
|
// head of its work list. So the widen is folded into the one scan
|
|
// rather than run as a second one (`server/startup.rs`).
|
|
if self.scan_owed() {
|
|
self.rescan_wanted = true;
|
|
return;
|
|
}
|
|
let started = self.timer();
|
|
let since_start = self.since_start();
|
|
if started.is_some() {
|
|
// The folders by name: which folder a jump widened into is the one
|
|
// thing the file log below it does not say in one line.
|
|
let paths: Vec<String> = added.iter().map(|p| p.display().to_string()).collect();
|
|
let folders = added.len();
|
|
self.timing(format!(
|
|
"widen started: {folders} folder{} added: {}",
|
|
plural(folders),
|
|
paths.join(", ")
|
|
));
|
|
}
|
|
let verbose = started.is_some();
|
|
let bar = self.begin_indexing();
|
|
let mut scan =
|
|
self.watching(bar.as_ref(), |ws, watch| ws.begin_widen(&added, watch, verbose));
|
|
let total = scan.total();
|
|
// No drain between the files: a widen is a rescan a message asked for,
|
|
// and `must_wait` would hold every request queued behind it anyway. It
|
|
// is also the point of the round that there is no longer a wait worth
|
|
// interleaving — a widen indexes one folder, not the whole scope.
|
|
loop {
|
|
let step = |workspace: &mut Workspace, watch: &mut dyn FnMut(IndexEvent)| {
|
|
workspace.scan_step(&mut scan, watch)
|
|
};
|
|
if !self.watching(bar.as_ref(), step) {
|
|
break;
|
|
}
|
|
}
|
|
let (skipped, cached) = (scan.skipped(), scan.cached());
|
|
let warnings = self.workspace.finish_scan(scan);
|
|
self.end_indexing(bar, skipped, cached);
|
|
self.timed(started, |took| {
|
|
format!(
|
|
"widen finished: {total} file{} ({skipped} skipped, {cached} from cache) in {took}",
|
|
plural(total)
|
|
)
|
|
});
|
|
self.say_scan_trace(since_start);
|
|
self.refresh_code_lenses();
|
|
self.hierarchy_index_changed();
|
|
self.generation += 1;
|
|
self.report_scope(warnings);
|
|
for uri in self.workspace.open_uris() {
|
|
self.publish(&uri);
|
|
}
|
|
}
|
|
|
|
/// Rescan, but only when the paths a file-operation notification named are
|
|
/// paths the index reads. The same test a watched-file notification gets.
|
|
pub(super) fn reindex_for(&mut self, paths: Vec<std::path::PathBuf>) {
|
|
let filelist = self.workspace.filelist_path();
|
|
if file_ops::touches_scope(&paths, filelist.as_deref()) {
|
|
self.reindex();
|
|
}
|
|
}
|
|
|
|
/// Scan, and go on scanning while the messages answered along the way keep
|
|
/// asking for one. Reports the last scan's scope warnings.
|
|
///
|
|
/// **The whole of the coalescing rule, and it is a loop rather than a
|
|
/// recursion on purpose.** A `didSave`, a widened scope, a changed
|
|
/// setting or a watched-file burst answered mid-scan sets
|
|
/// `rescan_wanted`; any number of them is one more scan. Finishing the
|
|
/// current scan and then running another was chosen over aborting and
|
|
/// restarting it because it needs no way to unwind a half-published index
|
|
/// and cannot loop for ever on its own — the follow-up scan reads the
|
|
/// persistent cache for every file the abandoned one already parsed, so
|
|
/// it is the cheap kind. Each scan gets its own progress token, which is
|
|
/// what a client showing a bar expects of two scans.
|
|
pub(super) fn scan_until_settled(&mut self, mut asked_for: bool) -> Vec<String> {
|
|
self.scanning = true;
|
|
let warnings = loop {
|
|
self.answering_a_rescan = asked_for;
|
|
let warnings = self.scan_once();
|
|
if !std::mem::take(&mut self.rescan_wanted) || self.stopped {
|
|
break warnings;
|
|
}
|
|
// Every scan after the first one here is one a message asked for,
|
|
// whatever the first one was.
|
|
asked_for = true;
|
|
};
|
|
self.answering_a_rescan = false;
|
|
self.scanning = false;
|
|
// A neighbourhood can only be owed by a scan, and there is none now.
|
|
self.head_owed = false;
|
|
warnings
|
|
}
|
|
|
|
/// One whole scan: the bar up, every file indexed with the client's
|
|
/// messages answered between two of them, the bar down.
|
|
///
|
|
/// A scan a `shutdown` interrupted stops where it stands and reports no
|
|
/// warnings: nothing after this may speak to a client that has gone. The
|
|
/// records of the files it did index are already in the persistent store —
|
|
/// they are written through per file — so the next session resumes from
|
|
/// them, exactly as it does after any other half-done scan.
|
|
fn scan_once(&mut self) -> Vec<String> {
|
|
// Before the bar and before the work list is built: resolving the
|
|
// scope and walking it is part of what a scan costs, and a number that
|
|
// left it out would be the wrong answer to "why was that slow?".
|
|
let started = self.timer();
|
|
let since_start = self.since_start();
|
|
let verbose = started.is_some();
|
|
let bar = self.begin_indexing();
|
|
let mut scan = self.watching(bar.as_ref(), |ws, watch| ws.begin_scan(watch, verbose));
|
|
let interleave = scan.total() > INTERLEAVE_ABOVE;
|
|
let total = scan.total();
|
|
if started.is_some() {
|
|
// Whether it interleaves is said here because it is decided here,
|
|
// and because it is the answer to "why did my hover wait?" — a
|
|
// scan that does not interleave answers nothing until it is done.
|
|
let interleaving = if interleave { ", interleaved" } else { "" };
|
|
self.timing(format!("scan started: {total} file{}{interleaving}", plural(total)));
|
|
}
|
|
while self.watching(bar.as_ref(), |workspace, watch| workspace.scan_step(&mut scan, watch))
|
|
{
|
|
if interleave && self.answer_what_is_waiting(&mut scan) {
|
|
// Nothing after this may speak to a client that has gone, the
|
|
// timing line included.
|
|
return Vec::new();
|
|
}
|
|
}
|
|
let (skipped, cached) = (scan.skipped(), scan.cached());
|
|
let warnings = self.workspace.finish_scan(scan);
|
|
self.end_indexing(bar, skipped, cached);
|
|
self.timed(started, |took| {
|
|
format!(
|
|
"scan finished: {total} file{} ({skipped} skipped, {cached} from cache) in {took}",
|
|
plural(total)
|
|
)
|
|
});
|
|
self.say_scan_trace(since_start);
|
|
warnings
|
|
}
|
|
|
|
/// The three `timing: scan …` lines the startup breakdown is read from —
|
|
/// stages, counts and the sizes the scan left resident.
|
|
///
|
|
/// Silent without `--verbose`, because a scan that was not traced left a
|
|
/// trace of zeroes and three lines of them would be a lie
|
|
/// (`docs/features/perf5-p4/README.md` holds the field list).
|
|
fn say_scan_trace(&mut self, since_start: f64) {
|
|
let mut trace = self.workspace.take_scan_trace();
|
|
if !trace.on() {
|
|
return;
|
|
}
|
|
trace.since_start_ms = since_start;
|
|
self.timing(trace.stage_line());
|
|
self.timing(trace.count_line());
|
|
self.timing(trace.memory_line());
|
|
}
|
|
|
|
/// Do one piece of a scan with the progress bar and the per-file log
|
|
/// watching it.
|
|
///
|
|
/// The fields are split apart because the watcher writes to the connection
|
|
/// while the workspace is borrowed mutably for the work it is watching —
|
|
/// the split `Server::new` used to make inline, made once here because a
|
|
/// scan is now several calls instead of one. A fresh `IndexWatch` per call
|
|
/// costs nothing and re-reads `lucisv.index.logFiles`, so a settings change
|
|
/// answered mid-scan is honoured by the very next file's log line.
|
|
fn watching<T>(
|
|
&mut self,
|
|
bar: Option<&ProgressToken>,
|
|
step: impl FnOnce(&mut Workspace, &mut dyn FnMut(IndexEvent)) -> T,
|
|
) -> T {
|
|
let Server { connection, workspace, .. } = self;
|
|
let mut watch = IndexWatch::new(connection, bar, workspace.client_settings());
|
|
step(workspace, &mut |event| watch.see(event))
|
|
}
|
|
|
|
/// Answer everything the client has already sent, against the index as far
|
|
/// as this scan has built it. Reports whether the client shut us down.
|
|
///
|
|
/// **This is the round's whole point, and its whole cost.** Polling costs
|
|
/// one `try_recv` per file and finds nothing in a scan nobody is waiting
|
|
/// on, so a quiet scan pays nothing at all; a scan somebody *is* waiting
|
|
/// on pays one merge per burst to make the records it has taken since the
|
|
/// last one answerable ([`Workspace::publish_scan`]). The messages then go
|
|
/// through the same `process_batch` the loop uses, so a `didChange` queued
|
|
/// ahead of a hover is applied before that hover is answered and a cancel
|
|
/// beside its request still answers it `RequestCancelled` — the order
|
|
/// contract is the loop's, unchanged, because it is the loop's code.
|
|
///
|
|
/// An error here is a client that has gone: there is nobody to report it
|
|
/// to and no reason to go on scanning, so it stops the scan the same way a
|
|
/// `shutdown` does.
|
|
fn answer_what_is_waiting(&mut self, scan: &mut Scan) -> bool {
|
|
// Nothing may be answered off a prefix that does not yet hold the
|
|
// folder the user is working in: a definition into the file next door
|
|
// would be `[]` (`docs/features/perf5-p4/README.md` §P4b3).
|
|
self.head_owed = !scan.neighbourhood_indexed();
|
|
let mut batch = self.waiting_for_this_scan();
|
|
while let Ok(message) = self.queue.try_recv() {
|
|
batch.push(message);
|
|
}
|
|
if batch.is_empty() {
|
|
return false;
|
|
}
|
|
// A `didOpen` in this burst names a folder the work list has not been
|
|
// ordered around yet, so the requests behind it in the burst are owed
|
|
// that ordering before they are answered — which is exactly what the
|
|
// widen this `didOpen` used to trigger made them wait for. The flag is
|
|
// set *before* the batch is applied, so `must_wait` holds them; it is
|
|
// recomputed below once the new head segment is known.
|
|
let opened = !opened_in(&batch).is_empty();
|
|
self.head_owed |= opened;
|
|
self.workspace.publish_scan(scan);
|
|
self.stopped = !matches!(self.process_batch(batch), Ok(false));
|
|
// The files the user has just opened, and the folders around them, are
|
|
// what they are looking at, so they are indexed next rather than when
|
|
// the alphabet reaches them — the open-first rule, for buffers opened
|
|
// after the work list was taken.
|
|
if opened {
|
|
self.workspace.open_next(scan);
|
|
self.head_owed = !scan.neighbourhood_indexed();
|
|
}
|
|
self.stopped
|
|
}
|
|
|
|
/// What the startup's configuration wait set aside, when this scan is the
|
|
/// one those messages were waiting for.
|
|
///
|
|
/// **Not an exemption from the order contract — the other side of it.** A
|
|
/// request that arrives before the startup scan has begun is held for the
|
|
/// same reason one arriving *during* it is answered: there is no index at
|
|
/// all to answer it from, and the scan it is waiting for is the server's
|
|
/// own startup, with nothing queued ahead of it and — being the session's
|
|
/// first — an empty `gone` set. So the startup scan answers it exactly as
|
|
/// it answers a message that turns up while it runs, off the same
|
|
/// published prefix.
|
|
///
|
|
/// A scan a **message** asked for takes nothing: its held messages are
|
|
/// queued behind the message that asked for it, and answering them off a
|
|
/// half-updated index is precisely the trade
|
|
/// `docs/features/scan-interleave/README.md`'s decision 2 refuses. Same
|
|
/// for a scan with a further rescan already wanted — the requests behind
|
|
/// that ask are owed its effect.
|
|
///
|
|
/// **And not at the very first file.** A prefix holding the user's own
|
|
/// buffer and nothing else answers a definition in the file next door with
|
|
/// `[]` — coherent, and useless, which is what §P4b2 measured
|
|
/// (`docs/features/perf5-p4/README.md`). So the startup scan takes what it
|
|
/// was handed once it has stepped the user's whole neighbourhood
|
|
/// ([`Server::head_owed`], from [`Scan::neighbourhood_indexed`]); the
|
|
/// `publish_scan` in [`Server::answer_what_is_waiting`] is what then makes
|
|
/// that neighbourhood answerable, in the same call. A scan with no
|
|
/// neighbourhood — nothing open, or a folder covering the workspace — says
|
|
/// so with `head` 0 and drains at the first file exactly as before.
|
|
/// Requests arriving *after* that are answered at every interleave point,
|
|
/// as they always were.
|
|
fn waiting_for_this_scan(&mut self) -> Vec<lsp_server::Message> {
|
|
if self.answering_a_rescan || self.rescan_wanted || self.head_owed {
|
|
return Vec::new();
|
|
}
|
|
std::mem::take(&mut self.held)
|
|
}
|
|
|
|
/// Publish a new completed hierarchy revision after any index mutation.
|
|
pub(super) fn hierarchy_index_changed(&mut self) {
|
|
self.hierarchy_revision = self.hierarchy_revision.saturating_add(1);
|
|
if !self.abilities.hierarchy_view {
|
|
return;
|
|
}
|
|
notify_on(
|
|
&self.connection,
|
|
design_hierarchy::CHANGED_METHOD,
|
|
serde_json::json!({ "revision": self.hierarchy_revision }),
|
|
);
|
|
}
|
|
}
|