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:
parent
8701f50d37
commit
e128400246
21 changed files with 1752 additions and 48 deletions
|
|
@ -1,5 +1,6 @@
|
|||
//! Language server commands of M3.3: `diagnostics.list`, `lsp.status` and
|
||||
//! `lsp.restart`. The wiring itself lives in [`crate::lsp`].
|
||||
//! `lsp.restart`, and `lsp.log` (H3). The wiring itself lives in
|
||||
//! [`crate::lsp`].
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
|
|
@ -16,6 +17,7 @@ pub fn register(registry: &mut Registry) -> Result<(), EngineError> {
|
|||
registry.register(Box::new(List))?;
|
||||
registry.register(Box::new(Status))?;
|
||||
registry.register(Box::new(Restart))?;
|
||||
registry.register(Box::new(Log))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -70,15 +72,20 @@ impl Command for Status {
|
|||
label: "Language Server Status",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "none; returns { state: starting|ready|restarting|failed|stopped, message?, startupMs? } (startupMs: from start to the first ready)",
|
||||
params: "none; returns { state: starting|ready|restarting|failed|stopped, message?, startupMs?, progress: { active, title?, message?, percentage? } } (startupMs: from start to the first ready; progress: the newest running work-done progress task, as lspProgress reports it)",
|
||||
}
|
||||
}
|
||||
|
||||
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
|
||||
let mut value = serde_json::to_value(state.lsp.status())
|
||||
.map_err(|e| EngineError::Internal(e.to_string()))?;
|
||||
if let (Some(ms), Some(object)) = (state.lsp.startup_ms(), value.as_object_mut()) {
|
||||
object.insert("startupMs".into(), Value::from(ms));
|
||||
let progress = serde_json::to_value(state.lsp.progress())
|
||||
.map_err(|e| EngineError::Internal(e.to_string()))?;
|
||||
if let Some(object) = value.as_object_mut() {
|
||||
if let Some(ms) = state.lsp.startup_ms() {
|
||||
object.insert("startupMs".into(), Value::from(ms));
|
||||
}
|
||||
object.insert("progress".into(), progress);
|
||||
}
|
||||
Ok(Outcome::value(value))
|
||||
}
|
||||
|
|
@ -108,3 +115,23 @@ impl Command for Restart {
|
|||
Ok(Outcome::value(Value::Null))
|
||||
}
|
||||
}
|
||||
|
||||
/// `lsp.log`: the language server's recent messages, log messages and
|
||||
/// stderr lines, oldest first.
|
||||
pub struct Log;
|
||||
|
||||
impl Command for Log {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "lsp.log",
|
||||
label: "Show PHP Language Server Log",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "none; returns { lines: string[] }, oldest first, at most 1000: \"[level] text\" for window/logMessage, \"[show level] text\" for window/showMessage, \"[stderr] text\" for stderr lines",
|
||||
}
|
||||
}
|
||||
|
||||
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
|
||||
Ok(Outcome::value(json!({ "lines": state.lsp.log() })))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -260,6 +260,13 @@ impl Engine {
|
|||
self.state.lsp.set_launch(launch);
|
||||
}
|
||||
|
||||
/// Runs `php -l` with `php` on every platform, or never with `None`,
|
||||
/// instead of the default (the toolchain's PHP on Windows only); see
|
||||
/// [`crate::lsp::php_lint`].
|
||||
pub fn set_php_lint(&mut self, php: Option<std::path::PathBuf>) {
|
||||
self.state.lsp.set_php_lint(php);
|
||||
}
|
||||
|
||||
/// Read access to the state.
|
||||
pub fn state(&self) -> &State {
|
||||
&self.state
|
||||
|
|
@ -488,6 +495,7 @@ mod tests {
|
|||
"lsp.completion",
|
||||
"lsp.definition",
|
||||
"lsp.hover",
|
||||
"lsp.log",
|
||||
"lsp.prepareRename",
|
||||
"lsp.references",
|
||||
"lsp.rename",
|
||||
|
|
|
|||
|
|
@ -205,6 +205,8 @@ pub enum Event {
|
|||
DiagnosticsChanged(crate::lsp::FileDiagnostics),
|
||||
/// The Git repository may have changed: `{available, repo, filesChanged}`; refetch `git.status` (M4.1).
|
||||
GitChanged(crate::cmd::git::GitChanged),
|
||||
/// The language server's newest work-done progress task: `{active, title?, message?, percentage?}`; `active: false` when none runs.
|
||||
LspProgress(crate::lsp::LspProgress),
|
||||
/// Terminal events (`terminalOutput`, `terminalExited`), see [`crate::cmd::terminal`].
|
||||
#[serde(untagged)]
|
||||
Terminal(crate::cmd::terminal::TerminalEvent),
|
||||
|
|
|
|||
|
|
@ -30,17 +30,31 @@
|
|||
//! Requests (completion, hover, definition, … in M3.4/M3.5) go through
|
||||
//! [`submit`], on the same queue as document sync, so they never overtake
|
||||
//! an edit.
|
||||
//!
|
||||
//! Phpactor UX (H3):
|
||||
//! - `$/progress` (work-done progress) becomes `lspProgress` with the
|
||||
//! newest running task, and while any task runs the session ticks fast
|
||||
//! ([`tick_interval`]).
|
||||
//! - `window/showMessage` becomes a notification (`notification.lsp.message`,
|
||||
//! a repeated text at most once per [`MESSAGE_REPEAT`]); it,
|
||||
//! `window/logMessage` and the server's stderr go to a bounded log
|
||||
//! (`lsp.log`).
|
||||
//! - [`php_lint`] runs `php -l` on open and save where Phpactor's own lint
|
||||
//! cannot (Windows); its diagnostics are merged into `diagnosticsChanged`
|
||||
//! with `source: "php -l"`.
|
||||
|
||||
pub mod php_lint;
|
||||
pub mod session;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::{BTreeMap, VecDeque};
|
||||
use std::ffi::OsString;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use lutra_lsp::{PublishDiagnostics, Server, ServerEvent, Severity, Status};
|
||||
use lutra_lsp::{MessageLevel, PublishDiagnostics, Server, ServerEvent, Severity, Status};
|
||||
use lutra_text::{Edit, Position};
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Value};
|
||||
|
|
@ -62,6 +76,17 @@ pub const MAX_EVENTS_PER_PUMP: usize = 2_000;
|
|||
/// ignored until others clear.
|
||||
pub const MAX_FILES: usize = 10_000;
|
||||
|
||||
/// Most lines kept in the language server log (`lsp.log`); older lines
|
||||
/// are dropped.
|
||||
pub const MAX_LOG_LINES: usize = 1_000;
|
||||
|
||||
/// Most work-done progress tasks tracked at once; further `begin`s are
|
||||
/// ignored until others end.
|
||||
pub const MAX_PROGRESS_TASKS: usize = 32;
|
||||
|
||||
/// How long the same `window/showMessage` text is not shown again.
|
||||
pub const MESSAGE_REPEAT: Duration = Duration::from_secs(30);
|
||||
|
||||
/// The state of the language server as `lspStatus` and `lsp.status`
|
||||
/// report it.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
|
|
@ -168,6 +193,33 @@ pub struct DiagnosticItem {
|
|||
pub code: Option<String>,
|
||||
}
|
||||
|
||||
/// The payload of `lspProgress`: the newest running work-done progress
|
||||
/// task of the language server, or none.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
|
||||
pub struct LspProgress {
|
||||
/// Whether a task runs; the other fields are absent when not.
|
||||
pub active: bool,
|
||||
/// Its title (`Indexing workspace`), at most 200 characters.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub title: Option<String>,
|
||||
/// Its latest message (`500/2000 (25.00%, 60MB)`), at most 500
|
||||
/// characters.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub message: Option<String>,
|
||||
/// Its percentage, 0–100, when the server gives one.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub percentage: Option<u32>,
|
||||
}
|
||||
|
||||
/// One running work-done progress task.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct ProgressTask {
|
||||
/// The token, as JSON text.
|
||||
token: String,
|
||||
/// Title, message and percentage as last reported.
|
||||
progress: LspProgress,
|
||||
}
|
||||
|
||||
/// The payload of `diagnosticsChanged`, and one file of
|
||||
/// `diagnostics.list`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
|
|
@ -191,6 +243,14 @@ pub struct Lsp {
|
|||
diagnostics: BTreeMap<String, Vec<DiagnosticItem>>,
|
||||
/// Set by [`shutdown_now`] (`app.quit`): no session starts any more.
|
||||
quitting: bool,
|
||||
/// Running work-done progress tasks, oldest first.
|
||||
progress: Vec<ProgressTask>,
|
||||
/// The server's log: messages, log messages and stderr lines.
|
||||
log: VecDeque<String>,
|
||||
/// The last `window/showMessage` text shown, and when.
|
||||
last_message: Option<(String, Instant)>,
|
||||
/// Lutra's own `php -l`.
|
||||
lint: php_lint::PhpLint,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Lsp {
|
||||
|
|
@ -200,6 +260,8 @@ impl std::fmt::Debug for Lsp {
|
|||
.field("running", &self.active.is_some())
|
||||
.field("status", &self.status)
|
||||
.field("files_with_diagnostics", &self.diagnostics.len())
|
||||
.field("progress", &self.progress.len())
|
||||
.field("lint", &self.lint)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
|
@ -223,19 +285,53 @@ impl Lsp {
|
|||
self.active.as_ref().and_then(|a| a.ready_ms)
|
||||
}
|
||||
|
||||
/// Uses `php` for [`php_lint`] on every platform (tests), `None` to
|
||||
/// turn it off, instead of the default: on Windows the toolchain's PHP,
|
||||
/// elsewhere off (Phpactor lints there itself).
|
||||
pub fn set_php_lint(&mut self, php: Option<PathBuf>) {
|
||||
self.lint.set_php(php);
|
||||
}
|
||||
|
||||
/// The diagnostics of one file (project-relative, `/`), or of every
|
||||
/// file, sorted by path.
|
||||
/// file, sorted by path: the server's, then those of `php -l`.
|
||||
pub fn diagnostics(&self, path: Option<&str>) -> Vec<FileDiagnostics> {
|
||||
self.diagnostics
|
||||
.iter()
|
||||
.filter(|(p, _)| path.is_none_or(|want| want == p.as_str()))
|
||||
.map(|(p, items)| FileDiagnostics {
|
||||
let mut paths: Vec<&String> = self
|
||||
.diagnostics
|
||||
.keys()
|
||||
.chain(self.lint.paths())
|
||||
.filter(|p| path.is_none_or(|want| want == p.as_str()))
|
||||
.collect();
|
||||
paths.sort();
|
||||
paths.dedup();
|
||||
paths
|
||||
.into_iter()
|
||||
.map(|p| FileDiagnostics {
|
||||
path: p.clone(),
|
||||
items: items.clone(),
|
||||
items: self.merged(p),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The server's diagnostics of `path` followed by those of `php -l`.
|
||||
fn merged(&self, path: &str) -> Vec<DiagnosticItem> {
|
||||
let mut items = self.diagnostics.get(path).cloned().unwrap_or_default();
|
||||
items.extend(self.lint.items(path).iter().cloned());
|
||||
items
|
||||
}
|
||||
|
||||
/// The newest running work-done progress task, or an inactive value.
|
||||
pub fn progress(&self) -> LspProgress {
|
||||
self.progress
|
||||
.last()
|
||||
.map(|t| t.progress.clone())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The server's log, oldest line first (at most [`MAX_LOG_LINES`]).
|
||||
pub fn log(&self) -> Vec<String> {
|
||||
self.log.iter().cloned().collect()
|
||||
}
|
||||
|
||||
/// The server of the running session, once its process started. For
|
||||
/// reading its status and capabilities; send requests with [`submit`].
|
||||
pub fn server(&self) -> Option<&Arc<Server>> {
|
||||
|
|
@ -281,6 +377,9 @@ struct Active {
|
|||
ready_ms: Option<u64>,
|
||||
/// Synced buffers.
|
||||
docs: BTreeMap<BufferId, Doc>,
|
||||
/// Set while the server reports work-done progress; the session
|
||||
/// thread ticks fast then.
|
||||
indexing: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
/// Queues `f` on the session thread, after every document change queued
|
||||
|
|
@ -340,6 +439,7 @@ pub fn pump(state: &mut State) {
|
|||
start(state, key);
|
||||
}
|
||||
}
|
||||
php_lint::pump(state);
|
||||
if state.lsp.active.is_none() {
|
||||
return;
|
||||
}
|
||||
|
|
@ -375,6 +475,7 @@ pub fn shutdown_now(state: &mut State) {
|
|||
drop(events);
|
||||
session.join();
|
||||
clear_diagnostics(state);
|
||||
clear_progress(state);
|
||||
set_status(state, LspState::Stopped, None);
|
||||
}
|
||||
|
||||
|
|
@ -462,6 +563,39 @@ pub const TICK_METHOD: &str = "$/lutra/tick";
|
|||
/// (from its own thread, independent of the host's pump).
|
||||
pub const TICK_INTERVAL: Duration = Duration::from_millis(25);
|
||||
|
||||
/// The tick while the server indexes or the session is young
|
||||
/// ([`tick_interval`]). Phpactor's indexer waits for a 1 ms timer after
|
||||
/// every file, which only fires when a message arrives, so it indexes one
|
||||
/// file per tick. Measured on 2,000 classes (Windows 11, debug build),
|
||||
/// project open to the first useful completion: 71.6 s with the 25 ms
|
||||
/// tick, 29.9 s at 5 ms, 23.8–26.9 s at 2 ms, 20.7–22.6 s at 1 ms. The
|
||||
/// price while the window runs and Phpactor is idle: about 10% of a core
|
||||
/// for Phpactor and 4.5% for Lutra's session thread at 1 ms (9% and 3% at
|
||||
/// 2 ms).
|
||||
pub const FAST_TICK_INTERVAL: Duration = Duration::from_millis(1);
|
||||
|
||||
/// How long after its start a session ticks fast even without progress
|
||||
/// reports: Phpactor's first indexing and analysis run then.
|
||||
pub const STARTUP_WINDOW: Duration = Duration::from_secs(120);
|
||||
|
||||
/// The tick interval of a session whose normal interval is `base`: fast
|
||||
/// ([`FAST_TICK_INTERVAL`], or `base` if that is shorter) while the server
|
||||
/// reports work-done progress (`indexing`) or during the first
|
||||
/// [`STARTUP_WINDOW`]; `base` otherwise. A session without ticks (`base`
|
||||
/// is `None`) gets none.
|
||||
pub fn tick_interval(
|
||||
base: Option<Duration>,
|
||||
indexing: bool,
|
||||
since_start: Duration,
|
||||
) -> Option<Duration> {
|
||||
let base = base?;
|
||||
if indexing || since_start < STARTUP_WINDOW {
|
||||
Some(base.min(FAST_TICK_INTERVAL))
|
||||
} else {
|
||||
Some(base)
|
||||
}
|
||||
}
|
||||
|
||||
/// How long a session counts as busy after its last document change,
|
||||
/// open, save or submitted request. Long enough for Phpactor's first
|
||||
/// analysis of a project on a cold cache (16 s measured in M3.3).
|
||||
|
|
@ -491,7 +625,8 @@ pub fn tick_due(
|
|||
/// Starts a session for `key` and reports `starting`, or `failed` if the
|
||||
/// thread cannot be started.
|
||||
fn start(state: &mut State, key: Key) {
|
||||
match Session::start(key.launch.clone(), key.root.clone()) {
|
||||
let indexing = Arc::new(AtomicBool::new(false));
|
||||
match Session::start(key.launch.clone(), key.root.clone(), Arc::clone(&indexing)) {
|
||||
Ok(session) => {
|
||||
state.lsp.active = Some(Active {
|
||||
key,
|
||||
|
|
@ -501,6 +636,7 @@ fn start(state: &mut State, key: Key) {
|
|||
started: Instant::now(),
|
||||
ready_ms: None,
|
||||
docs: BTreeMap::new(),
|
||||
indexing,
|
||||
});
|
||||
set_status(state, LspState::Starting, None);
|
||||
}
|
||||
|
|
@ -513,6 +649,7 @@ fn start(state: &mut State, key: Key) {
|
|||
fn stop(state: &mut State) {
|
||||
state.lsp.active = None;
|
||||
clear_diagnostics(state);
|
||||
clear_progress(state);
|
||||
set_status(state, LspState::Stopped, None);
|
||||
}
|
||||
|
||||
|
|
@ -520,6 +657,7 @@ fn stop(state: &mut State) {
|
|||
/// user once.
|
||||
fn fail(state: &mut State, message: String) {
|
||||
clear_diagnostics(state);
|
||||
clear_progress(state);
|
||||
let changed = set_status(state, LspState::Failed, Some(message.clone()));
|
||||
if changed {
|
||||
let mut params = Map::new();
|
||||
|
|
@ -595,11 +733,159 @@ fn drain_events(state: &mut State) {
|
|||
match event {
|
||||
ServerEvent::Status(status) => apply_status(state, status),
|
||||
ServerEvent::Diagnostics(published) => publish(state, published),
|
||||
ServerEvent::ShowMessage { level, message } => show_message(state, level, message),
|
||||
ServerEvent::LogMessage { level, message } => {
|
||||
log(state, &format!("[{}] {message}", level_name(level)));
|
||||
}
|
||||
ServerEvent::Stderr(line) => log(state, &format!("[stderr] {line}")),
|
||||
ServerEvent::Progress { token, value } => progress(state, &token, &value),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of a message level in the log.
|
||||
fn level_name(level: MessageLevel) -> &'static str {
|
||||
match level {
|
||||
MessageLevel::Error => "error",
|
||||
MessageLevel::Warning => "warning",
|
||||
MessageLevel::Info => "info",
|
||||
MessageLevel::Log => "log",
|
||||
}
|
||||
}
|
||||
|
||||
/// Appends a line to the server log, dropping the oldest beyond
|
||||
/// [`MAX_LOG_LINES`].
|
||||
fn log(state: &mut State, line: &str) {
|
||||
let log = &mut state.lsp.log;
|
||||
while log.len() >= MAX_LOG_LINES {
|
||||
log.pop_front();
|
||||
}
|
||||
log.push_back(line.to_string());
|
||||
}
|
||||
|
||||
/// Whether a `window/showMessage` text is a known, expected complaint that
|
||||
/// is logged but not shown: Phpactor on Windows reports once per session
|
||||
/// that its PHP lint provider cannot start `php` (`CreateProcess failed`)
|
||||
/// and removes it. Lutra runs `php -l` itself there ([`php_lint`]).
|
||||
pub fn is_hidden_message(message: &str) -> bool {
|
||||
message.contains("Diagnostic provider") && message.contains("CreateProcess failed")
|
||||
}
|
||||
|
||||
/// Shows a `window/showMessage` as a notification of the same level
|
||||
/// (`notification.lsp.message {message}`) and logs it. Hidden texts
|
||||
/// ([`is_hidden_message`]) and a text shown less than [`MESSAGE_REPEAT`]
|
||||
/// ago are only logged.
|
||||
fn show_message(state: &mut State, level: MessageLevel, message: String) {
|
||||
log(state, &format!("[show {}] {message}", level_name(level)));
|
||||
if is_hidden_message(&message) {
|
||||
return;
|
||||
}
|
||||
let now = Instant::now();
|
||||
if let Some((last, at)) = &state.lsp.last_message
|
||||
&& *last == message
|
||||
&& now.saturating_duration_since(*at) < MESSAGE_REPEAT
|
||||
{
|
||||
return;
|
||||
}
|
||||
state.lsp.last_message = Some((message.clone(), now));
|
||||
let level = match level {
|
||||
MessageLevel::Error => NotificationLevel::Error,
|
||||
MessageLevel::Warning => NotificationLevel::Warning,
|
||||
MessageLevel::Info | MessageLevel::Log => NotificationLevel::Info,
|
||||
};
|
||||
let mut params = Map::new();
|
||||
params.insert("message".into(), Value::from(message.clone()));
|
||||
state.notify(
|
||||
level,
|
||||
Some("notification.lsp.message"),
|
||||
Some(format!("PHP language server: {message}")),
|
||||
Some(params),
|
||||
);
|
||||
}
|
||||
|
||||
/// At most `max` characters of `text`.
|
||||
fn capped(text: &str, max: usize) -> String {
|
||||
text.chars().take(max).collect()
|
||||
}
|
||||
|
||||
/// Applies one `$/progress` notification (`begin`, `report` or `end` of
|
||||
/// work-done progress) and queues `lspProgress` when the shown task
|
||||
/// changed. Values of other shapes are ignored.
|
||||
fn progress(state: &mut State, token: &Value, value: &Value) {
|
||||
let token = token.to_string();
|
||||
let text = |key: &str, max: usize| {
|
||||
value
|
||||
.get(key)
|
||||
.and_then(Value::as_str)
|
||||
.map(|t| capped(t, max))
|
||||
};
|
||||
let percentage = value
|
||||
.get("percentage")
|
||||
.and_then(Value::as_u64)
|
||||
.map(|p| u32::try_from(p.min(100)).unwrap_or(100));
|
||||
let before = state.lsp.progress();
|
||||
let tasks = &mut state.lsp.progress;
|
||||
let existing = tasks.iter().position(|t| t.token == token);
|
||||
match value.get("kind").and_then(Value::as_str) {
|
||||
Some("begin") => {
|
||||
if let Some(i) = existing {
|
||||
tasks.remove(i);
|
||||
} else if tasks.len() >= MAX_PROGRESS_TASKS {
|
||||
return;
|
||||
}
|
||||
tasks.push(ProgressTask {
|
||||
token,
|
||||
progress: LspProgress {
|
||||
active: true,
|
||||
title: Some(text("title", 200).unwrap_or_default()),
|
||||
message: text("message", 500),
|
||||
percentage,
|
||||
},
|
||||
});
|
||||
}
|
||||
Some("report") => {
|
||||
let Some(task) = existing.and_then(|i| tasks.get_mut(i)) else {
|
||||
return;
|
||||
};
|
||||
if let Some(message) = text("message", 500) {
|
||||
task.progress.message = Some(message);
|
||||
}
|
||||
if percentage.is_some() {
|
||||
task.progress.percentage = percentage;
|
||||
}
|
||||
}
|
||||
Some("end") => {
|
||||
if let Some(i) = existing {
|
||||
tasks.remove(i);
|
||||
}
|
||||
}
|
||||
_ => return,
|
||||
}
|
||||
progress_changed(state, before);
|
||||
}
|
||||
|
||||
/// Updates the session's fast-tick flag and queues `lspProgress` if the
|
||||
/// shown task differs from `before`.
|
||||
fn progress_changed(state: &mut State, before: LspProgress) {
|
||||
let running = !state.lsp.progress.is_empty();
|
||||
if let Some(active) = &state.lsp.active {
|
||||
active.indexing.store(running, Ordering::Relaxed);
|
||||
}
|
||||
let now = state.lsp.progress();
|
||||
if now != before {
|
||||
state.emit(Event::LspProgress(now));
|
||||
}
|
||||
}
|
||||
|
||||
/// Forgets every progress task (the session ended or restarts), queuing
|
||||
/// an inactive `lspProgress` if one was shown.
|
||||
fn clear_progress(state: &mut State) {
|
||||
let before = state.lsp.progress();
|
||||
state.lsp.progress.clear();
|
||||
progress_changed(state, before);
|
||||
}
|
||||
|
||||
/// Maps a client status onto `lspStatus`.
|
||||
fn apply_status(state: &mut State, status: Status) {
|
||||
match status {
|
||||
|
|
@ -616,11 +902,13 @@ fn apply_status(state: &mut State, status: Status) {
|
|||
set_status(state, LspState::Ready, None);
|
||||
}
|
||||
Status::Restarting { reason, .. } => {
|
||||
clear_progress(state);
|
||||
set_status(state, LspState::Restarting, Some(reason));
|
||||
}
|
||||
Status::Failed { message } => fail(state, message),
|
||||
Status::Stopped => {
|
||||
clear_diagnostics(state);
|
||||
clear_progress(state);
|
||||
set_status(state, LspState::Stopped, None);
|
||||
}
|
||||
}
|
||||
|
|
@ -662,20 +950,24 @@ fn publish(state: &mut State, published: PublishDiagnostics) {
|
|||
if !diagnostics.contains_key(&path) && diagnostics.len() >= MAX_FILES {
|
||||
return;
|
||||
}
|
||||
diagnostics.insert(path.clone(), items.clone());
|
||||
diagnostics.insert(path.clone(), items);
|
||||
}
|
||||
emit_diagnostics(state, path);
|
||||
}
|
||||
|
||||
/// Queues `diagnosticsChanged` for `path` with the merged list of the
|
||||
/// server and `php -l` (empty when neither has any).
|
||||
fn emit_diagnostics(state: &mut State, path: String) {
|
||||
let items = state.lsp.merged(&path);
|
||||
state.emit(Event::DiagnosticsChanged(FileDiagnostics { path, items }));
|
||||
}
|
||||
|
||||
/// Drops every diagnostic, queuing an empty `diagnosticsChanged` per file
|
||||
/// that had some.
|
||||
/// Drops every diagnostic of the server, queuing a `diagnosticsChanged`
|
||||
/// per file that had some (empty, or only those of `php -l`).
|
||||
fn clear_diagnostics(state: &mut State) {
|
||||
let files = std::mem::take(&mut state.lsp.diagnostics);
|
||||
for path in files.into_keys() {
|
||||
state.emit(Event::DiagnosticsChanged(FileDiagnostics {
|
||||
path,
|
||||
items: Vec::new(),
|
||||
}));
|
||||
emit_diagnostics(state, path);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -785,7 +1077,8 @@ mod tests {
|
|||
use std::time::{Duration, Instant};
|
||||
|
||||
use super::{
|
||||
DiagnosticSeverity, TICK_INTERVAL, TICK_WINDOW, phpactor_launch, relative, tick_due,
|
||||
DiagnosticSeverity, FAST_TICK_INTERVAL, STARTUP_WINDOW, TICK_INTERVAL, TICK_WINDOW,
|
||||
is_hidden_message, phpactor_launch, relative, tick_due, tick_interval,
|
||||
};
|
||||
use lutra_lsp::Severity;
|
||||
|
||||
|
|
@ -865,4 +1158,30 @@ mod tests {
|
|||
));
|
||||
assert!(tick_due(tick, true, recent, Some(now - TICK_INTERVAL), now));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ticks_fast_while_indexing_or_young() {
|
||||
let base = Some(TICK_INTERVAL);
|
||||
let young = Duration::from_secs(5);
|
||||
let old = STARTUP_WINDOW + Duration::from_secs(1);
|
||||
assert_eq!(tick_interval(base, false, young), Some(FAST_TICK_INTERVAL));
|
||||
assert_eq!(tick_interval(base, true, old), Some(FAST_TICK_INTERVAL));
|
||||
assert_eq!(tick_interval(base, false, old), base);
|
||||
assert_eq!(tick_interval(None, true, young), None);
|
||||
let tiny = Some(Duration::from_micros(100));
|
||||
assert_eq!(tick_interval(tiny, true, young), tiny);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hides_only_the_known_lint_provider_complaint() {
|
||||
assert!(is_hidden_message(
|
||||
"Diagnostic provider \"php\" errored with \"CreateProcess failed\", removing from pool"
|
||||
));
|
||||
assert!(!is_hidden_message(
|
||||
"File watcher died: CreateProcess failed"
|
||||
));
|
||||
assert!(!is_hidden_message(
|
||||
"Diagnostic provider \"phpstan\" errored"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
456
crates/engine/src/lsp/php_lint.rs
Normal file
456
crates/engine/src/lsp/php_lint.rs
Normal file
|
|
@ -0,0 +1,456 @@
|
|||
//! Lutra's own `php -l` (H3): syntax errors of open PHP files where
|
||||
//! Phpactor cannot lint them.
|
||||
//!
|
||||
//! Phpactor 2026.06.25.0 on Windows cannot start subprocesses (see
|
||||
//! [`super::phpactor_launch`]), so its PHP lint provider removes itself and
|
||||
//! syntax errors only showed up as Phpactor's own parse diagnostics. Lutra
|
||||
//! therefore runs `php -l` itself there:
|
||||
//! - **When:** for every open PHP buffer when it is first seen and after
|
||||
//! every save (the saved version moved and the buffer is clean). Only on
|
||||
//! Windows by default, with the toolchain's PHP; elsewhere Phpactor lints
|
||||
//! itself. [`PhpLint::set_php`] (`Engine::set_php_lint`) overrides it.
|
||||
//! - **How:** on one worker thread, never under the engine lock. The pump
|
||||
//! hands over a cheap rope snapshot; the worker runs
|
||||
//! `php -n -l -d display_errors=1 -d log_errors=0` through
|
||||
//! [`lutra_proc::spawn`] (so PHP dies with Lutra), writes the text to its
|
||||
//! standard input and reads at most [`MAX_OUTPUT`] bytes of its output,
|
||||
//! killing it after [`TIMEOUT`]. Queued runs of the same file collapse to
|
||||
//! the newest. Files over [`MAX_BYTES`] are skipped.
|
||||
//! - **Result:** `Parse error` and `Fatal error` lines become error
|
||||
//! diagnostics on the whole reported line with `source: "php -l"`
|
||||
//! ([`parse_output`]). The pump stores them per file and merges them
|
||||
//! into `diagnosticsChanged` and `diagnostics.list` after the server's.
|
||||
//! They stay until the next run of that file and are dropped when the
|
||||
//! project closes. A PHP that cannot run gives no diagnostics.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::mpsc::{self, Receiver, Sender};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use lutra_text::Buffer;
|
||||
|
||||
use super::{DiagnosticItem, DiagnosticSeverity, LinePosition, LineRange, MAX_FILES};
|
||||
use crate::State;
|
||||
use crate::buffers::BufferId;
|
||||
use crate::workspace::path_string;
|
||||
|
||||
/// The `source` of `php -l` diagnostics.
|
||||
pub const SOURCE: &str = "php -l";
|
||||
|
||||
/// How long one `php -l` may run before it is killed.
|
||||
pub const TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// Largest text linted, in bytes.
|
||||
pub const MAX_BYTES: usize = 8 * 1024 * 1024;
|
||||
|
||||
/// Most output bytes read from one run.
|
||||
pub const MAX_OUTPUT: u64 = 64 * 1024;
|
||||
|
||||
/// Most diagnostics kept per file.
|
||||
pub const MAX_ITEMS: usize = 100;
|
||||
|
||||
/// Longest message kept, in characters.
|
||||
const MAX_MESSAGE: usize = 8 * 1024;
|
||||
|
||||
/// Which PHP lints.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
enum Php {
|
||||
/// The toolchain's PHP on Windows; off elsewhere.
|
||||
#[default]
|
||||
Auto,
|
||||
/// Off.
|
||||
Off,
|
||||
/// This executable, on every platform.
|
||||
With(PathBuf),
|
||||
}
|
||||
|
||||
/// One file to lint.
|
||||
struct Job {
|
||||
/// The PHP executable.
|
||||
php: PathBuf,
|
||||
/// Project-relative path with `/`.
|
||||
path: String,
|
||||
/// The text, as saved.
|
||||
text: Buffer,
|
||||
}
|
||||
|
||||
/// The diagnostics of one run.
|
||||
struct Linted {
|
||||
/// Project-relative path with `/`.
|
||||
path: String,
|
||||
/// What PHP reported; empty for a clean file or a run that failed.
|
||||
items: Vec<DiagnosticItem>,
|
||||
}
|
||||
|
||||
/// The worker thread's queues.
|
||||
struct Worker {
|
||||
/// Jobs to the worker; dropping it ends the thread.
|
||||
jobs: Sender<Job>,
|
||||
/// Results from it.
|
||||
results: Receiver<Linted>,
|
||||
}
|
||||
|
||||
/// The `php -l` state of the engine.
|
||||
#[derive(Default)]
|
||||
pub struct PhpLint {
|
||||
/// Which PHP lints.
|
||||
php: Php,
|
||||
/// The worker, started on the first job.
|
||||
worker: Option<Worker>,
|
||||
/// The project root the results belong to.
|
||||
root: Option<PathBuf>,
|
||||
/// The saved version of each buffer last linted.
|
||||
seen: BTreeMap<BufferId, u64>,
|
||||
/// Diagnostics by project-relative path; clean files are absent.
|
||||
items: BTreeMap<String, Vec<DiagnosticItem>>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for PhpLint {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("PhpLint")
|
||||
.field("php", &self.php)
|
||||
.field("running", &self.worker.is_some())
|
||||
.field("files_with_errors", &self.items.len())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl PhpLint {
|
||||
/// Lints with `php` on every platform, or never with `None`.
|
||||
pub fn set_php(&mut self, php: Option<PathBuf>) {
|
||||
self.php = php.map_or(Php::Off, Php::With);
|
||||
}
|
||||
|
||||
/// The files with diagnostics.
|
||||
pub fn paths(&self) -> impl Iterator<Item = &String> {
|
||||
self.items.keys()
|
||||
}
|
||||
|
||||
/// The diagnostics of `path` (empty when clean or not linted).
|
||||
pub fn items(&self, path: &str) -> &[DiagnosticItem] {
|
||||
self.items.get(path).map_or(&[], Vec::as_slice)
|
||||
}
|
||||
}
|
||||
|
||||
/// The PHP that lints now, if any.
|
||||
fn php(state: &State) -> Option<PathBuf> {
|
||||
match &state.lsp.lint.php {
|
||||
Php::Off => None,
|
||||
Php::With(path) => Some(path.clone()),
|
||||
Php::Auto if cfg!(windows) => {
|
||||
let status = &state.toolchain.status().php;
|
||||
status
|
||||
.found
|
||||
.then(|| status.path.clone().map(PathBuf::from))?
|
||||
}
|
||||
Php::Auto => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Queues runs for new and saved PHP buffers and applies finished runs.
|
||||
/// Called by [`super::pump`]; never blocks.
|
||||
pub fn pump(state: &mut State) {
|
||||
let root = state
|
||||
.project
|
||||
.as_ref()
|
||||
.map(|p| p.project.root().to_path_buf());
|
||||
if state.lsp.lint.root != root {
|
||||
clear(state);
|
||||
state.lsp.lint.root = root.clone();
|
||||
}
|
||||
apply_results(state);
|
||||
let (Some(_), Some(php)) = (root, php(state)) else {
|
||||
return;
|
||||
};
|
||||
let mut jobs = Vec::new();
|
||||
let State { buffers, lsp, .. } = state;
|
||||
let lint = &mut lsp.lint;
|
||||
lint.seen.retain(|id, _| buffers.get(*id).is_some());
|
||||
for id in buffers.ids() {
|
||||
let Some(open) = buffers.get(id) else {
|
||||
continue;
|
||||
};
|
||||
if !super::is_php(&open.path) || open.dirty() {
|
||||
continue;
|
||||
}
|
||||
if lint.seen.get(&id) == Some(&open.saved_version) {
|
||||
continue;
|
||||
}
|
||||
lint.seen.insert(id, open.saved_version);
|
||||
if open.buffer.len_bytes() > MAX_BYTES {
|
||||
continue;
|
||||
}
|
||||
jobs.push(Job {
|
||||
php: php.clone(),
|
||||
path: path_string(&open.path),
|
||||
text: open.buffer.clone(),
|
||||
});
|
||||
}
|
||||
for job in jobs {
|
||||
send(&mut state.lsp.lint, job);
|
||||
}
|
||||
}
|
||||
|
||||
/// Hands a job to the worker, starting it if needed. A worker that cannot
|
||||
/// be started or has died drops the job: linting is a convenience, and the
|
||||
/// next save tries again.
|
||||
fn send(lint: &mut PhpLint, job: Job) {
|
||||
if lint.worker.is_none() {
|
||||
lint.worker = start();
|
||||
}
|
||||
if let Some(worker) = &lint.worker
|
||||
&& worker.jobs.send(job).is_err()
|
||||
{
|
||||
lint.worker = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the worker thread; `None` if the thread cannot be started.
|
||||
fn start() -> Option<Worker> {
|
||||
let (jobs, queue) = mpsc::channel::<Job>();
|
||||
let (done, results) = mpsc::channel::<Linted>();
|
||||
std::thread::Builder::new()
|
||||
.name("lutra-php-lint".into())
|
||||
.spawn(move || work(&queue, &done))
|
||||
.ok()?;
|
||||
Some(Worker { jobs, results })
|
||||
}
|
||||
|
||||
/// The worker: runs queued jobs until the engine drops its sender,
|
||||
/// collapsing queued runs of one file to the newest. Results that cannot
|
||||
/// be delivered are dropped because the engine stopped listening.
|
||||
fn work(queue: &Receiver<Job>, done: &Sender<Linted>) {
|
||||
while let Ok(first) = queue.recv() {
|
||||
let mut batch = vec![first];
|
||||
while let Ok(job) = queue.try_recv() {
|
||||
batch.retain(|queued| queued.path != job.path);
|
||||
batch.push(job);
|
||||
}
|
||||
for job in batch {
|
||||
let text = job.text.text();
|
||||
let items = run(&job.php, &text)
|
||||
.map(|output| parse_output(&output, &job.text))
|
||||
.unwrap_or_default();
|
||||
if done
|
||||
.send(Linted {
|
||||
path: job.path,
|
||||
items,
|
||||
})
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Stores finished runs and queues `diagnosticsChanged` for files whose
|
||||
/// diagnostics changed.
|
||||
fn apply_results(state: &mut State) {
|
||||
let mut finished = Vec::new();
|
||||
if let Some(worker) = &state.lsp.lint.worker {
|
||||
while let Ok(linted) = worker.results.try_recv() {
|
||||
finished.push(linted);
|
||||
}
|
||||
}
|
||||
for Linted { path, items } in finished {
|
||||
let stored = &mut state.lsp.lint.items;
|
||||
if items.is_empty() {
|
||||
if stored.remove(&path).is_none() {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if stored.get(&path) == Some(&items) {
|
||||
continue;
|
||||
}
|
||||
if !stored.contains_key(&path) && stored.len() >= MAX_FILES {
|
||||
continue;
|
||||
}
|
||||
stored.insert(path.clone(), items);
|
||||
}
|
||||
super::emit_diagnostics(state, path);
|
||||
}
|
||||
}
|
||||
|
||||
/// Drops every `php -l` diagnostic (the project closed or changed),
|
||||
/// queuing `diagnosticsChanged` for each file that had some, and forgets
|
||||
/// what was linted.
|
||||
fn clear(state: &mut State) {
|
||||
state.lsp.lint.seen.clear();
|
||||
let files = std::mem::take(&mut state.lsp.lint.items);
|
||||
for path in files.into_keys() {
|
||||
super::emit_diagnostics(state, path);
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `php -l` on `text` and returns its output (standard output, where
|
||||
/// the CLI prints errors with `display_errors=1`).
|
||||
///
|
||||
/// # Errors
|
||||
/// PHP cannot be started, its pipes fail, or it runs longer than
|
||||
/// [`TIMEOUT`] (it is killed then).
|
||||
pub fn run(php: &std::path::Path, text: &str) -> Result<String, String> {
|
||||
let mut command = Command::new(php);
|
||||
command
|
||||
.args(["-n", "-l", "-d", "display_errors=1", "-d", "log_errors=0"])
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null());
|
||||
hide_window(&mut command);
|
||||
let mut child = lutra_proc::spawn(command).map_err(|e| e.to_string())?;
|
||||
let stdout = child.stdout.take().ok_or("no output pipe")?;
|
||||
let (tx, rx) = mpsc::channel();
|
||||
std::thread::Builder::new()
|
||||
.name("lutra-php-lint-out".into())
|
||||
.spawn(move || {
|
||||
let mut out = Vec::new();
|
||||
let _ = stdout.take(MAX_OUTPUT).read_to_end(&mut out);
|
||||
let _ = tx.send(out);
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
if let Some(mut stdin) = child.stdin.take() {
|
||||
stdin
|
||||
.write_all(text.as_bytes())
|
||||
.map_err(|e| format!("cannot write to php: {e}"))?;
|
||||
}
|
||||
let started = Instant::now();
|
||||
loop {
|
||||
match child.try_wait() {
|
||||
Ok(Some(_)) => break,
|
||||
Ok(None) if started.elapsed() >= TIMEOUT => {
|
||||
let _ = child.kill_and_wait();
|
||||
return Err(format!(
|
||||
"php -l did not finish within {} s",
|
||||
TIMEOUT.as_secs()
|
||||
));
|
||||
}
|
||||
Ok(None) => std::thread::sleep(Duration::from_millis(5)),
|
||||
Err(e) => return Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
let out = rx
|
||||
.recv_timeout(Duration::from_secs(1))
|
||||
.map_err(|_| "the output of php -l did not end".to_string())?;
|
||||
Ok(String::from_utf8_lossy(&out).into_owned())
|
||||
}
|
||||
|
||||
/// Keeps a console window from flashing up when the GUI runs PHP on
|
||||
/// Windows (`CREATE_NO_WINDOW`).
|
||||
#[cfg(windows)]
|
||||
fn hide_window(command: &mut Command) {
|
||||
use std::os::windows::process::CommandExt;
|
||||
command.creation_flags(0x0800_0000);
|
||||
}
|
||||
|
||||
/// Nothing to hide outside Windows.
|
||||
#[cfg(not(windows))]
|
||||
fn hide_window(_command: &mut Command) {}
|
||||
|
||||
/// Reads the errors in `php -l` output for `text`: lines like
|
||||
/// `PHP Parse error: syntax error, unexpected token "}" in Standard input code on line 5`
|
||||
/// (with or without the `PHP ` prefix; `Parse error` and `Fatal error`).
|
||||
/// The file name is cut off: `Standard input code` (Lutra lints through
|
||||
/// standard input), else everything after the last ` in `.
|
||||
/// Each becomes an error on the whole reported line (1-based in the
|
||||
/// output, clamped to the text). Other lines (`No syntax errors detected`,
|
||||
/// `Errors parsing …`) are ignored; at most [`MAX_ITEMS`] are read and
|
||||
/// messages are capped at 8 KiB.
|
||||
pub fn parse_output(output: &str, text: &Buffer) -> Vec<DiagnosticItem> {
|
||||
output
|
||||
.lines()
|
||||
.filter_map(|line| {
|
||||
let line = line.trim();
|
||||
let line = line.strip_prefix("PHP ").unwrap_or(line);
|
||||
let rest = line
|
||||
.strip_prefix("Parse error:")
|
||||
.or_else(|| line.strip_prefix("Fatal error:"))?;
|
||||
let (head, number) = rest.rsplit_once(" on line ")?;
|
||||
let number: usize = number.trim().parse().ok()?;
|
||||
let message = head
|
||||
.strip_suffix(" in Standard input code")
|
||||
.or_else(|| head.rsplit_once(" in ").map(|(message, _)| message))
|
||||
.unwrap_or(head)
|
||||
.trim();
|
||||
let last = text.line_count().saturating_sub(1);
|
||||
let row = number.saturating_sub(1).min(last);
|
||||
let width = text
|
||||
.line_text(row)
|
||||
.map(|l| l.encode_utf16().count())
|
||||
.unwrap_or(0);
|
||||
Some(DiagnosticItem {
|
||||
range: LineRange {
|
||||
start: LinePosition {
|
||||
line: row,
|
||||
column: 0,
|
||||
},
|
||||
end: LinePosition {
|
||||
line: row,
|
||||
column: width,
|
||||
},
|
||||
},
|
||||
severity: DiagnosticSeverity::Error,
|
||||
message: super::capped(message, MAX_MESSAGE),
|
||||
source: Some(SOURCE.into()),
|
||||
code: None,
|
||||
})
|
||||
})
|
||||
.take(MAX_ITEMS)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use lutra_text::Buffer;
|
||||
use proptest::prelude::*;
|
||||
|
||||
use super::{MAX_ITEMS, SOURCE, parse_output};
|
||||
use crate::lsp::DiagnosticSeverity;
|
||||
|
||||
#[test]
|
||||
fn reads_parse_and_fatal_errors() {
|
||||
let text = Buffer::new("<?php\n\nfunction f() {\n retur 1;\n}\r\n");
|
||||
let output = "PHP Parse error: syntax error, unexpected integer \"1\" in Standard input code on line 4\nErrors parsing Standard input code\n";
|
||||
let items = parse_output(output, &text);
|
||||
assert_eq!(items.len(), 1);
|
||||
let item = &items[0];
|
||||
assert_eq!(item.message, "syntax error, unexpected integer \"1\"");
|
||||
assert_eq!(item.severity, DiagnosticSeverity::Error);
|
||||
assert_eq!(item.source.as_deref(), Some(SOURCE));
|
||||
assert_eq!((item.range.start.line, item.range.start.column), (3, 0));
|
||||
assert_eq!((item.range.end.line, item.range.end.column), (3, 12));
|
||||
let fatal = parse_output(
|
||||
"\nFatal error: Cannot use x in this context in Standard input code on line 99\n",
|
||||
&text,
|
||||
);
|
||||
assert_eq!(fatal[0].message, "Cannot use x in this context");
|
||||
assert_eq!(fatal[0].range.start.line, 5);
|
||||
let named = parse_output("Parse error: oops in C:\\p\\a.php on line 1", &text);
|
||||
assert_eq!(named[0].message, "oops");
|
||||
assert!(
|
||||
parse_output("No syntax errors detected in Standard input code\n", &text).is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn columns_count_utf16() {
|
||||
let text = Buffer::new("<?php\n$a = '😀' +;\n");
|
||||
let items = parse_output("Parse error: x in - on line 2", &text);
|
||||
assert_eq!(items[0].range.end.column, 12);
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn never_panics_and_stays_in_the_text(output in ".{0,400}", text in ".{0,200}", n in 0usize..1000) {
|
||||
let buffer = Buffer::new(&text);
|
||||
let output = format!("{output}\nParse error: {output} in x on line {n}\n");
|
||||
let items = parse_output(&output, &buffer);
|
||||
prop_assert!(items.len() <= MAX_ITEMS);
|
||||
for item in items {
|
||||
prop_assert!(item.range.start.line < buffer.line_count());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue