Files
lucisv/crates/lucisv_server/src/state/mod.rs
T
lucioandClaude Opus 5.5 4fae8fb63a TODO §55: split lucisv_server state/mod.rs by responsibility (pure move)
state/mod.rs 986 -> 63 production lines: document.rs (one analyzed
buffer), buffers.rs (open buffers), views.rs (closed-file view cache),
scope.rs (roots, settings, scope ladder), rescan.rs (scan and widen
protocol, 411), incremental.rs (one saved file re-indexed), sources.rs
(file sets for refactorings, 170). The 3 944-line inline test module moved
to state/tests/ (14 topic files). No function body changed; only forced
pub(super) widenings. Lead's line-multiset check: no code line lost or
added. CLAUDE.md measurement refreshed (2026-10-14); a stale test path in
server/tests/folders.rs corrected.

Gates on the merged tree: fmt and clippy -D warnings clean; lucisv_server
1 823 passed, 0 failed, 38 ignored (= before); the lane's workspace run 80
suites, 5 078 passed. Refactor dev Claude Fable 5.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 13:38:04 -07:00

157 lines
6.8 KiB
Rust

//! What the server remembers: one analysis per open buffer, one index for the
//! workspace. The invalidation rules are in
//! `docs/features/lucisv-server/README.md`; the short version is that a buffer's
//! analysis is rebuilt on every change and the index only on save.
//!
//! This file holds [`Workspace`] itself and the readers of its tables. Each
//! part of its behaviour is a sibling module with one `impl Workspace`:
//! `document` (one buffer's analysis), `buffers` (the open buffers, and
//! which text speaks for a path), `views` (the closed-file view cache),
//! `scope` (roots, settings and the scope ladder), `discover` (the walk
//! that finds the files), `scan` (what a scan carries), `rescan` (driving a
//! scan to the landed tables), `incremental` (one file re-indexed in place)
//! and `sources` (the file sets handed to refactorings).
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use lsp_types::Uri;
use vlog_semantic::{FileFacts, WorkspaceIndex};
use crate::candidates::Candidates;
use crate::includes::IncludeGraph;
use crate::index_cache::IndexCache;
use crate::scan_trace::ScanTrace;
use crate::settings::ClientSettings;
mod buffers;
pub(crate) mod discover;
mod document;
mod incremental;
mod rescan;
mod scan;
mod scope;
mod sources;
mod views;
#[cfg(test)]
pub use document::mixed_buffer;
pub use document::{first_token_from, first_token_reaching, Document};
pub use scan::{IndexEvent, Scan};
pub use views::FileView;
/// How many `workspace/symbol` hits are returned. Each one costs a file read
/// to turn its byte span into a line, so an unbounded answer to the empty
/// query would read the whole workspace.
pub const WORKSPACE_SYMBOL_LIMIT: usize = 256;
/// Whether the persistent store this session reads and writes through has
/// been opened yet, and where it will be opened from when something first
/// needs it.
///
/// **The re-open rule is unchanged**: every whole rescan still opens the store
/// afresh, because the directory it belongs in depends on settings that arrive
/// after startup and because a second session over the same workspace may have
/// written records this one has not seen
/// (`docs/features/index-persistence/README.md`). What moved is *when*: from
/// the top of [`Workspace::begin_scan`] to the first file that asks the store
/// a question. A scan whose work list is empty asks none — and a session's
/// first scan runs at the default settings, before any buffer is open, so its
/// list is empty and it used to read and rewrite 44 MiB it never looked at
/// (`docs/features/perf5-p4/README.md`, §P4b (b1)).
#[derive(Default)]
enum StoreOpen {
/// Opened, or deliberately absent: [`Workspace::cache`] is what a lookup
/// gets.
#[default]
Done,
/// A whole rescan asked for a re-open and nothing has needed the store
/// since. The directory is the one that scan resolved, so a settings
/// change between then and the first file cannot move it underneath.
Deferred(Option<PathBuf>),
}
/// Every open buffer plus the workspace index they are resolved against.
#[derive(Default)]
pub struct Workspace {
docs: HashMap<Uri, Document>,
roots: Vec<PathBuf>,
index: WorkspaceIndex,
/// The project-wide completion tables, rebuilt with `index` and from the
/// same file list. Precomputed because completion fires per keystroke and
/// nothing that walks the workspace may.
candidates: Candidates,
/// What every in-scope file `` `include``s, and the `+incdir+`s an
/// include is looked for in. Rebuilt with `index`, from the same list.
includes: IncludeGraph,
/// The last `lucisv.*` settings a client pushed. Default until it does.
client: ClientSettings,
/// What previous sessions' scans parsed, keyed by content: a file whose
/// bytes are in here is indexed without being parsed again. Re-opened for
/// every whole rescan, because that is when the directory it should live
/// in can have moved (`docs/features/index-persistence`) — at the first
/// file of that rescan rather than before it, which is [`StoreOpen`].
cache: IndexCache,
/// Whether that re-open has happened yet — see [`StoreOpen`].
store: StoreOpen,
/// `initializationOptions.storagePath`: the per-workspace folder the
/// editor offered for exactly this. One rung of the cache-directory
/// ladder, and the only one the server cannot work out for itself.
storage: Option<PathBuf>,
/// Every directory a buffer has been opened from this session, unraised
/// and deduped. The folder scope is derived from these rather than from
/// the currently-open buffers, so **closing a file never narrows the
/// index** — see `docs/features/index-scope/README.md`.
opened_dirs: Vec<PathBuf>,
/// The last few closed files any answer needed the text of, newest first.
/// A cache and nothing else: every entry can be dropped at any moment
/// without changing an answer, and every way a file's text can move drops
/// its entry (see [`Workspace::file_view`]).
views: RefCell<Vec<Rc<FileView>>>,
/// The trace of the scan that finished most recently, waiting for the
/// server to read it out and log it. Held here rather than returned from
/// [`Workspace::finish_scan`] so that no caller's signature moves for a
/// diagnostic; empty and zeroed unless `--verbose` asked for one.
last_trace: ScanTrace,
/// How many scans this workspace has run, so a session's lines can be
/// told apart — a `workspace` scope pays two (`docs/features/perf5-p4`).
scans: u32,
}
impl Workspace {
pub fn index(&self) -> &WorkspaceIndex {
&self.index
}
/// The index record this workspace already holds for **exactly these
/// bytes**, or nothing.
///
/// What `vlog_refactor::KnownFacts` asks a refactoring's caller. Exactly
/// one thing in this server can promise it: the persistent index cache
/// files each record under the FNV-1a of the bytes it was read off, so
/// record and key are one store entry written by one read
/// ([`crate::index_cache::IndexCache::facts_now`]). Not a modified time,
/// not a size, and **not the workspace index's own record**, which carries
/// no hash of the text it came from. An unsaved buffer misses for the same
/// reason a changed file does, so precedence needs no rule here
/// (`docs/features/perf5r-scope-reuse/README.md`).
pub fn known_facts(&self, path: &Path, text: &str) -> Option<FileFacts> {
self.cache.facts_now(path, text)
}
/// The project-wide completion tables, as of the last index rebuild.
pub fn candidates(&self) -> &Candidates {
&self.candidates
}
/// The `` `include`` graph, as of the last index rebuild.
pub fn includes(&self) -> &IncludeGraph {
&self.includes
}
}
#[cfg(test)]
mod tests;