A hover, definition, type definition, highlight, prepare-rename or signature help on a name the file itself declares, and the outline, folding and selection ranges, are answered from the open buffer ahead of a scan, a rescan or a widen (server/ahead.rs, same_file.rs). The precondition is stated once, on Server::answer_from_the_buffer; ahead::REACH lists the notifications proved unable to change a buffer and everything else blocks. Completion offers what the file can reach (imported package members, inherited class members, included headers' macros) right after its own names (completion/related.rs), and its answers are half the size (completion/wire.rs). A new largeFile.threshold re-classifies buffers already open. The client's answer to a settings pull is now ordered like a notification (amends scan-ordering); colours answered while anything is held count as unsettled. TODO 71.2. Four dev rounds, four QA rounds; the last ran with Fable 5.2 as dev and QA after two QA failures. docs/features/fast-actions. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
441 lines
21 KiB
Rust
441 lines
21 KiB
Rust
//! Answering a request **ahead** of whatever holds the rest, from its buffer
|
|
//! alone: the colours (`viewport.rs`) and every request whose whole answer the
|
|
//! buffer gives (`crate::same_file`) — where in a burst, a held batch and a
|
|
//! widen such a request is taken out, and when its buffer's text is the text
|
|
//! the client asked about (`docs/features/fast-actions/README.md`).
|
|
//!
|
|
//! **What an exempted request reads, and why that is valid mid-scan.** The
|
|
//! loop is one thread, so a request is only ever answered "during" a scan by
|
|
//! being reached from inside it — the startup's configuration wait, the drain
|
|
//! between two of a scan's files (`answer_what_is_waiting`), both through
|
|
//! `process_batch`, or a widen's between-files drain — so the scan is between
|
|
//! two files and every buffer is whole. The exemption is taken where the
|
|
//! request would otherwise be held, and runs the ordinary dispatch arm for the
|
|
//! method *without* the all-buffers flush (`answer_flushed`). A colouring arm
|
|
//! reads the buffer and its overlay (`viewport.rs` says what and why a guess
|
|
//! off the overlay is fine there); every other exempted arm reads only the
|
|
//! part of the buffer that is a function of its text, which `same_file`
|
|
//! proves position by position — so its answer **is** the settled answer.
|
|
//!
|
|
//! **When.** The whole precondition — the buffer open, every older message
|
|
//! that could change its answer applied, its analysis current under the
|
|
//! settings in force, no settings still awaited, the answer the buffer's
|
|
//! alone — is stated once, on [`Server::answer_from_the_buffer`], the one
|
|
//! function that decides. Everything else in this file is *where* a request
|
|
//! is offered to it: at the moment the loop would hold it (`process_batch`),
|
|
//! behind a held message of its own burst ([`Server::set_aside`]), behind a
|
|
//! notification of its own burst that is about to be applied
|
|
//! ([`Server::answer_ahead_of`]), behind a `didOpen` that has been applied
|
|
//! and is about to widen ([`Server::open_then_widen`]), between
|
|
//! two files of a widen ([`Server::answer_ahead_mid_widen`]) and when the
|
|
//! startup's settings land ([`Server::answer_held_ahead`]).
|
|
//!
|
|
//! **Ahead of an older message only when that message cannot matter.** What
|
|
//! is older than a request and still unapplied is in one of four places:
|
|
//! `held`; the part of its burst being set aside in front of it (`ahead`);
|
|
//! the next notification of its own burst, which the loop is about to apply
|
|
//! ([`Server::answer_ahead_of`]); or the tail of a burst a scan is nested in
|
|
//! ([`Server::behind`]). Clause (b) is one test for all of them — a request
|
|
//! passes a waiting message only if [`REACH`] proves the message cannot
|
|
//! change its buffer — where before the escalation round each place had its
|
|
//! own, and the third let a request pass a settings push (QA's S1).
|
|
//!
|
|
//! **A widen** used to drain nothing. Its `didOpen` is applied first; the
|
|
//! requests behind it in its burst that can go ahead are answered before the
|
|
//! widen runs ([`Server::open_then_widen`]), and the ones that arrive while
|
|
//! it runs between two of its files ([`Server::answer_ahead_mid_widen`]),
|
|
//! everything else being held in arrival order — so the widen stays the
|
|
//! effect of one applied message and no notification overtakes an older one.
|
|
|
|
use std::collections::HashSet;
|
|
use std::error::Error;
|
|
|
|
use lsp_server::{Message, Notification, Request, RequestId, Response};
|
|
use lsp_types::notification::{
|
|
Cancel, DidChangeConfiguration, DidChangeTextDocument, DidChangeWatchedFiles,
|
|
DidChangeWorkspaceFolders, DidCloseTextDocument, DidDeleteFiles, DidOpenTextDocument,
|
|
DidRenameFiles, DidSaveTextDocument, Notification as _,
|
|
};
|
|
use lsp_types::Uri;
|
|
|
|
use super::batch::{cancelled_in, cancelled_response};
|
|
use super::dispatch::document_uri;
|
|
use super::{Pending, Server};
|
|
use crate::same_file;
|
|
|
|
/// What applying a client notification can change of what a request answered
|
|
/// ahead reads: its buffer's `Document` (clause (b) of
|
|
/// [`Server::answer_from_the_buffer`]).
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(super) enum Reach {
|
|
/// No buffer's `Document`.
|
|
Nothing,
|
|
/// The `Document` of the one buffer its `textDocument.uri` names.
|
|
Named,
|
|
/// Any buffer's — or nobody has proved otherwise.
|
|
Any,
|
|
}
|
|
|
|
/// Every notification the server handles — the arms of `handle_notification`,
|
|
/// and the cancel the loop consumes — with what applying it can reach. A
|
|
/// method that is not here reaches [`Reach::Any`]: the list says what is
|
|
/// *proved* harmless, and a test holds it to the handler's arms
|
|
/// (`tests/fast_actions_precondition.rs`), so a new arm cannot be let through
|
|
/// by forgetting it.
|
|
///
|
|
/// What every notification does before its own arm is flush the dirty
|
|
/// buffers (`handle_notification`) — the analysis of text already applied,
|
|
/// under the settings in force, which is exactly what a request answered
|
|
/// ahead does for its own buffer first. Past that:
|
|
///
|
|
/// * `didOpen`, `didChange`, `didSave`, `didClose` replace, dirty or drop the
|
|
/// `Document` of the buffer they name and no other. What else they start —
|
|
/// a widen, a rescan — moves the index and each buffer's overlay of it.
|
|
/// * `didChangeWatchedFiles` moves the index, the include graph and the
|
|
/// overlays (`reindex`, `reindex_watched`, `refresh_includes_naming`). A
|
|
/// buffer's text is the client's whatever the disk says, so the event
|
|
/// reaches no `Document` even when it names an open buffer's own file.
|
|
/// * `$/cancelRequest` changes nothing the server holds.
|
|
/// * `didChangeConfiguration` re-analyzes every open buffer the new
|
|
/// `largeFile.threshold` classifies differently
|
|
/// (`Server::reclassify_large_buffers`), and which buffers those are is not
|
|
/// read off the wire here.
|
|
/// * `didChangeWorkspaceFolders`, `didRenameFiles` and `didDeleteFiles` move
|
|
/// the index only, like a watcher event. They are a user's occasional
|
|
/// gesture, not what a rescan storm is made of, so holding an early answer
|
|
/// behind one costs nothing and they are left unproved.
|
|
pub(super) const REACH: [(&str, Reach); 10] = [
|
|
(DidOpenTextDocument::METHOD, Reach::Named),
|
|
(DidChangeTextDocument::METHOD, Reach::Named),
|
|
(DidSaveTextDocument::METHOD, Reach::Named),
|
|
(DidCloseTextDocument::METHOD, Reach::Named),
|
|
(DidChangeWatchedFiles::METHOD, Reach::Nothing),
|
|
(Cancel::METHOD, Reach::Nothing),
|
|
(DidChangeConfiguration::METHOD, Reach::Any),
|
|
(DidChangeWorkspaceFolders::METHOD, Reach::Any),
|
|
(DidRenameFiles::METHOD, Reach::Any),
|
|
(DidDeleteFiles::METHOD, Reach::Any),
|
|
];
|
|
|
|
impl Server {
|
|
/// Answer a request now, ahead of whatever holds the rest — `None` when
|
|
/// it must wait like any other request.
|
|
///
|
|
/// **The precondition, whole.** For the buffer the request names:
|
|
///
|
|
/// * **(a) it is open**;
|
|
/// * **(b) every older client message that can change what the handler
|
|
/// reads has been applied** ([`Server::nothing_unapplied_for`]);
|
|
/// * **(c) its analysis is current** for the text applied, under the
|
|
/// settings in force. A dirty buffer is analyzed for a `same_file`
|
|
/// request, that buffer alone, exactly as the flush would
|
|
/// ([`Server::flush_buffer`]); for a colouring request it waits
|
|
/// (`viewport.rs`, "A dirty buffer waits"). A clean one already is:
|
|
/// `largeFile.threshold` is the one setting a `Document` reads, and the
|
|
/// settings applier re-analyzes whatever a new value re-classifies
|
|
/// (`Server::reclassify_large_buffers`);
|
|
/// * **(d) no settings are awaited** ([`Server::settings_awaited`]) —
|
|
/// they may re-classify any buffer the moment they land. What the
|
|
/// startup held for them is offered here again when they do
|
|
/// ([`Server::answer_held_ahead`]). The colours are exempt: a guess the
|
|
/// refresh replaces, and a buffer that changes mode is refreshed;
|
|
/// * **(e) the answer is the buffer's alone** — a colouring request by
|
|
/// `viewport.rs`'s rule, any other by
|
|
/// [`same_file::answered_by_the_buffer`], position by position.
|
|
///
|
|
/// Then the answer is the one a server taking one message at a time gives
|
|
/// at this request's place in the client's stream — which is the claim
|
|
/// `tests/fast_actions_bursts.rs` holds this function to.
|
|
///
|
|
/// `ahead` is what of its own burst is unapplied ahead of it and is not in
|
|
/// `held`. A cancel the burst carries for it is honoured here,
|
|
/// exactly as the loop would honour it.
|
|
pub(super) fn answer_from_the_buffer(
|
|
&mut self,
|
|
request: &Request,
|
|
ahead: &[Message],
|
|
cancelled: &mut HashSet<RequestId>,
|
|
) -> Option<Response> {
|
|
let colours = match self.colours_may_go_ahead(&request.method) {
|
|
Some(allowed) => allowed.then_some(true)?,
|
|
None => same_file::METHODS.contains(&request.method.as_str()).then_some(false)?,
|
|
};
|
|
let uri = document_uri(&request.params)?;
|
|
// (a) and (b).
|
|
if !self.nothing_unapplied_for(&uri, ahead) {
|
|
return None;
|
|
}
|
|
// (d), then (c), then (e).
|
|
if !colours && self.settings_awaited() {
|
|
return None;
|
|
}
|
|
if colours && self.is_dirty(&uri) {
|
|
return None;
|
|
}
|
|
if !colours {
|
|
self.flush_buffer(&uri);
|
|
let doc = self.workspace.document(&uri)?;
|
|
if !same_file::answered_by_the_buffer(doc, &request.method, &request.params) {
|
|
return None;
|
|
}
|
|
}
|
|
if cancelled.remove(&request.id) {
|
|
return Some(cancelled_response(request.id.clone()));
|
|
}
|
|
let started = self.timer();
|
|
let unsettled = self.index_unsettled();
|
|
let copy = Request::new(request.id.clone(), request.method.clone(), request.params.clone());
|
|
let response = self.answer_flushed(copy);
|
|
if colours {
|
|
self.coloured_ahead(uri, &request.method, unsettled, &response);
|
|
}
|
|
self.timed(started, |took| {
|
|
format!(
|
|
"{} answered from the buffer ahead of a scan (held={}, settled={}) in {took}",
|
|
request.method,
|
|
self.held.len(),
|
|
!unsettled
|
|
)
|
|
});
|
|
Some(response)
|
|
}
|
|
|
|
/// [`Server::answer_from_the_buffer`] for one message of a burst, the
|
|
/// answer sent: whether it was a request the exemption took.
|
|
pub(super) fn answered_ahead(
|
|
&mut self,
|
|
message: &Message,
|
|
ahead: &[Message],
|
|
cancelled: &mut HashSet<RequestId>,
|
|
) -> Result<bool, Box<dyn Error + Sync + Send>> {
|
|
let Message::Request(request) = message else { return Ok(false) };
|
|
let Some(response) = self.answer_from_the_buffer(request, ahead, cancelled) else {
|
|
return Ok(false);
|
|
};
|
|
#[cfg(test)]
|
|
super::probe::went_ahead(&request.id);
|
|
self.connection.sender.send(Message::Response(response))?;
|
|
Ok(true)
|
|
}
|
|
|
|
/// Move the rest of a burst into `rest`, behind a request that must wait
|
|
/// — except a request the exemption can answer against everything ahead
|
|
/// of it, which is answered now instead of being held.
|
|
pub(super) fn set_aside(
|
|
&mut self,
|
|
rest: &mut Vec<Message>,
|
|
behind: impl Iterator<Item = Message>,
|
|
cancelled: &mut HashSet<RequestId>,
|
|
) -> Result<(), Box<dyn Error + Sync + Send>> {
|
|
for later in behind {
|
|
if !self.answered_ahead(&later, rest, cancelled)? {
|
|
rest.push(later);
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Before `notification` is applied, answer the `same_file` requests
|
|
/// behind it in its burst that need not wait for it. Returns the rest of
|
|
/// the burst, those answers taken out, in order.
|
|
///
|
|
/// The precondition is [`Server::answer_from_the_buffer`]'s, unchanged:
|
|
/// `notification` and whatever of the burst lies between it and the
|
|
/// request are passed as what is unapplied ahead of the request, so the
|
|
/// request goes only when clause (b) proves that none of them can change
|
|
/// its buffer — a watcher event, another buffer's edit or save. Behind a
|
|
/// settings push, or anything else [`REACH`] has no proof for, it stays
|
|
/// where it is and is handled after the notification, in order.
|
|
///
|
|
/// What this buys: a request read in one burst with a rescan's trigger
|
|
/// — the client wrote them together, or the server was busy when they
|
|
/// arrived — is not carried through the whole rescan in the tail of the
|
|
/// burst that started it, where no drain can reach it. A `didChange`
|
|
/// starts nothing, so nothing is answered ahead of one.
|
|
pub(super) fn answer_ahead_of(
|
|
&mut self,
|
|
notification: &Notification,
|
|
rest: std::vec::IntoIter<Message>,
|
|
cancelled: &mut HashSet<RequestId>,
|
|
) -> Result<std::vec::IntoIter<Message>, Box<dyn Error + Sync + Send>> {
|
|
let local = |m: &Message| matches!(m, Message::Request(r) if same_file::METHODS.contains(&r.method.as_str()));
|
|
if notification.method == DidChangeTextDocument::METHOD
|
|
|| !rest.as_slice().iter().any(local)
|
|
{
|
|
return Ok(rest);
|
|
}
|
|
// Everything unapplied in this burst is passed as what is ahead of
|
|
// each request, so it is taken out of the count around this one.
|
|
let around = self.behind;
|
|
let rest: Vec<Message> = rest.collect();
|
|
let in_rest = rest.iter().filter(|m| self.owes_a_state_change(m)).count();
|
|
self.behind = around.saturating_sub(in_rest);
|
|
let mut kept = vec![Message::Notification(notification.clone())];
|
|
let mut answered = Ok(());
|
|
for later in rest {
|
|
match local(&later) {
|
|
true => match self.answered_ahead(&later, &kept, cancelled) {
|
|
Ok(true) => {}
|
|
Ok(false) => kept.push(later),
|
|
Err(gone) => {
|
|
answered = Err(gone);
|
|
kept.push(later);
|
|
}
|
|
},
|
|
false => kept.push(later),
|
|
}
|
|
}
|
|
self.behind = around;
|
|
answered?;
|
|
kept.remove(0);
|
|
Ok(kept.into_iter())
|
|
}
|
|
|
|
/// Apply one notification of a burst whose not-yet-handled tail is
|
|
/// `rest`, returning that tail: a `didOpen` goes through
|
|
/// [`Server::open_then_widen`], everything else straight to the handler.
|
|
pub(super) fn apply_in_burst(
|
|
&mut self,
|
|
notification: Notification,
|
|
rest: std::vec::IntoIter<Message>,
|
|
cancelled: &mut HashSet<RequestId>,
|
|
) -> Result<std::vec::IntoIter<Message>, Box<dyn Error + Sync + Send>> {
|
|
if notification.method != DidOpenTextDocument::METHOD {
|
|
self.notification(notification);
|
|
return Ok(rest);
|
|
}
|
|
Ok(self.open_then_widen(notification, rest.collect(), cancelled)?.into_iter())
|
|
}
|
|
|
|
/// Apply a `didOpen`, and when it widens the scope, answer the requests
|
|
/// in `rest` — the burst behind it — that can go ahead **before** the
|
|
/// widen runs, then run it. Returns what is left of `rest`, in order.
|
|
///
|
|
/// The buffer is applied by then and the answers need nothing from the
|
|
/// index, so VS Code's `full`, which arrives in the same burst as its
|
|
/// `didOpen`, colours at once instead of after the widen. Everything else
|
|
/// in `rest` is untouched and runs after the widen exactly as before; the
|
|
/// widen is still the effect of the applied `didOpen` and nothing else.
|
|
fn open_then_widen(
|
|
&mut self,
|
|
notification: Notification,
|
|
rest: Vec<Message>,
|
|
cancelled: &mut HashSet<RequestId>,
|
|
) -> Result<Vec<Message>, Box<dyn Error + Sync + Send>> {
|
|
self.viewport.defer_widen = true;
|
|
self.notification(notification);
|
|
self.viewport.defer_widen = false;
|
|
let Some(added) = self.viewport.owed_widen.take() else { return Ok(rest) };
|
|
// `rest` is passed as what is ahead of each request, so it is taken
|
|
// out of the count of unapplied notifications around this one.
|
|
let around = self.behind;
|
|
let in_rest = rest.iter().filter(|m| self.owes_a_state_change(m)).count();
|
|
self.behind = around.saturating_sub(in_rest);
|
|
self.viewport.widening = true;
|
|
let mut kept = Vec::new();
|
|
let answered = self.set_aside(&mut kept, rest.into_iter(), cancelled);
|
|
self.behind = around;
|
|
answered?;
|
|
self.reindex_widened(added);
|
|
Ok(kept)
|
|
}
|
|
|
|
/// Once the settings have been applied: answer what staging held that its
|
|
/// buffer now answers alone, and keep the rest held, in arrival order.
|
|
///
|
|
/// The same exemption the drains take, at the one moment the scan's first
|
|
/// drain is not yet reachable: everything older than each held request is
|
|
/// either applied or held ahead of it, which is what `set_aside` checks.
|
|
pub(super) fn answer_held_ahead(&mut self) -> Result<(), Box<dyn Error + Sync + Send>> {
|
|
let held = std::mem::take(&mut self.held);
|
|
let mut cancelled = cancelled_in(&held);
|
|
let mut kept = Vec::new();
|
|
let answered = self.set_aside(&mut kept, held.into_iter(), &mut cancelled);
|
|
self.held = kept;
|
|
answered
|
|
}
|
|
|
|
/// Between two files of a widen: answer the requests that have arrived
|
|
/// and can be answered from their buffers, and hold everything else, in
|
|
/// arrival order, behind whatever is already held.
|
|
///
|
|
/// Safe for the rest of the burst that carried the `didOpen`: it is older
|
|
/// than anything arriving now, and while it holds a notification
|
|
/// [`Server::behind`] is non-zero, which makes every buffer not current.
|
|
pub(super) fn answer_ahead_mid_widen(&mut self) -> Result<(), Box<dyn Error + Sync + Send>> {
|
|
let arrived: Vec<Message> = self.queue.try_iter().collect();
|
|
if arrived.is_empty() {
|
|
return Ok(());
|
|
}
|
|
let mut cancelled = cancelled_in(&arrived);
|
|
let mut kept = Vec::new();
|
|
self.set_aside(&mut kept, arrived.into_iter(), &mut cancelled)?;
|
|
self.held.extend(kept);
|
|
Ok(())
|
|
}
|
|
|
|
/// Clauses (a) and (b) of [`Server::answer_from_the_buffer`]: whether
|
|
/// `uri` is open and nothing older than the request being answered that
|
|
/// can reach it is still unapplied.
|
|
///
|
|
/// Everything older is either applied or in one of three places
|
|
/// (`docs/features/scan-ordering/README.md`): `held`; `ahead`, the part
|
|
/// of the request's own burst that is unapplied in front of it — set
|
|
/// aside, or about to be applied; and the tail of a burst whose message
|
|
/// started the scan this request is answered inside. The first two are
|
|
/// read message by message
|
|
/// ([`Server::can_reach`]); the third is only a count
|
|
/// ([`Server::behind`]), so any unapplied message in it is taken as
|
|
/// reaching this buffer.
|
|
fn nothing_unapplied_for(&self, uri: &Uri, ahead: &[Message]) -> bool {
|
|
self.workspace.document(uri).is_some()
|
|
&& self.behind == 0
|
|
&& !self.held.iter().chain(ahead).any(|older| self.can_reach(older, uri))
|
|
}
|
|
|
|
/// Whether applying `message` can change the `Document` of the buffer
|
|
/// `uri` — the default for anything not proved otherwise.
|
|
///
|
|
/// A **request** changes no `Document`: the most one does on the way is
|
|
/// the flush, the analysis of text already applied. A **response** does
|
|
/// when it is the client's answer to a settings pull, which is applied as
|
|
/// a settings push is; the server acts on no other answer in a way that
|
|
/// touches a buffer (`Server::response`). A **notification** is read off
|
|
/// [`REACH`]; one that should name a buffer and names none that parses is
|
|
/// taken as naming this one.
|
|
fn can_reach(&self, message: &Message, uri: &Uri) -> bool {
|
|
let notification = match message {
|
|
Message::Request(_) => return false,
|
|
Message::Response(response) => {
|
|
return match self.pending.get(&response.id) {
|
|
Some(Pending::Configuration(_)) => true,
|
|
Some(Pending::ApplyEdit(_)) | None => false,
|
|
}
|
|
}
|
|
Message::Notification(notification) => notification,
|
|
};
|
|
match REACH.iter().find(|(method, _)| *method == notification.method) {
|
|
Some((_, Reach::Nothing)) => false,
|
|
Some((_, Reach::Named)) => {
|
|
document_uri(¬ification.params).is_none_or(|named| named == *uri)
|
|
}
|
|
Some((_, Reach::Any)) | None => true,
|
|
}
|
|
}
|
|
|
|
/// Clause (d): whether settings the client has are still on their way —
|
|
/// the startup's pull inside its wait ([`Server::staging`]), or a pull the
|
|
/// client itself asked for by pushing nothing
|
|
/// ([`Server::settings_announced`]). The startup's pull *after* its wait
|
|
/// ran out is not: a client that never answers must not close this path
|
|
/// for the session, and its late answer is applied as a push is.
|
|
fn settings_awaited(&self) -> bool {
|
|
self.staging || self.settings_announced.is_some()
|
|
}
|
|
|
|
/// Whether `uri` has a change applied to its text and not yet analyzed.
|
|
fn is_dirty(&self, uri: &Uri) -> bool {
|
|
self.dirty.iter().any(|(pending, _)| pending == uri)
|
|
}
|
|
}
|