H3: Phpactor UX: fast tick while indexing, progress, messages, php -l

- Session tick: 1 ms while Phpactor reports work-done progress and in a
  session's first 120 s, otherwise 25 ms as before. Fast intervals use
  thread::sleep, because recv_timeout wakes at most ~64/s on Windows.
  First useful completion on 2,000 classes: 71.6 s -> 20.7-26.9 s.
- $/progress -> lspProgress {active, title?, message?, percentage?},
  progress in lsp.status, a status-bar item (LspProgressItem).
- window/showMessage -> notification.lsp.message (repeats within 30 s and
  the known Windows lint-provider complaint only logged); showMessage,
  logMessage and stderr in a 1,000-line log, new command lsp.log.
- Lutra's own php -l on Windows (lsp/php_lint.rs): on open and save, one
  worker thread, lutra_proc::spawn, stdin, 10 s timeout, output caps;
  merged into diagnosticsChanged with source "php -l".
- editor.completion.delayMs stays 0 (measured: 100 ms only delays the
  list; Phpactor marks every list incomplete).
- Fake server: scripted notifications after initialized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-25 17:33:26 +01:00
parent 8701f50d37
commit e128400246
21 changed files with 1752 additions and 48 deletions

View file

@ -12,7 +12,8 @@
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
use std::time::{Duration, Instant};
use lutra_lsp::{LspError, Server, ServerEvent, ServerOptions, Status};
@ -29,8 +30,10 @@ pub struct Launch {
/// Client options (timeouts, restart policy, `initializationOptions`).
pub options: ServerOptions,
/// Send the no-op notification [`super::TICK_METHOD`] at this interval
/// while the session is busy (see [`super::TICK_WINDOW`]); `None` for
/// servers that need no nudge. Phpactor on Windows needs it, see
/// while the session is busy (see [`super::TICK_WINDOW`]), and at
/// [`super::FAST_TICK_INTERVAL`] while the server indexes or the
/// session is young (see [`super::tick_interval`]); `None` for servers
/// that need no nudge. Phpactor on Windows needs it, see
/// [`super::phpactor_launch`].
pub tick: Option<Duration>,
}
@ -103,14 +106,18 @@ pub struct Session {
impl Session {
/// Starts the session thread, which starts `launch` in `root`.
///
/// # Parameters
/// - `indexing`: set by the pump while the server reports work-done
/// progress; the thread ticks fast while it is set.
///
/// # Errors
/// The thread could not be started.
pub fn start(launch: Launch, root: PathBuf) -> Result<Self, String> {
pub fn start(launch: Launch, root: PathBuf, indexing: Arc<AtomicBool>) -> Result<Self, String> {
let (jobs, queue) = mpsc::channel::<Job>();
let (reporter, reports) = mpsc::channel::<Report>();
let thread = std::thread::Builder::new()
.name("lsp-session".into())
.spawn(move || run(launch, &root, &queue, &reporter))
.spawn(move || run(launch, &root, &indexing, &queue, &reporter))
.map_err(|e| format!("cannot start the language server thread: {e}"))?;
Ok(Self {
jobs,
@ -153,7 +160,13 @@ impl Session {
/// engine drops its sender, then shuts the server down (`shutdown`, `exit`,
/// kill), here and never under the engine lock. Reports that cannot be
/// delivered are dropped because the engine has stopped listening.
fn run(launch: Launch, root: &Path, queue: &Receiver<Job>, reporter: &Sender<Report>) {
fn run(
launch: Launch,
root: &Path,
indexing: &AtomicBool,
queue: &Receiver<Job>,
reporter: &Sender<Report>,
) {
let Launch {
command,
args,
@ -175,7 +188,7 @@ fn run(launch: Launch, root: &Path, queue: &Receiver<Job>, reporter: &Sender<Rep
return;
}
};
work(&server, tick, queue, reporter);
work(&server, tick, indexing, queue, reporter);
match Arc::try_unwrap(server) {
Ok(server) => server.shutdown(),
Err(shared) => drop(shared),
@ -186,11 +199,35 @@ fn run(launch: Launch, root: &Path, queue: &Receiver<Job>, reporter: &Sender<Rep
/// interval, also sends [`super::TICK_METHOD`] whenever one is due
/// ([`super::tick_due`]): the thread wakes at least once per interval, so
/// ticks do not depend on how often the host pumps.
fn work(server: &Server, tick: Option<Duration>, queue: &Receiver<Job>, reporter: &Sender<Report>) {
let mut last_job = Instant::now();
///
/// The interval comes from [`super::tick_interval`]. A fast one (below
/// [`COARSE_WAIT`]) is kept with [`std::thread::sleep`] between polls of
/// the queue, because a channel wait with a timeout wakes at most about
/// 64 times a second on Windows (its timer resolution of 15.6 ms;
/// measured: 63–67 wakes/s for 1, 2 and 5 ms timeouts), while a sleep
/// uses a high-resolution timer. A job then waits at most one interval.
fn work(
server: &Server,
tick: Option<Duration>,
indexing: &AtomicBool,
queue: &Receiver<Job>,
reporter: &Sender<Report>,
) {
let started = Instant::now();
let mut last_job = started;
let mut last_tick = None;
loop {
let next = match tick {
let fast = indexing.load(Ordering::Relaxed);
let interval = super::tick_interval(tick, fast, started.elapsed());
let next = match interval {
Some(interval) if interval < COARSE_WAIT => match queue.try_recv() {
Ok(job) => Ok(job),
Err(TryRecvError::Empty) => {
std::thread::sleep(interval);
Err(RecvTimeoutError::Timeout)
}
Err(TryRecvError::Disconnected) => Err(RecvTimeoutError::Disconnected),
},
Some(interval) => queue.recv_timeout(interval),
None => queue.recv().map_err(|_| RecvTimeoutError::Disconnected),
};
@ -204,13 +241,18 @@ fn work(server: &Server, tick: Option<Duration>, queue: &Receiver<Job>, reporter
}
let now = Instant::now();
let ready = server.status() == Status::Ready;
if super::tick_due(tick, ready, Some(last_job), last_tick, now) {
let activity = if interval == tick { last_job } else { now };
if super::tick_due(interval, ready, Some(activity), last_tick, now) {
last_tick = Some(now);
server.notify(super::TICK_METHOD, Value::Object(Map::new()));
}
}
}
/// Intervals below this are waited for with a sleep instead of a channel
/// timeout (see [`work`]).
const COARSE_WAIT: Duration = Duration::from_millis(16);
/// Runs one job against the server. Sync failures are expected races
/// (a buffer closed twice, a document already open after a restart of the
/// session) and change nothing, except a `didChange` that no longer fits,