M2.5: symbol index and go-to commands

crates/index: walks the project with the search/VFS rules (vendor as
library), parses .php files from tree-sitter outlines on a worker pool
off the engine lock, updates incrementally from disk and buffers through
an updater thread, caps at 200,000 files, and answers fuzzy file, class
and symbol queries (nucleo-matcher, MPL-2.0 as a separate crate).

Engine: index.status, goto.file, goto.class, goto.symbol; events
indexProgress and indexReady drained by the pump; the index starts on
project.open and is cancelled on close. Tests: unit, incremental,
property (random queries and sources), engine and TCP control tests,
release timing on a generated 20k-file project (3.9-4.2 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-17 12:53:33 +02:00
parent f13a49064d
commit 053441d066
26 changed files with 3455 additions and 9 deletions

View file

@ -12,6 +12,7 @@ publish.workspace = true
layer = 4
[dependencies]
lutra-index.workspace = true
lutra-search.workspace = true
lutra-text.workspace = true
lutra-vfs.workspace = true

View file

@ -0,0 +1,511 @@
//! The symbol index and the go-to commands: `index.status`, `goto.file`,
//! `goto.class` and `goto.symbol`.
//!
//! # Index lifecycle
//! [`sync`] runs on every [`crate::Engine::pump_vfs`] and before every
//! index command. When a project is open and no index exists for its root,
//! it starts one: [`lutra_index::spawn_build`] walks and parses the project
//! on background threads, and an [`Updater`] thread handles later changes.
//! Both send [`Update`]s over one channel; [`sync`] merges them into the
//! in-memory [`Index`] in [`State`], so the engine lock is only held for
//! cheap inserts, never while files are walked or parsed. Progress becomes
//! [`Event::IndexProgress`] (at most every [`PROGRESS_EVERY`]) and the end
//! of the build [`Event::IndexReady`]. Closing the project (or opening
//! another one) drops the index and cancels its threads.
//!
//! # Incremental updates
//! - Disk: [`note_disk_changes`] turns the workspace's VFS events into
//! updater jobs (created or modified files are re-read, created or
//! renamed directories rescanned) and drops removed paths at once. The
//! project watcher is not recursive (see [`crate::workspace`]), so changes
//! in directories that no client listed and no buffer pins are not seen
//! until the next project open.
//! - Buffers: when an open buffer's version changes, its text is
//! re-indexed instead of the file on disk; when it is closed with unsaved
//! changes, the file on disk is re-read. Only files the index already
//! lists (or, while building, may still list) are re-indexed from
//! buffers, so a buffer of an ignored file never enters the index.
//! - Updates from the updater win over build results for the same path
//! that arrive later.
//!
//! # Queries
//! Queries run on the current index, never wait for the build, and report
//! `indexing: true` while it runs. See [`lutra_index::Index`] for matching
//! and ranking.
use std::collections::{BTreeMap, HashSet};
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, TryRecvError, channel};
use std::time::{Duration, Instant};
use lutra_index::{CancelToken, Index, Job, SymbolHit, Update, Updater};
use lutra_vfs::{EntryKind, VfsEvent};
use serde::Deserialize;
use serde_json::{Value, json};
use crate::buffers::BufferId;
use crate::cmd::params::parse;
use crate::workspace::path_string;
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
/// Shortest time between two [`Event::IndexProgress`].
pub const PROGRESS_EVERY: Duration = Duration::from_millis(250);
/// Results of a go-to query when the params give no `limit`.
pub const DEFAULT_LIMIT: usize = 50;
/// Most updates merged by one [`sync`]; the rest wait for the next one, so
/// a pump never holds the engine lock for long.
pub const MAX_UPDATES_PER_SYNC: usize = 4_096;
/// The index of the open project, if one is being built or ready.
#[derive(Debug, Default)]
pub struct IndexState {
/// The index and its background threads.
running: Option<Running>,
}
impl IndexState {
/// Whether the initial build is still running.
pub fn indexing(&self) -> bool {
self.running.as_ref().is_some_and(|r| r.building)
}
/// The index, if a project is open.
pub fn index(&self) -> Option<&Index> {
self.running.as_ref().map(|r| &r.index)
}
}
/// The index of one project root and its background work.
#[derive(Debug)]
struct Running {
/// The project root.
root: PathBuf,
/// The files and symbols known so far.
index: Index,
/// Stops the build and the updater.
cancel: CancelToken,
/// Updates from the build and the updater.
updates: Receiver<Update>,
/// Re-indexes changed files; `None` if its thread could not start.
updater: Option<Updater>,
/// The initial build has not ended yet.
building: bool,
/// When the build started.
started: Instant,
/// The latest progress not yet emitted.
progress: Option<(usize, usize)>,
/// When progress was last emitted.
progress_at: Option<Instant>,
/// Paths the updater reported while the build runs; later build results
/// for them are stale.
touched: HashSet<String>,
/// The path and version each open buffer was last seen at.
buffers: BTreeMap<BufferId, (String, u64)>,
/// Paths whose index entry comes from an unsaved buffer, so disk
/// changes don't replace it.
overlay: HashSet<String>,
}
impl Drop for Running {
/// Cancels the build and the updater; their threads end at the next
/// file.
fn drop(&mut self) {
self.cancel.cancel();
}
}
impl Running {
/// Starts indexing `root`. Threads that cannot start leave an index that
/// stays empty (build) or is not updated (updater); queries still work.
fn start(root: PathBuf) -> Self {
let (tx, updates) = channel();
let cancel = CancelToken::new();
let building = lutra_index::spawn_build(
root.clone(),
lutra_index::default_threads(),
cancel.clone(),
tx.clone(),
)
.is_ok();
let updater = Updater::spawn(root.clone(), cancel.clone(), tx).ok();
Self {
root,
index: Index::new(),
cancel,
updates,
updater,
building,
started: Instant::now(),
progress: None,
progress_at: None,
touched: HashSet::new(),
buffers: BTreeMap::new(),
overlay: HashSet::new(),
}
}
/// Queues `job` for the updater.
fn send(&self, job: Job) {
if let Some(updater) = &self.updater {
updater.send(job);
}
}
/// Merges `update` into the index; returns the event it causes.
fn apply(&mut self, update: Update) -> Option<Event> {
match update {
Update::Files { files, live } => {
for file in files {
if live {
if self.building {
self.touched.insert(file.path.clone());
}
} else if self.touched.contains(&file.path) {
continue;
}
self.index.insert(file);
}
None
}
Update::Removed { path } => {
self.index.remove(&path);
if self.building {
self.touched.insert(path);
}
None
}
Update::Progress { done, total } => {
self.progress = Some((done, total));
None
}
Update::Ready(summary) => Some(self.ready(summary.elapsed)),
}
}
/// Ends the build after `elapsed` and returns [`Event::IndexReady`].
fn ready(&mut self, elapsed: Duration) -> Event {
self.building = false;
self.progress = None;
self.touched.clear();
Event::IndexReady {
files: self.index.len(),
symbols: self.index.symbol_count(),
elapsed_ms: u64::try_from(elapsed.as_millis()).unwrap_or(u64::MAX),
}
}
}
/// Brings the index in step with the state: starts it for a newly open
/// project, drops it without one, merges finished updates, re-indexes
/// changed buffers and emits progress and readiness events. Called by the
/// pump and by every index command.
pub fn sync(state: &mut State) {
let root = state
.project
.as_ref()
.map(|w| w.project.root().to_path_buf());
let Some(root) = root else {
state.index.running = None;
return;
};
if state.index.running.as_ref().is_none_or(|r| r.root != root) {
state.index.running = Some(Running::start(root));
}
let Some(running) = state.index.running.as_mut() else {
return;
};
let mut events = Vec::new();
for _ in 0..MAX_UPDATES_PER_SYNC {
match running.updates.try_recv() {
Ok(update) => events.extend(running.apply(update)),
Err(TryRecvError::Empty) => break,
Err(TryRecvError::Disconnected) => {
if running.building {
let elapsed = running.started.elapsed();
events.push(running.ready(elapsed));
}
break;
}
}
}
if let Some((done, total)) = running.progress
&& running.building
&& running
.progress_at
.is_none_or(|at| at.elapsed() >= PROGRESS_EVERY)
{
running.progress = None;
running.progress_at = Some(Instant::now());
events.push(Event::IndexProgress { done, total });
}
sync_buffers(state);
for event in events {
state.emit(event);
}
}
/// Re-indexes buffers whose text changed since the last sync, and re-reads
/// the files of closed buffers that were indexed from unsaved text.
fn sync_buffers(state: &mut State) {
let Some(running) = state.index.running.as_mut() else {
return;
};
let mut seen = BTreeMap::new();
for id in state.buffers.ids() {
let Some(open) = state.buffers.get(id) else {
continue;
};
let path = path_string(&open.path);
let dirty = open.dirty();
let changed = match running.buffers.get(&id) {
Some((old_path, version)) => *version != open.version || *old_path != path,
None => dirty,
};
let known = running.index.contains(&path) || running.building;
if changed && known {
if dirty {
running.overlay.insert(path.clone());
}
running.send(Job::File {
path: path.clone(),
text: Some(open.buffer.clone()),
});
}
if !dirty {
running.overlay.remove(&path);
}
seen.insert(id, (path, open.version));
}
let closed: Vec<String> = running
.buffers
.iter()
.filter(|(id, _)| !seen.contains_key(id))
.map(|(_, (path, _))| path.clone())
.collect();
for path in closed {
if running.overlay.remove(&path) {
running.send(Job::File { path, text: None });
}
}
running.buffers = seen;
}
/// Turns changes on disk into index updates: removed paths are dropped at
/// once, created and modified files are re-read, created directories and
/// rename targets are rescanned. Files indexed from an unsaved buffer keep
/// the buffer's text. Does nothing without an index.
pub fn note_disk_changes(state: &mut State, changes: &[VfsEvent]) {
let Some(running) = state.index.running.as_mut() else {
return;
};
for change in changes {
match change {
VfsEvent::Created { path, kind } => {
let path = path_string(path);
if *kind == EntryKind::Directory {
running.send(Job::Dir { path });
} else if !running.overlay.contains(&path) {
running.send(Job::File { path, text: None });
}
}
VfsEvent::Modified { path } => {
let path = path_string(path);
if !running.overlay.contains(&path) {
running.send(Job::File { path, text: None });
}
}
VfsEvent::Removed { path } => {
let path = path_string(path);
running.apply(Update::Removed { path });
}
VfsEvent::Renamed { from, to } => {
running.apply(Update::Removed {
path: path_string(from),
});
running.send(Job::Dir {
path: path_string(to),
});
}
VfsEvent::Failed(_) => {}
}
}
}
/// Drops the index and cancels its threads; `project.close` calls it.
pub fn close(state: &mut State) {
state.index.running = None;
}
/// `index.status`: whether the index is building and how big it is.
pub struct Status;
impl Command for Status {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "index.status",
label: "Index Status",
menu_path: None,
default_shortcut: None,
params: "none; returns { state: \"idle\" | \"indexing\", files, symbols }; events indexProgress { done, total } (at most every 250 ms) and indexReady { files, symbols, elapsedMs } report the build after project.open",
}
}
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
sync(state);
let index = state.index.index();
Ok(Outcome::value(json!({
"state": if state.index.indexing() { "indexing" } else { "idle" },
"files": index.map_or(0, Index::len),
"symbols": index.map_or(0, Index::symbol_count),
})))
}
}
/// Params of the go-to commands.
#[derive(Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct QueryParams {
/// What to look for.
query: String,
/// Most results.
limit: Option<usize>,
}
/// Parses the params of the go-to command `id`, brings the index up to
/// date and returns the query and the limit.
fn prepare(state: &mut State, id: &str, params: Value) -> Result<(String, usize), EngineError> {
let p: QueryParams = parse(id, params)?;
sync(state);
Ok((p.query, p.limit.unwrap_or(DEFAULT_LIMIT)))
}
/// The spec of a go-to command.
const fn goto_spec(id: &'static str, label: &'static str, params: &'static str) -> CommandSpec {
CommandSpec {
id,
label,
menu_path: None,
default_shortcut: None,
params,
}
}
/// `goto.file`: fuzzy search over the project's file paths.
pub struct File;
impl Command for File {
fn spec(&self) -> &CommandSpec {
const SPEC: CommandSpec = goto_spec(
"goto.file",
"Go to File",
"{ query, limit? (default 50, at most 1000) }; returns { items: [{ path, score, matches: [[start, end]] }], indexing }: fuzzy over project-relative paths, file names weighted higher, project files before vendor files; matches are UTF-16 ranges in path; indexing is true while the index is still being built (partial results)",
);
&SPEC
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let (query, limit) = prepare(state, self.spec().id, params)?;
let items: Vec<Value> = state
.index
.index()
.map(|index| index.find_files(&query, limit))
.unwrap_or_default()
.into_iter()
.map(|hit| json!({ "path": hit.path, "score": hit.score, "matches": hit.matches }))
.collect();
Ok(Outcome::value(
json!({ "items": items, "indexing": state.index.indexing() }),
))
}
}
/// `goto.class`: fuzzy search over classes, interfaces, traits and enums.
pub struct Class;
impl Command for Class {
fn spec(&self) -> &CommandSpec {
const SPEC: CommandSpec = goto_spec(
"goto.class",
"Go to Class",
"{ query, limit? (default 50, at most 1000) }; returns { items: [{ name, fqn, kind, path, line, column, library }], indexing }: classes, interfaces, traits and enums by name (by FQN when the query has a backslash), project before library; line and column (UTF-16) point at the name",
);
&SPEC
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let (query, limit) = prepare(state, self.spec().id, params)?;
let items: Vec<Value> = state
.index
.index()
.map(|index| index.find_classes(&query, limit))
.unwrap_or_default()
.iter()
.map(|hit| symbol_item(hit, false))
.collect();
Ok(Outcome::value(
json!({ "items": items, "indexing": state.index.indexing() }),
))
}
}
/// `goto.symbol`: fuzzy search over functions, methods, properties,
/// constants and enum cases.
pub struct Symbol;
impl Command for Symbol {
fn spec(&self) -> &CommandSpec {
const SPEC: CommandSpec = goto_spec(
"goto.symbol",
"Go to Symbol",
"{ query, limit? (default 50, at most 1000) }; returns { items: [{ name, fqn, kind, container, path, line, column, library }], indexing }: functions, methods, properties, constants and enum cases by name; Class::member matches the class and the member; container is the declaring class FQN or null",
);
&SPEC
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let (query, limit) = prepare(state, self.spec().id, params)?;
let items: Vec<Value> = state
.index
.index()
.map(|index| index.find_symbols(&query, limit))
.unwrap_or_default()
.iter()
.map(|hit| symbol_item(hit, true))
.collect();
Ok(Outcome::value(
json!({ "items": items, "indexing": state.index.indexing() }),
))
}
}
/// The JSON item of a class or symbol hit, with `container` for symbols.
fn symbol_item(hit: &SymbolHit<'_>, container: bool) -> Value {
let s = hit.symbol;
let mut item = json!({
"name": s.name,
"fqn": s.fqn,
"kind": s.kind.as_str(),
"path": hit.path,
"line": s.line,
"column": s.column,
"library": hit.library,
});
if container && let Some(map) = item.as_object_mut() {
map.insert("container".into(), json!(s.container));
}
item
}

View file

@ -3,6 +3,7 @@
mod app;
mod buffer;
mod file;
pub mod goto;
mod params;
mod project;
mod recovery;
@ -42,5 +43,9 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
registry.register(Box::new(ui::RegisterCommands))?;
registry.register(Box::new(search::Find))?;
registry.register(Box::new(search::Cancel))?;
registry.register(Box::new(goto::Status))?;
registry.register(Box::new(goto::File))?;
registry.register(Box::new(goto::Class))?;
registry.register(Box::new(goto::Symbol))?;
Ok(())
}

View file

@ -58,6 +58,7 @@ pub fn close_project(state: &mut State, force: bool) -> Result<(), EngineError>
}
state.journal = None;
state.project = None;
crate::cmd::goto::close(state);
state.emit(Event::ProjectClosed);
Ok(())
}

View file

@ -131,7 +131,8 @@ impl Engine {
/// Applies pending changes on disk (reloads clean buffers, reports
/// changed directories and conflicts), writes due recovery-journal
/// entries (see [`crate::journal`]), moves finished search results into
/// events (see [`cmd::search`]) and returns every queued event.
/// events (see [`cmd::search`]), merges index updates (see
/// [`cmd::goto`]) and returns every queued event.
/// Hosts call it regularly, e.g. every 100 ms from a background thread.
///
/// # Errors
@ -143,6 +144,7 @@ impl Engine {
crate::workspace::pump(state);
crate::journal::tick(state, std::time::Instant::now());
cmd::search::pump(state);
cmd::goto::sync(state);
Ok(())
})?;
Ok(state.take_events())
@ -368,6 +370,10 @@ mod tests {
"file.open",
"file.save",
"file.saveAll",
"goto.class",
"goto.file",
"goto.symbol",
"index.status",
"project.children",
"project.close",
"project.open",

View file

@ -169,6 +169,24 @@ pub enum Event {
/// Wall time of the search in milliseconds.
elapsed_ms: u64,
},
/// The symbol index is being built after `project.open`; sent at most
/// every 250 ms while it runs.
IndexProgress {
/// Files indexed so far.
done: usize,
/// Files found in the project.
total: usize,
},
/// The initial build of the symbol index ended (or was cut short); go-to
/// queries now see the whole project.
IndexReady {
/// Files in the index.
files: usize,
/// Symbols in the index.
symbols: usize,
/// Wall time of the build in milliseconds.
elapsed_ms: u64,
},
}
/// One match of [`Event::SearchResults`]. Positions are 0-based; columns
@ -312,6 +330,27 @@ mod tests {
);
}
#[test]
fn index_shapes() {
let e = Event::IndexProgress {
done: 10,
total: 20,
};
assert_eq!(
serde_json::to_value(e).unwrap(),
json!({"kind": "indexProgress", "done": 10, "total": 20})
);
let e = Event::IndexReady {
files: 20,
symbols: 300,
elapsed_ms: 1500,
};
assert_eq!(
serde_json::to_value(e).unwrap(),
json!({"kind": "indexReady", "files": 20, "symbols": 300, "elapsedMs": 1500})
);
}
#[test]
fn recovery_resolved_shape() {
let e = Event::RecoveryResolved {

View file

@ -37,6 +37,8 @@ pub struct State {
pub ui_commands: BTreeMap<String, UiCommand>,
/// Running searches (`search.find`).
pub searches: crate::cmd::search::Searches,
/// The symbol index of the open project (`index.status`, `goto.*`).
pub index: crate::cmd::goto::IndexState,
/// Number of notifications emitted, for their ids.
notifications: u64,
/// Events queued for the host, oldest first.

View file

@ -234,7 +234,8 @@ pub fn sync_buffer_dirs(state: &mut State) {
workspace.pin_dirs(dirs);
}
/// Applies pending changes on disk to the state: queues
/// Applies pending changes on disk to the state: hands them to the symbol
/// index ([`crate::cmd::goto::note_disk_changes`]), queues
/// [`Event::TreeChanged`] for affected directories, reloads clean buffers
/// whose file changed, and queues [`Event::FileChangedOnDisk`] for dirty
/// buffers and for buffers whose file disappeared.
@ -244,6 +245,7 @@ pub fn pump(state: &mut State) {
};
let (changes, failure) = workspace.drain();
let root = workspace.project.root().to_path_buf();
crate::cmd::goto::note_disk_changes(state, &changes);
let mut dirs: Vec<PathBuf> = Vec::new();
let mut add_dir = |path: &Path| {
let dir = path.parent().map(Path::to_path_buf).unwrap_or_default();

336
crates/engine/tests/goto.rs Normal file
View file

@ -0,0 +1,336 @@
//! The symbol index in the engine: `index.status`, the go-to commands,
//! progress and readiness events, and incremental updates from disk and
//! buffers.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::fs;
use std::path::Path;
use std::time::{Duration, Instant};
use lutra_engine::{Engine, Event};
use proptest::prelude::*;
use serde_json::{Value, json};
use tempfile::TempDir;
fn write(root: &Path, rel: &str, text: &str) {
let abs = root.join(rel);
fs::create_dir_all(abs.parent().unwrap()).unwrap();
fs::write(abs, text).unwrap();
}
/// A temp folder with `files`, opened in a fresh engine.
fn open_project(files: &[(&str, &str)]) -> (TempDir, Engine) {
let dir = tempfile::tempdir().unwrap();
for (path, text) in files {
write(dir.path(), path, text);
}
let mut engine = Engine::new().unwrap();
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
(dir, engine)
}
fn run(engine: &mut Engine, command: &str, params: Value) -> Value {
engine.execute(command, params).unwrap().value
}
/// Pumps until `indexReady`; returns every event seen.
fn until_ready(engine: &mut Engine) -> Vec<Event> {
let deadline = Instant::now() + Duration::from_secs(30);
let mut seen = Vec::new();
while Instant::now() < deadline {
let events = engine.pump_vfs().unwrap();
let ready = events.iter().any(|e| matches!(e, Event::IndexReady { .. }));
seen.extend(events);
if ready {
return seen;
}
std::thread::sleep(Duration::from_millis(10));
}
panic!("no indexReady within 30 s");
}
/// Pumps until `query` with `command` gives `count` items.
fn until_count(engine: &mut Engine, command: &str, query: &str, count: usize) -> Value {
let deadline = Instant::now() + Duration::from_secs(10);
loop {
engine.pump_vfs().unwrap();
let out = run(engine, command, json!({"query": query}));
if out["items"].as_array().unwrap().len() == count {
return out;
}
assert!(
Instant::now() < deadline,
"{command} {query}: wanted {count} items, got {out}"
);
std::thread::sleep(Duration::from_millis(20));
}
}
fn names(out: &Value, field: &str) -> Vec<String> {
out["items"]
.as_array()
.unwrap()
.iter()
.map(|i| i[field].as_str().unwrap().to_string())
.collect()
}
#[test]
fn commands_need_a_project_and_status_is_idle_without_one() {
let mut engine = Engine::new().unwrap();
assert_eq!(
run(&mut engine, "index.status", Value::Null),
json!({"state": "idle", "files": 0, "symbols": 0})
);
for command in ["goto.file", "goto.class", "goto.symbol"] {
let err = engine.execute(command, json!({"query": "a"})).unwrap_err();
assert_eq!(err.code(), "disabled");
}
}
#[test]
fn builds_in_the_background_and_answers_go_to_queries() {
let (_dir, mut engine) = open_project(&[
(".gitignore", "/var/\n/vendor/\n"),
(
"src/Models/User.php",
"<?php\nnamespace App\\Models;\n\nfinal class User\n{\n public const ROLE = 'user';\n private ?string $email = null;\n public function find(int $id): ?self { return null; }\n}\n",
),
(
"src/helpers.php",
"<?php\nfunction format_user(): string { return ''; }\n",
),
("templates/user.html.twig", "{{ user }}"),
("var/cache/UserCache.php", "<?php class UserCache {}"),
(
"vendor/acme/lib/User.php",
"<?php\nnamespace Acme;\n\ninterface User { public function find(); }\n",
),
]);
let events = until_ready(&mut engine);
let ready = events
.iter()
.find_map(|e| match e {
Event::IndexReady { files, symbols, .. } => Some((*files, *symbols)),
_ => None,
})
.unwrap();
assert_eq!(ready, (5, 7));
assert_eq!(
run(&mut engine, "index.status", Value::Null),
json!({"state": "idle", "files": 5, "symbols": 7})
);
let files = run(&mut engine, "goto.file", json!({"query": "user"}));
assert_eq!(files["indexing"], false);
assert_eq!(
names(&files, "path"),
vec![
"src/Models/User.php",
"templates/user.html.twig",
"vendor/acme/lib/User.php",
]
);
assert_eq!(files["items"][0]["matches"], json!([[11, 15]]));
assert!(files["items"][0]["score"].as_u64().unwrap() > 0);
let classes = run(&mut engine, "goto.class", json!({"query": "user"}));
assert_eq!(
classes["items"],
json!([
{"name": "User", "fqn": "App\\Models\\User", "kind": "class", "path": "src/Models/User.php", "line": 3, "column": 12, "library": false},
{"name": "User", "fqn": "Acme\\User", "kind": "interface", "path": "vendor/acme/lib/User.php", "line": 3, "column": 10, "library": true},
])
);
let symbols = run(
&mut engine,
"goto.symbol",
json!({"query": "find", "limit": 1}),
);
assert_eq!(
symbols["items"],
json!([{"name": "find", "fqn": "App\\Models\\User::find", "kind": "method", "container": "App\\Models\\User", "path": "src/Models/User.php", "line": 7, "column": 20, "library": false}])
);
let function = run(&mut engine, "goto.symbol", json!({"query": "format_user"}));
assert_eq!(function["items"][0]["container"], Value::Null);
let members = run(&mut engine, "goto.symbol", json!({"query": "User::"}));
assert_eq!(
names(&members, "name"),
vec!["ROLE", "find", "$email", "find"]
);
let empty = run(&mut engine, "goto.class", json!({"query": ""}));
assert_eq!(empty["items"], json!([]));
for bad in [
json!({}),
json!({"query": 3}),
json!({"query": "a", "limit": -1}),
json!({"query": "a", "extra": true}),
] {
let err = engine.execute("goto.file", bad).unwrap_err();
assert_eq!(err.code(), "invalid_params");
}
}
#[test]
fn reports_progress_and_partial_results_while_indexing() {
let dir = tempfile::tempdir().unwrap();
let project = lutra_testkit::generate_php_project(dir.path(), 3_000);
let mut engine = Engine::new().unwrap();
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
let status = run(&mut engine, "index.status", Value::Null);
assert_eq!(status["state"], "indexing");
let early = run(&mut engine, "goto.class", json!({"query": "Service"}));
assert_eq!(early["indexing"], true);
let events = until_ready(&mut engine);
let progress: Vec<(usize, usize)> = events
.iter()
.filter_map(|e| match e {
Event::IndexProgress { done, total } => Some((*done, *total)),
_ => None,
})
.collect();
assert!(
progress
.iter()
.all(|(_, total)| *total == project.indexed_files)
);
assert!(progress.windows(2).all(|w| w[0].0 <= w[1].0));
let status = run(&mut engine, "index.status", Value::Null);
assert_eq!(status["state"], "idle");
assert_eq!(status["files"], project.indexed_files);
let classes = run(
&mut engine,
"goto.class",
json!({"query": "Service1", "limit": 1000}),
);
assert_eq!(classes["indexing"], false);
assert!(!classes["items"].as_array().unwrap().is_empty());
}
#[test]
fn follows_buffers_and_disk_changes() {
let (dir, mut engine) = open_project(&[("src/A.php", "<?php class Alpha {}")]);
until_ready(&mut engine);
run(&mut engine, "project.children", json!({"dir": "src"}));
let opened = run(&mut engine, "file.open", json!({"path": "src/A.php"}));
let id = opened["id"].as_u64().unwrap();
let version = opened["version"].as_u64().unwrap();
run(
&mut engine,
"buffer.edit",
json!({"id": id, "baseVersion": version, "edits": [{"from": 12, "to": 17, "insert": "Omega"}]}),
);
until_count(&mut engine, "goto.class", "Omega", 1);
until_count(&mut engine, "goto.class", "Alpha", 0);
write(dir.path(), "src/A.php", "<?php class Disk {}");
std::thread::sleep(Duration::from_millis(300));
for _ in 0..10 {
engine.pump_vfs().unwrap();
}
assert_eq!(
names(
&run(&mut engine, "goto.class", json!({"query": "Omega"})),
"name"
),
vec!["Omega"],
"the unsaved buffer wins over the disk"
);
run(
&mut engine,
"buffer.close",
json!({"id": id, "force": true}),
);
until_count(&mut engine, "goto.class", "Disk", 1);
until_count(&mut engine, "goto.class", "Omega", 0);
write(dir.path(), "src/B.php", "<?php class Beta {}");
until_count(&mut engine, "goto.class", "Beta", 1);
fs::remove_file(dir.path().join("src/B.php")).unwrap();
until_count(&mut engine, "goto.class", "Beta", 0);
until_count(&mut engine, "goto.file", "B.php", 0);
}
#[test]
fn closing_the_project_drops_the_index() {
let dir = tempfile::tempdir().unwrap();
lutra_testkit::generate_php_project(dir.path(), 2_000);
let mut engine = Engine::new().unwrap();
engine
.execute("project.open", json!({"path": dir.path()}))
.unwrap();
assert_eq!(
run(&mut engine, "index.status", Value::Null)["state"],
"indexing"
);
run(&mut engine, "project.close", json!({}));
assert_eq!(
run(&mut engine, "index.status", Value::Null),
json!({"state": "idle", "files": 0, "symbols": 0})
);
for _ in 0..5 {
let events = engine.pump_vfs().unwrap();
assert!(
!events
.iter()
.any(|e| matches!(e, Event::IndexProgress { .. } | Event::IndexReady { .. }))
);
}
let (other, mut engine2) = open_project(&[("a.php", "<?php class First {}")]);
until_ready(&mut engine2);
let second = tempfile::tempdir().unwrap();
write(second.path(), "b.php", "<?php class Second {}");
engine2
.execute(
"project.open",
json!({"path": second.path(), "force": true}),
)
.unwrap();
until_ready(&mut engine2);
assert!(
run(&mut engine2, "goto.class", json!({"query": "First"}))["items"]
.as_array()
.unwrap()
.is_empty()
);
assert_eq!(
run(&mut engine2, "goto.class", json!({"query": "Second"}))["items"]
.as_array()
.unwrap()
.len(),
1
);
drop(other);
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(64))]
#[test]
fn random_go_to_params_never_panic(query in "\\PC{0,64}", limit in proptest::option::of(0usize..5_000)) {
let (_dir, mut engine) = open_project(&[("src/A.php", "<?php namespace N; class Alpha { function go() {} }")]);
for command in ["goto.file", "goto.class", "goto.symbol"] {
let mut params = json!({"query": query});
if let Some(limit) = limit {
params["limit"] = json!(limit);
}
let out = engine.execute(command, params).unwrap().value;
let items = out["items"].as_array().unwrap();
prop_assert!(items.len() <= limit.unwrap_or(50).min(1_000));
}
}
}