Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
210
crates/luxe-lsp/src/client.rs
Normal file
210
crates/luxe-lsp/src/client.rs
Normal file
@@ -0,0 +1,210 @@
|
||||
//! Generic, warm LSP client.
|
||||
//!
|
||||
//! Spawns a language server, speaks JSON-RPC over stdio with Content-Length framing, and stays
|
||||
//! **warm**: one client is reused across many tool calls. A background reader folds
|
||||
//! `publishDiagnostics` and `$/progress` into shared state so `get_diagnostics` can wait for the
|
||||
//! server to *settle* (indexing/flycheck idle + a quiescence window) and return the real result,
|
||||
//! not the first publish, which servers routinely send empty before indexing. Documents are opened
|
||||
//! once and updated with `didChange`; an unchanged warm read returns the cached diagnostics
|
||||
//! immediately.
|
||||
//!
|
||||
//! # Push and pull
|
||||
//!
|
||||
//! Two protocols answer the same question. Most servers *push* `publishDiagnostics` unasked;
|
||||
//! LSP 3.17 added a *pull* model where the client requests `textDocument/diagnostic` and the
|
||||
//! server answers when it has an answer. A server that only pulls — TypeScript 7's native LSP is
|
||||
//! one — says nothing at all about an open file, which a push-only client cannot tell apart from
|
||||
//! a clean one. See `LspClient::pulls_diagnostics_for` for which of the two a read uses.
|
||||
|
||||
use crate::message::JsonRpcMessage;
|
||||
use crate::protocol::{Diagnostic, Location, MarkupContent, Position};
|
||||
use luxe_core::jsonrpc_frame::write_frame;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::io::{AsyncBufRead, AsyncReadExt};
|
||||
use tokio::process::{Child, ChildStdin, Command};
|
||||
use tokio::sync::{mpsc, oneshot, Mutex};
|
||||
use tokio::time::timeout;
|
||||
use tracing::{debug, error};
|
||||
|
||||
mod diagnostics;
|
||||
mod error;
|
||||
mod language;
|
||||
mod lifecycle;
|
||||
mod nav;
|
||||
mod state;
|
||||
mod transport;
|
||||
|
||||
pub use error::LspError;
|
||||
use language::language_id_for_extension;
|
||||
use state::{PendingEntry, PullProvider, PulledReport};
|
||||
pub(crate) use state::{PendingRequests, SharedState};
|
||||
|
||||
use transport::read_loop;
|
||||
|
||||
/// Configuration for an LSP client.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LspConfig {
|
||||
/// Command to launch the language server.
|
||||
pub command: Vec<String>,
|
||||
/// Language server ID (e.g., "rust-analyzer").
|
||||
pub server_id: String,
|
||||
/// Working directory for the language server.
|
||||
pub root_path: PathBuf,
|
||||
/// Timeout for the initialize handshake.
|
||||
pub init_timeout: Duration,
|
||||
}
|
||||
|
||||
/// State of the LSP client.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ClientState {
|
||||
/// Client has not been initialized.
|
||||
Uninitialized,
|
||||
/// Client is initialized and ready.
|
||||
Initialized,
|
||||
/// Client has been shut down.
|
||||
Shutdown,
|
||||
}
|
||||
|
||||
/// A generic, warm LSP client.
|
||||
pub struct LspClient {
|
||||
config: LspConfig,
|
||||
/// Child process (stdio); `kill_on_drop` reaps it when the client is dropped.
|
||||
child: Option<Child>,
|
||||
/// Request ID counter.
|
||||
next_id: u64,
|
||||
/// Current client state.
|
||||
state: ClientState,
|
||||
/// Pending requests: id → response channel (shared with the reader task).
|
||||
pending_requests: PendingRequests,
|
||||
/// Best-effort notification tap (kept for `wait_for_notification`).
|
||||
notification_rx: mpsc::Receiver<JsonRpcMessage>,
|
||||
notification_tx: mpsc::Sender<JsonRpcMessage>,
|
||||
/// Stdin handle, shared so the reader can auto-reply to server→client requests.
|
||||
stdin: Option<Arc<Mutex<ChildStdin>>>,
|
||||
/// Live diagnostics/progress state folded by the reader.
|
||||
shared: Arc<SharedState>,
|
||||
/// Open documents: URI → (version, content hash) for `didChange` sync + warm reuse.
|
||||
open_docs: HashMap<String, (i32, u64)>,
|
||||
/// The server's `diagnosticProvider` capability, when it answers `textDocument/diagnostic`.
|
||||
pull_diagnostics: Option<PullProvider>,
|
||||
/// Latest pull-diagnostics report per URI.
|
||||
///
|
||||
/// Kept beside the pushed cache rather than merged into it, so "this server has never said
|
||||
/// anything about this file" stays a question the client can answer.
|
||||
pulled: HashMap<String, PulledReport>,
|
||||
/// Keeps the server's stderr drained for as long as it runs; see [`drain_stderr`].
|
||||
stderr_drain: Option<tokio::task::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl LspClient {
|
||||
/// Create a new LSP client with the given configuration.
|
||||
pub fn new(config: LspConfig) -> Self {
|
||||
let (notification_tx, notification_rx) = mpsc::channel(100);
|
||||
Self {
|
||||
config,
|
||||
child: None,
|
||||
next_id: 1,
|
||||
state: ClientState::Uninitialized,
|
||||
pending_requests: Arc::new(Mutex::new(HashMap::new())),
|
||||
notification_rx,
|
||||
notification_tx,
|
||||
stdin: None,
|
||||
shared: SharedState::new(),
|
||||
open_docs: HashMap::new(),
|
||||
pull_diagnostics: None,
|
||||
pulled: HashMap::new(),
|
||||
stderr_drain: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The spawned server's pid, while it is running.
|
||||
///
|
||||
/// For asserting that a failed handshake actually reaped its child rather than abandoning it.
|
||||
pub fn child_id(&self) -> Option<u32> {
|
||||
self.child.as_ref().and_then(tokio::process::Child::id)
|
||||
}
|
||||
|
||||
/// The `languageId` to announce for `uri`.
|
||||
///
|
||||
/// The extension decides, and this server's own language is the fallback — not `rust`. A `.c`
|
||||
/// file opened against clangd announced as Rust, or a `.tsx` as plain `typescript`, is an
|
||||
/// assertion about the file that is false, and a server is entitled to act on it.
|
||||
fn language_id_for(&self, uri: &str) -> String {
|
||||
uri.rsplit('.')
|
||||
.next()
|
||||
.filter(|extension| !extension.contains('/'))
|
||||
.and_then(language_id_for_extension)
|
||||
.map_or_else(|| self.config.server_id.clone(), str::to_string)
|
||||
}
|
||||
|
||||
/// Send a request and wait for the matching response.
|
||||
pub async fn send_request(&mut self, method: &str, params: Value) -> Result<Value, LspError> {
|
||||
if self.state != ClientState::Initialized {
|
||||
return Err(LspError::NotInitialized);
|
||||
}
|
||||
self.request(method, params).await
|
||||
}
|
||||
|
||||
/// Sends a request whatever the client's state: the handshake's own `initialize` goes out
|
||||
/// before the client is ready.
|
||||
async fn request(&mut self, method: &str, params: Value) -> Result<Value, LspError> {
|
||||
let stdin = self
|
||||
.stdin
|
||||
.clone()
|
||||
.ok_or_else(|| LspError::IoError(std::io::Error::other("Stdin not available")))?;
|
||||
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
let message = JsonRpcMessage::request(id, method.to_owned(), params);
|
||||
let json = serde_json::to_string(&message).map_err(LspError::Serialization)?;
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
self.pending_requests.lock().await.insert(id, tx);
|
||||
// a failed write or a dropped future would otherwise leave the entry behind
|
||||
let _forget = PendingEntry { pending: Arc::clone(&self.pending_requests), id };
|
||||
{
|
||||
let mut guard = stdin.lock().await;
|
||||
write_frame(&mut *guard, json.as_bytes()).await.map_err(LspError::IoError)?;
|
||||
}
|
||||
let response = rx.await.map_err(|_| LspError::RequestCancelled)?;
|
||||
response.map_err(LspError::ServerRefused)
|
||||
}
|
||||
|
||||
/// Send a notification (no response expected).
|
||||
pub async fn send_notification(&mut self, method: &str, params: Value) -> Result<(), LspError> {
|
||||
if self.state != ClientState::Initialized {
|
||||
return Err(LspError::NotInitialized);
|
||||
}
|
||||
self.notify(method, params).await
|
||||
}
|
||||
|
||||
/// Sends a notification whatever the client's state, as the handshake's `initialized` is.
|
||||
async fn notify(&mut self, method: &str, params: Value) -> Result<(), LspError> {
|
||||
let stdin = self
|
||||
.stdin
|
||||
.clone()
|
||||
.ok_or_else(|| LspError::IoError(std::io::Error::other("Stdin not available")))?;
|
||||
let message = JsonRpcMessage::notification(method.to_owned(), params);
|
||||
let json = serde_json::to_string(&message).map_err(LspError::Serialization)?;
|
||||
let mut guard = stdin.lock().await;
|
||||
write_frame(&mut *guard, json.as_bytes()).await.map_err(LspError::IoError)
|
||||
}
|
||||
|
||||
/// Receive a notification without blocking (best-effort tap).
|
||||
pub async fn receive_notification(&mut self) -> Option<JsonRpcMessage> {
|
||||
self.notification_rx.try_recv().ok()
|
||||
}
|
||||
|
||||
/// Wait for the next notification (best-effort tap).
|
||||
pub async fn wait_for_notification(&mut self) -> Option<JsonRpcMessage> {
|
||||
self.notification_rx.recv().await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
262
crates/luxe-lsp/src/client/diagnostics.rs
Normal file
262
crates/luxe-lsp/src/client/diagnostics.rs
Normal file
@@ -0,0 +1,262 @@
|
||||
//! Diagnostics — opening documents, and settling on what the server has to say about them.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Hash document content to a `u64` so an unchanged warm read can skip a resync.
|
||||
pub(super) fn hash_content(content: &str) -> u64 {
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
content.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
/// Default budget for a cold `get_diagnostics` settle wait.
|
||||
pub(super) const DEFAULT_DIAGNOSTICS_WAIT: Duration = Duration::from_secs(10);
|
||||
/// Minimum time to give the server to react to a document open/change before settling.
|
||||
pub(super) const SETTLE_MIN_WAIT: Duration = Duration::from_millis(150);
|
||||
/// How long the diagnostics cache must be unchanged (and progress idle) to count as settled.
|
||||
pub(super) const SETTLE_QUIESCENCE: Duration = Duration::from_millis(250);
|
||||
/// After this long idle + quiescent with no publish at all, conclude "clean, nothing to report"
|
||||
/// rather than waiting the whole budget. A server still working holds `$/progress` tokens
|
||||
/// (progress_active > 0), so this only fires for a genuinely silent server.
|
||||
pub(super) const SETTLE_NO_PUBLISH_GRACE: Duration = Duration::from_millis(1500);
|
||||
|
||||
impl LspClient {
|
||||
/// Ensure a document is open and synced; returns `true` when a fresh publish should be awaited.
|
||||
///
|
||||
/// A brand-new document is opened (`didOpen`); a changed one is synced (`didChange`); an
|
||||
/// unchanged, already-open document returns `false` so a warm read can use the cache directly.
|
||||
pub(super) async fn ensure_document(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
content: &str,
|
||||
) -> Result<bool, LspError> {
|
||||
let hash = hash_content(content);
|
||||
match self.open_docs.get(uri).copied() {
|
||||
Some((_, old_hash)) if old_hash == hash => return Ok(false),
|
||||
Some((version, _)) => {
|
||||
let version = version + 1;
|
||||
self.send_notification(
|
||||
"textDocument/didChange",
|
||||
serde_json::json!({
|
||||
"textDocument": { "uri": uri, "version": version },
|
||||
"contentChanges": [{ "text": content }]
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
self.open_docs.insert(uri.to_string(), (version, hash));
|
||||
}
|
||||
None => {
|
||||
self.send_notification(
|
||||
"textDocument/didOpen",
|
||||
serde_json::json!({
|
||||
"textDocument": {
|
||||
"uri": uri,
|
||||
"languageId": self.language_id_for(uri),
|
||||
"version": 1,
|
||||
"text": content
|
||||
}
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
self.open_docs.insert(uri.to_string(), (1, hash));
|
||||
}
|
||||
}
|
||||
// Reset quiescence from our own action so a silent-clean server still settles cleanly.
|
||||
self.shared.mark_change();
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Ensure a document is open (synced from the given content) so document-scoped requests
|
||||
/// (`documentSymbol`, navigation) resolve. Idempotent for unchanged content.
|
||||
pub async fn open_document(&mut self, uri: &str, content: &str) -> Result<(), LspError> {
|
||||
self.ensure_document(uri, content).await.map(|_| ())
|
||||
}
|
||||
|
||||
/// Wait until the server is ready to answer navigation/symbol requests.
|
||||
///
|
||||
/// Prefers rust-analyzer's `experimental/serverStatus { quiescent: true }`; for servers that
|
||||
/// don't send it, falls back to "no in-flight `$/progress` and quiet for a settle window".
|
||||
/// Returns immediately for a warm, already-quiescent server.
|
||||
pub async fn wait_until_idle(&self, wait: Duration) {
|
||||
const FALLBACK_QUIESCENCE: Duration = Duration::from_millis(1200);
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
let (progress_active, quiescent, since_change) = {
|
||||
let inner = self.shared.inner.lock().expect("diagnostics state poisoned");
|
||||
(inner.progress_active, inner.quiescent, inner.since_change())
|
||||
};
|
||||
if quiescent || (progress_active == 0 && since_change >= FALLBACK_QUIESCENCE) {
|
||||
break;
|
||||
}
|
||||
if start.elapsed() >= wait {
|
||||
break;
|
||||
}
|
||||
tokio::select! {
|
||||
_ = self.shared.notify.notified() => {}
|
||||
_ = tokio::time::sleep(Duration::from_millis(80)) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get diagnostics for a document, waiting up to the default budget for the server to settle.
|
||||
pub async fn get_diagnostics(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
content: &str,
|
||||
) -> Result<Vec<Diagnostic>, LspError> {
|
||||
self.get_diagnostics_with_wait(uri, content, DEFAULT_DIAGNOSTICS_WAIT).await
|
||||
}
|
||||
|
||||
/// Get diagnostics for a document with an explicit settle budget.
|
||||
///
|
||||
/// Opens/syncs the document, then waits until the server is idle (no in-flight `$/progress`),
|
||||
/// the cache has been quiescent, and at least one publish arrived — or the budget elapses.
|
||||
/// A warm, unchanged document returns the cached result immediately. A server that has never
|
||||
/// published about this document and can be asked is asked, rather than reported as clean.
|
||||
pub async fn get_diagnostics_with_wait(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
content: &str,
|
||||
wait: Duration,
|
||||
) -> Result<Vec<Diagnostic>, LspError> {
|
||||
if self.state != ClientState::Initialized {
|
||||
return Err(LspError::NotInitialized);
|
||||
}
|
||||
if !self.ensure_document(uri, content).await? {
|
||||
return Ok(self.cached_diagnostics(uri));
|
||||
}
|
||||
|
||||
let base_publish = self.publish_count();
|
||||
let start = Instant::now();
|
||||
loop {
|
||||
let (progress_active, since_change, publish_count) = {
|
||||
let inner = self.shared.inner.lock().expect("diagnostics state poisoned");
|
||||
(inner.progress_active, inner.since_change(), inner.publish_count)
|
||||
};
|
||||
let idle_quiescent = start.elapsed() >= SETTLE_MIN_WAIT
|
||||
&& progress_active == 0
|
||||
&& since_change >= SETTLE_QUIESCENCE;
|
||||
// Waiting out the no-publish grace is pointless when the answer is going to be asked
|
||||
// for: the request itself is the synchronization point, since the server holds it
|
||||
// until it can answer. Only the idle check still has to pass, so a server that is
|
||||
// still loading the project is not interrupted mid-index.
|
||||
let settled = idle_quiescent
|
||||
&& (publish_count > base_publish
|
||||
|| self.pulls_diagnostics_for(uri)
|
||||
|| start.elapsed() >= SETTLE_NO_PUBLISH_GRACE);
|
||||
if settled || start.elapsed() >= wait {
|
||||
break;
|
||||
}
|
||||
tokio::select! {
|
||||
_ = self.shared.notify.notified() => {}
|
||||
_ = tokio::time::sleep(Duration::from_millis(80)) => {}
|
||||
}
|
||||
}
|
||||
if self.pulls_diagnostics_for(uri) {
|
||||
// What is left of the caller's budget: the request is where a pull server does its
|
||||
// thinking, so it has to answer inside the window the settle loop would have spent.
|
||||
self.pull_document_diagnostics(uri, wait.saturating_sub(start.elapsed())).await;
|
||||
}
|
||||
Ok(self.cached_diagnostics(uri))
|
||||
}
|
||||
|
||||
/// Whether this document's diagnostics have to be asked for rather than waited on.
|
||||
///
|
||||
/// Push wins wherever it is offered: a server that has published about this file has answered,
|
||||
/// and asking again would only replace a settled answer with a second opinion — rust-analyzer
|
||||
/// advertises both and its pushed set is cargo's, which is the one a reader means. What is left
|
||||
/// is the file the server has said nothing about, which is where a pull-only server lives.
|
||||
pub(super) fn pulls_diagnostics_for(&self, uri: &str) -> bool {
|
||||
self.pull_diagnostics.is_some() && !self.has_published(uri)
|
||||
}
|
||||
|
||||
/// Ask for one document's diagnostics and cache the answer.
|
||||
///
|
||||
/// Best-effort: a refusal, or a server that sits on the request past `budget`, is logged and
|
||||
/// leaves the previous answer in place — the caller is a diagnostics read that still has a
|
||||
/// pushed cache to fall back on.
|
||||
pub(super) async fn pull_document_diagnostics(&mut self, uri: &str, budget: Duration) {
|
||||
let mut params = serde_json::json!({ "textDocument": { "uri": uri } });
|
||||
if let Some(identifier) = self.pull_diagnostics.as_ref().and_then(|p| p.identifier.clone())
|
||||
{
|
||||
params["identifier"] = Value::String(identifier);
|
||||
}
|
||||
if let Some(result_id) = self.pulled.get(uri).and_then(|report| report.result_id.clone()) {
|
||||
params["previousResultId"] = Value::String(result_id);
|
||||
}
|
||||
let request = self.send_request("textDocument/diagnostic", params);
|
||||
let response = match tokio::time::timeout(budget, request).await {
|
||||
Ok(Ok(response)) => response,
|
||||
Ok(Err(error)) => {
|
||||
debug!(uri, %error, "lsp: pull diagnostics refused");
|
||||
return;
|
||||
}
|
||||
Err(_) => {
|
||||
debug!(uri, ?budget, "lsp: pull diagnostics did not answer in the budget");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let result_id = response.get("resultId").and_then(Value::as_str).map(str::to_string);
|
||||
// `unchanged` means the previous answer still stands; only a `full` report carries items.
|
||||
if response.get("kind").and_then(Value::as_str) == Some("unchanged") {
|
||||
if let Some(report) = self.pulled.get_mut(uri) {
|
||||
report.result_id = result_id;
|
||||
}
|
||||
return;
|
||||
}
|
||||
let items = response
|
||||
.get("items")
|
||||
.and_then(Value::as_array)
|
||||
.map(|raw| {
|
||||
raw.iter()
|
||||
.filter_map(|item| match serde_json::from_value::<Diagnostic>(item.clone()) {
|
||||
Ok(diagnostic) => Some(diagnostic),
|
||||
Err(error) => {
|
||||
debug!(uri, %error, "lsp: dropping unparseable pulled diagnostic");
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
self.pulled.insert(uri.to_string(), PulledReport { result_id, items });
|
||||
}
|
||||
|
||||
/// Recent server-reported Error/Warning notices, formatted, newest last. Explains an empty
|
||||
/// diagnostics result when a server is misconfigured or can't drive its toolchain.
|
||||
pub fn server_notices(&self) -> Vec<String> {
|
||||
self.shared
|
||||
.inner
|
||||
.lock()
|
||||
.map(|inner| {
|
||||
inner
|
||||
.server_notices
|
||||
.iter()
|
||||
.map(|notice| {
|
||||
let label = if notice.severity == 1 { "error" } else { "warning" };
|
||||
format!("[{label}] {}", notice.text)
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub(super) fn cached_diagnostics(&self, uri: &str) -> Vec<Diagnostic> {
|
||||
let published = {
|
||||
let inner = self.shared.inner.lock().expect("diagnostics state poisoned");
|
||||
inner.diagnostics.get(uri).cloned()
|
||||
};
|
||||
published.unwrap_or_else(|| {
|
||||
self.pulled.get(uri).map(|report| report.items.clone()).unwrap_or_default()
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the server has published about this document, even to say it is clean.
|
||||
pub(super) fn has_published(&self, uri: &str) -> bool {
|
||||
self.shared.inner.lock().expect("diagnostics state poisoned").diagnostics.contains_key(uri)
|
||||
}
|
||||
|
||||
pub(super) fn publish_count(&self) -> u64 {
|
||||
self.shared.inner.lock().expect("diagnostics state poisoned").publish_count
|
||||
}
|
||||
}
|
||||
51
crates/luxe-lsp/src/client/error.rs
Normal file
51
crates/luxe-lsp/src/client/error.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
//! Errors — what a language-server client can fail with.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::message::JsonRpcError;
|
||||
|
||||
/// LSP client errors.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum LspError {
|
||||
#[error("client not initialized")]
|
||||
NotInitialized,
|
||||
|
||||
#[error("failed to spawn language server '{server}': {error}")]
|
||||
SpawnFailed { server: String, error: String },
|
||||
|
||||
#[error("initialize timed out after {timeout:?} for server '{server}'")]
|
||||
InitTimeout { server: String, timeout: Duration },
|
||||
|
||||
/// The server was found and started, and then stopped before answering the handshake.
|
||||
///
|
||||
/// Distinct from [`Self::SpawnFailed`] (nothing to run) and [`Self::InitTimeout`] (running but
|
||||
/// silent), because it is the only one whose cause the *server* already explained — and the
|
||||
/// commonest way to reach it is a version-manager shim that resolves and then refuses.
|
||||
#[error("language server '{server}' exited during startup ({status}){}", detail(stderr))]
|
||||
ServerExited { server: String, status: String, stderr: String },
|
||||
|
||||
/// The server answered the request with a JSON-RPC error.
|
||||
///
|
||||
/// Its own words, propagated rather than flattened to an empty result — "no references found"
|
||||
/// and "this server cannot answer you" are not the same sentence.
|
||||
#[error("the language server refused the request: {} (code {})", .0.message, .0.code)]
|
||||
ServerRefused(JsonRpcError),
|
||||
|
||||
#[error("IO error: {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
|
||||
#[error("serialization error: {0}")]
|
||||
Serialization(#[from] serde_json::Error),
|
||||
|
||||
#[error("request cancelled")]
|
||||
RequestCancelled,
|
||||
}
|
||||
|
||||
/// The server's own complaint, appended to an error when it made one.
|
||||
pub(super) fn detail(stderr: &str) -> String {
|
||||
let first: Vec<&str> = stderr.lines().filter(|line| !line.trim().is_empty()).take(3).collect();
|
||||
match first.is_empty() {
|
||||
true => String::new(),
|
||||
false => format!(": {}", first.join(" / ")),
|
||||
}
|
||||
}
|
||||
40
crates/luxe-lsp/src/client/language.rs
Normal file
40
crates/luxe-lsp/src/client/language.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
//! Languages — which `languageId` a file's extension is opened under.
|
||||
|
||||
/// The LSP `languageId` for a document, by extension.
|
||||
///
|
||||
/// These are the identifiers the specification enumerates, not free-form names: `typescriptreact`
|
||||
/// for `.tsx`, `c` and `cpp` as separate languages, `csharp` rather than `cs`. A server is entitled
|
||||
/// to reject or mishandle one it does not know, and the wrong one is worse than a missing one
|
||||
/// because it asserts something false about the file.
|
||||
pub(super) fn language_id_for_extension(extension: &str) -> Option<&'static str> {
|
||||
Some(match extension {
|
||||
"rs" => "rust",
|
||||
"ts" | "mts" | "cts" => "typescript",
|
||||
"tsx" => "typescriptreact",
|
||||
"js" | "mjs" | "cjs" => "javascript",
|
||||
"jsx" => "javascriptreact",
|
||||
"py" | "pyi" => "python",
|
||||
"go" => "go",
|
||||
"c" => "c",
|
||||
"cc" | "cpp" | "cxx" | "c++" | "hh" | "hpp" | "hxx" => "cpp",
|
||||
"h" => "c",
|
||||
"java" => "java",
|
||||
"rb" => "ruby",
|
||||
"php" => "php",
|
||||
"cs" => "csharp",
|
||||
"swift" => "swift",
|
||||
"kt" | "kts" => "kotlin",
|
||||
"scala" | "sc" => "scala",
|
||||
"lua" => "lua",
|
||||
"zig" => "zig",
|
||||
"sh" | "bash" => "shellscript",
|
||||
"json" => "json",
|
||||
"jsonc" => "jsonc",
|
||||
"yaml" | "yml" => "yaml",
|
||||
"toml" => "toml",
|
||||
"md" | "markdown" => "markdown",
|
||||
"html" => "html",
|
||||
"css" => "css",
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
300
crates/luxe-lsp/src/client/lifecycle.rs
Normal file
300
crates/luxe-lsp/src/client/lifecycle.rs
Normal file
@@ -0,0 +1,300 @@
|
||||
//! Lifecycle — starting a server, handshaking with it, and stopping it.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Keep the server's stderr drained for as long as it runs, retaining a bounded prefix.
|
||||
///
|
||||
/// Two jobs, and the second is the load-bearing one. The prefix is what a failed handshake
|
||||
/// reports. The *draining* is what keeps a talkative server alive: a pipe nobody reads fills at
|
||||
/// 64 KB and the next write blocks forever, so a server that narrates every request — clangd
|
||||
/// does, by default — wedges and then dies. Stopping the reader once the prefix is full and
|
||||
/// cancelling it once the handshake succeeds are the same bug twice.
|
||||
pub(super) fn drain_stderr(
|
||||
stderr: tokio::process::ChildStderr,
|
||||
) -> (tokio::task::JoinHandle<()>, Arc<Mutex<String>>) {
|
||||
/// Enough for a shim's refusal or a stack trace's first frames; a server that logs forever
|
||||
/// cannot grow this.
|
||||
const MAX: usize = 2_000;
|
||||
let collected = Arc::new(Mutex::new(String::new()));
|
||||
let sink = Arc::clone(&collected);
|
||||
let handle = tokio::spawn(async move {
|
||||
use tokio::io::AsyncReadExt;
|
||||
let mut reader = tokio::io::BufReader::new(stderr);
|
||||
let mut chunk = [0u8; 4096];
|
||||
let mut kept = Vec::new();
|
||||
while let Ok(read) = reader.read(&mut chunk).await {
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
if kept.len() < MAX {
|
||||
let room = MAX - kept.len();
|
||||
kept.extend_from_slice(&chunk[..read.min(room)]);
|
||||
*sink.lock().await = String::from_utf8_lossy(&kept).trim().to_string();
|
||||
}
|
||||
// Past the prefix the bytes are dropped — but they are still read. See above.
|
||||
}
|
||||
});
|
||||
(handle, collected)
|
||||
}
|
||||
|
||||
/// Wait for the child to stop, rendering how it went.
|
||||
pub(super) async fn wait_for_exit(child: &mut tokio::process::Child) -> String {
|
||||
match child.wait().await {
|
||||
Ok(status) => status.to_string(),
|
||||
Err(error) => format!("could not be waited on: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// How the `initialize` handshake ended, so the borrow of `self` can be released before reacting.
|
||||
pub(super) enum Handshake {
|
||||
/// The server answered.
|
||||
Answered(Result<serde_json::Value, LspError>),
|
||||
/// The server stopped first, rendered as its exit status.
|
||||
Exited(String),
|
||||
/// Neither happened inside the timeout.
|
||||
Silent,
|
||||
}
|
||||
|
||||
/// Reads a `diagnosticProvider` out of an `initialize` result.
|
||||
///
|
||||
/// The capability is either an options object or a registration object with an `id`; both carry the
|
||||
/// optional `identifier` a pull request has to echo back, and neither is a bare boolean.
|
||||
pub(super) fn pull_provider(init_response: &Value) -> Option<PullProvider> {
|
||||
let provider = init_response.get("capabilities")?.get("diagnosticProvider")?;
|
||||
provider.is_object().then(|| PullProvider {
|
||||
identifier: provider.get("identifier").and_then(Value::as_str).map(str::to_string),
|
||||
})
|
||||
}
|
||||
|
||||
/// How long a failed handshake waits for its server's exit status to become collectible.
|
||||
///
|
||||
/// Generous next to the gap it covers (a process that has already closed its pipes is reaped in
|
||||
/// microseconds, milliseconds on a loaded machine) and short enough to be invisible on the only
|
||||
/// other path that reaches it — a live server that refused the handshake for some other reason.
|
||||
pub(super) const EXIT_GRACE: Duration = Duration::from_millis(250);
|
||||
|
||||
/// How long a server gets to exit after `shutdown` and `exit` before it is killed.
|
||||
pub(super) const SHUTDOWN_GRACE: Duration = Duration::from_secs(2);
|
||||
|
||||
/// What the client tells a server about itself.
|
||||
///
|
||||
/// Advertises progress and diagnostics, so a server streams `$/progress` and `publishDiagnostics`.
|
||||
///
|
||||
/// And `codeAction`, which is not optional politeness: a server answers `textDocument/codeAction`
|
||||
/// with an empty list unless the client says it understands code action *literals*, because the
|
||||
/// older reply shape is a `Command` it cannot assume we can run. Undeclared, rust-analyzer answers
|
||||
/// every `lsp_navigation action=code_action` with nothing. `resolveSupport` is the other half: an
|
||||
/// action whose `edit` is computed lazily arrives without one, and applying it means asking for it.
|
||||
fn initialize_params(root: &std::path::Path) -> Value {
|
||||
serde_json::json!({
|
||||
"processId": std::process::id(),
|
||||
"rootUri": crate::uri::from_path(root),
|
||||
"capabilities": {
|
||||
"window": { "workDoneProgress": true },
|
||||
"textDocument": {
|
||||
"synchronization": { "didSave": true, "dynamicRegistration": false },
|
||||
"publishDiagnostics": { "relatedInformation": true },
|
||||
// The pull half of diagnostics. A server only advertises
|
||||
// `diagnosticProvider` when the client says it can ask, and one that answers
|
||||
// *only* this way — TypeScript 7's native LSP — pushes nothing for an open
|
||||
// file, so without this its errors are invisible and read as "clean".
|
||||
// `relatedDocumentSupport: false` keeps an answer to one file about that file.
|
||||
"diagnostic": {
|
||||
"dynamicRegistration": false,
|
||||
"relatedDocumentSupport": false
|
||||
},
|
||||
"codeAction": {
|
||||
"dataSupport": true,
|
||||
"resolveSupport": { "properties": ["edit"] },
|
||||
"codeActionLiteralSupport": {
|
||||
"codeActionKind": {
|
||||
"valueSet": [
|
||||
"", "quickfix", "refactor", "refactor.extract",
|
||||
"refactor.inline", "refactor.rewrite", "source",
|
||||
"source.organizeImports", "source.fixAll"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
// rust-analyzer signals readiness via this experimental notification.
|
||||
"experimental": { "serverStatusNotification": true }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// A server's stderr drain: the task, and what it has kept.
|
||||
type Complaints = (tokio::task::JoinHandle<()>, Arc<Mutex<String>>);
|
||||
|
||||
impl LspClient {
|
||||
/// Initialize the language server: spawn the process and perform the LSP handshake.
|
||||
pub async fn initialize(&mut self) -> Result<(), LspError> {
|
||||
let (mut child, complaints) = self.spawn_server()?;
|
||||
let outcome = self.race_handshake(&mut child).await;
|
||||
self.child = Some(child);
|
||||
let init_response = self.handshake_result(outcome, complaints).await?;
|
||||
debug!("LSP server initialized: {init_response:?}");
|
||||
self.pull_diagnostics = pull_provider(&init_response);
|
||||
|
||||
self.notify("initialized", serde_json::json!({})).await?;
|
||||
// Ready only now: a client whose handshake failed must not report that it can be asked.
|
||||
self.state = ClientState::Initialized;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Starts the server process and the reader that routes what it says, and keeps its stderr.
|
||||
fn spawn_server(&mut self) -> Result<(Child, Option<Complaints>), LspError> {
|
||||
let mut cmd = Command::new(&self.config.command[0]);
|
||||
cmd.args(&self.config.command[1..]);
|
||||
cmd.current_dir(&self.config.root_path);
|
||||
cmd.stdin(std::process::Stdio::piped());
|
||||
cmd.stdout(std::process::Stdio::piped());
|
||||
// Piped, not null: when a server refuses to start it says why here, and that sentence is
|
||||
// the whole answer. Discarded, every startup failure would look like a timeout.
|
||||
cmd.stderr(std::process::Stdio::piped());
|
||||
cmd.kill_on_drop(true);
|
||||
|
||||
let mut child = cmd.spawn().map_err(|e| LspError::SpawnFailed {
|
||||
server: self.config.server_id.clone(),
|
||||
error: e.to_string(),
|
||||
})?;
|
||||
|
||||
let stdin = child
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| LspError::IoError(std::io::Error::other("Failed to take stdin")))?;
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| LspError::IoError(std::io::Error::other("Failed to take stdout")))?;
|
||||
let complaints = child.stderr.take().map(drain_stderr);
|
||||
let stdin = Arc::new(Mutex::new(stdin));
|
||||
self.stdin = Some(stdin.clone());
|
||||
|
||||
// Background reader must run before the initialize request so the response is routed.
|
||||
let tx = self.notification_tx.clone();
|
||||
let pending = self.pending_requests.clone();
|
||||
let shared = self.shared.clone();
|
||||
let mut stdout = tokio::io::BufReader::new(stdout);
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = read_loop(&mut stdout, shared, stdin, tx, pending).await {
|
||||
error!("LSP reader task exited with error: {e}");
|
||||
}
|
||||
});
|
||||
Ok((child, complaints))
|
||||
}
|
||||
|
||||
/// Sends `initialize` and waits for whichever comes first: the answer, the server stopping, or
|
||||
/// the timeout.
|
||||
async fn race_handshake(&mut self, child: &mut Child) -> Handshake {
|
||||
let init_params = initialize_params(&self.config.root_path);
|
||||
let init_timeout = self.config.init_timeout;
|
||||
// Race the handshake against the server *stopping*. A server that dies in ten milliseconds
|
||||
// — a shim with no version set, a missing runtime, a bad config — is not the same as one
|
||||
// that is merely slow, and waiting out the whole timeout would tell the caller the wrong
|
||||
// thing. Whichever finishes first is the answer.
|
||||
let mut outcome = {
|
||||
let handshake = self.request("initialize", init_params);
|
||||
tokio::pin!(handshake);
|
||||
tokio::select! {
|
||||
biased;
|
||||
response = &mut handshake => Handshake::Answered(response),
|
||||
status = wait_for_exit(child) => Handshake::Exited(status),
|
||||
() = tokio::time::sleep(init_timeout) => Handshake::Silent,
|
||||
}
|
||||
};
|
||||
// A server can lose the race with its own pipes: die fast enough and the *write* fails
|
||||
// before the exit is observable, leaving a bare "Broken pipe". A handshake that
|
||||
// never got an answer, against a process that has since exited, *is* that exit — and the
|
||||
// exit is the useful half.
|
||||
//
|
||||
// Waited for rather than polled once. The kernel delivers `EPIPE` the moment the read end
|
||||
// closes, but the exit *status* only becomes collectible when this process is notified,
|
||||
// which under load lands later — so a single `try_wait` answers "still running" about a
|
||||
// process that is already gone and the caller is told about a broken pipe instead. Bounded,
|
||||
// because a live server that refused for some other reason must not be waited on for long.
|
||||
if matches!(
|
||||
&outcome,
|
||||
Handshake::Answered(Err(LspError::IoError(_) | LspError::RequestCancelled))
|
||||
) {
|
||||
if let Ok(status) = timeout(EXIT_GRACE, wait_for_exit(child)).await {
|
||||
outcome = Handshake::Exited(status);
|
||||
}
|
||||
}
|
||||
outcome
|
||||
}
|
||||
|
||||
/// The answer to `initialize`, or why there is none: the server's exit with what it said, or
|
||||
/// the silence.
|
||||
async fn handshake_result(
|
||||
&mut self,
|
||||
outcome: Handshake,
|
||||
complaints: Option<Complaints>,
|
||||
) -> Result<Value, LspError> {
|
||||
let server_id = self.config.server_id.clone();
|
||||
let init_timeout = self.config.init_timeout;
|
||||
let init_response = match outcome {
|
||||
Handshake::Exited(status) => {
|
||||
let stderr = match complaints {
|
||||
Some((handle, buffer)) => {
|
||||
// The child is gone, so its stderr is at EOF and the drain ends on its own.
|
||||
let _ = handle.await;
|
||||
buffer.lock().await.clone()
|
||||
}
|
||||
None => String::new(),
|
||||
};
|
||||
return Err(LspError::ServerExited { server: server_id, status, stderr });
|
||||
}
|
||||
// The drain runs for the life of the server, and the handle is held so shutdown can end
|
||||
// it. Cancelling it here instead, to avoid detaching a `JoinHandle`, wedges any server
|
||||
// that logs more than a pipe buffer: clangd narrates every request to stderr by
|
||||
// default, fills 64 KB during its first index, and blocks forever on the next write.
|
||||
other => {
|
||||
if let Some((handle, _)) = complaints {
|
||||
self.stderr_drain = Some(handle);
|
||||
}
|
||||
match other {
|
||||
Handshake::Answered(response) => response?,
|
||||
Handshake::Silent => {
|
||||
return Err(LspError::InitTimeout {
|
||||
server: server_id,
|
||||
timeout: init_timeout,
|
||||
})
|
||||
}
|
||||
Handshake::Exited(_) => unreachable!("handled above"),
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(init_response)
|
||||
}
|
||||
|
||||
/// Shut the server down cleanly, sending `shutdown` + `exit` and reaping the child.
|
||||
pub async fn shutdown(&mut self) -> Result<(), LspError> {
|
||||
if self.state == ClientState::Shutdown {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(stdin) = self.stdin.clone() {
|
||||
let mut guard = stdin.lock().await;
|
||||
for message in [
|
||||
JsonRpcMessage::request(0, "shutdown".to_string(), serde_json::json!({})),
|
||||
JsonRpcMessage::notification("exit".to_string(), serde_json::json!({})),
|
||||
] {
|
||||
if let Ok(json) = serde_json::to_string(&message) {
|
||||
let _ = write_frame(&mut *guard, json.as_bytes()).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(handle) = self.stderr_drain.take() {
|
||||
handle.abort();
|
||||
}
|
||||
if let Some(mut child) = self.child.take() {
|
||||
// a server that ignores `exit` must not hold its caller forever
|
||||
if timeout(SHUTDOWN_GRACE, child.wait()).await.is_err() {
|
||||
let _ = child.start_kill();
|
||||
let _ = child.wait().await;
|
||||
}
|
||||
}
|
||||
self.state = ClientState::Shutdown;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
423
crates/luxe-lsp/src/client/nav.rs
Normal file
423
crates/luxe-lsp/src/client/nav.rs
Normal file
@@ -0,0 +1,423 @@
|
||||
//! LSP navigation requests — definition, references, symbols, hover, call hierarchy, and more.
|
||||
//!
|
||||
//! Responses are parsed **leniently**: real servers return `LocationLink[]` for definition and
|
||||
//! hierarchical `DocumentSymbol[]` for document symbols, not just the simplest shapes, so the
|
||||
//! helpers here normalize both. Complex results (signature help, call hierarchy, code actions,
|
||||
//! rename edits) are returned as raw `Value` for the tool layer to format.
|
||||
|
||||
use super::*;
|
||||
use crate::protocol::FlatSymbol;
|
||||
|
||||
impl LspClient {
|
||||
/// Go to definition; returns the first resolved location.
|
||||
pub async fn goto_definition(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Option<Location>, LspError> {
|
||||
let result = self.position_request("textDocument/definition", uri, position).await?;
|
||||
Ok(parse_locations(&result).into_iter().next())
|
||||
}
|
||||
|
||||
/// Go to the type definition of the symbol at a position.
|
||||
pub async fn goto_type_definition(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Vec<Location>, LspError> {
|
||||
let result = self.position_request("textDocument/typeDefinition", uri, position).await?;
|
||||
Ok(parse_locations(&result))
|
||||
}
|
||||
|
||||
/// Go to the declaration of the symbol at a position.
|
||||
pub async fn goto_declaration(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Vec<Location>, LspError> {
|
||||
let result = self.position_request("textDocument/declaration", uri, position).await?;
|
||||
Ok(parse_locations(&result))
|
||||
}
|
||||
|
||||
/// Find implementations of the symbol at a position.
|
||||
pub async fn goto_implementation(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Vec<Location>, LspError> {
|
||||
let result = self.position_request("textDocument/implementation", uri, position).await?;
|
||||
Ok(parse_locations(&result))
|
||||
}
|
||||
|
||||
/// Find all references to a symbol.
|
||||
pub async fn find_references(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Vec<Location>, LspError> {
|
||||
let result = self
|
||||
.send_request(
|
||||
"textDocument/references",
|
||||
serde_json::json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": position.line, "character": position.character },
|
||||
"context": { "includeDeclaration": false }
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
Ok(parse_locations(&result))
|
||||
}
|
||||
|
||||
/// List the symbols in a document (flattened; handles both server shapes).
|
||||
pub async fn document_symbols(&mut self, uri: &str) -> Result<Vec<FlatSymbol>, LspError> {
|
||||
let result = self
|
||||
.send_request(
|
||||
"textDocument/documentSymbol",
|
||||
serde_json::json!({ "textDocument": { "uri": uri } }),
|
||||
)
|
||||
.await?;
|
||||
let mut symbols = Vec::new();
|
||||
flatten_symbols(&result, uri, None, &mut symbols);
|
||||
Ok(symbols)
|
||||
}
|
||||
|
||||
/// Search for symbols across the workspace by name.
|
||||
pub async fn workspace_symbol(&mut self, query: &str) -> Result<Vec<FlatSymbol>, LspError> {
|
||||
let result =
|
||||
self.send_request("workspace/symbol", serde_json::json!({ "query": query })).await?;
|
||||
let mut symbols = Vec::new();
|
||||
flatten_symbols(&result, "", None, &mut symbols);
|
||||
Ok(symbols)
|
||||
}
|
||||
|
||||
/// Get hover information for a symbol at a position.
|
||||
pub async fn hover(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Option<MarkupContent>, LspError> {
|
||||
let result = self.position_request("textDocument/hover", uri, position).await?;
|
||||
if let Value::Object(obj) = &result {
|
||||
if let Some(contents) = obj.get("contents") {
|
||||
// `contents` may be MarkupContent, a MarkedString, or an array of them.
|
||||
if let Ok(markup) = serde_json::from_value::<MarkupContent>(contents.clone()) {
|
||||
return Ok(Some(markup));
|
||||
}
|
||||
if let Some(text) = hover_plain_text(contents) {
|
||||
return Ok(Some(MarkupContent { kind: "plaintext".to_string(), value: text }));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Get signature help at a call-site position (raw result).
|
||||
pub async fn signature_help(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Value, LspError> {
|
||||
self.position_request("textDocument/signatureHelp", uri, position).await
|
||||
}
|
||||
|
||||
/// Prepare a call hierarchy at a position; returns raw `CallHierarchyItem`s.
|
||||
pub async fn prepare_call_hierarchy(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Vec<Value>, LspError> {
|
||||
let result =
|
||||
self.position_request("textDocument/prepareCallHierarchy", uri, position).await?;
|
||||
Ok(as_array(result))
|
||||
}
|
||||
|
||||
/// Fetch incoming calls for a prepared call-hierarchy item.
|
||||
pub async fn incoming_calls(&mut self, item: Value) -> Result<Vec<Value>, LspError> {
|
||||
let result = self
|
||||
.send_request("callHierarchy/incomingCalls", serde_json::json!({ "item": item }))
|
||||
.await?;
|
||||
Ok(as_array(result))
|
||||
}
|
||||
|
||||
/// Fetch outgoing calls for a prepared call-hierarchy item.
|
||||
pub async fn outgoing_calls(&mut self, item: Value) -> Result<Vec<Value>, LspError> {
|
||||
let result = self
|
||||
.send_request("callHierarchy/outgoingCalls", serde_json::json!({ "item": item }))
|
||||
.await?;
|
||||
Ok(as_array(result))
|
||||
}
|
||||
|
||||
/// List code actions (quick fixes/refactors) for a range (raw results).
|
||||
pub async fn code_action(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
range: crate::protocol::Range,
|
||||
) -> Result<Vec<Value>, LspError> {
|
||||
// The diagnostics overlapping `range`, not an empty list. The spec is explicit that this is
|
||||
// how a server learns "which errors are currently presented to the user for the given
|
||||
// range", and a quickfix is offered *for a diagnostic* — so a client that sends none is
|
||||
// asking for refactors and opting out of every fix. We already hold these; sending them
|
||||
// costs a filter.
|
||||
let overlapping = self.diagnostics_overlapping(uri, &range);
|
||||
let result = self
|
||||
.send_request(
|
||||
"textDocument/codeAction",
|
||||
serde_json::json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"range": range,
|
||||
"context": { "diagnostics": overlapping }
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
Ok(as_array(result))
|
||||
}
|
||||
|
||||
/// This document's cached diagnostics whose range overlaps `range`.
|
||||
///
|
||||
/// Line granularity, deliberately: a caller points at a line, and a diagnostic that starts
|
||||
/// mid-line would be missed by a strict character comparison against a zero-width position.
|
||||
fn diagnostics_overlapping(
|
||||
&self,
|
||||
uri: &str,
|
||||
range: &crate::protocol::Range,
|
||||
) -> Vec<serde_json::Value> {
|
||||
let Ok(state) = self.shared.inner.lock() else { return Vec::new() };
|
||||
state
|
||||
.diagnostics
|
||||
.get(uri)
|
||||
.map(|diagnostics| {
|
||||
diagnostics
|
||||
.iter()
|
||||
.filter(|diagnostic| {
|
||||
diagnostic.range.start.line <= range.end.line
|
||||
&& diagnostic.range.end.line >= range.start.line
|
||||
})
|
||||
.filter_map(|diagnostic| serde_json::to_value(diagnostic).ok())
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Compute a rename edit (raw `WorkspaceEdit`); the caller previews or applies it.
|
||||
pub async fn rename(
|
||||
&mut self,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
new_name: &str,
|
||||
) -> Result<Value, LspError> {
|
||||
self.send_request(
|
||||
"textDocument/rename",
|
||||
serde_json::json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": position.line, "character": position.character },
|
||||
"newName": new_name
|
||||
}),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Shared `{ textDocument, position }` request.
|
||||
async fn position_request(
|
||||
&mut self,
|
||||
method: &str,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
) -> Result<Value, LspError> {
|
||||
self.send_request(
|
||||
method,
|
||||
serde_json::json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": position.line, "character": position.character }
|
||||
}),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
/// Coerce a result into an array of values (single object → one-element vec).
|
||||
fn as_array(result: Value) -> Vec<Value> {
|
||||
match result {
|
||||
Value::Array(items) => items,
|
||||
Value::Null => Vec::new(),
|
||||
other => vec![other],
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a definition-style result into `Location`s, accepting `Location`, `Location[]`, or
|
||||
/// `LocationLink[]` (target-based, as rust-analyzer/tsserver return).
|
||||
fn parse_locations(result: &Value) -> Vec<Location> {
|
||||
match result {
|
||||
Value::Array(items) => items.iter().filter_map(parse_location).collect(),
|
||||
Value::Object(_) => parse_location(result).into_iter().collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse one `Location` or `LocationLink` leniently.
|
||||
fn parse_location(value: &Value) -> Option<Location> {
|
||||
if let Ok(location) = serde_json::from_value::<Location>(value.clone()) {
|
||||
return Some(location);
|
||||
}
|
||||
// LocationLink: `targetUri` + `targetSelectionRange`/`targetRange`.
|
||||
let uri = value.get("targetUri")?.as_str()?.to_string();
|
||||
let range = value
|
||||
.get("targetSelectionRange")
|
||||
.or_else(|| value.get("targetRange"))
|
||||
.and_then(|r| serde_json::from_value::<crate::protocol::Range>(r.clone()).ok())?;
|
||||
Some(Location { uri, range })
|
||||
}
|
||||
|
||||
/// Flatten a `documentSymbol`/`workspace/symbol` result into [`FlatSymbol`]s, handling both
|
||||
/// `SymbolInformation` (flat, has `location`) and `DocumentSymbol` (nested, has `selectionRange` +
|
||||
/// `children`).
|
||||
fn flatten_symbols(
|
||||
value: &Value,
|
||||
default_uri: &str,
|
||||
container: Option<&str>,
|
||||
out: &mut Vec<FlatSymbol>,
|
||||
) {
|
||||
let Value::Array(items) = value else { return };
|
||||
for item in items {
|
||||
let name = item.get("name").and_then(Value::as_str).unwrap_or("").to_string();
|
||||
let kind = item.get("kind").and_then(Value::as_u64).unwrap_or(0) as u32;
|
||||
|
||||
if let Some(location) = item.get("location") {
|
||||
// SymbolInformation / WorkspaceSymbol: `location.range` is the full extent, and there
|
||||
// is no separate selection range.
|
||||
let uri =
|
||||
location.get("uri").and_then(Value::as_str).unwrap_or(default_uri).to_string();
|
||||
let (line, character) = range_start(location.get("range"));
|
||||
let (start_line, end_line) = range_span(location.get("range"));
|
||||
let container = item
|
||||
.get("containerName")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_string)
|
||||
.or_else(|| container.map(str::to_string));
|
||||
out.push(FlatSymbol {
|
||||
name,
|
||||
kind,
|
||||
uri,
|
||||
line,
|
||||
character,
|
||||
start_line,
|
||||
end_line,
|
||||
container,
|
||||
});
|
||||
} else if item.get("selectionRange").is_some() || item.get("range").is_some() {
|
||||
// DocumentSymbol (hierarchical): `selectionRange` names it, `range` is its body.
|
||||
let (line, character) =
|
||||
range_start(item.get("selectionRange").or_else(|| item.get("range")));
|
||||
let (start_line, end_line) =
|
||||
range_span(item.get("range").or_else(|| item.get("selectionRange")));
|
||||
out.push(FlatSymbol {
|
||||
name: name.clone(),
|
||||
kind,
|
||||
uri: default_uri.to_string(),
|
||||
line,
|
||||
character,
|
||||
start_line,
|
||||
end_line,
|
||||
container: container.map(str::to_string),
|
||||
});
|
||||
if let Some(children) = item.get("children") {
|
||||
flatten_symbols(children, default_uri, Some(&name), out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract a range's 0-based `(line, character)` start, defaulting to `(0, 0)`.
|
||||
fn range_start(range: Option<&Value>) -> (u32, u32) {
|
||||
let start = range.and_then(|r| r.get("start"));
|
||||
let line = start.and_then(|s| s.get("line")).and_then(Value::as_u64).unwrap_or(0) as u32;
|
||||
let character =
|
||||
start.and_then(|s| s.get("character")).and_then(Value::as_u64).unwrap_or(0) as u32;
|
||||
(line, character)
|
||||
}
|
||||
|
||||
/// Extract a range's 0-based `(start_line, end_line)`, defaulting the end to the start.
|
||||
fn range_span(range: Option<&Value>) -> (u32, u32) {
|
||||
let line_of = |key: &str| {
|
||||
range
|
||||
.and_then(|r| r.get(key))
|
||||
.and_then(|p| p.get("line"))
|
||||
.and_then(Value::as_u64)
|
||||
.unwrap_or(0) as u32
|
||||
};
|
||||
let start = line_of("start");
|
||||
(start, line_of("end").max(start))
|
||||
}
|
||||
|
||||
/// Best-effort plain text from a hover `contents` that is a `MarkedString` or an array of them.
|
||||
fn hover_plain_text(contents: &Value) -> Option<String> {
|
||||
match contents {
|
||||
Value::String(s) => Some(s.clone()),
|
||||
Value::Object(obj) => obj.get("value").and_then(Value::as_str).map(str::to_string),
|
||||
Value::Array(items) => {
|
||||
let joined = items.iter().filter_map(hover_plain_text).collect::<Vec<_>>().join("\n");
|
||||
(!joined.is_empty()).then_some(joined)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn parse_locations_accepts_location_and_location_link() {
|
||||
let plain = serde_json::json!([{
|
||||
"uri": "file:///a.rs",
|
||||
"range": { "start": { "line": 3, "character": 2 }, "end": { "line": 3, "character": 8 } }
|
||||
}]);
|
||||
let link = serde_json::json!([{
|
||||
"targetUri": "file:///b.rs",
|
||||
"targetSelectionRange": {
|
||||
"start": { "line": 10, "character": 4 }, "end": { "line": 10, "character": 9 }
|
||||
},
|
||||
"targetRange": {
|
||||
"start": { "line": 9, "character": 0 }, "end": { "line": 12, "character": 1 }
|
||||
}
|
||||
}]);
|
||||
assert_eq!(parse_locations(&plain)[0].uri, "file:///a.rs");
|
||||
let linked = parse_locations(&link);
|
||||
assert_eq!(linked[0].uri, "file:///b.rs");
|
||||
assert_eq!(linked[0].range.start.line, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flatten_handles_both_symbol_shapes() {
|
||||
// SymbolInformation
|
||||
let flat = serde_json::json!([{
|
||||
"name": "main", "kind": 12,
|
||||
"location": { "uri": "file:///m.rs",
|
||||
"range": { "start": { "line": 1, "character": 0 }, "end": { "line": 1, "character": 4 } } },
|
||||
"containerName": "app"
|
||||
}]);
|
||||
let mut out = Vec::new();
|
||||
flatten_symbols(&flat, "file:///m.rs", None, &mut out);
|
||||
assert_eq!(out[0].name, "main");
|
||||
assert_eq!(out[0].line, 1);
|
||||
assert_eq!(out[0].container.as_deref(), Some("app"));
|
||||
|
||||
// DocumentSymbol (nested), children inherit the parent as container.
|
||||
let nested = serde_json::json!([{
|
||||
"name": "Foo", "kind": 23,
|
||||
"range": { "start": { "line": 0, "character": 0 }, "end": { "line": 9, "character": 0 } },
|
||||
"selectionRange": { "start": { "line": 0, "character": 7 }, "end": { "line": 0, "character": 10 } },
|
||||
"children": [{
|
||||
"name": "bar", "kind": 6,
|
||||
"range": { "start": { "line": 2, "character": 4 }, "end": { "line": 4, "character": 5 } },
|
||||
"selectionRange": { "start": { "line": 2, "character": 7 }, "end": { "line": 2, "character": 10 } }
|
||||
}]
|
||||
}]);
|
||||
let mut out = Vec::new();
|
||||
flatten_symbols(&nested, "file:///t.rs", None, &mut out);
|
||||
assert_eq!(out.len(), 2);
|
||||
assert_eq!(out[0].name, "Foo");
|
||||
assert_eq!(out[0].character, 7);
|
||||
assert_eq!(out[1].name, "bar");
|
||||
assert_eq!(out[1].container.as_deref(), Some("Foo"));
|
||||
}
|
||||
}
|
||||
138
crates/luxe-lsp/src/client/state.rs
Normal file
138
crates/luxe-lsp/src/client/state.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
//! Client state — what the background reader folds in, and the requests waiting on it.
|
||||
|
||||
use crate::message::JsonRpcError;
|
||||
use crate::protocol::Diagnostic;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex as SyncMutex};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::sync::{oneshot, Mutex, Notify};
|
||||
|
||||
/// Live server→client state folded by the background reader.
|
||||
///
|
||||
/// Guarded by a std mutex held only for brief, non-async updates: the diagnostics cache keyed by
|
||||
/// document URI, a count of in-flight `$/progress` work-done tokens, a monotonic publish counter,
|
||||
/// and the last-change instant used for quiescence.
|
||||
pub(crate) struct DiagnosticsState {
|
||||
/// Latest *published* diagnostics per document URI.
|
||||
///
|
||||
/// The key is itself an answer: present with an empty vec means the server said "clean", and
|
||||
/// absent means it has said nothing at all, which is what decides whether a pull-capable
|
||||
/// server still has to be asked directly. Only `publishDiagnostics` writes here — a pulled
|
||||
/// report lives on the client, in [`LspClient::pulled`], so the distinction survives.
|
||||
pub(crate) diagnostics: HashMap<String, Vec<Diagnostic>>,
|
||||
/// In-flight work-done progress tokens; `0` means the server is idle.
|
||||
pub(crate) progress_active: usize,
|
||||
/// Monotonic count of `publishDiagnostics` notifications seen.
|
||||
pub(crate) publish_count: u64,
|
||||
/// Whether the server reported itself quiescent (ready) via `experimental/serverStatus` — the
|
||||
/// reliable readiness signal for rust-analyzer. Other servers leave this `false` and readiness
|
||||
/// falls back to a progress-settle window.
|
||||
pub(crate) quiescent: bool,
|
||||
/// Recent server-reported Error/Warning notices (`window/showMessage` + `window/logMessage`),
|
||||
/// bounded. Surfaced when a diagnostics read comes back empty so a broken/misconfigured server
|
||||
/// (e.g. it can't find a usable toolchain) isn't a silent "no diagnostics".
|
||||
pub(crate) server_notices: Vec<ServerNotice>,
|
||||
/// Instant of the last diagnostics/progress change, for quiescence checks.
|
||||
last_change: Instant,
|
||||
}
|
||||
|
||||
/// One Error/Warning message a server sent via `window/showMessage` or `window/logMessage`.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ServerNotice {
|
||||
/// LSP `MessageType`: 1 = Error, 2 = Warning.
|
||||
pub(crate) severity: u8,
|
||||
pub(crate) text: String,
|
||||
}
|
||||
|
||||
/// A server's advertised `diagnosticProvider`, as far as a pull request needs it.
|
||||
#[derive(Debug)]
|
||||
pub(super) struct PullProvider {
|
||||
/// The identifier the server registered under, echoed back on every request it asked for.
|
||||
pub(super) identifier: Option<String>,
|
||||
}
|
||||
|
||||
/// The last `textDocument/diagnostic` answer for one document.
|
||||
#[derive(Debug)]
|
||||
pub(super) struct PulledReport {
|
||||
/// The server's token for this answer, sent back so it can reply `unchanged`.
|
||||
pub(super) result_id: Option<String>,
|
||||
pub(super) items: Vec<Diagnostic>,
|
||||
}
|
||||
|
||||
impl DiagnosticsState {
|
||||
pub(crate) fn mark_change(&mut self) {
|
||||
self.last_change = Instant::now();
|
||||
}
|
||||
|
||||
/// Record a server Error/Warning notice (deduping the immediately-repeated one, bounded).
|
||||
pub(crate) fn push_notice(&mut self, severity: u8, text: String) {
|
||||
if self.server_notices.last().is_some_and(|n| n.text == text) {
|
||||
return;
|
||||
}
|
||||
self.server_notices.push(ServerNotice { severity, text });
|
||||
const MAX_NOTICES: usize = 8;
|
||||
let len = self.server_notices.len();
|
||||
if len > MAX_NOTICES {
|
||||
self.server_notices.drain(0..len - MAX_NOTICES);
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn since_change(&self) -> Duration {
|
||||
self.last_change.elapsed()
|
||||
}
|
||||
}
|
||||
|
||||
/// In-flight requests, each waiting on the server's answer or its refusal.
|
||||
pub(crate) type PendingRequests =
|
||||
Arc<Mutex<HashMap<u64, oneshot::Sender<Result<Value, JsonRpcError>>>>>;
|
||||
|
||||
/// Removes a request's pending entry however the request ends.
|
||||
pub(super) struct PendingEntry {
|
||||
pub(super) pending: PendingRequests,
|
||||
pub(super) id: u64,
|
||||
}
|
||||
|
||||
impl Drop for PendingEntry {
|
||||
fn drop(&mut self) {
|
||||
let id = self.id;
|
||||
if let Ok(mut map) = self.pending.try_lock() {
|
||||
map.remove(&id);
|
||||
return;
|
||||
}
|
||||
// the reader holds it for a moment; finish the removal behind it
|
||||
let pending = Arc::clone(&self.pending);
|
||||
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
|
||||
runtime.spawn(async move { pending.lock().await.remove(&id) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared state between the client and its background reader.
|
||||
pub(crate) struct SharedState {
|
||||
pub(crate) inner: SyncMutex<DiagnosticsState>,
|
||||
pub(crate) notify: Notify,
|
||||
}
|
||||
|
||||
impl SharedState {
|
||||
pub(super) fn new() -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
inner: SyncMutex::new(DiagnosticsState {
|
||||
diagnostics: HashMap::new(),
|
||||
progress_active: 0,
|
||||
publish_count: 0,
|
||||
quiescent: false,
|
||||
server_notices: Vec::new(),
|
||||
last_change: Instant::now(),
|
||||
}),
|
||||
notify: Notify::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn mark_change(&self) {
|
||||
if let Ok(mut inner) = self.inner.lock() {
|
||||
inner.mark_change();
|
||||
}
|
||||
self.notify.notify_waiters();
|
||||
}
|
||||
}
|
||||
218
crates/luxe-lsp/src/client/tests.rs
Normal file
218
crates/luxe-lsp/src/client/tests.rs
Normal file
@@ -0,0 +1,218 @@
|
||||
use super::diagnostics::hash_content;
|
||||
use super::lifecycle::pull_provider;
|
||||
use super::*;
|
||||
use crate::protocol::Range;
|
||||
|
||||
fn config() -> LspConfig {
|
||||
LspConfig {
|
||||
command: vec!["rust-analyzer".to_string()],
|
||||
server_id: "rust-analyzer".to_string(),
|
||||
root_path: PathBuf::from("/tmp"),
|
||||
init_timeout: Duration::from_secs(30),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_shutdown_when_not_initialized() {
|
||||
let mut client = LspClient::new(config());
|
||||
assert_eq!(client.state, ClientState::Uninitialized);
|
||||
client.shutdown().await.unwrap();
|
||||
assert_eq!(client.state, ClientState::Shutdown);
|
||||
}
|
||||
|
||||
/// The spec enumerates these identifiers; a wrong one asserts something false about the file.
|
||||
#[test]
|
||||
fn language_id_is_inferred_from_the_uri_extension() {
|
||||
let client = |language: &str| {
|
||||
LspClient::new(LspConfig {
|
||||
command: vec!["true".to_string()],
|
||||
server_id: language.to_string(),
|
||||
root_path: PathBuf::from("/tmp"),
|
||||
init_timeout: Duration::from_secs(1),
|
||||
})
|
||||
};
|
||||
let rust = client("rust");
|
||||
assert_eq!(rust.language_id_for("file:///a/b.rs"), "rust");
|
||||
assert_eq!(rust.language_id_for("file:///a/b.ts"), "typescript");
|
||||
assert_eq!(rust.language_id_for("file:///a/b.py"), "python");
|
||||
assert_eq!(rust.language_id_for("file:///a/b.go"), "go");
|
||||
// React variants are their own languages, not the plain ones.
|
||||
assert_eq!(rust.language_id_for("file:///a/b.tsx"), "typescriptreact");
|
||||
assert_eq!(rust.language_id_for("file:///a/b.jsx"), "javascriptreact");
|
||||
// C and C++ are separate, and neither is Rust.
|
||||
assert_eq!(rust.language_id_for("file:///a/b.c"), "c");
|
||||
assert_eq!(rust.language_id_for("file:///a/b.cpp"), "cpp");
|
||||
|
||||
// The fallback is *this server's* language. Everything unrecognized used to claim `rust`,
|
||||
// so a `.vue` file opened against a Vue server announced itself as Rust.
|
||||
assert_eq!(client("vue").language_id_for("file:///a/b.vue"), "vue");
|
||||
assert_eq!(client("vue").language_id_for("file:///a/noextension"), "vue");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unchanged_content_hashes_match() {
|
||||
assert_eq!(hash_content("fn main() {}"), hash_content("fn main() {}"));
|
||||
assert_ne!(hash_content("fn main() {}"), hash_content("fn main() { }"));
|
||||
}
|
||||
|
||||
/// The capability object decides whether a read asks or waits, so a shape that is not an
|
||||
/// object must not be read as one: `false` and a missing key both mean push-only, and a
|
||||
/// truthiness check on the JSON would have turned the first into a pull server.
|
||||
#[test]
|
||||
fn a_pull_provider_is_read_from_the_capability_object_only() {
|
||||
let with = serde_json::json!({
|
||||
"capabilities": { "diagnosticProvider": {
|
||||
"identifier": "typescript", "interFileDependencies": true
|
||||
}}
|
||||
});
|
||||
assert_eq!(pull_provider(&with).unwrap().identifier.as_deref(), Some("typescript"));
|
||||
|
||||
// An options object without an identifier is still a pull server; the request omits it.
|
||||
let bare = serde_json::json!({ "capabilities": { "diagnosticProvider": {} } });
|
||||
assert_eq!(pull_provider(&bare).unwrap().identifier, None);
|
||||
|
||||
for push_only in [
|
||||
serde_json::json!({ "capabilities": {} }),
|
||||
serde_json::json!({ "capabilities": { "diagnosticProvider": false } }),
|
||||
serde_json::json!({ "capabilities": { "diagnosticProvider": true } }),
|
||||
serde_json::json!({ "capabilities": { "diagnosticProvider": null } }),
|
||||
serde_json::json!({}),
|
||||
] {
|
||||
assert!(pull_provider(&push_only).is_none(), "not a pull provider: {push_only}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Push wins where it is offered, and silence is not an answer.
|
||||
#[test]
|
||||
fn only_a_document_the_server_has_said_nothing_about_is_asked_about() {
|
||||
let mut client = LspClient::new(config());
|
||||
let uri = "file:///tmp/a.ts";
|
||||
|
||||
// No advertised provider: there is nothing to ask, whatever the cache holds.
|
||||
assert!(!client.pulls_diagnostics_for(uri));
|
||||
|
||||
client.pull_diagnostics = Some(PullProvider { identifier: None });
|
||||
assert!(client.pulls_diagnostics_for(uri));
|
||||
|
||||
// An empty push is the server saying "clean" — an answer, so it is not asked again.
|
||||
{
|
||||
let mut inner = client.shared.inner.lock().expect("state");
|
||||
inner.diagnostics.insert(uri.to_string(), Vec::new());
|
||||
}
|
||||
assert!(!client.pulls_diagnostics_for(uri));
|
||||
assert!(client.pulls_diagnostics_for("file:///tmp/b.ts"));
|
||||
}
|
||||
|
||||
/// A pulled answer is only read where the server pushed nothing — it never shadows a push.
|
||||
#[test]
|
||||
fn a_pushed_answer_outranks_a_pulled_one() {
|
||||
let mut client = LspClient::new(config());
|
||||
let uri = "file:///tmp/a.ts";
|
||||
let pulled = Diagnostic {
|
||||
range: Range {
|
||||
start: Position { line: 0, character: 0 },
|
||||
end: Position { line: 0, character: 1 },
|
||||
},
|
||||
severity: None,
|
||||
source: None,
|
||||
code: None,
|
||||
message: "pulled".to_string(),
|
||||
};
|
||||
client.pulled.insert(uri.to_string(), PulledReport { result_id: None, items: vec![pulled] });
|
||||
assert_eq!(client.cached_diagnostics(uri)[0].message, "pulled");
|
||||
|
||||
{
|
||||
let mut inner = client.shared.inner.lock().expect("state");
|
||||
inner.diagnostics.insert(uri.to_string(), Vec::new());
|
||||
}
|
||||
assert!(client.cached_diagnostics(uri).is_empty(), "the push is the answer");
|
||||
}
|
||||
|
||||
/// A server that answers `initialize`, then reads on and answers nothing, `exit` included.
|
||||
#[cfg(unix)]
|
||||
fn deaf_server(dir: &std::path::Path) -> LspConfig {
|
||||
let script = dir.join("deaf.py");
|
||||
std::fs::write(
|
||||
&script,
|
||||
r#"
|
||||
import json, sys
|
||||
def read_msg():
|
||||
header = b""
|
||||
while b"\r\n\r\n" not in header:
|
||||
b = sys.stdin.buffer.read(1)
|
||||
if not b: return None
|
||||
header += b
|
||||
n = int([l for l in header.split(b"\r\n") if l.lower().startswith(b"content-length:")][0].split(b":")[1])
|
||||
return json.loads(sys.stdin.buffer.read(n))
|
||||
while True:
|
||||
msg = read_msg()
|
||||
if msg is None: break
|
||||
if msg.get("method") == "initialize":
|
||||
data = json.dumps({"jsonrpc": "2.0", "id": msg["id"], "result": {"capabilities": {}}}).encode()
|
||||
sys.stdout.buffer.write(b"Content-Length: %d\r\n\r\n" % len(data) + data)
|
||||
sys.stdout.buffer.flush()
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
LspConfig {
|
||||
command: vec!["python3".into(), script.display().to_string()],
|
||||
server_id: "deaf".into(),
|
||||
root_path: dir.to_path_buf(),
|
||||
init_timeout: Duration::from_secs(10),
|
||||
}
|
||||
}
|
||||
|
||||
/// Shutting down a server that ignores `exit` ends, by killing it, instead of waiting forever.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_server_that_ignores_exit_is_killed_at_shutdown() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut client = LspClient::new(deaf_server(dir.path()));
|
||||
client.initialize().await.expect("handshake");
|
||||
let pid = client.child_id().expect("running");
|
||||
|
||||
let started = Instant::now();
|
||||
timeout(Duration::from_secs(10), client.shutdown()).await.expect("bounded").unwrap();
|
||||
assert!(started.elapsed() < Duration::from_secs(5), "{:?}", started.elapsed());
|
||||
assert!(!luxe_core::program::process_is_alive(pid), "the server was reaped");
|
||||
}
|
||||
|
||||
/// A request given up on leaves nothing behind in the pending map.
|
||||
///
|
||||
/// The break this catches: the entry was removed only after an answer, so a dropped request
|
||||
/// future, as a tool timeout drops it, left it there until the server exited.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn an_abandoned_request_leaves_no_pending_entry() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut client = LspClient::new(deaf_server(dir.path()));
|
||||
client.initialize().await.expect("handshake");
|
||||
|
||||
let asked = timeout(
|
||||
Duration::from_millis(200),
|
||||
client.send_request("textDocument/hover", serde_json::json!({})),
|
||||
)
|
||||
.await;
|
||||
assert!(asked.is_err(), "the deaf server never answers");
|
||||
assert!(client.pending_requests.lock().await.is_empty(), "the entry went with the request");
|
||||
client.shutdown().await.unwrap();
|
||||
}
|
||||
|
||||
/// A client whose handshake failed does not report that it can be asked.
|
||||
///
|
||||
/// The break this catches: the state became `Initialized` right after the spawn, so a failed
|
||||
/// handshake left a client that answered as ready and failed each request on a dead pipe instead.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_failed_handshake_leaves_the_client_uninitialized() {
|
||||
let mut client = LspClient::new(LspConfig {
|
||||
command: vec!["sh".into(), "-c".into(), "exit 3".into()],
|
||||
server_id: "gone".into(),
|
||||
root_path: PathBuf::from("/tmp"),
|
||||
init_timeout: Duration::from_secs(10),
|
||||
});
|
||||
assert!(client.initialize().await.is_err());
|
||||
assert_eq!(client.state, ClientState::Uninitialized);
|
||||
let asked = client.send_request("textDocument/hover", serde_json::json!({})).await;
|
||||
assert!(matches!(asked, Err(LspError::NotInitialized)), "{asked:?}");
|
||||
}
|
||||
189
crates/luxe-lsp/src/client/transport.rs
Normal file
189
crates/luxe-lsp/src/client/transport.rs
Normal file
@@ -0,0 +1,189 @@
|
||||
//! Background reader task — frames server stdout and dispatches messages.
|
||||
//!
|
||||
//! The reader owns the live server→client stream: it routes responses to their pending request,
|
||||
//! auto-replies to server→client requests (so `$/progress`, capability registration, and config
|
||||
//! requests don't stall the server), and folds `publishDiagnostics` + `$/progress` into the shared
|
||||
//! [`SharedState`] so a warm client can report settled diagnostics without a one-shot wait.
|
||||
|
||||
use super::*;
|
||||
use luxe_core::jsonrpc_frame::{read_frame, write_frame, MAX_FRAME_BYTES};
|
||||
|
||||
/// Background reader: continuously read messages from stdout and dispatch them.
|
||||
pub(crate) async fn read_loop<R: AsyncReadExt + AsyncBufRead + Unpin>(
|
||||
reader: &mut R,
|
||||
shared: Arc<SharedState>,
|
||||
stdin: Arc<Mutex<ChildStdin>>,
|
||||
tx: mpsc::Sender<JsonRpcMessage>,
|
||||
pending_requests: PendingRequests,
|
||||
) -> Result<(), LspError> {
|
||||
let outcome = route_messages(reader, shared, stdin, tx, &pending_requests).await;
|
||||
// The reader is the only thing that can ever answer an in-flight request, so when it stops —
|
||||
// EOF because the server exited, or a transport error — nothing is coming for any of them.
|
||||
// Dropping the senders turns each waiter's `rx.await` into `RequestCancelled`; left alive, a
|
||||
// server that died mid-session would leave its turn waiting forever instead of failing
|
||||
pending_requests.lock().await.clear();
|
||||
outcome
|
||||
}
|
||||
|
||||
/// The reader's loop proper, split out so [`read_loop`] can clean up on every exit path.
|
||||
async fn route_messages<R: AsyncReadExt + AsyncBufRead + Unpin>(
|
||||
reader: &mut R,
|
||||
shared: Arc<SharedState>,
|
||||
stdin: Arc<Mutex<ChildStdin>>,
|
||||
tx: mpsc::Sender<JsonRpcMessage>,
|
||||
pending_requests: &PendingRequests,
|
||||
) -> Result<(), LspError> {
|
||||
loop {
|
||||
let Some(bytes) = read_frame(reader, MAX_FRAME_BYTES).await? else {
|
||||
break; // EOF — server exited.
|
||||
};
|
||||
let msg: JsonRpcMessage = match serde_json::from_slice(&bytes) {
|
||||
Ok(msg) => msg,
|
||||
Err(e) => {
|
||||
debug!(error = %e, "lsp: dropping unparseable server message");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
match (&msg.id, &msg.method) {
|
||||
// Response to one of our requests: id present, no method.
|
||||
(Some(id), None) => {
|
||||
if let Some(id) = id.as_u64() {
|
||||
if let Some(sender) = pending_requests.lock().await.remove(&id) {
|
||||
// A JSON-RPC failure carries `error` and no `result`. Reading only
|
||||
// `result` turned every server error into `Null`, which each caller then
|
||||
// rendered as an empty answer: "No definition found" for a server that had
|
||||
// in fact said "Could not find a valid TypeScript installation".
|
||||
let _ = sender.send(match &msg.error {
|
||||
Some(error) => Err(error.clone()),
|
||||
None => Ok(msg.result.clone().unwrap_or(Value::Null)),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// Server→client request: id and method both present. Reply so it never blocks.
|
||||
(Some(id), Some(method)) => {
|
||||
reply_to_server_request(&stdin, id.clone(), method, msg.params.as_ref()).await;
|
||||
}
|
||||
// Notification: method, no id.
|
||||
(None, Some(method)) => {
|
||||
apply_notification(&shared, method, msg.params.as_ref());
|
||||
// Best-effort tap for `wait_for_notification`; never block the reader on a full tap.
|
||||
let _ = tx.try_send(msg);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Folds a server notification into shared state: diagnostics cache + progress tracking.
|
||||
fn apply_notification(shared: &SharedState, method: &str, params: Option<&Value>) {
|
||||
match method {
|
||||
"textDocument/publishDiagnostics" => {
|
||||
let Some(params) = params else { return };
|
||||
let Some(uri) = params.get("uri").and_then(Value::as_str) else { return };
|
||||
let diagnostics = params
|
||||
.get("diagnostics")
|
||||
.and_then(Value::as_array)
|
||||
.map(|raw| {
|
||||
raw.iter()
|
||||
.filter_map(|d| match serde_json::from_value::<Diagnostic>(d.clone()) {
|
||||
Ok(diag) => Some(diag),
|
||||
Err(e) => {
|
||||
debug!(uri, error = %e, "lsp: dropping unparseable diagnostic");
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
{
|
||||
let mut inner = shared.inner.lock().expect("diagnostics state poisoned");
|
||||
inner.diagnostics.insert(uri.to_string(), diagnostics);
|
||||
inner.publish_count += 1;
|
||||
inner.mark_change();
|
||||
}
|
||||
shared.notify.notify_waiters();
|
||||
}
|
||||
"$/progress" => {
|
||||
let kind = params
|
||||
.and_then(|p| p.get("value"))
|
||||
.and_then(|v| v.get("kind"))
|
||||
.and_then(Value::as_str);
|
||||
let mut inner = shared.inner.lock().expect("diagnostics state poisoned");
|
||||
match kind {
|
||||
Some("begin") => inner.progress_active += 1,
|
||||
Some("end") => {
|
||||
inner.progress_active = inner.progress_active.saturating_sub(1);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
inner.mark_change();
|
||||
drop(inner);
|
||||
shared.notify.notify_waiters();
|
||||
}
|
||||
// Server-surfaced messages: keep Error/Warning ones so an empty diagnostics result from a
|
||||
// misconfigured/incompatible server (e.g. tsserver that can't find a usable TypeScript)
|
||||
// isn't silent. Info/log chatter (type >= 3) is ignored.
|
||||
"window/showMessage" | "window/logMessage" => {
|
||||
let Some(params) = params else { return };
|
||||
let severity = params.get("type").and_then(Value::as_u64).unwrap_or(0);
|
||||
if severity == 1 || severity == 2 {
|
||||
if let Some(message) = params.get("message").and_then(Value::as_str) {
|
||||
if let Ok(mut inner) = shared.inner.lock() {
|
||||
inner.push_notice(severity as u8, message.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// rust-analyzer's readiness signal: `quiescent: true` once indexing/priming settles.
|
||||
"experimental/serverStatus" => {
|
||||
let quiescent =
|
||||
params.and_then(|p| p.get("quiescent")).and_then(Value::as_bool).unwrap_or(false);
|
||||
{
|
||||
let mut inner = shared.inner.lock().expect("diagnostics state poisoned");
|
||||
inner.quiescent = quiescent;
|
||||
inner.mark_change();
|
||||
}
|
||||
shared.notify.notify_waiters();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Auto-replies to a server→client request so the server never stalls waiting on us.
|
||||
///
|
||||
/// We don't implement these capabilities, so the replies are deliberately empty: `null` for
|
||||
/// capability registration / progress-create / message requests, and an array of `null`s for
|
||||
/// `workspace/configuration` (the server then falls back to defaults).
|
||||
async fn reply_to_server_request(
|
||||
stdin: &Arc<Mutex<ChildStdin>>,
|
||||
id: Value,
|
||||
method: &str,
|
||||
params: Option<&Value>,
|
||||
) {
|
||||
let result = if method == "workspace/configuration" {
|
||||
let items = params
|
||||
.and_then(|p| p.get("items"))
|
||||
.and_then(Value::as_array)
|
||||
.map_or(1, |items| items.len());
|
||||
Value::Array(vec![Value::Null; items.max(1)])
|
||||
} else {
|
||||
Value::Null
|
||||
};
|
||||
|
||||
let response = JsonRpcMessage {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
id: Some(id),
|
||||
method: None,
|
||||
params: None,
|
||||
result: Some(result),
|
||||
error: None,
|
||||
};
|
||||
let Ok(json) = serde_json::to_string(&response) else { return };
|
||||
let mut guard = stdin.lock().await;
|
||||
if let Err(e) = write_frame(&mut *guard, json.as_bytes()).await {
|
||||
debug!(method, error = %e, "lsp: failed to reply to server request");
|
||||
}
|
||||
}
|
||||
209
crates/luxe-lsp/src/default-lsp-servers.json
Normal file
209
crates/luxe-lsp/src/default-lsp-servers.json
Normal file
@@ -0,0 +1,209 @@
|
||||
{
|
||||
"rust": {
|
||||
"command": "rust-analyzer",
|
||||
"root_markers": [
|
||||
"Cargo.toml"
|
||||
],
|
||||
"extensions": [
|
||||
"rs"
|
||||
],
|
||||
"install_hint": "rustup component add rust-analyzer"
|
||||
},
|
||||
"typescript": {
|
||||
"command": "typescript-language-server",
|
||||
"args": [
|
||||
"--stdio"
|
||||
],
|
||||
"root_markers": [
|
||||
"package.json",
|
||||
"tsconfig.json",
|
||||
"jsconfig.json"
|
||||
],
|
||||
"extensions": [
|
||||
"ts",
|
||||
"tsx",
|
||||
"mts",
|
||||
"cts",
|
||||
"js",
|
||||
"jsx",
|
||||
"mjs",
|
||||
"cjs"
|
||||
],
|
||||
"install_hint": "npm i -g typescript-language-server typescript (TypeScript 7 projects: npm i -g @typescript/native-preview, which provides `tsgo --lsp`)",
|
||||
"alternatives": [
|
||||
{
|
||||
"command": "tsgo",
|
||||
"args": [
|
||||
"--lsp",
|
||||
"--stdio"
|
||||
]
|
||||
},
|
||||
{
|
||||
"command": "tsc",
|
||||
"args": [
|
||||
"--lsp",
|
||||
"--stdio"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"python": {
|
||||
"command": "pyright-langserver",
|
||||
"args": [
|
||||
"--stdio"
|
||||
],
|
||||
"root_markers": [
|
||||
"pyproject.toml",
|
||||
"setup.py",
|
||||
"requirements.txt",
|
||||
"setup.cfg"
|
||||
],
|
||||
"extensions": [
|
||||
"py",
|
||||
"pyi"
|
||||
],
|
||||
"install_hint": "npm i -g pyright (or: pip install pyright)",
|
||||
"alternatives": [
|
||||
{
|
||||
"command": "basedpyright-langserver",
|
||||
"args": [
|
||||
"--stdio"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"go": {
|
||||
"command": "gopls",
|
||||
"root_markers": [
|
||||
"go.mod"
|
||||
],
|
||||
"extensions": [
|
||||
"go"
|
||||
],
|
||||
"install_hint": "go install golang.org/x/tools/gopls@latest"
|
||||
},
|
||||
"cpp": {
|
||||
"command": "clangd",
|
||||
"root_markers": [
|
||||
"compile_commands.json",
|
||||
"compile_flags.txt",
|
||||
".clangd",
|
||||
"CMakeLists.txt",
|
||||
"Makefile"
|
||||
],
|
||||
"extensions": [
|
||||
"c",
|
||||
"cc",
|
||||
"cpp",
|
||||
"cxx",
|
||||
"c++",
|
||||
"h",
|
||||
"hh",
|
||||
"hpp",
|
||||
"hxx"
|
||||
],
|
||||
"install_hint": "install clangd (LLVM; e.g. `apt install clangd` / `brew install llvm`)"
|
||||
},
|
||||
"bash": {
|
||||
"command": "bash-language-server",
|
||||
"args": [
|
||||
"start"
|
||||
],
|
||||
"root_markers": [
|
||||
".git"
|
||||
],
|
||||
"extensions": [
|
||||
"sh",
|
||||
"bash"
|
||||
],
|
||||
"install_hint": "npm i -g bash-language-server"
|
||||
},
|
||||
"json": {
|
||||
"command": "vscode-json-language-server",
|
||||
"args": [
|
||||
"--stdio"
|
||||
],
|
||||
"root_markers": [
|
||||
"package.json",
|
||||
".git"
|
||||
],
|
||||
"extensions": [
|
||||
"json",
|
||||
"jsonc"
|
||||
],
|
||||
"install_hint": "npm i -g vscode-langservers-extracted"
|
||||
},
|
||||
"yaml": {
|
||||
"command": "yaml-language-server",
|
||||
"args": [
|
||||
"--stdio"
|
||||
],
|
||||
"root_markers": [
|
||||
".git"
|
||||
],
|
||||
"extensions": [
|
||||
"yaml",
|
||||
"yml"
|
||||
],
|
||||
"install_hint": "npm i -g yaml-language-server"
|
||||
},
|
||||
"ruby": {
|
||||
"command": "ruby-lsp",
|
||||
"alternatives": [
|
||||
{
|
||||
"command": "solargraph",
|
||||
"args": [
|
||||
"stdio"
|
||||
]
|
||||
}
|
||||
],
|
||||
"root_markers": [
|
||||
"Gemfile",
|
||||
".ruby-version",
|
||||
"Rakefile",
|
||||
".git"
|
||||
],
|
||||
"extensions": [
|
||||
"rb",
|
||||
"rake",
|
||||
"gemspec"
|
||||
],
|
||||
"install_hint": "gem install ruby-lsp (it needs a bundled project; `gem install solargraph` works without one and is tried next)"
|
||||
},
|
||||
"lua": {
|
||||
"command": "lua-language-server",
|
||||
"root_markers": [
|
||||
".luarc.json",
|
||||
".luarc.jsonc",
|
||||
"stylua.toml",
|
||||
".git"
|
||||
],
|
||||
"extensions": [
|
||||
"lua"
|
||||
],
|
||||
"install_hint": "install lua-language-server (LuaLS; e.g. `mise use -g lua-language-server`)"
|
||||
},
|
||||
"svelte": {
|
||||
"command": "svelteserver",
|
||||
"args": [
|
||||
"--stdio"
|
||||
],
|
||||
"root_markers": [
|
||||
"svelte.config.js",
|
||||
"svelte.config.ts",
|
||||
"package.json"
|
||||
],
|
||||
"extensions": [
|
||||
"svelte"
|
||||
],
|
||||
"install_hint": "npm i -g svelte-language-server",
|
||||
"alternatives": [
|
||||
{
|
||||
"command": "svelte-language-server",
|
||||
"args": [
|
||||
"--stdio"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
280
crates/luxe-lsp/src/edit_apply.rs
Normal file
280
crates/luxe-lsp/src/edit_apply.rs
Normal file
@@ -0,0 +1,280 @@
|
||||
//! Workspace edits — a language server's `WorkspaceEdit` applied to disk, all of it or none.
|
||||
//!
|
||||
//! The file list comes from the server, so it is planned in memory first: every file read, every
|
||||
//! edit placed, and any file outside the project or inside a fetched repository refuses the whole
|
||||
//! edit before a byte is written. Then each changed file is written, and a failed write puts back
|
||||
//! the ones already done. The result counts what actually landed, says what was skipped and which
|
||||
//! resource operations were not applied, and each written file goes through `FileChanged` and the
|
||||
//! read tracker like an `edit` would.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use luxe_core::read_tracker::ReadTracker;
|
||||
use luxe_core::{ToolContext, ToolResultData};
|
||||
use serde_json::Value;
|
||||
|
||||
/// Where an applied edit reports to: the call's context and, when wired, the read tracker.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct Writes<'a> {
|
||||
pub(crate) cx: &'a ToolContext,
|
||||
pub(crate) tracker: Option<&'a ReadTracker>,
|
||||
}
|
||||
|
||||
/// A single text edit as `(start_line, start_char, end_line, end_char, new_text)` in LSP coords.
|
||||
type TextEdit = (u32, u32, u32, u32, String);
|
||||
|
||||
/// What a `WorkspaceEdit` asks for: text edits per file, and the resource operations it carried.
|
||||
struct Requested {
|
||||
files: Vec<(String, Vec<TextEdit>)>,
|
||||
operations: Vec<String>,
|
||||
}
|
||||
|
||||
/// One file's change, worked out in memory before anything is written.
|
||||
struct FilePlan {
|
||||
path: PathBuf,
|
||||
display: String,
|
||||
original: String,
|
||||
updated: String,
|
||||
applied: usize,
|
||||
skipped: usize,
|
||||
}
|
||||
|
||||
/// Apply a `WorkspaceEdit`, returning a summary plus any context `FileChanged` hooks injected.
|
||||
pub(crate) async fn apply_workspace_edit(edit: &Value, writes: Writes<'_>) -> ToolResultData {
|
||||
let requested = collect_workspace_edits(edit);
|
||||
let plans = match plan(requested.files, &writes.cx.cwd) {
|
||||
Ok(plans) => plans,
|
||||
Err(message) => return ToolResultData::error(&message),
|
||||
};
|
||||
let changed: Vec<&FilePlan> =
|
||||
plans.iter().filter(|file| file.updated != file.original).collect();
|
||||
if let Err(message) = write_all(&changed) {
|
||||
return ToolResultData::error(&message);
|
||||
}
|
||||
let mut injected = Vec::new();
|
||||
for file in &changed {
|
||||
injected.extend(writes.cx.file_changed(&file.path, "edit").await);
|
||||
// after the hook, so a format-on-save rewrite is what counts as read
|
||||
if let Some(tracker) = writes.tracker {
|
||||
tracker.record(&writes.cx.session_id, &file.path);
|
||||
}
|
||||
}
|
||||
ToolResultData::text(summary(&plans, &requested.operations, &injected))
|
||||
}
|
||||
|
||||
fn plan(files: Vec<(String, Vec<TextEdit>)>, cwd: &Path) -> Result<Vec<FilePlan>, String> {
|
||||
// one plan per file, or a second entry for it would be placed against the unedited text
|
||||
let mut merged: Vec<(PathBuf, Vec<TextEdit>)> = Vec::new();
|
||||
for (uri, edits) in files {
|
||||
// through the URI parser: a Windows drive or a percent-encoded space breaks a prefix strip
|
||||
let path = crate::uri::to_path(&uri);
|
||||
match merged.iter_mut().find(|(known, _)| *known == path) {
|
||||
Some((_, known)) => known.extend(edits),
|
||||
None => merged.push((path, edits)),
|
||||
}
|
||||
}
|
||||
merged.into_iter().map(|(path, edits)| plan_file(path, edits, cwd)).collect()
|
||||
}
|
||||
|
||||
fn plan_file(path: PathBuf, edits: Vec<TextEdit>, cwd: &Path) -> Result<FilePlan, String> {
|
||||
let display = luxe_core::paths::display(&path);
|
||||
if let Some(reason) = refusal(&path, cwd) {
|
||||
return Err(format!("Error: the edit names {display}, {reason}; nothing was written"));
|
||||
}
|
||||
let original = std::fs::read_to_string(&path)
|
||||
.map_err(|e| format!("Error: read {display}: {e}; nothing was written"))?;
|
||||
let requested = edits.len();
|
||||
let (updated, applied) = apply_text_edits(&original, edits);
|
||||
Ok(FilePlan { path, display, original, updated, applied, skipped: requested - applied })
|
||||
}
|
||||
|
||||
/// Why `path` may not be written from here, if it may not. Canonical on both sides, so a symlink
|
||||
/// cannot lead out of the project.
|
||||
fn refusal(path: &Path, cwd: &Path) -> Option<&'static str> {
|
||||
let canonical =
|
||||
|path: &Path| std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
|
||||
let target = canonical(path);
|
||||
if !luxe_core::paths::contains(&canonical(cwd), &target) {
|
||||
return Some("outside the project");
|
||||
}
|
||||
luxe_core::fetched::contains(&target).then_some("inside a fetched repository")
|
||||
}
|
||||
|
||||
/// Write every changed file, or put back the ones already written and say what could not be.
|
||||
fn write_all(changed: &[&FilePlan]) -> Result<(), String> {
|
||||
for (index, file) in changed.iter().enumerate() {
|
||||
let Err(error) = luxe_core::fs_atomic::replace(&file.path, file.updated.as_bytes()) else {
|
||||
continue;
|
||||
};
|
||||
let unrestored: Vec<&str> = changed[..index]
|
||||
.iter()
|
||||
.filter(|done| {
|
||||
luxe_core::fs_atomic::replace(&done.path, done.original.as_bytes()).is_err()
|
||||
})
|
||||
.map(|done| done.display.as_str())
|
||||
.collect();
|
||||
let after = match unrestored.as_slice() {
|
||||
[] => "the files already written were restored".to_string(),
|
||||
files => format!("could not restore {}", files.join(", ")),
|
||||
};
|
||||
return Err(format!("Error: write {}: {error}; {after}", file.display));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn summary(plans: &[FilePlan], operations: &[String], injected: &[String]) -> String {
|
||||
let changed: Vec<&FilePlan> = plans.iter().filter(|file| file.applied > 0).collect();
|
||||
let total: usize = changed.iter().map(|file| file.applied).sum();
|
||||
let mut out = if total == 0 {
|
||||
"Rename/edit produced no changes\n".to_string()
|
||||
} else {
|
||||
format!("Applied {total} edit(s) across {} file(s):\n", changed.len())
|
||||
};
|
||||
for file in &changed {
|
||||
out.push_str(&format!(" {} ({} edit(s))\n", file.display, file.applied));
|
||||
}
|
||||
let skipped: Vec<String> = plans
|
||||
.iter()
|
||||
.filter(|file| file.skipped > 0)
|
||||
.map(|file| format!("{} ({})", file.display, file.skipped))
|
||||
.collect();
|
||||
if !skipped.is_empty() {
|
||||
let count: usize = plans.iter().map(|file| file.skipped).sum();
|
||||
out.push_str(&format!(
|
||||
"skipped {count} edit(s) outside their file or overlapping another: {}\n",
|
||||
skipped.join(", ")
|
||||
));
|
||||
}
|
||||
if !operations.is_empty() {
|
||||
out.push_str(&format!(
|
||||
"not applied: {} (file operations are not supported here; use write or bash)\n",
|
||||
operations.join(", ")
|
||||
));
|
||||
}
|
||||
if !injected.is_empty() {
|
||||
out.push_str(&format!("\n--- Diagnostics ---\n{}", injected.join("\n")));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Collect a `WorkspaceEdit`'s per-file edits and resource operations.
|
||||
///
|
||||
/// `documentChanges` wins when both are present, as the spec says, so an edit sent both ways
|
||||
/// lands once.
|
||||
fn collect_workspace_edits(edit: &Value) -> Requested {
|
||||
let mut requested = Requested { files: Vec::new(), operations: Vec::new() };
|
||||
if let Some(document_changes) = edit.get("documentChanges").and_then(Value::as_array) {
|
||||
for change in document_changes {
|
||||
match change.get("textDocument").and_then(|d| d.get("uri")).and_then(Value::as_str) {
|
||||
Some(uri) => {
|
||||
let edits = change.get("edits").map(parse_text_edits).unwrap_or_default();
|
||||
requested.files.push((uri.to_string(), edits));
|
||||
}
|
||||
None => requested.operations.push(describe_operation(change)),
|
||||
}
|
||||
}
|
||||
} else if let Some(changes) = edit.get("changes").and_then(Value::as_object) {
|
||||
for (uri, edits) in changes {
|
||||
requested.files.push((uri.clone(), parse_text_edits(edits)));
|
||||
}
|
||||
}
|
||||
requested
|
||||
}
|
||||
|
||||
fn describe_operation(change: &Value) -> String {
|
||||
let path = |key: &str| {
|
||||
change
|
||||
.get(key)
|
||||
.and_then(Value::as_str)
|
||||
.map(|uri| luxe_core::paths::display(&crate::uri::to_path(uri)))
|
||||
.unwrap_or_default()
|
||||
};
|
||||
match change.get("kind").and_then(Value::as_str).unwrap_or("unknown") {
|
||||
"rename" => format!("rename {} to {}", path("oldUri"), path("newUri")),
|
||||
kind => format!("{kind} {}", path("uri")),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_text_edits(edits: &Value) -> Vec<TextEdit> {
|
||||
let Some(array) = edits.as_array() else { return Vec::new() };
|
||||
array
|
||||
.iter()
|
||||
.filter_map(|edit| {
|
||||
let range = edit.get("range")?;
|
||||
let start = range.get("start")?;
|
||||
let end = range.get("end")?;
|
||||
let num = |v: &Value, k: &str| v.get(k).and_then(Value::as_u64).unwrap_or(0) as u32;
|
||||
Some((
|
||||
num(start, "line"),
|
||||
num(start, "character"),
|
||||
num(end, "line"),
|
||||
num(end, "character"),
|
||||
edit.get("newText").and_then(Value::as_str).unwrap_or("").to_string(),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Apply LSP text edits to `content`, returning the text and how many edits landed.
|
||||
///
|
||||
/// Descending start order, so an edit never shifts a range still to come. An edit on a line the
|
||||
/// file does not have, or overlapping one already applied, is skipped rather than guessed at.
|
||||
/// `character` offsets are UTF-16 code units (the LSP default).
|
||||
fn apply_text_edits(content: &str, edits: Vec<TextEdit>) -> (String, usize) {
|
||||
let line_starts = line_start_bytes(content);
|
||||
let mut spans: Vec<(usize, usize, usize, String)> = edits
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, (sl, sc, el, ec, new_text))| {
|
||||
let start = position_to_byte(content, &line_starts, sl, sc)?;
|
||||
let end = position_to_byte(content, &line_starts, el, ec)?;
|
||||
Some((start.min(end), index, start.max(end), new_text))
|
||||
})
|
||||
.collect();
|
||||
// edits sharing a start apply in the order sent, so the later one goes in first
|
||||
spans.sort_by_key(|span| std::cmp::Reverse((span.0, span.1)));
|
||||
let (mut out, mut applied, mut limit) = (content.to_string(), 0, content.len());
|
||||
for (start, _, end, new_text) in spans {
|
||||
if end <= limit {
|
||||
out.replace_range(start..end, &new_text);
|
||||
applied += 1;
|
||||
limit = start;
|
||||
}
|
||||
}
|
||||
(out, applied)
|
||||
}
|
||||
|
||||
/// Byte offset of the start of each line (line 0 at 0, then after each `\n`).
|
||||
fn line_start_bytes(content: &str) -> Vec<usize> {
|
||||
let mut starts = vec![0];
|
||||
for (index, byte) in content.bytes().enumerate() {
|
||||
if byte == b'\n' {
|
||||
starts.push(index + 1);
|
||||
}
|
||||
}
|
||||
starts
|
||||
}
|
||||
|
||||
/// Convert an LSP `(line, character_utf16)` position to a byte offset, clamped to the line's end;
|
||||
/// `None` for a line the file does not have.
|
||||
fn position_to_byte(
|
||||
content: &str,
|
||||
line_starts: &[usize],
|
||||
line: u32,
|
||||
character: u32,
|
||||
) -> Option<usize> {
|
||||
let line_start = *line_starts.get(line as usize)?;
|
||||
let mut byte = line_start;
|
||||
let mut utf16 = 0u32;
|
||||
for ch in content[line_start..].chars() {
|
||||
if ch == '\n' || utf16 >= character {
|
||||
break;
|
||||
}
|
||||
utf16 += ch.len_utf16() as u32;
|
||||
byte += ch.len_utf8();
|
||||
}
|
||||
Some(byte)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
117
crates/luxe-lsp/src/edit_apply/tests.rs
Normal file
117
crates/luxe-lsp/src/edit_apply/tests.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
use super::*;
|
||||
use luxe_core::ContentBlock;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod plan;
|
||||
|
||||
pub(super) fn context(cwd: &Path) -> ToolContext {
|
||||
let (event_tx, _) = tokio::sync::mpsc::channel(8);
|
||||
let (_, cancelled) = tokio::sync::watch::channel(false);
|
||||
ToolContext {
|
||||
cwd: cwd.to_path_buf(),
|
||||
session_id: "t".to_string(),
|
||||
cancelled: Arc::new(cancelled),
|
||||
event_tx,
|
||||
hook_ctx: None,
|
||||
question_broker: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn apply(
|
||||
edit: &Value,
|
||||
cwd: &Path,
|
||||
tracker: Option<&ReadTracker>,
|
||||
) -> ToolResultData {
|
||||
let cx = context(cwd);
|
||||
apply_workspace_edit(edit, Writes { cx: &cx, tracker }).await
|
||||
}
|
||||
|
||||
pub(super) fn edit_at(sl: u32, sc: u32, el: u32, ec: u32, new_text: &str) -> Value {
|
||||
serde_json::json!({
|
||||
"range": { "start": { "line": sl, "character": sc }, "end": { "line": el, "character": ec } },
|
||||
"newText": new_text
|
||||
})
|
||||
}
|
||||
|
||||
/// A `documentChanges` edit, in the given file order.
|
||||
pub(super) fn changes(files: &[(&Path, Vec<Value>)]) -> Value {
|
||||
let changes: Vec<Value> = files
|
||||
.iter()
|
||||
.map(|(path, edits)| {
|
||||
serde_json::json!({
|
||||
"textDocument": { "uri": crate::uri::from_path(path), "version": 1 },
|
||||
"edits": edits
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
serde_json::json!({ "documentChanges": changes })
|
||||
}
|
||||
|
||||
pub(super) fn text_of(result: &ToolResultData) -> String {
|
||||
result
|
||||
.content
|
||||
.iter()
|
||||
.filter_map(|block| match block {
|
||||
ContentBlock::Text(text) => Some(text.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_edits_apply_in_descending_order() {
|
||||
let src = "let foo = 1;\nlet bar = foo + foo;\n";
|
||||
// Rename `foo` → `qux` at its 3 occurrences (L1 c4-7, L2 c10-13, L2 c16-19).
|
||||
let edits = vec![
|
||||
(0, 4, 0, 7, "qux".to_string()),
|
||||
(1, 10, 1, 13, "qux".to_string()),
|
||||
(1, 16, 1, 19, "qux".to_string()),
|
||||
];
|
||||
let (out, applied) = apply_text_edits(src, edits);
|
||||
assert_eq!(out, "let qux = 1;\nlet bar = qux + qux;\n");
|
||||
assert_eq!(applied, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn position_to_byte_handles_multibyte_utf16() {
|
||||
// "café" then an edit after it: é is 1 UTF-16 unit but 2 bytes.
|
||||
let src = "let café = 1;\n";
|
||||
let starts = line_start_bytes(src);
|
||||
// character 8 (after "let café") → byte 9 (é = 2 bytes).
|
||||
assert_eq!(position_to_byte(src, &starts, 0, 8), Some(9));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn apply_workspace_edit_writes_and_reports() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let file = dir.path().join("m.rs");
|
||||
std::fs::write(&file, "let foo = 1;\nlet bar = foo;\n").unwrap();
|
||||
let edit = changes(&[(&file, vec![edit_at(0, 4, 0, 7, "qux"), edit_at(1, 10, 1, 13, "qux")])]);
|
||||
let result = apply(&edit, dir.path(), None).await;
|
||||
assert!(!result.is_error, "{result:?}");
|
||||
assert_eq!(std::fs::read_to_string(&file).unwrap(), "let qux = 1;\nlet bar = qux;\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collect_edits_reads_changes_and_document_changes() {
|
||||
let changes = serde_json::json!({
|
||||
"changes": { "file:///a.rs": [
|
||||
{ "range": { "start": { "line": 0, "character": 0 }, "end": { "line": 0, "character": 3 } }, "newText": "xyz" }
|
||||
]}
|
||||
});
|
||||
let files = collect_workspace_edits(&changes).files;
|
||||
assert_eq!(files[0].0, "file:///a.rs");
|
||||
assert_eq!(files[0].1.len(), 1);
|
||||
|
||||
let doc_changes = serde_json::json!({
|
||||
"documentChanges": [
|
||||
{ "textDocument": { "uri": "file:///b.rs", "version": 1 }, "edits": [
|
||||
{ "range": { "start": { "line": 1, "character": 2 }, "end": { "line": 1, "character": 5 } }, "newText": "q" }
|
||||
]}
|
||||
]
|
||||
});
|
||||
let files = collect_workspace_edits(&doc_changes).files;
|
||||
assert_eq!(files[0].0, "file:///b.rs");
|
||||
assert_eq!(files[0].1[0].4, "q");
|
||||
}
|
||||
177
crates/luxe-lsp/src/edit_apply/tests/plan.rs
Normal file
177
crates/luxe-lsp/src/edit_apply/tests/plan.rs
Normal file
@@ -0,0 +1,177 @@
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
/// A server naming a file outside the project must not get it written, and nothing else either.
|
||||
#[tokio::test]
|
||||
async fn an_edit_naming_a_file_outside_the_project_writes_nothing() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let elsewhere = tempfile::tempdir().unwrap();
|
||||
let inside = project.path().join("a.rs");
|
||||
let hosts = elsewhere.path().join("hosts");
|
||||
std::fs::write(&inside, "let a = 1;\n").unwrap();
|
||||
std::fs::write(&hosts, "127.0.0.1 localhost\n").unwrap();
|
||||
let edit = changes(&[
|
||||
(&inside, vec![edit_at(0, 4, 0, 5, "b")]),
|
||||
(&hosts, vec![edit_at(0, 0, 0, 0, "6.6.6.6 bank\n")]),
|
||||
]);
|
||||
let result = apply(&edit, project.path(), None).await;
|
||||
assert!(result.is_error, "{result:?}");
|
||||
assert!(text_of(&result).contains("outside the project"), "{}", text_of(&result));
|
||||
assert_eq!(std::fs::read_to_string(&inside).unwrap(), "let a = 1;\n");
|
||||
assert_eq!(std::fs::read_to_string(&hosts).unwrap(), "127.0.0.1 localhost\n");
|
||||
}
|
||||
|
||||
/// A fetched repository is a stranger's code inside the project, and no edit lands there.
|
||||
#[tokio::test]
|
||||
async fn an_edit_inside_a_fetched_repository_writes_nothing() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let clone = luxe_core::fetched::cache_dir(project.path()).join("someone").join("repo");
|
||||
std::fs::create_dir_all(&clone).unwrap();
|
||||
let fetched = clone.join("lib.rs");
|
||||
std::fs::write(&fetched, "fn a() {}\n").unwrap();
|
||||
let edit = changes(&[(&fetched, vec![edit_at(0, 3, 0, 4, "b")])]);
|
||||
let result = apply(&edit, project.path(), None).await;
|
||||
assert!(result.is_error, "{result:?}");
|
||||
assert_eq!(std::fs::read_to_string(&fetched).unwrap(), "fn a() {}\n");
|
||||
}
|
||||
|
||||
/// Every file is read and planned before any is written, so a bad second file leaves the first.
|
||||
#[tokio::test]
|
||||
async fn an_unreadable_second_file_leaves_the_first_untouched() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let first = project.path().join("a.rs");
|
||||
std::fs::write(&first, "let a = 1;\n").unwrap();
|
||||
let missing = project.path().join("missing.rs");
|
||||
let edit = changes(&[
|
||||
(&first, vec![edit_at(0, 4, 0, 5, "b")]),
|
||||
(&missing, vec![edit_at(0, 0, 0, 0, "x")]),
|
||||
]);
|
||||
let result = apply(&edit, project.path(), None).await;
|
||||
assert!(result.is_error, "{result:?}");
|
||||
assert_eq!(std::fs::read_to_string(&first).unwrap(), "let a = 1;\n");
|
||||
}
|
||||
|
||||
/// A write that fails partway puts back what was already written.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_failed_write_restores_the_files_already_written() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let first = project.path().join("a.rs");
|
||||
let locked = project.path().join("locked");
|
||||
std::fs::create_dir_all(&locked).unwrap();
|
||||
let second = locked.join("b.rs");
|
||||
std::fs::write(&first, "let a = 1;\n").unwrap();
|
||||
std::fs::write(&second, "let b = 1;\n").unwrap();
|
||||
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o555)).unwrap();
|
||||
if std::fs::write(locked.join("probe"), "").is_ok() {
|
||||
return; // running as root: a read-only directory cannot make the write fail
|
||||
}
|
||||
let edit = changes(&[
|
||||
(&first, vec![edit_at(0, 4, 0, 5, "x")]),
|
||||
(&second, vec![edit_at(0, 4, 0, 5, "y")]),
|
||||
]);
|
||||
let result = apply(&edit, project.path(), None).await;
|
||||
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
assert!(result.is_error, "{result:?}");
|
||||
assert_eq!(std::fs::read_to_string(&first).unwrap(), "let a = 1;\n");
|
||||
assert_eq!(std::fs::read_to_string(&second).unwrap(), "let b = 1;\n");
|
||||
}
|
||||
|
||||
/// An edit past the end of its file is reported as skipped, not counted, and not appended.
|
||||
#[tokio::test]
|
||||
async fn an_out_of_range_edit_is_reported_as_skipped() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let file = project.path().join("a.rs");
|
||||
std::fs::write(&file, "let a = 1;\n").unwrap();
|
||||
let edit = changes(&[(&file, vec![edit_at(0, 4, 0, 5, "b"), edit_at(40, 0, 40, 3, "zzz")])]);
|
||||
let result = apply(&edit, project.path(), None).await;
|
||||
assert!(!result.is_error, "{result:?}");
|
||||
let text = text_of(&result);
|
||||
assert!(text.contains("Applied 1 edit(s)"), "{text}");
|
||||
assert!(text.contains("skipped 1 edit(s)"), "{text}");
|
||||
assert_eq!(std::fs::read_to_string(&file).unwrap(), "let b = 1;\n");
|
||||
}
|
||||
|
||||
/// Creating, renaming or deleting a file is not something this applies, and the result says so.
|
||||
#[tokio::test]
|
||||
async fn resource_operations_are_reported_not_dropped() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let file = project.path().join("a.rs");
|
||||
std::fs::write(&file, "let a = 1;\n").unwrap();
|
||||
let mut edit = changes(&[(&file, vec![edit_at(0, 4, 0, 5, "b")])]);
|
||||
let created = crate::uri::from_path(&project.path().join("new.rs"));
|
||||
edit["documentChanges"].as_array_mut().unwrap().push(json!({"kind": "create", "uri": created}));
|
||||
let text = text_of(&apply(&edit, project.path(), None).await);
|
||||
assert!(text.contains("not applied") && text.contains("create"), "{text}");
|
||||
}
|
||||
|
||||
/// `documentChanges` wins over `changes` when a server sends both, so an edit lands once.
|
||||
#[tokio::test]
|
||||
async fn changes_and_document_changes_together_apply_once() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let file = project.path().join("a.rs");
|
||||
std::fs::write(&file, "a\n").unwrap();
|
||||
let mut edit = changes(&[(&file, vec![edit_at(0, 0, 0, 0, "x")])]);
|
||||
edit["changes"] = json!({ crate::uri::from_path(&file): [edit_at(0, 0, 0, 0, "x")] });
|
||||
apply(&edit, project.path(), None).await;
|
||||
assert_eq!(std::fs::read_to_string(&file).unwrap(), "xa\n");
|
||||
}
|
||||
|
||||
/// A file an applied edit rewrote is current for the read guard, so the next `edit` is not refused.
|
||||
#[tokio::test]
|
||||
async fn applied_files_are_recorded_for_the_read_guard() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let file = project.path().join("a.rs");
|
||||
std::fs::write(&file, "let a = 1;\n").unwrap();
|
||||
let tracker = ReadTracker::new();
|
||||
let edit = changes(&[(&file, vec![edit_at(0, 4, 0, 5, "bee")])]);
|
||||
apply(&edit, project.path(), Some(&tracker)).await;
|
||||
assert_eq!(tracker.status("t", &file), luxe_core::read_tracker::ReadStatus::Current);
|
||||
}
|
||||
|
||||
/// Inserts and a replace sharing one start all land, in the order the server sent them.
|
||||
#[tokio::test]
|
||||
async fn edits_sharing_a_start_apply_in_the_order_sent() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let file = project.path().join("a.rs");
|
||||
std::fs::write(&file, "let a = 1;\n").unwrap();
|
||||
let edit = changes(&[(
|
||||
&file,
|
||||
vec![edit_at(0, 4, 0, 4, "x"), edit_at(0, 4, 0, 4, "y"), edit_at(0, 4, 0, 5, "b")],
|
||||
)]);
|
||||
let text = text_of(&apply(&edit, project.path(), None).await);
|
||||
assert!(text.contains("Applied 3 edit(s)"), "{text}");
|
||||
assert_eq!(std::fs::read_to_string(&file).unwrap(), "let xyb = 1;\n");
|
||||
}
|
||||
|
||||
/// Two entries for one file are planned together, or the second is placed on the unedited text
|
||||
/// and its write undoes the first.
|
||||
#[tokio::test]
|
||||
async fn two_entries_for_one_file_both_land() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let file = project.path().join("a.rs");
|
||||
std::fs::write(&file, "let a = b;\n").unwrap();
|
||||
let edit = changes(&[
|
||||
(&file, vec![edit_at(0, 4, 0, 5, "x")]),
|
||||
(&file, vec![edit_at(0, 8, 0, 9, "y")]),
|
||||
]);
|
||||
apply(&edit, project.path(), None).await;
|
||||
assert_eq!(std::fs::read_to_string(&file).unwrap(), "let x = y;\n");
|
||||
}
|
||||
|
||||
/// A link inside the project that points out of it is judged by where it points.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_symlink_out_of_the_project_is_refused() {
|
||||
let project = tempfile::tempdir().unwrap();
|
||||
let elsewhere = tempfile::tempdir().unwrap();
|
||||
let target = elsewhere.path().join("secret.rs");
|
||||
std::fs::write(&target, "let a = 1;\n").unwrap();
|
||||
let link = project.path().join("link.rs");
|
||||
std::os::unix::fs::symlink(&target, &link).unwrap();
|
||||
let result =
|
||||
apply(&changes(&[(&link, vec![edit_at(0, 4, 0, 5, "b")])]), project.path(), None).await;
|
||||
assert!(result.is_error, "{result:?}");
|
||||
assert_eq!(std::fs::read_to_string(&target).unwrap(), "let a = 1;\n");
|
||||
}
|
||||
30
crates/luxe-lsp/src/graph.rs
Normal file
30
crates/luxe-lsp/src/graph.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
//! Code graph — a structural map of the project for blast-radius impact (design-code-graph.md).
|
||||
//!
|
||||
//! A directed graph of files and symbols (`model`) built from tree-sitter (`extract`) and held in a
|
||||
//! swappable store (`store`). It answers "if this changes, what else is affected?" so an agent
|
||||
//! reads only what matters and Guided Review can ground its relevant files in real structure. Later
|
||||
//! phases add review integration (G5) and LSP enrichment (G6). Call/test resolution (G2) lives in
|
||||
//! `resolve`; `build` assembles a fully-resolved graph from files; the warm `service` +
|
||||
//! content-addressed `cache` (G3) keep it fast and incremental; `blast` computes impact (G4).
|
||||
|
||||
mod blast;
|
||||
mod build;
|
||||
mod cache;
|
||||
mod enrich;
|
||||
mod extract;
|
||||
mod model;
|
||||
mod resolve;
|
||||
mod review;
|
||||
mod service;
|
||||
mod store;
|
||||
mod testing;
|
||||
|
||||
pub use blast::{blast_radius, BlastRadius, ImpactedFile, DEFAULT_MAX_DEPTH};
|
||||
pub use build::build_graph;
|
||||
pub use cache::{content_hash, GraphCache};
|
||||
pub use enrich::{verify_callers, CallerVerification, LspReferenceSource, ReferenceSource};
|
||||
pub use extract::{extract_file, FileGraph};
|
||||
pub use model::{Edge, EdgeKind, EdgeTarget, Node, NodeKind, Precision, QualifiedName};
|
||||
pub use review::{review_insight, ChangedRegion};
|
||||
pub use service::{build_graph_cached, BuildStats, GraphError, GraphService};
|
||||
pub use store::{GraphStore, InMemoryGraph};
|
||||
379
crates/luxe-lsp/src/graph/blast.rs
Normal file
379
crates/luxe-lsp/src/graph/blast.rs
Normal file
@@ -0,0 +1,379 @@
|
||||
//! Blast radius — the transitive impact of a change (design-code-graph.md §7).
|
||||
//!
|
||||
//! Given seed symbols (a changed file's definitions, or one symbol), a **reverse** BFS over the
|
||||
//! resolved reverse index answers *"who is affected if this changes?"* (callers, subclasses,
|
||||
//! covering tests), and a shallower **forward** BFS answers *"what does this rely on?"* (the context
|
||||
//! a reviewer/agent needs). Results are bounded by depth and a node cap, rolled up to ranked files
|
||||
//! with a one-line reason, plus **test-gap** signals (seed symbols no test covers).
|
||||
//!
|
||||
//! Only *resolved* edges are traversed, so this is as precise as resolution (G2) made it. Import
|
||||
//! edges are still name-unresolved, so file-level importers don't yet appear — callers/tests/
|
||||
//! subclasses (the review-critical relationships) do.
|
||||
|
||||
use super::model::{EdgeKind, EdgeTarget, QualifiedName};
|
||||
use super::store::{GraphStore, InMemoryGraph};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// Default maximum BFS depth (like CRG).
|
||||
pub const DEFAULT_MAX_DEPTH: u32 = 2;
|
||||
/// Safety cap on nodes visited per direction, so a hub symbol can't fan out unboundedly.
|
||||
const NODE_CAP: usize = 400;
|
||||
/// How many ranked files each direction lists.
|
||||
const MAX_FILES: usize = 20;
|
||||
/// How many files the read-ready list suggests.
|
||||
const MAX_READY: usize = 15;
|
||||
|
||||
/// Which way to walk the graph.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Direction {
|
||||
/// Reverse edges — who depends on the seed.
|
||||
Dependents,
|
||||
/// Forward edges — what the seed depends on.
|
||||
Dependencies,
|
||||
}
|
||||
|
||||
/// A file reached by the walk, with why and how far.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ImpactedFile {
|
||||
pub file: String,
|
||||
pub min_depth: u32,
|
||||
/// A one-line reason, e.g. `session_handler calls login`.
|
||||
pub reason: String,
|
||||
/// Ranking score (higher = more impacted).
|
||||
pub score: f64,
|
||||
}
|
||||
|
||||
/// The computed blast radius for a target.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BlastRadius {
|
||||
/// The seed symbols the walk started from.
|
||||
pub seeds: Vec<QualifiedName>,
|
||||
pub max_depth: u32,
|
||||
/// Files affected if the seeds change (reverse), ranked.
|
||||
pub dependents: Vec<ImpactedFile>,
|
||||
/// Files the seeds rely on (forward), ranked.
|
||||
pub dependencies: Vec<ImpactedFile>,
|
||||
/// Function/method seeds with no covering test (a test-gap signal).
|
||||
pub untested_seeds: Vec<QualifiedName>,
|
||||
}
|
||||
|
||||
/// Resolve `target` to seed nodes: an exact qualified name, else a file's defined symbols, else all
|
||||
/// definitions of a bare name.
|
||||
fn seeds_for(graph: &InMemoryGraph, target: &str) -> Vec<QualifiedName> {
|
||||
let as_qn = QualifiedName::from_raw(target);
|
||||
// An exact symbol name (a file node isn't a useful seed — its reverse edges are unresolved
|
||||
// imports; a file target should seed the file's symbols instead).
|
||||
if !as_qn.is_file() && graph.node(&as_qn).is_some() {
|
||||
return vec![as_qn];
|
||||
}
|
||||
let file_symbols = graph.symbols_in_file(target);
|
||||
if !file_symbols.is_empty() {
|
||||
return file_symbols;
|
||||
}
|
||||
graph.defs_by_name(target).to_vec()
|
||||
}
|
||||
|
||||
/// Compute the blast radius of `target` (a qualified name, file path, or bare symbol name).
|
||||
pub fn blast_radius(graph: &InMemoryGraph, target: &str, max_depth: u32) -> BlastRadius {
|
||||
let seeds = seeds_for(graph, target);
|
||||
let dependents = rank_files(walk(graph, &seeds, Direction::Dependents, max_depth));
|
||||
// Forward context is intentionally shallow — one hop of dependencies is enough to understand a
|
||||
// change without dragging in the whole transitive world.
|
||||
let dependencies = rank_files(walk(graph, &seeds, Direction::Dependencies, 1));
|
||||
let untested_seeds = untested(graph, &seeds);
|
||||
BlastRadius { seeds, max_depth, dependents, dependencies, untested_seeds }
|
||||
}
|
||||
|
||||
/// One reached node during a walk.
|
||||
struct Reached {
|
||||
file: String,
|
||||
depth: u32,
|
||||
reason: String,
|
||||
score: f64,
|
||||
}
|
||||
|
||||
/// BFS from `seeds` following reverse or forward resolved edges, bounded by depth and a node cap.
|
||||
fn walk(
|
||||
graph: &InMemoryGraph,
|
||||
seeds: &[QualifiedName],
|
||||
direction: Direction,
|
||||
max_depth: u32,
|
||||
) -> Vec<Reached> {
|
||||
let mut visited: HashSet<QualifiedName> = seeds.iter().cloned().collect();
|
||||
let mut frontier: Vec<QualifiedName> = seeds.to_vec();
|
||||
let mut reached: Vec<Reached> = Vec::new();
|
||||
|
||||
let mut depth = 1;
|
||||
while depth <= max_depth && !frontier.is_empty() && reached.len() < NODE_CAP {
|
||||
let mut next = Vec::new();
|
||||
for node in &frontier {
|
||||
let edges = match direction {
|
||||
Direction::Dependents => graph.in_edges(node),
|
||||
Direction::Dependencies => graph.out_edges(node),
|
||||
};
|
||||
for edge in edges {
|
||||
let neighbor = match direction {
|
||||
Direction::Dependents => edge.from.clone(),
|
||||
Direction::Dependencies => match &edge.to {
|
||||
EdgeTarget::Resolved(qn) => qn.clone(),
|
||||
EdgeTarget::Unresolved(_) => continue,
|
||||
},
|
||||
};
|
||||
if neighbor.is_file() || !visited.insert(neighbor.clone()) {
|
||||
continue;
|
||||
}
|
||||
reached.push(Reached {
|
||||
file: neighbor.file_path().to_string(),
|
||||
depth,
|
||||
reason: reason_text(edge),
|
||||
score: weight(edge.kind) / depth as f64,
|
||||
});
|
||||
next.push(neighbor);
|
||||
if reached.len() >= NODE_CAP {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
frontier = next;
|
||||
depth += 1;
|
||||
}
|
||||
reached
|
||||
}
|
||||
|
||||
/// Roll reached nodes up to ranked files: max score wins the reason, min depth kept.
|
||||
fn rank_files(reached: Vec<Reached>) -> Vec<ImpactedFile> {
|
||||
let mut by_file: HashMap<String, ImpactedFile> = HashMap::new();
|
||||
for r in reached {
|
||||
let entry = by_file.entry(r.file.clone()).or_insert_with(|| ImpactedFile {
|
||||
file: r.file.clone(),
|
||||
min_depth: r.depth,
|
||||
reason: r.reason.clone(),
|
||||
score: r.score,
|
||||
});
|
||||
entry.min_depth = entry.min_depth.min(r.depth);
|
||||
if r.score > entry.score {
|
||||
entry.score = r.score;
|
||||
entry.reason = r.reason;
|
||||
}
|
||||
}
|
||||
let mut files: Vec<ImpactedFile> = by_file.into_values().collect();
|
||||
files.sort_by(|a, b| {
|
||||
b.score
|
||||
.partial_cmp(&a.score)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
.then(a.min_depth.cmp(&b.min_depth))
|
||||
.then(a.file.cmp(&b.file))
|
||||
});
|
||||
files.truncate(MAX_FILES);
|
||||
files
|
||||
}
|
||||
|
||||
/// Function/method seeds with no incoming `TestCovers` edge.
|
||||
fn untested(graph: &InMemoryGraph, seeds: &[QualifiedName]) -> Vec<QualifiedName> {
|
||||
use super::model::NodeKind;
|
||||
seeds
|
||||
.iter()
|
||||
.filter(|qn| {
|
||||
matches!(graph.node(qn).map(|n| n.kind), Some(NodeKind::Function | NodeKind::Method))
|
||||
})
|
||||
.filter(|qn| !graph.in_edges(qn).iter().any(|e| e.kind == EdgeKind::TestCovers))
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Impact weight of an edge kind — direct behavior (tests/calls) outweighs structure (imports).
|
||||
fn weight(kind: EdgeKind) -> f64 {
|
||||
match kind {
|
||||
EdgeKind::TestCovers => 4.0,
|
||||
EdgeKind::Calls => 3.0,
|
||||
EdgeKind::Inherits | EdgeKind::Implements => 2.0,
|
||||
EdgeKind::Imports => 1.0,
|
||||
EdgeKind::Defines => 0.5,
|
||||
}
|
||||
}
|
||||
|
||||
/// The verb for a reason phrase.
|
||||
fn verb(kind: EdgeKind) -> &'static str {
|
||||
match kind {
|
||||
EdgeKind::Calls => "calls",
|
||||
EdgeKind::TestCovers => "tests",
|
||||
EdgeKind::Inherits => "inherits",
|
||||
EdgeKind::Implements => "implements",
|
||||
EdgeKind::Imports => "imports",
|
||||
EdgeKind::Defines => "defines",
|
||||
}
|
||||
}
|
||||
|
||||
/// `subject verb object`, e.g. `session_handler calls login`.
|
||||
fn reason_text(edge: &super::model::Edge) -> String {
|
||||
let object = match &edge.to {
|
||||
EdgeTarget::Resolved(qn) => qn.base_name().to_string(),
|
||||
EdgeTarget::Unresolved(name) => name.rsplit('.').next().unwrap_or(name).to_string(),
|
||||
};
|
||||
format!("{} {} {}", edge.from.base_name(), verb(edge.kind), object)
|
||||
}
|
||||
|
||||
impl BlastRadius {
|
||||
/// Whether nothing was reached (unknown target or an isolated symbol).
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.dependents.is_empty() && self.dependencies.is_empty()
|
||||
}
|
||||
|
||||
/// Deduped, ranked files worth reading — dependents first, then dependencies.
|
||||
pub fn read_ready(&self) -> Vec<String> {
|
||||
let mut seen = HashSet::new();
|
||||
self.dependents
|
||||
.iter()
|
||||
.chain(self.dependencies.iter())
|
||||
.map(|f| f.file.clone())
|
||||
.filter(|f| seen.insert(f.clone()))
|
||||
.take(MAX_READY)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A terse one-liner for `module_report` (counts only).
|
||||
pub fn summary_line(&self) -> String {
|
||||
format!(
|
||||
"{} dependent file(s), {} dependency file(s), {} untested seed(s)",
|
||||
self.dependents.len(),
|
||||
self.dependencies.len(),
|
||||
self.untested_seeds.len()
|
||||
)
|
||||
}
|
||||
|
||||
/// A full, token-compact rendering for the `impact` tool.
|
||||
pub fn render(&self, target: &str) -> String {
|
||||
if self.seeds.is_empty() {
|
||||
return format!(
|
||||
"No graph node found for '{target}'. Try a file path or a symbol name."
|
||||
);
|
||||
}
|
||||
let mut out = format!(
|
||||
"Blast radius of `{target}` (depth <= {}, {} seed symbol(s)):\n",
|
||||
self.max_depth,
|
||||
self.seeds.len()
|
||||
);
|
||||
|
||||
out.push_str(&format!(
|
||||
"\nDependents — affected if this changes ({}):\n",
|
||||
self.dependents.len()
|
||||
));
|
||||
render_files(&mut out, &self.dependents);
|
||||
|
||||
out.push_str(&format!(
|
||||
"\nDependencies — what this relies on ({}):\n",
|
||||
self.dependencies.len()
|
||||
));
|
||||
render_files(&mut out, &self.dependencies);
|
||||
|
||||
out.push_str("\nTest coverage: ");
|
||||
if self.untested_seeds.is_empty() {
|
||||
out.push_str("every function/method seed has a covering test.\n");
|
||||
} else {
|
||||
let names: Vec<&str> = self.untested_seeds.iter().map(|s| s.base_name()).collect();
|
||||
out.push_str(&format!("no covering test for {}.\n", names.join(", ")));
|
||||
}
|
||||
|
||||
let ready = self.read_ready();
|
||||
if !ready.is_empty() {
|
||||
out.push_str(&format!("\nRead next: {}\n", ready.join(", ")));
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
fn render_files(out: &mut String, files: &[ImpactedFile]) {
|
||||
if files.is_empty() {
|
||||
out.push_str(" (none)\n");
|
||||
return;
|
||||
}
|
||||
for file in files {
|
||||
out.push_str(&format!(" {} · {} [depth {}]\n", file.file, file.reason, file.min_depth));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::build::build_graph;
|
||||
|
||||
fn project() -> Vec<(&'static str, &'static str)> {
|
||||
vec![
|
||||
("src/auth.rs", "pub fn login() -> bool { true }\npub fn logout() {}\n"),
|
||||
("src/session.rs", "use crate::auth::login;\npub fn start() -> bool { login() }\n"),
|
||||
("src/api.rs", "use crate::session::start;\npub fn handle() -> bool { start() }\n"),
|
||||
(
|
||||
"tests/auth_test.rs",
|
||||
"#[cfg(test)]\nmod tests {\n fn test_login() { login(); }\n}\n",
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse_bfs_finds_transitive_callers_and_tests() {
|
||||
let graph = build_graph(project());
|
||||
let br = blast_radius(&graph, "src/auth.rs::login", DEFAULT_MAX_DEPTH);
|
||||
|
||||
let dep_files: Vec<&str> = br.dependents.iter().map(|f| f.file.as_str()).collect();
|
||||
// Direct caller (depth 1) and transitive caller (depth 2).
|
||||
assert!(dep_files.contains(&"src/session.rs"), "direct caller");
|
||||
assert!(dep_files.contains(&"src/api.rs"), "transitive caller");
|
||||
// The covering test shows up too.
|
||||
assert!(dep_files.contains(&"tests/auth_test.rs"), "covering test");
|
||||
|
||||
// The direct caller/test outrank the transitive caller.
|
||||
let session = br.dependents.iter().find(|f| f.file == "src/session.rs").unwrap();
|
||||
let api = br.dependents.iter().find(|f| f.file == "src/api.rs").unwrap();
|
||||
assert_eq!(session.min_depth, 1);
|
||||
assert_eq!(api.min_depth, 2);
|
||||
assert!(session.score > api.score);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn depth_bound_is_respected() {
|
||||
let graph = build_graph(project());
|
||||
let shallow = blast_radius(&graph, "src/auth.rs::login", 1);
|
||||
let files: Vec<&str> = shallow.dependents.iter().map(|f| f.file.as_str()).collect();
|
||||
assert!(files.contains(&"src/session.rs"));
|
||||
// api.rs is two hops away → excluded at depth 1.
|
||||
assert!(!files.contains(&"src/api.rs"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn untested_seed_is_flagged() {
|
||||
let graph = build_graph(project());
|
||||
// logout has no test; login does.
|
||||
let logout = blast_radius(&graph, "src/auth.rs::logout", DEFAULT_MAX_DEPTH);
|
||||
assert!(logout.untested_seeds.iter().any(|s| s.base_name() == "logout"));
|
||||
|
||||
let login = blast_radius(&graph, "src/auth.rs::login", DEFAULT_MAX_DEPTH);
|
||||
assert!(login.untested_seeds.is_empty(), "login is covered by test_login");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forward_walk_lists_dependencies() {
|
||||
let graph = build_graph(project());
|
||||
let br = blast_radius(&graph, "src/session.rs::start", DEFAULT_MAX_DEPTH);
|
||||
// start() calls login() in auth.rs.
|
||||
assert!(br.dependencies.iter().any(|f| f.file == "src/auth.rs"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_target_seeds_all_its_symbols() {
|
||||
let graph = build_graph(project());
|
||||
let br = blast_radius(&graph, "src/auth.rs", DEFAULT_MAX_DEPTH);
|
||||
// Seeds are auth.rs's functions (login, logout), not the file node.
|
||||
assert!(br.seeds.iter().all(|s| !s.is_file()));
|
||||
assert!(br.seeds.iter().any(|s| s.base_name() == "login"));
|
||||
// Its callers are still found.
|
||||
assert!(br.dependents.iter().any(|f| f.file == "src/session.rs"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_target_renders_a_hint() {
|
||||
let graph = build_graph(project());
|
||||
let br = blast_radius(&graph, "does_not_exist", DEFAULT_MAX_DEPTH);
|
||||
assert!(br.seeds.is_empty());
|
||||
assert!(br.render("does_not_exist").contains("No graph node found"));
|
||||
}
|
||||
}
|
||||
100
crates/luxe-lsp/src/graph/build.rs
Normal file
100
crates/luxe-lsp/src/graph/build.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
//! Build — assemble a resolved in-memory graph from a set of files.
|
||||
//!
|
||||
//! The cold-build path (design-code-graph.md §4): extract every file, index every symbol name,
|
||||
//! resolve each file's cross-file references against that index, then load the store so its reverse
|
||||
//! index reflects resolved edges. G3's `GraphService` drives this on `spawn_blocking`; keeping it a
|
||||
//! free function here means the same code powers both the warm service and headless tests.
|
||||
|
||||
use super::extract::{extract_file, FileGraph};
|
||||
use super::resolve::{resolve_file, NameIndex};
|
||||
use super::store::{GraphStore, InMemoryGraph};
|
||||
|
||||
/// Build a fully-resolved [`InMemoryGraph`] from `(project_relative_path, source)` pairs.
|
||||
/// Unsupported-language files are skipped. Deterministic for a given input set.
|
||||
pub fn build_graph<I, P, S>(files: I) -> InMemoryGraph
|
||||
where
|
||||
I: IntoIterator<Item = (P, S)>,
|
||||
P: AsRef<str>,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
let fragments: Vec<(String, FileGraph)> = files
|
||||
.into_iter()
|
||||
.filter_map(|(path, src)| {
|
||||
let path = path.as_ref().to_string();
|
||||
extract_file(&path, src.as_ref()).map(|fg| (path, fg))
|
||||
})
|
||||
.collect();
|
||||
assemble(fragments)
|
||||
}
|
||||
|
||||
/// Resolve + load pre-extracted `(path, fragment)` pairs into a graph.
|
||||
///
|
||||
/// The shared back half of every build path: one project-wide [`NameIndex`] over all symbol nodes,
|
||||
/// then per-file resolution and upsert so the reverse index reflects resolved cross-file edges.
|
||||
/// G3's cached build feeds this fragments loaded from disk instead of freshly extracted ones.
|
||||
pub(super) fn assemble(fragments: Vec<(String, FileGraph)>) -> InMemoryGraph {
|
||||
let index = NameIndex::from_nodes(fragments.iter().flat_map(|(_, fg)| fg.nodes.iter()));
|
||||
let mut graph = InMemoryGraph::new();
|
||||
for (path, fg) in fragments {
|
||||
let resolved = resolve_file(fg, &index);
|
||||
graph.upsert_file(&path, resolved.nodes, resolved.edges);
|
||||
}
|
||||
graph
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::model::{EdgeKind, QualifiedName};
|
||||
|
||||
fn files() -> Vec<(&'static str, &'static str)> {
|
||||
vec![
|
||||
("src/lib.rs", "pub fn compute() -> i32 { 1 }\n"),
|
||||
(
|
||||
"src/app.rs",
|
||||
"use crate::lib::compute;\n\
|
||||
fn run() { compute(); }\n\
|
||||
#[cfg(test)]\nmod tests {\n fn test_run() { compute(); }\n}\n",
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_wires_cross_file_calls_into_the_reverse_index() {
|
||||
let graph = build_graph(files());
|
||||
let compute = QualifiedName::from_raw("src/lib.rs::compute");
|
||||
|
||||
// Someone depends on compute: run() calls it, and the reverse index knows.
|
||||
let callers: Vec<&str> = graph
|
||||
.in_edges(&compute)
|
||||
.iter()
|
||||
.filter(|e| e.kind == EdgeKind::Calls)
|
||||
.map(|e| e.from.as_str())
|
||||
.collect();
|
||||
assert!(callers.contains(&"src/app.rs::run"));
|
||||
|
||||
// And a test covers it.
|
||||
assert!(graph
|
||||
.in_edges(&compute)
|
||||
.iter()
|
||||
.any(|e| e.kind == EdgeKind::TestCovers
|
||||
&& e.from.as_str() == "src/app.rs::tests.test_run"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_is_deterministic_regardless_of_file_order() {
|
||||
let mut reversed = files();
|
||||
reversed.reverse();
|
||||
let a = build_graph(files());
|
||||
let b = build_graph(reversed);
|
||||
assert_eq!(a.node_count(), b.node_count());
|
||||
assert_eq!(a.edge_count(), b.edge_count());
|
||||
|
||||
let compute = QualifiedName::from_raw("src/lib.rs::compute");
|
||||
let mut ca: Vec<String> = a.in_edges(&compute).iter().map(|e| e.from.to_string()).collect();
|
||||
let mut cb: Vec<String> = b.in_edges(&compute).iter().map(|e| e.from.to_string()).collect();
|
||||
ca.sort();
|
||||
cb.sort();
|
||||
assert_eq!(ca, cb);
|
||||
}
|
||||
}
|
||||
246
crates/luxe-lsp/src/graph/cache.rs
Normal file
246
crates/luxe-lsp/src/graph/cache.rs
Normal file
@@ -0,0 +1,246 @@
|
||||
//! Cache — content-addressed per-file graph shards under `.luxe/graph/` (design-code-graph.md §5).
|
||||
//!
|
||||
//! Warm-start without re-parsing: each file's *pre-resolution* [`FileGraph`] is written to a shard
|
||||
//! validated by the content hash it came from. On rebuild, a file whose content hash still matches
|
||||
//! its shard is loaded straight from disk (no tree-sitter parse); changed/new files re-extract and
|
||||
//! rewrite their shard. Edge *resolution* is never cached — it's recomputed in memory on load, so a
|
||||
//! dependency's shard never needs rewriting when a resolution target moves. `.luxe/` is gitignored
|
||||
//! and every shard is regenerable, so a corrupt/missing shard just triggers a re-parse.
|
||||
//!
|
||||
//! A shard also records the [`EXTRACTOR_VERSION`] that wrote it, because a matching content hash
|
||||
//! only says the file is unchanged, not that today's extractor would read it the same way. Writes
|
||||
//! are atomic, so a second Luxe in the same project never reads a torn shard.
|
||||
|
||||
use super::extract::FileGraph;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// SHA-256 hex digest of `bytes`.
|
||||
fn hash_hex(bytes: &[u8]) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
base16ct::lower::encode_string(&hasher.finalize())
|
||||
}
|
||||
|
||||
/// Content hash for a file's source — the shard's validity key.
|
||||
pub fn content_hash(content: &str) -> String {
|
||||
hash_hex(content.as_bytes())
|
||||
}
|
||||
|
||||
/// The version of what [`super::extract`] produces. Bump it whenever extraction changes — a new
|
||||
/// symbol kind, an edge rule — so every shard written before is a miss and re-parses once.
|
||||
const EXTRACTOR_VERSION: u32 = 1;
|
||||
|
||||
/// Borrowed shard for writing (avoids cloning the fragment).
|
||||
#[derive(Serialize)]
|
||||
struct ShardOut<'a> {
|
||||
extractor_version: u32,
|
||||
content_hash: &'a str,
|
||||
graph: &'a FileGraph,
|
||||
}
|
||||
|
||||
/// Owned shard for reading. A shard from before versioning reads as version 0, a miss.
|
||||
#[derive(Deserialize)]
|
||||
struct ShardIn {
|
||||
#[serde(default)]
|
||||
extractor_version: u32,
|
||||
content_hash: String,
|
||||
graph: FileGraph,
|
||||
}
|
||||
|
||||
/// The manifest as written: which extractor indexed which file at which content hash.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Manifest {
|
||||
extractor_version: u32,
|
||||
files: HashMap<String, String>,
|
||||
}
|
||||
|
||||
/// On-disk graph cache rooted at `<project>/.luxe/graph/`.
|
||||
pub struct GraphCache {
|
||||
dir: PathBuf,
|
||||
}
|
||||
|
||||
impl GraphCache {
|
||||
/// A cache under `project_root/.luxe/graph`.
|
||||
pub fn new(project_root: &Path) -> Self {
|
||||
Self { dir: project_root.join(".luxe").join("graph") }
|
||||
}
|
||||
|
||||
fn files_dir(&self) -> PathBuf {
|
||||
self.dir.join("files")
|
||||
}
|
||||
|
||||
/// A shard's path — filename is a hash of the project-relative path (stable per file).
|
||||
fn shard_path(&self, rel_path: &str) -> PathBuf {
|
||||
self.files_dir().join(format!("{}.json", hash_hex(rel_path.as_bytes())))
|
||||
}
|
||||
|
||||
fn manifest_path(&self) -> PathBuf {
|
||||
self.dir.join("manifest.json")
|
||||
}
|
||||
|
||||
/// Load a file's cached fragment iff its shard's content hash matches `content_hash` and this
|
||||
/// extractor wrote it — a hit means the file needn't be re-parsed. Any read/parse failure is a
|
||||
/// miss.
|
||||
pub fn load(&self, rel_path: &str, content_hash: &str) -> Option<FileGraph> {
|
||||
let bytes = std::fs::read(self.shard_path(rel_path)).ok()?;
|
||||
let shard: ShardIn = serde_json::from_slice(&bytes).ok()?;
|
||||
(shard.extractor_version == EXTRACTOR_VERSION && shard.content_hash == content_hash)
|
||||
.then_some(shard.graph)
|
||||
}
|
||||
|
||||
/// Write a file's fragment shard (best-effort; a cache write failure is non-fatal).
|
||||
pub fn store(&self, rel_path: &str, content_hash: &str, graph: &FileGraph) {
|
||||
let shard = ShardOut { extractor_version: EXTRACTOR_VERSION, content_hash, graph };
|
||||
write_best_effort(
|
||||
&self.files_dir(),
|
||||
&self.shard_path(rel_path),
|
||||
serde_json::to_vec(&shard),
|
||||
);
|
||||
}
|
||||
|
||||
/// The persisted manifest: project-relative path → last-indexed content hash, empty when a
|
||||
/// different extractor wrote it.
|
||||
pub fn load_manifest(&self) -> HashMap<String, String> {
|
||||
std::fs::read(self.manifest_path())
|
||||
.ok()
|
||||
.and_then(|bytes| serde_json::from_slice::<Manifest>(&bytes).ok())
|
||||
.filter(|manifest| manifest.extractor_version == EXTRACTOR_VERSION)
|
||||
.map(|manifest| manifest.files)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Persist the manifest (best-effort).
|
||||
pub fn store_manifest(&self, manifest: &HashMap<String, String>) {
|
||||
let manifest = Manifest { extractor_version: EXTRACTOR_VERSION, files: manifest.clone() };
|
||||
write_best_effort(&self.dir, &self.manifest_path(), serde_json::to_vec_pretty(&manifest));
|
||||
}
|
||||
|
||||
/// Delete shards for files no longer in `manifest` (renamed/removed files leave orphans).
|
||||
pub fn prune(&self, manifest: &HashMap<String, String>) {
|
||||
let keep: HashSet<String> =
|
||||
manifest.keys().map(|rel| format!("{}.json", hash_hex(rel.as_bytes()))).collect();
|
||||
let Ok(entries) = std::fs::read_dir(self.files_dir()) else { return };
|
||||
for entry in entries.flatten() {
|
||||
let name = entry.file_name();
|
||||
if let Some(name) = name.to_str() {
|
||||
if !keep.contains(name) {
|
||||
let _ = std::fs::remove_file(entry.path());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes `json` to `path`, creating `dir` first: atomically, so a reader never sees half a shard,
|
||||
/// and unsynced, since a cold build writes thousands and a lost one costs only a re-parse. A
|
||||
/// failure is logged and dropped.
|
||||
fn write_best_effort(dir: &Path, path: &Path, json: serde_json::Result<Vec<u8>>) {
|
||||
let written = json
|
||||
.map_err(std::io::Error::other)
|
||||
.and_then(|json| std::fs::create_dir_all(dir).map(|()| json))
|
||||
.and_then(|json| luxe_core::fs_atomic::replace_unsynced(path, &json));
|
||||
if let Err(error) = written {
|
||||
tracing::debug!(%error, path = %path.display(), "graph cache write failed");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::extract::extract_file;
|
||||
|
||||
#[test]
|
||||
fn content_hash_keeps_the_hex_spelling_written_shards_are_keyed_by() {
|
||||
// Literal computed outside this crate. Shards on disk carry this string as their validity
|
||||
// key, so a re-spelled digest turns every warm cache into a silent full re-parse.
|
||||
assert_eq!(
|
||||
content_hash("fn a() { b(); }\nfn b() {}\n"),
|
||||
"f1bcafc4e7c84d96ad4dd5062fe4f5f6854b2edb4ced6ee98f972264ed565399"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shard_round_trips_and_validates_content_hash() {
|
||||
let tmp_guard = tempfile::tempdir().expect("temp dir");
|
||||
let tmp = tmp_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&tmp).unwrap();
|
||||
let cache = GraphCache::new(&tmp);
|
||||
|
||||
let src = "fn a() { b(); }\nfn b() {}\n";
|
||||
let fg = extract_file("src/x.rs", src).unwrap();
|
||||
let hash = content_hash(src);
|
||||
cache.store("src/x.rs", &hash, &fg);
|
||||
|
||||
// Same hash → hit with identical fragment.
|
||||
let loaded = cache.load("src/x.rs", &hash).expect("cache hit");
|
||||
assert_eq!(loaded.nodes, fg.nodes);
|
||||
assert_eq!(loaded.edges, fg.edges);
|
||||
|
||||
// Different hash (edited file) → miss.
|
||||
assert!(cache.load("src/x.rs", &content_hash("fn a() {}\n")).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prune_removes_orphan_shards() {
|
||||
let tmp_guard = tempfile::tempdir().expect("temp dir");
|
||||
let tmp = tmp_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&tmp).unwrap();
|
||||
let cache = GraphCache::new(&tmp);
|
||||
|
||||
let fg = extract_file("a.rs", "fn a() {}\n").unwrap();
|
||||
cache.store("a.rs", &content_hash("fn a() {}\n"), &fg);
|
||||
cache.store("b.rs", &content_hash("fn b() {}\n"), &fg);
|
||||
|
||||
// Keep only a.rs.
|
||||
let manifest: HashMap<String, String> =
|
||||
[("a.rs".to_string(), content_hash("fn a() {}\n"))].into_iter().collect();
|
||||
cache.prune(&manifest);
|
||||
|
||||
assert!(cache.load("a.rs", &content_hash("fn a() {}\n")).is_some());
|
||||
assert!(cache.load("b.rs", &content_hash("fn b() {}\n")).is_none());
|
||||
}
|
||||
|
||||
/// A shard another extractor wrote is a miss even when the file is unchanged, and so is one
|
||||
/// from before shards were versioned.
|
||||
///
|
||||
/// The break this catches: validity was the content hash alone, so after an upgrade changed
|
||||
/// extraction every unchanged file kept serving its old fragment.
|
||||
#[test]
|
||||
fn a_shard_from_another_extractor_misses() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let cache = GraphCache::new(tmp.path());
|
||||
let src = "fn a() {}\n";
|
||||
let (fg, hash) = (extract_file("a.rs", src).unwrap(), content_hash(src));
|
||||
cache.store("a.rs", &hash, &fg);
|
||||
assert!(cache.load("a.rs", &hash).is_some(), "this extractor's shard hits");
|
||||
|
||||
let older = |version: Option<u32>| {
|
||||
let mut shard = serde_json::json!({ "content_hash": hash, "graph": fg });
|
||||
if let Some(version) = version {
|
||||
shard["extractor_version"] = version.into();
|
||||
}
|
||||
std::fs::write(cache.shard_path("a.rs"), shard.to_string()).unwrap();
|
||||
};
|
||||
older(Some(EXTRACTOR_VERSION - 1));
|
||||
assert!(cache.load("a.rs", &hash).is_none(), "an older extractor's shard misses");
|
||||
older(None);
|
||||
assert!(cache.load("a.rs", &hash).is_none(), "an unversioned shard misses");
|
||||
older(Some(EXTRACTOR_VERSION + 1));
|
||||
assert!(cache.load("a.rs", &hash).is_none(), "and so does a newer extractor's");
|
||||
}
|
||||
|
||||
/// The manifest says which extractor wrote it, and one from another is no manifest at all.
|
||||
#[test]
|
||||
fn a_manifest_from_another_extractor_reads_as_empty() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let cache = GraphCache::new(tmp.path());
|
||||
let files: HashMap<String, String> = [("a.rs".into(), "h".into())].into_iter().collect();
|
||||
cache.store_manifest(&files);
|
||||
assert_eq!(cache.load_manifest(), files);
|
||||
std::fs::write(cache.manifest_path(), r#"{"a.rs":"h"}"#).unwrap();
|
||||
assert!(cache.load_manifest().is_empty(), "the unversioned shape");
|
||||
}
|
||||
}
|
||||
187
crates/luxe-lsp/src/graph/enrich.rs
Normal file
187
crates/luxe-lsp/src/graph/enrich.rs
Normal file
@@ -0,0 +1,187 @@
|
||||
//! Enrichment — verify heuristic graph edges against the LSP (design-code-graph.md §6, §8.2).
|
||||
//!
|
||||
//! Tree-sitter `Calls` edges are name-based heuristics; for the review-critical symbols in a diff
|
||||
//! we want precision. When a language server is warm, [`verify_callers`] reconciles the graph's
|
||||
//! heuristic callers of a symbol with the server's exact `textDocument/references`, labeling each
|
||||
//! caller file as graph-known, LSP-confirmed, or both. It never fabricates: if no server answers,
|
||||
//! it returns `None` and callers fall back to the heuristic graph.
|
||||
|
||||
use super::model::{EdgeKind, QualifiedName};
|
||||
use super::store::{GraphStore, InMemoryGraph};
|
||||
use crate::{LspClient, Position, WorkspaceLspClient};
|
||||
use async_trait::async_trait;
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// A source of precise "who references this symbol" answers — the LSP in production, mockable in
|
||||
/// tests. Returns `None` when no server can answer (so enrichment is skipped, not read as "no
|
||||
/// callers").
|
||||
#[async_trait]
|
||||
pub trait ReferenceSource: Send + Sync {
|
||||
/// Project-relative files that reference `symbol` (defined in project-relative `file`).
|
||||
async fn referencing_files(&self, file: &str, symbol: &str) -> Option<Vec<String>>;
|
||||
}
|
||||
|
||||
/// One caller file's provenance after reconciling the graph with the LSP.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CallerVerification {
|
||||
/// Project-relative caller file.
|
||||
pub file: String,
|
||||
/// The heuristic graph had this caller.
|
||||
pub in_graph: bool,
|
||||
/// The LSP confirmed this caller.
|
||||
pub in_lsp: bool,
|
||||
}
|
||||
|
||||
impl CallerVerification {
|
||||
/// Confirmed by the LSP — a precise, trustworthy caller.
|
||||
pub fn verified(&self) -> bool {
|
||||
self.in_lsp
|
||||
}
|
||||
/// Found by the LSP but missing from the heuristic graph (a false negative it recovers).
|
||||
pub fn lsp_only(&self) -> bool {
|
||||
self.in_lsp && !self.in_graph
|
||||
}
|
||||
/// In the heuristic graph but not confirmed by the LSP (a possible false positive — kept, not
|
||||
/// dropped, since references can be incomplete for macros/dynamic dispatch).
|
||||
pub fn unconfirmed(&self) -> bool {
|
||||
self.in_graph && !self.in_lsp
|
||||
}
|
||||
}
|
||||
|
||||
/// Reconcile the graph's heuristic callers of `seed` with the LSP's exact references.
|
||||
///
|
||||
/// Returns the union of caller files, each labeled with its provenance, or `None` when no server
|
||||
/// answered (callers should then trust the heuristic graph unchanged).
|
||||
pub async fn verify_callers(
|
||||
graph: &InMemoryGraph,
|
||||
seed: &QualifiedName,
|
||||
source: &dyn ReferenceSource,
|
||||
) -> Option<Vec<CallerVerification>> {
|
||||
let lsp_files = source.referencing_files(seed.file_path(), seed.base_name()).await?;
|
||||
let lsp: HashSet<String> = lsp_files.into_iter().collect();
|
||||
|
||||
let graph_files: HashSet<String> = graph
|
||||
.in_edges(seed)
|
||||
.iter()
|
||||
.filter(|edge| matches!(edge.kind, EdgeKind::Calls | EdgeKind::TestCovers))
|
||||
.map(|edge| edge.from.file_path().to_string())
|
||||
.collect();
|
||||
|
||||
let mut files: Vec<String> = lsp.union(&graph_files).cloned().collect();
|
||||
files.sort();
|
||||
Some(
|
||||
files
|
||||
.into_iter()
|
||||
.map(|file| CallerVerification {
|
||||
in_graph: graph_files.contains(&file),
|
||||
in_lsp: lsp.contains(&file),
|
||||
file,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// An LSP-backed [`ReferenceSource`] over the warm workspace client.
|
||||
///
|
||||
/// Resolves the symbol's name position offline (tree-sitter), then asks the server for references
|
||||
/// and maps them back to project-relative files. Degrades to `None` whenever any step is
|
||||
/// unavailable (no server, file unreadable, symbol not found).
|
||||
pub struct LspReferenceSource {
|
||||
service: Arc<WorkspaceLspClient>,
|
||||
root: PathBuf,
|
||||
}
|
||||
|
||||
impl LspReferenceSource {
|
||||
/// Create a source over the shared warm service, resolving paths against `root`.
|
||||
pub fn new(service: Arc<WorkspaceLspClient>, root: impl Into<PathBuf>) -> Self {
|
||||
Self { service, root: root.into() }
|
||||
}
|
||||
|
||||
/// Map a `file://` reference URI back to a project-relative path.
|
||||
fn relativize(&self, uri: &str) -> Option<String> {
|
||||
let path = crate::uri::to_path(uri);
|
||||
path.strip_prefix(&self.root).ok().map(luxe_core::paths::display)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ReferenceSource for LspReferenceSource {
|
||||
async fn referencing_files(&self, file: &str, symbol: &str) -> Option<Vec<String>> {
|
||||
let full = self.root.join(file);
|
||||
let source = std::fs::read_to_string(&full).ok()?;
|
||||
// The symbol's name position comes from the offline outline (no server needed).
|
||||
let outline = crate::syntax::outline(&full, &source)?;
|
||||
let target = outline.iter().find(|s| s.name == symbol)?;
|
||||
let position = Position { line: target.line, character: target.character };
|
||||
|
||||
let client: Arc<Mutex<LspClient>> = { self.service.client_for_path(&full).await }?;
|
||||
let uri = crate::uri::from_path(&full);
|
||||
let mut guard = client.lock().await;
|
||||
let _ = guard.open_document(&uri, &source).await;
|
||||
let locations = guard.find_references(&uri, position).await.ok()?;
|
||||
drop(guard);
|
||||
|
||||
let files: HashSet<String> =
|
||||
locations.iter().filter_map(|loc| self.relativize(&loc.uri)).collect();
|
||||
Some(files.into_iter().collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::build::build_graph;
|
||||
|
||||
/// A canned reference source: `Some(files)` for a warm server, `None` for none.
|
||||
struct MockRefs(Option<Vec<String>>);
|
||||
|
||||
#[async_trait]
|
||||
impl ReferenceSource for MockRefs {
|
||||
async fn referencing_files(&self, _file: &str, _symbol: &str) -> Option<Vec<String>> {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn graph() -> InMemoryGraph {
|
||||
build_graph(vec![
|
||||
("src/auth.rs", "pub fn login() {}\n"),
|
||||
("src/session.rs", "use crate::auth::login;\npub fn start() { login() }\n"),
|
||||
])
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn verify_merges_graph_and_lsp_callers() {
|
||||
let graph = graph();
|
||||
let seed = QualifiedName::from_raw("src/auth.rs::login");
|
||||
// The LSP sees the graph's caller (session.rs) plus one the heuristic missed (api.rs).
|
||||
let source = MockRefs(Some(vec!["src/session.rs".into(), "src/api.rs".into()]));
|
||||
let result = verify_callers(&graph, &seed, &source).await.expect("server answered");
|
||||
|
||||
let session = result.iter().find(|c| c.file == "src/session.rs").unwrap();
|
||||
assert!(session.in_graph && session.verified(), "confirmed caller");
|
||||
|
||||
let api = result.iter().find(|c| c.file == "src/api.rs").unwrap();
|
||||
assert!(api.lsp_only(), "LSP recovered a false-negative caller");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unconfirmed_heuristic_caller_is_kept_not_dropped() {
|
||||
let graph = graph();
|
||||
let seed = QualifiedName::from_raw("src/auth.rs::login");
|
||||
// LSP returns no references for login (e.g. reached only via a macro) → keep the heuristic.
|
||||
let source = MockRefs(Some(vec![]));
|
||||
let result = verify_callers(&graph, &seed, &source).await.unwrap();
|
||||
let session = result.iter().find(|c| c.file == "src/session.rs").unwrap();
|
||||
assert!(session.unconfirmed(), "graph caller retained though LSP didn't confirm");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_server_yields_none() {
|
||||
let graph = graph();
|
||||
let seed = QualifiedName::from_raw("src/auth.rs::login");
|
||||
assert!(verify_callers(&graph, &seed, &MockRefs(None)).await.is_none());
|
||||
}
|
||||
}
|
||||
389
crates/luxe-lsp/src/graph/extract.rs
Normal file
389
crates/luxe-lsp/src/graph/extract.rs
Normal file
@@ -0,0 +1,389 @@
|
||||
//! Extraction — one file's source becomes nodes + edges (design-code-graph.md §4).
|
||||
//!
|
||||
//! Nodes and `Defines` edges are lifted from the tested [`crate::syntax::outline`] symbol layer;
|
||||
//! `Imports`, `Inherits`/`Implements`, and `Calls` come from small per-language tree-sitter queries
|
||||
//! here. This is the exact-to-heuristic structural layer: `Defines`/`Imports`/heritage are exact,
|
||||
//! `Calls` is a name-based heuristic (§6). Same-file references resolve eagerly; everything
|
||||
//! cross-file stays [`EdgeTarget::Unresolved`] for `resolve.rs` (G2) — we never invent an edge.
|
||||
|
||||
use super::model::{Edge, EdgeKind, EdgeTarget, Node, NodeKind, Precision, QualifiedName};
|
||||
use crate::protocol::symbol_kinds as k;
|
||||
use crate::syntax::{self, Lang};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::Path;
|
||||
use std::sync::OnceLock;
|
||||
use streaming_iterator::StreamingIterator;
|
||||
use tree_sitter::{Parser, Query, QueryCursor, Tree};
|
||||
|
||||
/// One file's extracted graph fragment. Serializable so it can be cached as a per-file shard (G3).
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct FileGraph {
|
||||
pub nodes: Vec<Node>,
|
||||
pub edges: Vec<Edge>,
|
||||
}
|
||||
|
||||
/// Extract nodes + edges for a single file.
|
||||
///
|
||||
/// `rel_path` is the project-relative path — it seeds every qualified name and selects the
|
||||
/// language. Returns `None` for an unsupported language. Output is sorted for determinism. Parses
|
||||
/// once: the node layer (`syntax::outline_from_tree`) and the reference queries share the tree.
|
||||
pub fn extract_file(rel_path: &str, source: &str) -> Option<FileGraph> {
|
||||
let lang = syntax::detect(Path::new(rel_path))?;
|
||||
let language = syntax::ts_language(lang);
|
||||
let mut parser = Parser::new();
|
||||
parser.set_language(&language).ok()?;
|
||||
let tree = parser.parse(source, None)?;
|
||||
let bytes = source.as_bytes();
|
||||
let symbols = syntax::outline_from_tree(lang, &tree, bytes);
|
||||
|
||||
let file_qn = QualifiedName::file(rel_path);
|
||||
let mut nodes = Vec::with_capacity(symbols.len() + 1);
|
||||
nodes.push(Node::file(rel_path));
|
||||
|
||||
// Simple name → qualified name for top-level (unnested) symbols; used to attach members to
|
||||
// their container and to resolve same-file heritage. Function/method ranges map a call site's
|
||||
// line back to its enclosing definition.
|
||||
let mut top_level: HashMap<String, QualifiedName> = HashMap::new();
|
||||
let mut func_ranges: Vec<(u32, u32, QualifiedName)> = Vec::new();
|
||||
let mut edges: Vec<Edge> = Vec::new();
|
||||
|
||||
for sym in &symbols {
|
||||
let qn = QualifiedName::symbol(rel_path, sym.container.as_deref(), &sym.name);
|
||||
let is_test =
|
||||
super::testing::is_test_symbol(sym.kind, &sym.name, sym.container.as_deref(), rel_path);
|
||||
nodes.push(Node {
|
||||
name: qn.clone(),
|
||||
kind: NodeKind::from_symbol_kind(sym.kind),
|
||||
start_line: sym.start_line,
|
||||
end_line: sym.end_line,
|
||||
is_test,
|
||||
});
|
||||
if sym.container.is_none() {
|
||||
top_level.insert(sym.name.clone(), qn.clone());
|
||||
}
|
||||
if sym.kind == k::FUNCTION || sym.kind == k::METHOD {
|
||||
func_ranges.push((sym.start_line, sym.end_line, qn));
|
||||
}
|
||||
}
|
||||
|
||||
// Defines edges: File → top-level symbol; container → member (falling back to File when the
|
||||
// container symbol itself wasn't captured).
|
||||
for sym in &symbols {
|
||||
let qn = QualifiedName::symbol(rel_path, sym.container.as_deref(), &sym.name);
|
||||
let parent = match &sym.container {
|
||||
None => file_qn.clone(),
|
||||
Some(c) => top_level.get(c).cloned().unwrap_or_else(|| file_qn.clone()),
|
||||
};
|
||||
edges.push(Edge::defines(parent, qn));
|
||||
}
|
||||
|
||||
// The reference queries (imports / heritage / calls) run against the same parse.
|
||||
let mut seen_imports: HashSet<String> = HashSet::new();
|
||||
for module in extract_imports(lang, &tree, bytes) {
|
||||
if seen_imports.insert(module.clone()) {
|
||||
edges.push(Edge {
|
||||
from: file_qn.clone(),
|
||||
to: EdgeTarget::Unresolved(module),
|
||||
kind: EdgeKind::Imports,
|
||||
precision: Precision::Exact,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (type_name, base_name, kind) in extract_heritage(lang, &tree, bytes) {
|
||||
let from = top_level
|
||||
.get(&type_name)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| QualifiedName::symbol(rel_path, None, &type_name));
|
||||
let to = match top_level.get(&base_name) {
|
||||
Some(qn) => EdgeTarget::Resolved(qn.clone()),
|
||||
None => EdgeTarget::Unresolved(base_name),
|
||||
};
|
||||
edges.push(Edge { from, to, kind, precision: Precision::Exact });
|
||||
}
|
||||
|
||||
let aliases = alias_map(lang, &tree, bytes);
|
||||
for (callee, row) in call_sites(lang, &tree, bytes) {
|
||||
// Resolve a locally-renamed import back to the name the rest of the graph knows.
|
||||
let callee = aliases.get(&callee).cloned().unwrap_or(callee);
|
||||
if let Some(from) = enclosing_function(row, &func_ranges) {
|
||||
edges.push(Edge {
|
||||
from,
|
||||
to: EdgeTarget::Unresolved(callee),
|
||||
kind: EdgeKind::Calls,
|
||||
precision: Precision::Heuristic,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
nodes.sort_by(|a, b| (a.start_line, a.name.as_str()).cmp(&(b.start_line, b.name.as_str())));
|
||||
edges.sort_by(|a, b| a.sort_key().cmp(&b.sort_key()));
|
||||
edges.dedup();
|
||||
|
||||
Some(FileGraph { nodes, edges })
|
||||
}
|
||||
|
||||
/// Innermost function/method whose line range contains `row` — a call site's enclosing definition.
|
||||
fn enclosing_function(row: u32, ranges: &[(u32, u32, QualifiedName)]) -> Option<QualifiedName> {
|
||||
ranges
|
||||
.iter()
|
||||
.filter(|(start, end, _)| *start <= row && row <= *end)
|
||||
.min_by_key(|(start, end, _)| end - start)
|
||||
.map(|(_, _, qn)| qn.clone())
|
||||
}
|
||||
|
||||
// === per-language queries ===
|
||||
|
||||
/// Import query per language; the captured node is named `mod`.
|
||||
fn import_query(lang: Lang) -> &'static str {
|
||||
match lang {
|
||||
Lang::Rust => "(use_declaration argument: (_) @mod)",
|
||||
Lang::Python => {
|
||||
"(import_statement name: (dotted_name) @mod)
|
||||
(import_statement name: (aliased_import name: (dotted_name) @mod))
|
||||
(import_from_statement module_name: (dotted_name) @mod)"
|
||||
}
|
||||
Lang::TypeScript | Lang::Tsx | Lang::JavaScript => {
|
||||
"(import_statement source: (string (string_fragment) @mod))"
|
||||
}
|
||||
Lang::Go => {
|
||||
"(import_spec path: (interpreted_string_literal (interpreted_string_literal_content) @mod))"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Heritage query per language, plus which base-capture maps to which edge kind.
|
||||
fn heritage_spec(lang: Lang) -> Option<(&'static str, &'static [(&'static str, EdgeKind)])> {
|
||||
match lang {
|
||||
Lang::Rust => Some((
|
||||
"(impl_item trait: (type_identifier) @implements type: (type_identifier) @type)",
|
||||
&[("implements", EdgeKind::Implements)],
|
||||
)),
|
||||
Lang::Python => Some((
|
||||
"(class_definition name: (identifier) @type superclasses: (argument_list (identifier) @inherits))",
|
||||
&[("inherits", EdgeKind::Inherits)],
|
||||
)),
|
||||
Lang::TypeScript | Lang::Tsx => Some((
|
||||
"(class_declaration name: (type_identifier) @type (class_heritage (extends_clause value: (identifier) @inherits)))
|
||||
(class_declaration name: (type_identifier) @type (class_heritage (implements_clause (type_identifier) @implements)))",
|
||||
&[("inherits", EdgeKind::Inherits), ("implements", EdgeKind::Implements)],
|
||||
)),
|
||||
Lang::JavaScript => Some((
|
||||
"(class_declaration name: (identifier) @type (class_heritage (identifier) @inherits))",
|
||||
&[("inherits", EdgeKind::Inherits)],
|
||||
)),
|
||||
Lang::Go => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Call-site query per language; the callee identifier is named `callee`. Covers bare calls,
|
||||
/// method/attribute calls, and (Rust/Go) scoped/selector calls.
|
||||
///
|
||||
/// Rust also reaches inside macros. A macro's arguments parse as an unstructured `token_tree`, so
|
||||
/// none of the `call_expression` patterns can see them — and in Rust the calls that prove a test
|
||||
/// exercises something live almost entirely inside `assert!`/`assert_eq!`. Skipping them would
|
||||
/// derive no `TestCovers` edge for the one call that matters and label a covered function untested.
|
||||
/// Inside a token tree an identifier immediately followed by another token tree *is* the call
|
||||
/// syntax; anything it over-matches (`vec![..]`, an index, a struct literal) resolves to nothing in
|
||||
/// the project index and is dropped as external.
|
||||
fn call_query(lang: Lang) -> &'static str {
|
||||
match lang {
|
||||
Lang::Rust => {
|
||||
"(call_expression function: (identifier) @callee)
|
||||
(call_expression function: (scoped_identifier name: (identifier) @callee))
|
||||
(call_expression function: (field_expression field: (field_identifier) @callee))
|
||||
(token_tree (identifier) @callee . (token_tree))"
|
||||
}
|
||||
Lang::Python => {
|
||||
"(call function: (identifier) @callee)
|
||||
(call function: (attribute attribute: (identifier) @callee))"
|
||||
}
|
||||
Lang::TypeScript | Lang::Tsx | Lang::JavaScript => {
|
||||
"(call_expression function: (identifier) @callee)
|
||||
(call_expression function: (member_expression property: (property_identifier) @callee))"
|
||||
}
|
||||
Lang::Go => {
|
||||
"(call_expression function: (identifier) @callee)
|
||||
(call_expression function: (selector_expression field: (field_identifier) @callee))"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// === query runner ===
|
||||
|
||||
/// One captured node: its text and 0-based start row.
|
||||
struct Cap {
|
||||
text: String,
|
||||
row: u32,
|
||||
}
|
||||
|
||||
/// A language's compiled reference queries, built once and reused for every file.
|
||||
///
|
||||
/// Compiling a tree-sitter `Query` is the dominant cost of a cold build (a state-machine
|
||||
/// compilation, far pricier than parsing a file). Recompiling per file made a whole-repo build
|
||||
/// seconds slower; caching per language keeps it well under the budget.
|
||||
struct LangQueries {
|
||||
import: Option<Query>,
|
||||
heritage: Option<Query>,
|
||||
calls: Option<Query>,
|
||||
aliases: Option<Query>,
|
||||
}
|
||||
|
||||
fn compile_queries(lang: Lang) -> LangQueries {
|
||||
let language = syntax::ts_language(lang);
|
||||
LangQueries {
|
||||
import: Query::new(&language, import_query(lang)).ok(),
|
||||
heritage: heritage_spec(lang).and_then(|(src, _)| Query::new(&language, src).ok()),
|
||||
calls: Query::new(&language, call_query(lang)).ok(),
|
||||
aliases: alias_query(lang).and_then(|src| Query::new(&language, src).ok()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cached compiled queries for `lang` — compiled on first use, then shared process-wide.
|
||||
fn lang_queries(lang: Lang) -> &'static LangQueries {
|
||||
match lang {
|
||||
Lang::Rust => {
|
||||
static Q: OnceLock<LangQueries> = OnceLock::new();
|
||||
Q.get_or_init(|| compile_queries(Lang::Rust))
|
||||
}
|
||||
Lang::Python => {
|
||||
static Q: OnceLock<LangQueries> = OnceLock::new();
|
||||
Q.get_or_init(|| compile_queries(Lang::Python))
|
||||
}
|
||||
Lang::TypeScript => {
|
||||
static Q: OnceLock<LangQueries> = OnceLock::new();
|
||||
Q.get_or_init(|| compile_queries(Lang::TypeScript))
|
||||
}
|
||||
Lang::Tsx => {
|
||||
static Q: OnceLock<LangQueries> = OnceLock::new();
|
||||
Q.get_or_init(|| compile_queries(Lang::Tsx))
|
||||
}
|
||||
Lang::JavaScript => {
|
||||
static Q: OnceLock<LangQueries> = OnceLock::new();
|
||||
Q.get_or_init(|| compile_queries(Lang::JavaScript))
|
||||
}
|
||||
Lang::Go => {
|
||||
static Q: OnceLock<LangQueries> = OnceLock::new();
|
||||
Q.get_or_init(|| compile_queries(Lang::Go))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run a compiled query over the parsed tree; return, per match, a map from wanted capture name →
|
||||
/// capture.
|
||||
fn collect_matches(
|
||||
query: &Query,
|
||||
tree: &Tree,
|
||||
bytes: &[u8],
|
||||
wanted: &[&str],
|
||||
) -> Vec<HashMap<String, Cap>> {
|
||||
let indices: Vec<(String, u32)> = wanted
|
||||
.iter()
|
||||
.filter_map(|name| query.capture_index_for_name(name).map(|i| ((*name).to_string(), i)))
|
||||
.collect();
|
||||
|
||||
let mut cursor = QueryCursor::new();
|
||||
let mut matches = cursor.matches(query, tree.root_node(), bytes);
|
||||
let mut out = Vec::new();
|
||||
while let Some(m) = matches.next() {
|
||||
let mut fields = HashMap::new();
|
||||
for (name, index) in &indices {
|
||||
if let Some(cap) = m.captures.iter().find(|c| c.index == *index) {
|
||||
if let Ok(text) = cap.node.utf8_text(bytes) {
|
||||
fields.insert(
|
||||
name.clone(),
|
||||
Cap { text: text.to_string(), row: cap.node.start_position().row as u32 },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if !fields.is_empty() {
|
||||
out.push(fields);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Normalized, deduplicatable module reference strings.
|
||||
fn extract_imports(lang: Lang, tree: &Tree, bytes: &[u8]) -> Vec<String> {
|
||||
let Some(query) = &lang_queries(lang).import else { return Vec::new() };
|
||||
collect_matches(query, tree, bytes, &["mod"])
|
||||
.into_iter()
|
||||
.filter_map(|mut m| m.remove("mod"))
|
||||
.map(|c| normalize_ws(&c.text))
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `(type_name, base_name, kind)` triples for each inheritance/implementation relationship.
|
||||
fn extract_heritage(lang: Lang, tree: &Tree, bytes: &[u8]) -> Vec<(String, String, EdgeKind)> {
|
||||
let Some((_, bases)) = heritage_spec(lang) else { return Vec::new() };
|
||||
let Some(query) = &lang_queries(lang).heritage else { return Vec::new() };
|
||||
let wanted: Vec<&str> = std::iter::once("type").chain(bases.iter().map(|(n, _)| *n)).collect();
|
||||
|
||||
let mut out = Vec::new();
|
||||
for m in collect_matches(query, tree, bytes, &wanted) {
|
||||
let Some(type_cap) = m.get("type") else { continue };
|
||||
for (cap, kind) in bases {
|
||||
if let Some(base) = m.get(*cap) {
|
||||
out.push((type_cap.text.clone(), base.text.clone(), *kind));
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// `(callee_name, row)` for each call site.
|
||||
fn call_sites(lang: Lang, tree: &Tree, bytes: &[u8]) -> Vec<(String, u32)> {
|
||||
let Some(query) = &lang_queries(lang).calls else { return Vec::new() };
|
||||
collect_matches(query, tree, bytes, &["callee"])
|
||||
.into_iter()
|
||||
.filter_map(|mut m| m.remove("callee"))
|
||||
.map(|c| (c.text, c.row))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Locally-renamed imports: `alias -> original name`.
|
||||
///
|
||||
/// Call edges are matched by name, so a renamed import silently breaks the link — a file doing
|
||||
/// `from x import calculate_credits as _calc` and then calling `_calc(...)` recorded a call to
|
||||
/// `_calc`, which resolves to nothing, and the symbol's blast radius came back one caller short.
|
||||
/// Under-reporting a blast radius is the one failure this tool must not have.
|
||||
fn alias_map(lang: Lang, tree: &Tree, bytes: &[u8]) -> std::collections::HashMap<String, String> {
|
||||
let Some(query) = &lang_queries(lang).aliases else { return Default::default() };
|
||||
collect_matches(query, tree, bytes, &["orig", "alias"])
|
||||
.into_iter()
|
||||
.filter_map(|mut m| {
|
||||
let orig = m.remove("orig")?.text;
|
||||
let alias = m.remove("alias")?.text;
|
||||
// Only the final segment matters: calls are matched on the bare name.
|
||||
let orig = orig.rsplit('.').next()?.rsplit("::").next()?.to_string();
|
||||
(orig != alias).then_some((alias, orig))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Renamed-import query per language, capturing `@orig` (the real name) and `@alias`.
|
||||
fn alias_query(lang: Lang) -> Option<&'static str> {
|
||||
match lang {
|
||||
Lang::Python => Some(
|
||||
"(import_from_statement name: (aliased_import name: (dotted_name) @orig alias: (identifier) @alias))
|
||||
(import_statement name: (aliased_import name: (dotted_name) @orig alias: (identifier) @alias))",
|
||||
),
|
||||
Lang::Rust => Some("(use_as_clause path: (_) @orig alias: (identifier) @alias)"),
|
||||
Lang::TypeScript | Lang::Tsx | Lang::JavaScript => Some(
|
||||
"(import_specifier name: (identifier) @orig alias: (identifier) @alias)",
|
||||
),
|
||||
Lang::Go => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Collapse internal whitespace runs to single spaces and trim — so an import reference is stable
|
||||
/// regardless of the source's formatting.
|
||||
fn normalize_ws(text: &str) -> String {
|
||||
text.split_whitespace().collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
217
crates/luxe-lsp/src/graph/extract/tests.rs
Normal file
217
crates/luxe-lsp/src/graph/extract/tests.rs
Normal file
@@ -0,0 +1,217 @@
|
||||
use super::*;
|
||||
|
||||
fn extract(path: &str, source: &str) -> FileGraph {
|
||||
extract_file(path, source).expect("supported language")
|
||||
}
|
||||
|
||||
/// Edges of `kind`, as `(from, target-string, resolved?)` — stable, comparable tuples.
|
||||
fn edges_of(fg: &FileGraph, kind: EdgeKind) -> Vec<(String, String, bool)> {
|
||||
fg.edges
|
||||
.iter()
|
||||
.filter(|e| e.kind == kind)
|
||||
.map(|e| {
|
||||
(e.from.as_str().to_string(), e.to.as_str().to_string(), e.to.resolved().is_some())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn node_names(fg: &FileGraph) -> Vec<String> {
|
||||
fg.nodes.iter().map(|n| n.name.as_str().to_string()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_nodes_defines_imports_and_impls() {
|
||||
let src = "use crate::auth::AuthService;\n\
|
||||
pub struct Point { x: i32 }\n\
|
||||
impl Point { pub fn origin() -> Self { Point { x: 0 } } }\n\
|
||||
impl Display for Point { fn fmt(&self) {} }\n";
|
||||
let fg = extract("src/geo.rs", src);
|
||||
|
||||
// File node + symbols present.
|
||||
assert!(node_names(&fg).contains(&"src/geo.rs".to_string()));
|
||||
assert!(node_names(&fg).contains(&"src/geo.rs::Point".to_string()));
|
||||
assert!(node_names(&fg).contains(&"src/geo.rs::Point.origin".to_string()));
|
||||
assert!(node_names(&fg).contains(&"src/geo.rs::Point.x".to_string()));
|
||||
|
||||
// Defines: File → Point (top-level), Point → origin / x (members).
|
||||
let defines = edges_of(&fg, EdgeKind::Defines);
|
||||
assert!(defines.contains(&("src/geo.rs".into(), "src/geo.rs::Point".into(), true)));
|
||||
assert!(defines.contains(&(
|
||||
"src/geo.rs::Point".into(),
|
||||
"src/geo.rs::Point.origin".into(),
|
||||
true
|
||||
)));
|
||||
assert!(defines.contains(&("src/geo.rs::Point".into(), "src/geo.rs::Point.x".into(), true)));
|
||||
|
||||
// Imports: unresolved raw module reference.
|
||||
let imports = edges_of(&fg, EdgeKind::Imports);
|
||||
assert_eq!(imports, vec![("src/geo.rs".into(), "crate::auth::AuthService".into(), false)]);
|
||||
|
||||
// Implements: Point implements Display — Display isn't defined here, so unresolved.
|
||||
let implements = edges_of(&fg, EdgeKind::Implements);
|
||||
assert_eq!(implements, vec![("src/geo.rs::Point".into(), "Display".into(), false)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_same_file_impl_target_resolves() {
|
||||
let src = "trait Greet {}\nstruct Bot;\nimpl Greet for Bot {}\n";
|
||||
let fg = extract("src/a.rs", src);
|
||||
let implements = edges_of(&fg, EdgeKind::Implements);
|
||||
// Greet is defined in this file → the edge resolves to its node.
|
||||
assert_eq!(implements, vec![("src/a.rs::Bot".into(), "src/a.rs::Greet".into(), true)]);
|
||||
}
|
||||
|
||||
/// In Rust the calls that prove a test exercises something are almost all inside an assertion
|
||||
/// macro, whose arguments parse as an opaque token tree. Missing them meant a tested function
|
||||
/// had no `TestCovers` edge and the review told the reviewer it was untested.
|
||||
#[test]
|
||||
fn rust_calls_inside_macros_are_attributed_like_any_other() {
|
||||
let src = "fn t() {\n app.tick_spinner();\n assert_eq!(app.spinner_char(), 'x');\n \
|
||||
assert!(helper(&build_line(&app, 80)).contains(\"y\"));\n}\n";
|
||||
let fg = extract("src/c.rs", src);
|
||||
let calls = edges_of(&fg, EdgeKind::Calls);
|
||||
for callee in ["tick_spinner", "spinner_char", "helper", "build_line", "contains"] {
|
||||
assert!(
|
||||
calls.contains(&("src/c.rs::t".into(), callee.into(), false)),
|
||||
"{callee} is a call site: {calls:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_calls_attributed_to_enclosing_function_unresolved() {
|
||||
let src = "fn helper() {}\nfn caller() { helper(); missing(); }\n";
|
||||
let fg = extract("src/c.rs", src);
|
||||
let calls = edges_of(&fg, EdgeKind::Calls);
|
||||
// Both calls attributed to `caller`; unresolved at extract time (resolve.rs resolves).
|
||||
assert!(calls.contains(&("src/c.rs::caller".into(), "helper".into(), false)));
|
||||
assert!(calls.contains(&("src/c.rs::caller".into(), "missing".into(), false)));
|
||||
// A call site with no enclosing function is dropped (nothing to attribute it to).
|
||||
assert!(calls.iter().all(|(from, _, _)| from == "src/c.rs::caller"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn python_inherits_and_imports() {
|
||||
let src = "import os\n\
|
||||
from a.b import c\n\
|
||||
class Base: pass\n\
|
||||
class Widget(Base, Mixin):\n def render(self):\n return 1\n";
|
||||
let fg = extract("app.py", src);
|
||||
|
||||
let imports: Vec<String> = edges_of(&fg, EdgeKind::Imports).into_iter().map(|e| e.1).collect();
|
||||
assert!(imports.contains(&"os".to_string()));
|
||||
assert!(imports.contains(&"a.b".to_string()));
|
||||
|
||||
let inherits = edges_of(&fg, EdgeKind::Inherits);
|
||||
// Base is defined here (resolved); Mixin is not (unresolved).
|
||||
assert!(inherits.contains(&("app.py::Widget".into(), "app.py::Base".into(), true)));
|
||||
assert!(inherits.contains(&("app.py::Widget".into(), "Mixin".into(), false)));
|
||||
|
||||
// A method inside a class is defined by that class, and Defines wires File → class.
|
||||
let defines = edges_of(&fg, EdgeKind::Defines);
|
||||
assert!(defines.contains(&("app.py".into(), "app.py::Widget".into(), true)));
|
||||
assert!(defines.contains(&("app.py::Widget".into(), "app.py::Widget.render".into(), true)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typescript_extends_implements_and_imports() {
|
||||
let src = "import { A } from \"./mod\";\n\
|
||||
interface Named { name: string; }\n\
|
||||
class Service extends Base implements Named, Other {\n fetch(): number { return 1; }\n}\n";
|
||||
let fg = extract("svc.ts", src);
|
||||
|
||||
let imports: Vec<String> = edges_of(&fg, EdgeKind::Imports).into_iter().map(|e| e.1).collect();
|
||||
assert_eq!(imports, vec!["./mod".to_string()]);
|
||||
|
||||
let inherits = edges_of(&fg, EdgeKind::Inherits);
|
||||
assert_eq!(inherits, vec![("svc.ts::Service".into(), "Base".into(), false)]);
|
||||
|
||||
let implements = edges_of(&fg, EdgeKind::Implements);
|
||||
// Named is defined here (interface) → resolved; Other is not.
|
||||
assert!(implements.contains(&("svc.ts::Service".into(), "svc.ts::Named".into(), true)));
|
||||
assert!(implements.contains(&("svc.ts::Service".into(), "Other".into(), false)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn javascript_extends_and_imports() {
|
||||
let src =
|
||||
"import { A } from \"./mod\";\nclass Service extends Base { fetch() { return 1; } }\n";
|
||||
let fg = extract("svc.js", src);
|
||||
assert_eq!(edges_of(&fg, EdgeKind::Imports), vec![("svc.js".into(), "./mod".into(), false)]);
|
||||
assert_eq!(
|
||||
edges_of(&fg, EdgeKind::Inherits),
|
||||
vec![("svc.js::Service".into(), "Base".into(), false)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn go_imports_and_calls_extracted() {
|
||||
let src = "package main\nimport (\n \"fmt\"\n m \"example.com/mod\"\n)\n\
|
||||
func Add(a, b int) int { return a + b }\n\
|
||||
func Run() { Add(1, 2) }\n";
|
||||
let fg = extract("main.go", src);
|
||||
let imports: Vec<String> = edges_of(&fg, EdgeKind::Imports).into_iter().map(|e| e.1).collect();
|
||||
assert!(imports.contains(&"fmt".to_string()));
|
||||
assert!(imports.contains(&"example.com/mod".to_string()));
|
||||
|
||||
let calls = edges_of(&fg, EdgeKind::Calls);
|
||||
assert!(calls.contains(&("main.go::Run".into(), "Add".into(), false)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_functions_are_flagged() {
|
||||
let src = "fn helper() {}\n#[cfg(test)]\nmod tests {\n fn works() {}\n fn test_direct() {}\n}\n";
|
||||
let fg = extract("src/x.rs", src);
|
||||
let is_test = |name: &str| fg.nodes.iter().find(|n| n.name.as_str() == name).unwrap().is_test;
|
||||
assert!(!is_test("src/x.rs::helper"));
|
||||
// `works` lives in `mod tests` → flagged by container.
|
||||
assert!(is_test("src/x.rs::tests.works"));
|
||||
// `test_direct` flagged by name too.
|
||||
assert!(is_test("src/x.rs::tests.test_direct"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extraction_is_deterministic() {
|
||||
let src = "use a::b;\nstruct S;\nimpl T for S {}\nfn f() { g(); }\nfn g() {}\n";
|
||||
let a = extract("d.rs", src);
|
||||
let b = extract("d.rs", src);
|
||||
assert_eq!(node_names(&a), node_names(&b));
|
||||
assert_eq!(a.edges, b.edges);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_language_is_none() {
|
||||
assert!(extract_file("notes.md", "# hi").is_none());
|
||||
}
|
||||
|
||||
/// The measured miss: a renamed import broke the call edge, and the symbol's blast radius
|
||||
/// came back one caller short without saying so.
|
||||
#[test]
|
||||
fn a_renamed_import_still_links_the_call() {
|
||||
let fg = extract(
|
||||
"services/agent_service.py",
|
||||
"from utils.credit_helpers import calculate_credits as _calc_credits\n\
|
||||
def charge(usage):\n\
|
||||
\x20 return _calc_credits(usage)\n",
|
||||
);
|
||||
let calls = edges_of(&fg, EdgeKind::Calls);
|
||||
assert!(
|
||||
calls.iter().any(|c| c.1.contains("calculate_credits")),
|
||||
"the alias resolves to the real name: {calls:?}"
|
||||
);
|
||||
assert!(
|
||||
!calls.iter().any(|c| c.1.contains("_calc_credits")),
|
||||
"and not to the local alias: {calls:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unrenamed_call_is_unaffected() {
|
||||
let fg = extract(
|
||||
"services/plain.py",
|
||||
"from utils.credit_helpers import calculate_credits\n\
|
||||
def charge(usage):\n\
|
||||
\x20 return calculate_credits(usage)\n",
|
||||
);
|
||||
assert!(edges_of(&fg, EdgeKind::Calls).iter().any(|c| c.1.contains("calculate_credits")));
|
||||
}
|
||||
303
crates/luxe-lsp/src/graph/model.rs
Normal file
303
crates/luxe-lsp/src/graph/model.rs
Normal file
@@ -0,0 +1,303 @@
|
||||
//! Graph model — nodes, edges, and the qualified names that address them.
|
||||
//!
|
||||
//! The structural layer under blast-radius (design-code-graph.md §3). A [`Node`] is a file or a
|
||||
//! symbol; an [`Edge`] is a directed relationship (`Defines`/`Imports`/`Calls`/…). Two independent
|
||||
//! axes describe an edge: [`Precision`] — *is this edge real?* (tree-sitter `Defines` is exact,
|
||||
//! name-based `Calls` is a heuristic) — and [`EdgeTarget`] — *do we know which node it points at?*
|
||||
//! (same-file targets resolve immediately; cross-file resolution is `resolve.rs`, phase G2).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A node's stable, human-readable address within a project.
|
||||
///
|
||||
/// File nodes are the project-relative path (`src/auth.rs`); symbol nodes append `::` and a dotted
|
||||
/// member path — top-level `src/auth.rs::authenticate`, member `src/auth.rs::AuthService.login`.
|
||||
/// This mirrors the scheme `read_symbol` already uses implicitly.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
pub struct QualifiedName(String);
|
||||
|
||||
impl QualifiedName {
|
||||
/// A file node's qualified name (the project-relative path as-is).
|
||||
pub fn file(path: &str) -> Self {
|
||||
Self(path.to_string())
|
||||
}
|
||||
|
||||
/// A symbol node's qualified name: `file::[Container.]name`.
|
||||
pub fn symbol(file: &str, container: Option<&str>, name: &str) -> Self {
|
||||
match container {
|
||||
Some(c) => Self(format!("{file}::{c}.{name}")),
|
||||
None => Self(format!("{file}::{name}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap a pre-built qualified-name string (e.g. loaded from cache).
|
||||
pub fn from_raw(raw: impl Into<String>) -> Self {
|
||||
Self(raw.into())
|
||||
}
|
||||
|
||||
/// The full qualified string.
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// The project-relative file path (everything before the first `::`).
|
||||
pub fn file_path(&self) -> &str {
|
||||
self.0.split_once("::").map(|(f, _)| f).unwrap_or(&self.0)
|
||||
}
|
||||
|
||||
/// The member path within the file (everything after `::`), or `None` for a file node.
|
||||
pub fn symbol_path(&self) -> Option<&str> {
|
||||
self.0.split_once("::").map(|(_, s)| s)
|
||||
}
|
||||
|
||||
/// The simple, unqualified name — the last dotted segment of the symbol path, or the file path
|
||||
/// for a file node. `src/a.rs::Auth.login` → `login`; `src/a.rs` → `src/a.rs`.
|
||||
pub fn base_name(&self) -> &str {
|
||||
match self.symbol_path() {
|
||||
Some(sym) => sym.rsplit('.').next().unwrap_or(sym),
|
||||
None => &self.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this addresses a file (has no `::` symbol path).
|
||||
pub fn is_file(&self) -> bool {
|
||||
!self.0.contains("::")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for QualifiedName {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// What a node is. Mirrors the LSP `SymbolKind` set we already emit, plus `File`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum NodeKind {
|
||||
File,
|
||||
Function,
|
||||
Method,
|
||||
Struct,
|
||||
Enum,
|
||||
/// A trait (Rust) or interface (TS/Go) — a nominal contract other types implement.
|
||||
Trait,
|
||||
/// A class, impl block, or type alias — anything `syntax.rs` maps to `CLASS`.
|
||||
Class,
|
||||
Field,
|
||||
Constant,
|
||||
Module,
|
||||
EnumMember,
|
||||
/// Any kind we don't model explicitly.
|
||||
Other,
|
||||
}
|
||||
|
||||
impl NodeKind {
|
||||
/// Map an LSP `SymbolKind` integer (as produced by `syntax::outline`) to a `NodeKind`.
|
||||
pub fn from_symbol_kind(kind: u32) -> Self {
|
||||
use crate::protocol::symbol_kinds as k;
|
||||
match kind {
|
||||
k::FUNCTION => Self::Function,
|
||||
k::METHOD => Self::Method,
|
||||
k::STRUCT => Self::Struct,
|
||||
k::ENUM => Self::Enum,
|
||||
k::INTERFACE => Self::Trait,
|
||||
k::CLASS => Self::Class,
|
||||
k::FIELD | k::PROPERTY => Self::Field,
|
||||
k::CONSTANT => Self::Constant,
|
||||
k::MODULE | k::NAMESPACE | k::PACKAGE => Self::Module,
|
||||
k::ENUM_MEMBER => Self::EnumMember,
|
||||
_ => Self::Other,
|
||||
}
|
||||
}
|
||||
|
||||
/// A short, stable label for output.
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::File => "file",
|
||||
Self::Function => "function",
|
||||
Self::Method => "method",
|
||||
Self::Struct => "struct",
|
||||
Self::Enum => "enum",
|
||||
Self::Trait => "trait",
|
||||
Self::Class => "class",
|
||||
Self::Field => "field",
|
||||
Self::Constant => "constant",
|
||||
Self::Module => "module",
|
||||
Self::EnumMember => "enum-member",
|
||||
Self::Other => "symbol",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A file or symbol in the graph. Line numbers are 0-based, matching `FlatSymbol`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Node {
|
||||
/// Stable address (the graph key).
|
||||
pub name: QualifiedName,
|
||||
pub kind: NodeKind,
|
||||
/// First line of the node's full extent (0-based); 0 for a file node.
|
||||
pub start_line: u32,
|
||||
/// Last line of the node's full extent (0-based); 0 for a file node.
|
||||
pub end_line: u32,
|
||||
/// Whether this looks like a test (heuristic — name/container based; see `extract`).
|
||||
pub is_test: bool,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// A file node for `path` (project-relative).
|
||||
pub fn file(path: &str) -> Self {
|
||||
Self {
|
||||
name: QualifiedName::file(path),
|
||||
kind: NodeKind::File,
|
||||
start_line: 0,
|
||||
end_line: 0,
|
||||
is_test: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Directed relationship kinds. `Defines`/`Imports`/`Inherits`/`Implements` land in G1;
|
||||
/// `Calls`/`TestCovers` in G2.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum EdgeKind {
|
||||
/// File → symbol, or container → member.
|
||||
Defines,
|
||||
/// File → an imported module/file.
|
||||
Imports,
|
||||
/// Function → the function it calls (name-based heuristic).
|
||||
Calls,
|
||||
/// Type → a base class it extends.
|
||||
Inherits,
|
||||
/// Type → a trait/interface it implements.
|
||||
Implements,
|
||||
/// Test → the symbol it exercises (heuristic).
|
||||
TestCovers,
|
||||
}
|
||||
|
||||
impl EdgeKind {
|
||||
/// A short, stable label for output.
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Defines => "defines",
|
||||
Self::Imports => "imports",
|
||||
Self::Calls => "calls",
|
||||
Self::Inherits => "inherits",
|
||||
Self::Implements => "implements",
|
||||
Self::TestCovers => "tests",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How much to trust that an edge reflects real program structure.
|
||||
///
|
||||
/// Independent of resolution: an `Exact` edge can still be `Unresolved` (we know a `use` statement
|
||||
/// exists but haven't mapped the module to a file yet), and a `Heuristic` edge can be `Resolved`
|
||||
/// (a name-based call site that happens to have exactly one candidate).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Precision {
|
||||
/// Read straight from syntax — the relationship provably exists (`Defines`, `Imports` stmt).
|
||||
Exact,
|
||||
/// Name-based inference that may be wrong (`Calls`, `TestCovers`).
|
||||
Heuristic,
|
||||
}
|
||||
|
||||
/// The far end of an edge: either a known node, or a raw name still awaiting resolution.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum EdgeTarget {
|
||||
/// Resolved to a specific node in the graph.
|
||||
Resolved(QualifiedName),
|
||||
/// An unresolved raw reference — a module path, base-type name, or callee to resolve later.
|
||||
Unresolved(String),
|
||||
}
|
||||
|
||||
impl EdgeTarget {
|
||||
/// The resolved node, if this target has one.
|
||||
pub fn resolved(&self) -> Option<&QualifiedName> {
|
||||
match self {
|
||||
Self::Resolved(qn) => Some(qn),
|
||||
Self::Unresolved(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw display string either way (qualified name or the raw reference).
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
Self::Resolved(qn) => qn.as_str(),
|
||||
Self::Unresolved(name) => name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A directed edge between a known `from` node and a (possibly unresolved) `to` target.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Edge {
|
||||
pub from: QualifiedName,
|
||||
pub to: EdgeTarget,
|
||||
pub kind: EdgeKind,
|
||||
pub precision: Precision,
|
||||
}
|
||||
|
||||
impl Edge {
|
||||
/// A `Defines` edge (exact, resolved) from `from` to `to`.
|
||||
pub fn defines(from: QualifiedName, to: QualifiedName) -> Self {
|
||||
Self {
|
||||
from,
|
||||
to: EdgeTarget::Resolved(to),
|
||||
kind: EdgeKind::Defines,
|
||||
precision: Precision::Exact,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sort key for deterministic output: `(from, kind, target-string)`.
|
||||
pub fn sort_key(&self) -> (&str, u8, &str) {
|
||||
(self.from.as_str(), self.kind as u8, self.to.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn qualified_name_parts() {
|
||||
let f = QualifiedName::file("src/auth.rs");
|
||||
assert!(f.is_file());
|
||||
assert_eq!(f.file_path(), "src/auth.rs");
|
||||
assert_eq!(f.symbol_path(), None);
|
||||
assert_eq!(f.base_name(), "src/auth.rs");
|
||||
|
||||
let top = QualifiedName::symbol("src/auth.rs", None, "authenticate");
|
||||
assert_eq!(top.as_str(), "src/auth.rs::authenticate");
|
||||
assert!(!top.is_file());
|
||||
assert_eq!(top.file_path(), "src/auth.rs");
|
||||
assert_eq!(top.symbol_path(), Some("authenticate"));
|
||||
assert_eq!(top.base_name(), "authenticate");
|
||||
|
||||
let member = QualifiedName::symbol("src/auth.rs", Some("AuthService"), "login");
|
||||
assert_eq!(member.as_str(), "src/auth.rs::AuthService.login");
|
||||
assert_eq!(member.file_path(), "src/auth.rs");
|
||||
assert_eq!(member.symbol_path(), Some("AuthService.login"));
|
||||
assert_eq!(member.base_name(), "login");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_kind_maps_from_symbol_kind() {
|
||||
use crate::protocol::symbol_kinds as k;
|
||||
assert_eq!(NodeKind::from_symbol_kind(k::FUNCTION), NodeKind::Function);
|
||||
assert_eq!(NodeKind::from_symbol_kind(k::METHOD), NodeKind::Method);
|
||||
assert_eq!(NodeKind::from_symbol_kind(k::INTERFACE), NodeKind::Trait);
|
||||
assert_eq!(NodeKind::from_symbol_kind(k::CLASS), NodeKind::Class);
|
||||
assert_eq!(NodeKind::from_symbol_kind(9999), NodeKind::Other);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edge_target_accessors() {
|
||||
let r = EdgeTarget::Resolved(QualifiedName::file("a.rs"));
|
||||
assert_eq!(r.resolved().map(QualifiedName::as_str), Some("a.rs"));
|
||||
assert_eq!(r.as_str(), "a.rs");
|
||||
|
||||
let u = EdgeTarget::Unresolved("std::collections".to_string());
|
||||
assert!(u.resolved().is_none());
|
||||
assert_eq!(u.as_str(), "std::collections");
|
||||
}
|
||||
}
|
||||
228
crates/luxe-lsp/src/graph/resolve.rs
Normal file
228
crates/luxe-lsp/src/graph/resolve.rs
Normal file
@@ -0,0 +1,228 @@
|
||||
//! Resolution — turn a file's raw name references into edges pointing at real nodes.
|
||||
//!
|
||||
//! Extraction leaves cross-file `Calls`/`Inherits`/`Implements` targets as
|
||||
//! [`EdgeTarget::Unresolved`] simple names. Here we map each name to a definition through the
|
||||
//! funnel of design-code-graph.md §6: **same-file symbol → project-wide unique → (ambiguous)**.
|
||||
//! Ambiguity is never guessed away — an unresolvable-or-ambiguous project reference stays
|
||||
//! `Unresolved` (a candidate for LSP enrichment in G6). External `Calls` (a name the project never
|
||||
//! defines — stdlib/deps) are dropped as inert blast-radius noise; external heritage is kept as
|
||||
//! context. Finally we derive `TestCovers` from resolved calls out of test nodes.
|
||||
|
||||
use super::extract::FileGraph;
|
||||
use super::model::{Edge, EdgeKind, EdgeTarget, Node, Precision, QualifiedName};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum Resolution {
|
||||
/// Exactly one definition — a confident target.
|
||||
Resolved(QualifiedName),
|
||||
/// Several candidate definitions — kept unresolved rather than guessed.
|
||||
Ambiguous(Vec<QualifiedName>),
|
||||
/// No project definition of this name (external/library reference).
|
||||
NotFound,
|
||||
}
|
||||
|
||||
/// A project-wide map from a symbol's simple name to the qualified names that define it. Built once
|
||||
/// over every file's nodes, then used to resolve each file's references (mirrors how the BM25
|
||||
/// index is built once and queried many times).
|
||||
pub(super) struct NameIndex {
|
||||
by_name: HashMap<String, Vec<QualifiedName>>,
|
||||
}
|
||||
|
||||
impl NameIndex {
|
||||
/// Index every symbol node (file nodes aren't resolution targets).
|
||||
pub(super) fn from_nodes<'a>(nodes: impl Iterator<Item = &'a Node>) -> Self {
|
||||
let mut by_name: HashMap<String, Vec<QualifiedName>> = HashMap::new();
|
||||
for node in nodes {
|
||||
if node.name.is_file() {
|
||||
continue;
|
||||
}
|
||||
by_name.entry(node.name.base_name().to_string()).or_default().push(node.name.clone());
|
||||
}
|
||||
for defs in by_name.values_mut() {
|
||||
defs.sort();
|
||||
defs.dedup();
|
||||
}
|
||||
Self { by_name }
|
||||
}
|
||||
|
||||
/// Resolve `name` as referenced from `caller_file`, honoring the funnel: a unique same-file
|
||||
/// definition wins; otherwise a unique project-wide definition; otherwise ambiguous/not-found.
|
||||
fn resolve(&self, caller_file: &str, name: &str) -> Resolution {
|
||||
let Some(candidates) = self.by_name.get(name) else { return Resolution::NotFound };
|
||||
|
||||
let same_file: Vec<&QualifiedName> =
|
||||
candidates.iter().filter(|qn| qn.file_path() == caller_file).collect();
|
||||
match same_file.len() {
|
||||
1 => return Resolution::Resolved(same_file[0].clone()),
|
||||
n if n > 1 => return Resolution::Ambiguous(same_file.into_iter().cloned().collect()),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match candidates.len() {
|
||||
1 => Resolution::Resolved(candidates[0].clone()),
|
||||
_ => Resolution::Ambiguous(candidates.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a file's edges against the project name index, in place, and derive `TestCovers`.
|
||||
///
|
||||
/// Upgrades `Calls`/`Inherits`/`Implements` to resolved targets; drops external `Calls`; keeps
|
||||
/// ambiguous and external-heritage references unresolved; drops self-edges. Returns the file graph
|
||||
/// with its edges rewritten (nodes untouched).
|
||||
pub(super) fn resolve_file(mut fg: FileGraph, index: &NameIndex) -> FileGraph {
|
||||
let test_nodes: HashSet<QualifiedName> =
|
||||
fg.nodes.iter().filter(|n| n.is_test).map(|n| n.name.clone()).collect();
|
||||
|
||||
let mut resolved: Vec<Edge> = Vec::with_capacity(fg.edges.len());
|
||||
let mut test_covers: Vec<Edge> = Vec::new();
|
||||
|
||||
for mut edge in fg.edges.drain(..) {
|
||||
if is_name_ref(edge.kind) {
|
||||
if let EdgeTarget::Unresolved(name) = &edge.to {
|
||||
match index.resolve(edge.from.file_path(), name) {
|
||||
Resolution::Resolved(qn) => {
|
||||
if qn == edge.from {
|
||||
continue; // self-edge (recursion / self-impl) — inert
|
||||
}
|
||||
edge.to = EdgeTarget::Resolved(qn);
|
||||
}
|
||||
Resolution::Ambiguous(_) => {} // keep unresolved candidate — never guess
|
||||
Resolution::NotFound => {
|
||||
if edge.kind == EdgeKind::Calls {
|
||||
continue; // external/library call — inert blast-radius noise
|
||||
}
|
||||
// external base type — keep as unresolved context
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if edge.kind == EdgeKind::Calls && test_nodes.contains(&edge.from) {
|
||||
if let EdgeTarget::Resolved(target) = &edge.to {
|
||||
test_covers.push(Edge {
|
||||
from: edge.from.clone(),
|
||||
to: EdgeTarget::Resolved(target.clone()),
|
||||
kind: EdgeKind::TestCovers,
|
||||
precision: Precision::Heuristic,
|
||||
});
|
||||
}
|
||||
}
|
||||
resolved.push(edge);
|
||||
}
|
||||
|
||||
resolved.extend(test_covers);
|
||||
resolved.sort_by(|a, b| a.sort_key().cmp(&b.sort_key()));
|
||||
resolved.dedup();
|
||||
fg.edges = resolved;
|
||||
fg
|
||||
}
|
||||
|
||||
/// Whether an edge kind carries a symbol-name reference the funnel should resolve.
|
||||
fn is_name_ref(kind: EdgeKind) -> bool {
|
||||
matches!(kind, EdgeKind::Calls | EdgeKind::Inherits | EdgeKind::Implements)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::extract::extract_file;
|
||||
use crate::graph::model::NodeKind;
|
||||
|
||||
fn node(qn: &str, kind: NodeKind, is_test: bool) -> Node {
|
||||
Node { name: QualifiedName::from_raw(qn), kind, start_line: 0, end_line: 0, is_test }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_prefers_same_file_then_project_unique() {
|
||||
let nodes = [
|
||||
node("a.rs::foo", NodeKind::Function, false),
|
||||
node("b.rs::foo", NodeKind::Function, false),
|
||||
node("a.rs::only", NodeKind::Function, false),
|
||||
];
|
||||
let index = NameIndex::from_nodes(nodes.iter());
|
||||
|
||||
// `foo` exists in two files → ambiguous from a third file...
|
||||
assert_eq!(
|
||||
index.resolve("c.rs", "foo"),
|
||||
Resolution::Ambiguous(vec![
|
||||
QualifiedName::from_raw("a.rs::foo"),
|
||||
QualifiedName::from_raw("b.rs::foo"),
|
||||
])
|
||||
);
|
||||
// ...but a same-file definition wins.
|
||||
assert_eq!(
|
||||
index.resolve("a.rs", "foo"),
|
||||
Resolution::Resolved(QualifiedName::from_raw("a.rs::foo"))
|
||||
);
|
||||
// A project-unique name resolves from anywhere.
|
||||
assert_eq!(
|
||||
index.resolve("z.rs", "only"),
|
||||
Resolution::Resolved(QualifiedName::from_raw("a.rs::only"))
|
||||
);
|
||||
// An unknown name is not found.
|
||||
assert_eq!(index.resolve("z.rs", "nope"), Resolution::NotFound);
|
||||
}
|
||||
|
||||
/// End-to-end: two files, a cross-file call resolves, an external call is dropped, and a test
|
||||
/// that calls the covered symbol yields a `TestCovers` edge.
|
||||
#[test]
|
||||
fn resolve_file_wires_calls_and_test_covers() {
|
||||
let lib = extract_file("src/lib.rs", "pub fn compute() -> i32 { helperx() }\n").unwrap();
|
||||
let caller = extract_file(
|
||||
"src/app.rs",
|
||||
"fn run() { compute(); println(); }\n\
|
||||
#[cfg(test)]\nmod tests {\n fn test_run() { compute(); }\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let index = NameIndex::from_nodes(lib.nodes.iter().chain(caller.nodes.iter()));
|
||||
let caller = resolve_file(caller, &index);
|
||||
|
||||
let calls: Vec<(String, String, bool)> = caller
|
||||
.edges
|
||||
.iter()
|
||||
.filter(|e| e.kind == EdgeKind::Calls)
|
||||
.map(|e| (e.from.as_str().into(), e.to.as_str().into(), e.to.resolved().is_some()))
|
||||
.collect();
|
||||
// `compute` resolves cross-file; the external `println` call is dropped as noise.
|
||||
assert!(calls.contains(&("src/app.rs::run".into(), "src/lib.rs::compute".into(), true)));
|
||||
assert!(!calls.iter().any(|(_, to, _)| to.contains("println")));
|
||||
|
||||
// The test's call to compute() becomes a TestCovers edge.
|
||||
let covers: Vec<(String, String)> = caller
|
||||
.edges
|
||||
.iter()
|
||||
.filter(|e| e.kind == EdgeKind::TestCovers)
|
||||
.map(|e| (e.from.as_str().into(), e.to.as_str().into()))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
covers,
|
||||
vec![("src/app.rs::tests.test_run".to_string(), "src/lib.rs::compute".to_string())]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recursion_self_edge_is_dropped() {
|
||||
let fg = extract_file("r.rs", "fn fact(n: u64) -> u64 { fact(n - 1) }\n").unwrap();
|
||||
let index = NameIndex::from_nodes(fg.nodes.iter());
|
||||
let fg = resolve_file(fg, &index);
|
||||
assert!(fg.edges.iter().all(|e| e.kind != EdgeKind::Calls), "self-recursion dropped");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ambiguous_call_stays_unresolved() {
|
||||
// Two `render` methods across files → a call to `render` can't be disambiguated.
|
||||
let a = extract_file("a.rs", "struct A;\nimpl A { fn render(&self) {} }\n").unwrap();
|
||||
let b = extract_file("b.rs", "struct B;\nimpl B { fn render(&self) {} }\n").unwrap();
|
||||
let c = extract_file("c.rs", "fn draw(x: A) { x.render(); }\n").unwrap();
|
||||
let index =
|
||||
NameIndex::from_nodes(a.nodes.iter().chain(b.nodes.iter()).chain(c.nodes.iter()));
|
||||
let c = resolve_file(c, &index);
|
||||
let render_call =
|
||||
c.edges.iter().find(|e| e.kind == EdgeKind::Calls && e.to.as_str() == "render");
|
||||
assert!(render_call.is_some(), "kept as an ambiguous candidate");
|
||||
assert!(render_call.unwrap().to.resolved().is_none(), "not guessed to a single target");
|
||||
}
|
||||
}
|
||||
236
crates/luxe-lsp/src/graph/review.rs
Normal file
236
crates/luxe-lsp/src/graph/review.rs
Normal file
@@ -0,0 +1,236 @@
|
||||
//! Review insight — graph-derived grounding for Guided Review.
|
||||
//!
|
||||
//! Turns a set of changed regions into advisory structural context: the ranked callers/dependents/
|
||||
//! covering-tests per file, plus the changed symbols a test does and does not cover. Returns the
|
||||
//! `luxe-core` review type directly so the engine can attach it to a review without `luxe-core`
|
||||
//! depending on the graph. Pure and offline — fixture-testable in isolation.
|
||||
//!
|
||||
//! Coverage claims are scoped to the symbols the **diff actually touches**. Seeding from every symbol
|
||||
//! in a changed file buries the reader in "no covering test" call-outs, test functions in a changed
|
||||
//! test file included, and a signal meant to focus attention stops doing it.
|
||||
|
||||
use super::blast::{blast_radius, DEFAULT_MAX_DEPTH};
|
||||
use super::model::{EdgeKind, NodeKind};
|
||||
use super::store::{GraphStore, InMemoryGraph};
|
||||
use super::testing::is_test_path;
|
||||
use luxe_core::guided_review::{FileInsight, RelevantFile, ReviewInsight};
|
||||
|
||||
/// Max relevant files listed per changed file (keeps the review prompt/UI compact).
|
||||
const MAX_RELEVANT_PER_FILE: usize = 6;
|
||||
|
||||
/// One changed file and the new-side line spans the diff touched.
|
||||
///
|
||||
/// Spans are 1-based and inclusive, matching unified-diff line numbers. An empty span list means
|
||||
/// "somewhere in this file" — no symbol-level claim is made for it.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ChangedRegion {
|
||||
/// Project-relative path of the changed file.
|
||||
pub file: String,
|
||||
/// New-side line spans the diff touched, 1-based inclusive.
|
||||
pub changed_lines: Vec<(u32, u32)>,
|
||||
}
|
||||
|
||||
impl ChangedRegion {
|
||||
/// A whole-file region, for a change with no usable new-side line information.
|
||||
pub fn whole_file(file: impl Into<String>) -> Self {
|
||||
Self { file: file.into(), changed_lines: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute advisory structural grounding for a set of changed regions.
|
||||
///
|
||||
/// Files with no known structural relationships are skipped; the result is empty for a docs-only
|
||||
/// diff or an unbuilt graph.
|
||||
pub fn review_insight(graph: &InMemoryGraph, changed: &[ChangedRegion]) -> ReviewInsight {
|
||||
let mut files = Vec::new();
|
||||
let mut untested: Vec<String> = Vec::new();
|
||||
let mut covered: Vec<String> = Vec::new();
|
||||
|
||||
for region in changed {
|
||||
let br = blast_radius(graph, ®ion.file, DEFAULT_MAX_DEPTH);
|
||||
if br.seeds.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let relevant: Vec<RelevantFile> = br
|
||||
.dependents
|
||||
.iter()
|
||||
.take(MAX_RELEVANT_PER_FILE)
|
||||
.map(|impacted| RelevantFile {
|
||||
file: impacted.file.clone(),
|
||||
reason: impacted.reason.clone(),
|
||||
})
|
||||
.collect();
|
||||
if !relevant.is_empty() {
|
||||
files.push(FileInsight { file: region.file.clone(), relevant });
|
||||
}
|
||||
// A test file's own functions need no covering test; saying otherwise is noise a reviewer
|
||||
// learns to ignore, which costs every other signal its credibility.
|
||||
if is_test_path(®ion.file) {
|
||||
continue;
|
||||
}
|
||||
for symbol in br.seeds.iter().filter(|seed| touches_change(graph, seed, region)) {
|
||||
let Some(node) = graph.node(symbol) else { continue };
|
||||
if node.is_test || !matches!(node.kind, NodeKind::Function | NodeKind::Method) {
|
||||
continue;
|
||||
}
|
||||
let has_test =
|
||||
graph.in_edges(symbol).iter().any(|edge| edge.kind == EdgeKind::TestCovers);
|
||||
if has_test {
|
||||
covered.push(symbol.to_string());
|
||||
} else {
|
||||
untested.push(symbol.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
untested.sort();
|
||||
untested.dedup();
|
||||
covered.sort();
|
||||
covered.dedup();
|
||||
ReviewInsight { files, untested_symbols: untested, covered_symbols: covered }
|
||||
}
|
||||
|
||||
/// Whether a symbol's extent overlaps one of the region's changed line spans.
|
||||
///
|
||||
/// A region with no spans matches every symbol in its file: without new-side line numbers (a pure
|
||||
/// deletion, an unparsed hunk) the honest answer is "we don't know which symbol", and the file-wide
|
||||
/// claim is the one we can still stand behind.
|
||||
fn touches_change(
|
||||
graph: &InMemoryGraph,
|
||||
symbol: &super::model::QualifiedName,
|
||||
region: &ChangedRegion,
|
||||
) -> bool {
|
||||
if region.changed_lines.is_empty() {
|
||||
return true;
|
||||
}
|
||||
let Some(node) = graph.node(symbol) else { return false };
|
||||
// Graph extents are 0-based; diff line numbers are 1-based.
|
||||
let start = node.start_line.saturating_add(1);
|
||||
let end = node.end_line.saturating_add(1).max(start);
|
||||
region.changed_lines.iter().any(|(from, to)| start <= *to && *from <= end)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::build::build_graph;
|
||||
|
||||
fn project() -> Vec<(&'static str, &'static str)> {
|
||||
vec![
|
||||
("src/auth.rs", "pub fn login() {}\npub fn logout() {}\n"),
|
||||
("src/session.rs", "use crate::auth::login;\npub fn start() { login() }\n"),
|
||||
(
|
||||
"tests/auth_test.rs",
|
||||
"#[cfg(test)]\nmod tests {\n fn test_login() { login(); }\n}\n",
|
||||
),
|
||||
("docs/notes.md", "# just docs\n"),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insight_grounds_relevant_files_and_splits_covered_from_untested() {
|
||||
let graph = build_graph(project());
|
||||
let insight = review_insight(&graph, &[ChangedRegion::whole_file("src/auth.rs")]);
|
||||
|
||||
let auth = insight.files.iter().find(|f| f.file == "src/auth.rs").expect("auth insight");
|
||||
let relevant: Vec<&str> = auth.relevant.iter().map(|r| r.file.as_str()).collect();
|
||||
assert!(relevant.contains(&"src/session.rs"), "caller grounded");
|
||||
assert!(relevant.contains(&"tests/auth_test.rs"), "covering test grounded");
|
||||
|
||||
// logout has no test; login does — and both facts are now reported.
|
||||
assert!(insight.untested_symbols.iter().any(|s| s.ends_with("::logout")));
|
||||
assert!(!insight.untested_symbols.iter().any(|s| s.ends_with("::login")));
|
||||
assert!(insight.covers("src/auth.rs::login"), "coverage is evidence the budget can spend");
|
||||
}
|
||||
|
||||
/// The defect that produced 236 call-outs: a one-line change flagged every symbol in the file.
|
||||
#[test]
|
||||
fn only_the_symbols_the_diff_touches_are_judged() {
|
||||
let graph = build_graph(project());
|
||||
// `login` occupies line 1, `logout` line 2; the diff touches only line 2.
|
||||
let insight = review_insight(
|
||||
&graph,
|
||||
&[ChangedRegion { file: "src/auth.rs".into(), changed_lines: vec![(2, 2)] }],
|
||||
);
|
||||
|
||||
assert!(insight.untested_symbols.iter().any(|s| s.ends_with("::logout")));
|
||||
assert!(
|
||||
!insight.covered_symbols.iter().any(|s| s.ends_with("::login")),
|
||||
"an untouched symbol is not part of this review: {:?}",
|
||||
insight.covered_symbols
|
||||
);
|
||||
}
|
||||
|
||||
/// A changed test file must not have every one of its test functions flagged as untested.
|
||||
#[test]
|
||||
fn a_changed_test_file_is_never_asked_for_a_covering_test() {
|
||||
let graph = build_graph(project());
|
||||
let insight = review_insight(&graph, &[ChangedRegion::whole_file("tests/auth_test.rs")]);
|
||||
|
||||
assert!(
|
||||
insight.untested_symbols.is_empty(),
|
||||
"tests need no tests: {:?}",
|
||||
insight.untested_symbols
|
||||
);
|
||||
}
|
||||
|
||||
/// Rust's other test layout: `mod tests;` pointing at a sibling file, with plain `#[test]`
|
||||
/// names. Neither a container nor a name says "test" there, so the covering test was invisible
|
||||
/// and both halves of the badge were wrong at once — the changed code read as untested, and the
|
||||
/// test file was asked for a test of its own.
|
||||
#[test]
|
||||
fn a_sibling_tests_module_covers_the_code_it_calls() {
|
||||
let graph = build_graph(vec![
|
||||
(
|
||||
"src/app/toasts.rs",
|
||||
"pub fn spinner_char() -> char { '|' }\npub fn tick_spinner() {}\n",
|
||||
),
|
||||
(
|
||||
"src/app/tests.rs",
|
||||
"fn spinner_char_cycles_through_frames() {\n spinner_char();\n \
|
||||
tick_spinner();\n}\n",
|
||||
),
|
||||
]);
|
||||
|
||||
let insight = review_insight(&graph, &[ChangedRegion::whole_file("src/app/toasts.rs")]);
|
||||
assert!(
|
||||
insight.untested_symbols.is_empty(),
|
||||
"the sibling test module covers both: {:?}",
|
||||
insight.untested_symbols
|
||||
);
|
||||
assert!(insight.covers("src/app/toasts.rs::spinner_char"));
|
||||
assert!(insight.covers("src/app/toasts.rs::tick_spinner"));
|
||||
|
||||
let on_the_test_file =
|
||||
review_insight(&graph, &[ChangedRegion::whole_file("src/app/tests.rs")]);
|
||||
assert!(
|
||||
on_the_test_file.untested_symbols.is_empty(),
|
||||
"a test file is never asked for a covering test: {:?}",
|
||||
on_the_test_file.untested_symbols
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn docs_only_change_yields_empty_insight() {
|
||||
let graph = build_graph(project());
|
||||
let insight = review_insight(&graph, &[ChangedRegion::whole_file("docs/notes.md")]);
|
||||
assert!(insight.is_empty());
|
||||
assert!(insight.render_prompt_block().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_changed_files_aggregate_and_dedup_test_gaps() {
|
||||
let graph = build_graph(project());
|
||||
let insight = review_insight(
|
||||
&graph,
|
||||
&[
|
||||
ChangedRegion::whole_file("src/auth.rs"),
|
||||
ChangedRegion::whole_file("src/session.rs"),
|
||||
],
|
||||
);
|
||||
|
||||
let mut sorted = insight.untested_symbols.clone();
|
||||
sorted.sort();
|
||||
assert_eq!(insight.untested_symbols, sorted);
|
||||
}
|
||||
}
|
||||
218
crates/luxe-lsp/src/graph/service.rs
Normal file
218
crates/luxe-lsp/src/graph/service.rs
Normal file
@@ -0,0 +1,218 @@
|
||||
//! Service — the warm code graph and its cached build (design-code-graph.md §4).
|
||||
//!
|
||||
//! Mirrors the BM25 `SymbolSearchTool` lifecycle: the graph builds lazily on `spawn_blocking`, is
|
||||
//! cached as an `Arc<InMemoryGraph>` behind a `Mutex<Option<…>>`, and is shared across tools. The
|
||||
//! build reads every source file but re-parses only files whose content hash changed since the last
|
||||
//! run (the rest load from `.luxe/graph/` shards). On a file change the service invalidates the
|
||||
//! cached graph; the next query rebuilds from the cache, re-parsing just the changed file — so an
|
||||
//! edit costs one parse, and dependents' resolutions are recomputed for free during the reload.
|
||||
|
||||
use super::build::assemble;
|
||||
use super::cache::{content_hash, GraphCache};
|
||||
use super::extract::{extract_file, FileGraph};
|
||||
use super::store::InMemoryGraph;
|
||||
use crate::symbol_index::source_files;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// What a cached build did — for perf assertions and diagnostics.
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct BuildStats {
|
||||
/// Indexed source files.
|
||||
pub files: usize,
|
||||
/// Files loaded from a valid cache shard (not re-parsed).
|
||||
pub cache_hits: usize,
|
||||
/// Files re-parsed (new or changed).
|
||||
pub cache_misses: usize,
|
||||
}
|
||||
|
||||
/// Build a resolved graph for `root`, reusing `cache` for unchanged files. Blocking (filesystem +
|
||||
/// parsing); call from `spawn_blocking`.
|
||||
pub fn build_graph_cached(root: &Path, cache: &GraphCache) -> (InMemoryGraph, BuildStats) {
|
||||
let mut fragments: Vec<(String, FileGraph)> = Vec::new();
|
||||
let mut manifest = std::collections::HashMap::new();
|
||||
let mut stats = BuildStats::default();
|
||||
|
||||
for abs in source_files(root).files {
|
||||
let Ok(content) = std::fs::read_to_string(&abs) else { continue };
|
||||
// Forward slashes, because the key is also what the model reads back and hands to `read`.
|
||||
let rel = luxe_core::paths::display(abs.strip_prefix(root).unwrap_or(&abs));
|
||||
let hash = content_hash(&content);
|
||||
|
||||
let fragment = match cache.load(&rel, &hash) {
|
||||
Some(fg) => {
|
||||
stats.cache_hits += 1;
|
||||
fg
|
||||
}
|
||||
None => {
|
||||
let Some(fg) = extract_file(&rel, &content) else { continue };
|
||||
cache.store(&rel, &hash, &fg);
|
||||
stats.cache_misses += 1;
|
||||
fg
|
||||
}
|
||||
};
|
||||
stats.files += 1;
|
||||
manifest.insert(rel.clone(), hash);
|
||||
fragments.push((rel, fragment));
|
||||
}
|
||||
|
||||
cache.store_manifest(&manifest);
|
||||
cache.prune(&manifest);
|
||||
(assemble(fragments), stats)
|
||||
}
|
||||
|
||||
/// Why there is no graph to answer from.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum GraphError {
|
||||
/// The build panicked or was cancelled.
|
||||
#[error("the code graph could not be built: {0}")]
|
||||
Build(String),
|
||||
}
|
||||
|
||||
/// The build every service runs: from the shard cache under `root`.
|
||||
fn build_from_cache(root: &Path) -> InMemoryGraph {
|
||||
build_graph_cached(root, &GraphCache::new(root)).0
|
||||
}
|
||||
|
||||
/// A warm, shared code graph for one project root.
|
||||
pub struct GraphService {
|
||||
root: PathBuf,
|
||||
graph: Mutex<Option<Arc<InMemoryGraph>>>,
|
||||
build: fn(&Path) -> InMemoryGraph,
|
||||
}
|
||||
|
||||
impl GraphService {
|
||||
/// A service over `root` (nothing is built until the first query).
|
||||
pub fn new(root: impl Into<PathBuf>) -> Self {
|
||||
Self { root: root.into(), graph: Mutex::new(None), build: build_from_cache }
|
||||
}
|
||||
|
||||
/// Get (or build) the shared graph. `refresh` forces a full rebuild from the cache.
|
||||
///
|
||||
/// A build that fails is an error and is not kept: an empty graph would read as "nothing
|
||||
/// depends on this", the one answer a failure must never give.
|
||||
pub async fn graph(&self, refresh: bool) -> Result<Arc<InMemoryGraph>, GraphError> {
|
||||
let mut guard = self.graph.lock().await;
|
||||
if let Some(graph) = guard.as_ref().filter(|_| !refresh) {
|
||||
return Ok(Arc::clone(graph));
|
||||
}
|
||||
let (root, build) = (self.root.clone(), self.build);
|
||||
let built = tokio::task::spawn_blocking(move || build(&root))
|
||||
.await
|
||||
.map_err(|error| GraphError::Build(error.to_string()))?;
|
||||
let built = Arc::new(built);
|
||||
*guard = Some(Arc::clone(&built));
|
||||
Ok(built)
|
||||
}
|
||||
|
||||
/// Invalidate the cached graph after a file change; the next `graph()` rebuilds from the cache,
|
||||
/// re-parsing only the changed file.
|
||||
pub async fn invalidate(&self) {
|
||||
*self.graph.lock().await = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::store::GraphStore;
|
||||
|
||||
/// A throwaway project dir with a couple of source files.
|
||||
fn scratch(name: &str) -> tempfile::TempDir {
|
||||
let scratch = tempfile::Builder::new()
|
||||
.prefix(&format!("luxe-graph-svc-{name}-"))
|
||||
.tempdir()
|
||||
.expect("temp dir");
|
||||
let dir = scratch.path();
|
||||
std::fs::create_dir_all(dir.join("src")).unwrap();
|
||||
std::fs::write(dir.join("src/lib.rs"), "pub fn compute() -> i32 { 1 }\n").unwrap();
|
||||
std::fs::write(
|
||||
dir.join("src/app.rs"),
|
||||
"fn run() { compute(); }\n#[cfg(test)]\nmod tests {\n fn test_run() { compute(); }\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
scratch
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warm_start_hits_cache_and_edit_reparses_only_changed_file() {
|
||||
let scratch = scratch("warm");
|
||||
let dir = scratch.path().to_path_buf();
|
||||
let cache = GraphCache::new(&dir);
|
||||
|
||||
// Cold build: everything is a miss.
|
||||
let (_, cold) = build_graph_cached(&dir, &cache);
|
||||
assert_eq!(cold.files, 2);
|
||||
assert_eq!(cold.cache_misses, 2);
|
||||
assert_eq!(cold.cache_hits, 0);
|
||||
|
||||
// Warm build with no changes: every file hits the cache (no re-parse).
|
||||
let (_, warm) = build_graph_cached(&dir, &cache);
|
||||
assert_eq!(warm.cache_hits, 2);
|
||||
assert_eq!(warm.cache_misses, 0);
|
||||
|
||||
// Edit one file: exactly one miss, the rest still hit.
|
||||
std::fs::write(dir.join("src/lib.rs"), "pub fn compute() -> i32 { 2 }\n").unwrap();
|
||||
let (_, after) = build_graph_cached(&dir, &cache);
|
||||
assert_eq!(after.cache_misses, 1);
|
||||
assert_eq!(after.cache_hits, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cached_build_matches_a_fresh_build() {
|
||||
let scratch = scratch("determinism");
|
||||
let dir = scratch.path().to_path_buf();
|
||||
let cache = GraphCache::new(&dir);
|
||||
|
||||
let (fresh, _) = build_graph_cached(&dir, &cache);
|
||||
// Second build entirely from cache must produce the same graph shape.
|
||||
let (cached, stats) = build_graph_cached(&dir, &cache);
|
||||
assert_eq!(stats.cache_hits, 2);
|
||||
assert_eq!(fresh.node_count(), cached.node_count());
|
||||
assert_eq!(fresh.edge_count(), cached.edge_count());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn service_builds_invalidates_and_rebuilds() {
|
||||
let scratch = scratch("service");
|
||||
let dir = scratch.path().to_path_buf();
|
||||
let service = GraphService::new(&dir);
|
||||
|
||||
let g1 = service.graph(false).await.unwrap();
|
||||
assert!(g1.node_count() > 0);
|
||||
// Cached: same Arc handed back.
|
||||
let g2 = service.graph(false).await.unwrap();
|
||||
assert!(Arc::ptr_eq(&g1, &g2));
|
||||
|
||||
// After a change + invalidate, a fresh graph is built.
|
||||
std::fs::write(
|
||||
dir.join("src/lib.rs"),
|
||||
"pub fn compute() -> i32 { 3 }\npub fn extra() {}\n",
|
||||
)
|
||||
.unwrap();
|
||||
service.invalidate().await;
|
||||
let g3 = service.graph(false).await.unwrap();
|
||||
assert!(!Arc::ptr_eq(&g1, &g3));
|
||||
assert!(g3.node_count() > g1.node_count());
|
||||
}
|
||||
|
||||
/// A build that panics is an error the caller sees, and the next query tries again.
|
||||
///
|
||||
/// The break this catches: a panicking build became an empty graph and was cached, so `impact`
|
||||
/// answered "nothing depends on this" with no error, for the rest of the session.
|
||||
#[tokio::test]
|
||||
async fn a_panicking_build_reports_an_error_and_is_not_kept() {
|
||||
fn panics(_: &Path) -> InMemoryGraph {
|
||||
panic!("an extractor bug")
|
||||
}
|
||||
let service = GraphService {
|
||||
root: PathBuf::from("/nowhere"),
|
||||
graph: Mutex::new(None),
|
||||
build: panics,
|
||||
};
|
||||
let Err(error) = service.graph(false).await else { panic!("a graph from a panic") };
|
||||
assert!(error.to_string().contains("could not be built"), "{error}");
|
||||
assert!(service.graph.lock().await.is_none(), "nothing cached");
|
||||
}
|
||||
}
|
||||
230
crates/luxe-lsp/src/graph/store.rs
Normal file
230
crates/luxe-lsp/src/graph/store.rs
Normal file
@@ -0,0 +1,230 @@
|
||||
//! Graph storage — the `GraphStore` trait and an in-memory implementation.
|
||||
//!
|
||||
//! The load-bearing storage decision (design-code-graph.md §5): keep the whole graph in RAM behind
|
||||
//! a trait so a `SqliteGraphStore` can drop in later, but don't take the C/SQL dependency now.
|
||||
//! [`InMemoryGraph`] holds forward adjacency, a reverse index (resolved targets only — the
|
||||
//! blast-radius hot path), and a name→definitions map for `resolve.rs` (G2). All mutation is
|
||||
//! per-file (`upsert_file`/`remove_file`) so the `FileChanged` hook can re-index one file (G3).
|
||||
|
||||
use super::model::{Edge, EdgeTarget, Node, QualifiedName};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// A structural code graph. Trait-first so the backing store is swappable (§5).
|
||||
pub trait GraphStore {
|
||||
/// Replace all nodes/edges for `file` (add on first sight, update on re-index).
|
||||
fn upsert_file(&mut self, file: &str, nodes: Vec<Node>, edges: Vec<Edge>);
|
||||
|
||||
/// Drop every node/edge owned by `file`.
|
||||
fn remove_file(&mut self, file: &str);
|
||||
|
||||
/// The node at `name`, if present.
|
||||
fn node(&self, name: &QualifiedName) -> Option<&Node>;
|
||||
|
||||
/// Outgoing edges from `name` (what it depends on / defines).
|
||||
fn out_edges(&self, name: &QualifiedName) -> &[Edge];
|
||||
|
||||
/// Incoming resolved edges to `name` (who depends on it) — the reverse BFS frontier.
|
||||
fn in_edges(&self, name: &QualifiedName) -> &[Edge];
|
||||
|
||||
/// Qualified names defining the simple identifier `base` (for name resolution).
|
||||
fn defs_by_name(&self, base: &str) -> &[QualifiedName];
|
||||
|
||||
/// Qualified names of the symbols `file` defines (excludes the file node itself) — the seed set
|
||||
/// for a file-level blast radius.
|
||||
fn symbols_in_file(&self, file: &str) -> Vec<QualifiedName>;
|
||||
|
||||
fn node_count(&self) -> usize;
|
||||
|
||||
fn edge_count(&self) -> usize;
|
||||
|
||||
/// Edges touching `name` (out + in) — a cheap degree-centrality proxy for ranking.
|
||||
fn degree(&self, name: &QualifiedName) -> usize {
|
||||
self.out_edges(name).len() + self.in_edges(name).len()
|
||||
}
|
||||
}
|
||||
|
||||
const NO_EDGES: &[Edge] = &[];
|
||||
const NO_NAMES: &[QualifiedName] = &[];
|
||||
|
||||
/// A whole-graph, in-RAM store. Fast native adjacency for the blast-radius hot path.
|
||||
#[derive(Default)]
|
||||
pub struct InMemoryGraph {
|
||||
nodes: HashMap<QualifiedName, Node>,
|
||||
out_edges: HashMap<QualifiedName, Vec<Edge>>,
|
||||
in_edges: HashMap<QualifiedName, Vec<Edge>>,
|
||||
defs_by_name: HashMap<String, Vec<QualifiedName>>,
|
||||
/// Which qualified names each file owns — the unit of incremental replacement.
|
||||
file_nodes: HashMap<String, Vec<QualifiedName>>,
|
||||
}
|
||||
|
||||
impl InMemoryGraph {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Every node currently in the graph, order unspecified (tests should sort).
|
||||
pub fn nodes(&self) -> impl Iterator<Item = &Node> {
|
||||
self.nodes.values()
|
||||
}
|
||||
|
||||
/// Total edge records (forward adjacency), for diagnostics/tests.
|
||||
fn total_forward_edges(&self) -> usize {
|
||||
self.out_edges.values().map(Vec::len).sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl GraphStore for InMemoryGraph {
|
||||
fn upsert_file(&mut self, file: &str, nodes: Vec<Node>, edges: Vec<Edge>) {
|
||||
self.remove_file(file);
|
||||
|
||||
let mut owned = Vec::with_capacity(nodes.len());
|
||||
for node in nodes {
|
||||
let qn = node.name.clone();
|
||||
self.defs_by_name.entry(qn.base_name().to_string()).or_default().push(qn.clone());
|
||||
owned.push(qn.clone());
|
||||
self.nodes.insert(qn, node);
|
||||
}
|
||||
self.file_nodes.insert(file.to_string(), owned);
|
||||
|
||||
for edge in edges {
|
||||
if let EdgeTarget::Resolved(target) = &edge.to {
|
||||
self.in_edges.entry(target.clone()).or_default().push(edge.clone());
|
||||
}
|
||||
self.out_edges.entry(edge.from.clone()).or_default().push(edge);
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_file(&mut self, file: &str) {
|
||||
let Some(owned) = self.file_nodes.remove(file) else { return };
|
||||
for qn in &owned {
|
||||
self.nodes.remove(qn);
|
||||
|
||||
// Drop this node's outgoing edges, unhooking each from its target's reverse index.
|
||||
if let Some(outs) = self.out_edges.remove(qn) {
|
||||
for edge in outs {
|
||||
if let EdgeTarget::Resolved(target) = &edge.to {
|
||||
if let Some(ins) = self.in_edges.get_mut(target) {
|
||||
ins.retain(|e| e.from != *qn);
|
||||
if ins.is_empty() {
|
||||
self.in_edges.remove(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Drop reverse edges pointing at this node (their sources live in other files).
|
||||
self.in_edges.remove(qn);
|
||||
|
||||
// Drop this node from the name index.
|
||||
if let Some(defs) = self.defs_by_name.get_mut(qn.base_name()) {
|
||||
defs.retain(|d| d != qn);
|
||||
if defs.is_empty() {
|
||||
self.defs_by_name.remove(qn.base_name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn node(&self, name: &QualifiedName) -> Option<&Node> {
|
||||
self.nodes.get(name)
|
||||
}
|
||||
|
||||
fn out_edges(&self, name: &QualifiedName) -> &[Edge] {
|
||||
self.out_edges.get(name).map(Vec::as_slice).unwrap_or(NO_EDGES)
|
||||
}
|
||||
|
||||
fn in_edges(&self, name: &QualifiedName) -> &[Edge] {
|
||||
self.in_edges.get(name).map(Vec::as_slice).unwrap_or(NO_EDGES)
|
||||
}
|
||||
|
||||
fn defs_by_name(&self, base: &str) -> &[QualifiedName] {
|
||||
self.defs_by_name.get(base).map(Vec::as_slice).unwrap_or(NO_NAMES)
|
||||
}
|
||||
|
||||
fn symbols_in_file(&self, file: &str) -> Vec<QualifiedName> {
|
||||
self.file_nodes
|
||||
.get(file)
|
||||
.map(|owned| owned.iter().filter(|qn| !qn.is_file()).cloned().collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn node_count(&self) -> usize {
|
||||
self.nodes.len()
|
||||
}
|
||||
|
||||
fn edge_count(&self) -> usize {
|
||||
self.total_forward_edges()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::graph::model::{EdgeKind, NodeKind, Precision};
|
||||
|
||||
fn func(file: &str, name: &str) -> Node {
|
||||
Node {
|
||||
name: QualifiedName::symbol(file, None, name),
|
||||
kind: NodeKind::Function,
|
||||
start_line: 0,
|
||||
end_line: 1,
|
||||
is_test: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn resolved_edge(from: QualifiedName, to: QualifiedName, kind: EdgeKind) -> Edge {
|
||||
Edge { from, to: EdgeTarget::Resolved(to), kind, precision: Precision::Exact }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upsert_populates_forward_reverse_and_name_index() {
|
||||
let mut g = InMemoryGraph::new();
|
||||
let file = "src/a.rs";
|
||||
let caller = func(file, "caller");
|
||||
let callee = func(file, "callee");
|
||||
let edge = resolved_edge(caller.name.clone(), callee.name.clone(), EdgeKind::Calls);
|
||||
|
||||
g.upsert_file(file, vec![caller.clone(), callee.clone()], vec![edge.clone()]);
|
||||
|
||||
assert_eq!(g.node_count(), 2);
|
||||
assert_eq!(g.edge_count(), 1);
|
||||
assert!(g.node(&caller.name).is_some());
|
||||
assert_eq!(g.out_edges(&caller.name), std::slice::from_ref(&edge));
|
||||
assert_eq!(g.in_edges(&callee.name), std::slice::from_ref(&edge));
|
||||
assert_eq!(g.defs_by_name("callee"), std::slice::from_ref(&callee.name));
|
||||
// Nothing points at the caller yet.
|
||||
assert!(g.in_edges(&caller.name).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_file_clears_nodes_edges_and_reverse_index() {
|
||||
let mut g = InMemoryGraph::new();
|
||||
let file = "src/a.rs";
|
||||
let caller = func(file, "caller");
|
||||
let callee = func(file, "callee");
|
||||
let edge = resolved_edge(caller.name.clone(), callee.name.clone(), EdgeKind::Calls);
|
||||
g.upsert_file(file, vec![caller.clone(), callee.clone()], vec![edge]);
|
||||
|
||||
g.remove_file(file);
|
||||
|
||||
assert_eq!(g.node_count(), 0);
|
||||
assert_eq!(g.edge_count(), 0);
|
||||
assert!(g.in_edges(&callee.name).is_empty());
|
||||
assert!(g.defs_by_name("callee").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn upsert_replaces_prior_version_of_same_file() {
|
||||
let mut g = InMemoryGraph::new();
|
||||
let file = "src/a.rs";
|
||||
g.upsert_file(file, vec![func(file, "old")], vec![]);
|
||||
assert_eq!(g.defs_by_name("old").len(), 1);
|
||||
|
||||
// Re-index the file with a renamed symbol — the old one must disappear.
|
||||
g.upsert_file(file, vec![func(file, "new")], vec![]);
|
||||
assert!(g.defs_by_name("old").is_empty());
|
||||
assert_eq!(g.defs_by_name("new").len(), 1);
|
||||
assert_eq!(g.node_count(), 1);
|
||||
}
|
||||
}
|
||||
124
crates/luxe-lsp/src/graph/testing.rs
Normal file
124
crates/luxe-lsp/src/graph/testing.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
//! Testing — the one answer to "is this test code?".
|
||||
//!
|
||||
//! Two callers need it and they must not disagree: [`extract`](super::extract) marks a node
|
||||
//! `is_test` (which is what derives `TestCovers`, and so what "covered" means), and
|
||||
//! [`review`](super::review) refuses to ask a test file for a covering test. If they drift, a
|
||||
//! layout like Rust's `mod tests;` in a sibling file reads every real test as production code: the
|
||||
//! changed code is badged untested while its test sits one file away.
|
||||
//!
|
||||
//! The predicates stay deliberately conservative: an outline carries no attributes, so `#[test]`
|
||||
//! is invisible here and layout is all there is to go on.
|
||||
|
||||
/// Whether a path is test code by convention, across the languages the graph extracts.
|
||||
///
|
||||
/// Paths are compared with forward slashes, so a Windows-shaped path answers the same.
|
||||
pub(super) fn is_test_path(path: &str) -> bool {
|
||||
let normalized = path.replace('\\', "/");
|
||||
let name = normalized.rsplit('/').next().unwrap_or(&normalized);
|
||||
normalized.starts_with("tests/")
|
||||
|| normalized.starts_with("test/")
|
||||
|| normalized.contains("/tests/")
|
||||
|| normalized.contains("/test/")
|
||||
|| normalized.contains("/__tests__/")
|
||||
|| normalized.contains("/spec/")
|
||||
// Rust's `mod tests;` in a sibling file: the module *is* the file, so there is no `tests`
|
||||
// container for a symbol to sit in and no `test_` prefix on any of its functions.
|
||||
|| name == "tests.rs"
|
||||
|| name == "test.rs"
|
||||
|| name.starts_with("test_")
|
||||
|| name.ends_with("_test.rs")
|
||||
|| name.ends_with("_tests.rs")
|
||||
|| name.ends_with("_test.go")
|
||||
|| name.ends_with("_test.py")
|
||||
|| [".test.ts", ".test.tsx", ".test.js", ".spec.ts", ".spec.tsx", ".spec.js"]
|
||||
.iter()
|
||||
.any(|suffix| name.ends_with(suffix))
|
||||
}
|
||||
|
||||
/// Whether a function/method is test code, from its file, its container and its name.
|
||||
///
|
||||
/// `kind` is an LSP `SymbolKind`; anything that is not a function or method is never a test. The
|
||||
/// file is consulted first because it is the only signal that survives a layout where neither the
|
||||
/// container nor the name carries the word "test".
|
||||
pub(super) fn is_test_symbol(
|
||||
kind: u32,
|
||||
name: &str,
|
||||
container: Option<&str>,
|
||||
rel_path: &str,
|
||||
) -> bool {
|
||||
use crate::protocol::symbol_kinds as k;
|
||||
if kind != k::FUNCTION && kind != k::METHOD {
|
||||
return false;
|
||||
}
|
||||
if is_test_path(rel_path) {
|
||||
return true;
|
||||
}
|
||||
let container_is_test = container
|
||||
.is_some_and(|c| c.eq_ignore_ascii_case("tests") || c.eq_ignore_ascii_case("test"));
|
||||
let lower = name.to_ascii_lowercase();
|
||||
container_is_test
|
||||
|| name.starts_with("test_")
|
||||
|| name.starts_with("Test")
|
||||
|| lower.ends_with("_test")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::protocol::symbol_kinds as k;
|
||||
|
||||
#[test]
|
||||
fn test_paths_are_recognized_across_conventions() {
|
||||
for path in [
|
||||
"tests/auth_test.rs",
|
||||
// Each of these is carried by exactly one clause, so none of them can lean on
|
||||
// another to pass.
|
||||
"tests/integration.rs",
|
||||
"test/helpers.rs",
|
||||
"crates/x/tests/integration.rs",
|
||||
"src/parser_test.rs",
|
||||
"src/__tests__/button.tsx",
|
||||
"web/src/button.test.tsx",
|
||||
"api/handlers_test.go",
|
||||
"app/spec/models/user.rb",
|
||||
] {
|
||||
assert!(is_test_path(path), "{path} is test code");
|
||||
}
|
||||
for path in ["src/auth.rs", "crates/luxe-core/src/latest.rs", "src/contest.rs"] {
|
||||
assert!(!is_test_path(path), "{path} is production code");
|
||||
}
|
||||
}
|
||||
|
||||
/// The layout that broke it: `mod tests;` pointing at a sibling file. Luxe lays five of its own
|
||||
/// modules out this way, so the miss was never exotic.
|
||||
#[test]
|
||||
fn a_sibling_tests_module_is_test_code() {
|
||||
for path in [
|
||||
"src/app/tests.rs",
|
||||
"crates/luxe-tui/src/guided_review/tests.rs",
|
||||
"src/test.rs",
|
||||
"src\\app\\tests.rs",
|
||||
] {
|
||||
assert!(is_test_path(path), "{path} is test code");
|
||||
}
|
||||
// A file whose name merely ends in those letters is not.
|
||||
for path in ["src/latests.rs", "src/protest.rs"] {
|
||||
assert!(!is_test_path(path), "{path} is production code");
|
||||
}
|
||||
}
|
||||
|
||||
/// A `#[test]` function in a sibling test module has no `tests` container and no `test_`
|
||||
/// prefix, so its file is the only thing left that can identify it.
|
||||
#[test]
|
||||
fn a_test_functions_file_identifies_it_when_nothing_else_does() {
|
||||
assert!(is_test_symbol(k::FUNCTION, "spinner_char_cycles", None, "src/app/tests.rs"));
|
||||
assert!(is_test_symbol(k::FUNCTION, "works", Some("tests"), "src/auth.rs"));
|
||||
assert!(is_test_symbol(k::FUNCTION, "test_login", None, "src/auth.rs"));
|
||||
assert!(is_test_symbol(k::FUNCTION, "TestLogin", None, "src/auth.rs"));
|
||||
// Go and Python spell it the other way round, and this is the only clause that catches it.
|
||||
assert!(is_test_symbol(k::FUNCTION, "login_test", None, "src/auth.rs"));
|
||||
assert!(!is_test_symbol(k::FUNCTION, "login", None, "src/auth.rs"));
|
||||
// Only functions and methods are ever tests, wherever they live.
|
||||
assert!(!is_test_symbol(k::STRUCT, "Fixture", None, "src/app/tests.rs"));
|
||||
}
|
||||
}
|
||||
31
crates/luxe-lsp/src/lib.rs
Normal file
31
crates/luxe-lsp/src/lib.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
// luxe-lsp — Code intelligence: a warm LSP client plus tree-sitter outlines.
|
||||
//
|
||||
// Provides diagnostics, IDE-style navigation (definition/references/hover/call hierarchy/…),
|
||||
// and a discovery funnel (symbol_search → module_report → read_symbol) via the Language Server
|
||||
// Protocol, backed by warm, reused servers. The `syntax` module adds fast, offline tree-sitter
|
||||
// outlines so file overviews and symbol reads don't require a running server.
|
||||
|
||||
pub mod client;
|
||||
mod edit_apply;
|
||||
pub mod graph;
|
||||
mod message;
|
||||
pub mod protocol;
|
||||
pub mod registry;
|
||||
mod symbol_index;
|
||||
pub mod syntax;
|
||||
mod tools;
|
||||
mod uri;
|
||||
mod workspace;
|
||||
|
||||
pub use message::{JsonRpcError, JsonRpcMessage};
|
||||
pub use registry::{ServerEntry, ServerEntryPatch, ServerRegistry};
|
||||
|
||||
// Re-export public types for use by luxe-cli.
|
||||
pub use client::{LspClient, LspConfig, LspError};
|
||||
pub use protocol::{DiagnosticSeverity, Position, TextDocumentSyncKind};
|
||||
pub use symbol_index::{IndexError, IndexedSymbol, SymbolIndex, SymbolIndexCache};
|
||||
pub use tools::{
|
||||
ImpactTool, LspDiagnosticsTool, LspNavigationTool, ModuleReportTool, ReadSymbolTool,
|
||||
SymbolSearchTool,
|
||||
};
|
||||
pub use workspace::WorkspaceLspClient;
|
||||
153
crates/luxe-lsp/src/message.rs
Normal file
153
crates/luxe-lsp/src/message.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
// LSP message types for JSON-RPC communication.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A JSON-RPC message envelope.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JsonRpcMessage {
|
||||
/// JSON-RPC version (must be "2.0").
|
||||
pub jsonrpc: String,
|
||||
|
||||
/// Message ID (for requests/responses) or null (for notifications).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<serde_json::Value>,
|
||||
|
||||
/// Method name (for requests/notifications) or null (for responses).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub method: Option<String>,
|
||||
|
||||
/// Parameters (for requests/notifications).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub params: Option<serde_json::Value>,
|
||||
|
||||
/// Response result (only for responses).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub result: Option<serde_json::Value>,
|
||||
|
||||
/// Error object (only for error responses).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub error: Option<JsonRpcError>,
|
||||
}
|
||||
|
||||
/// JSON-RPC error object.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JsonRpcError {
|
||||
/// Error code.
|
||||
pub code: i32,
|
||||
|
||||
/// Error message.
|
||||
pub message: String,
|
||||
|
||||
/// Optional error data.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub data: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
impl JsonRpcMessage {
|
||||
/// Create a request message.
|
||||
pub fn request(id: u64, method: String, params: serde_json::Value) -> Self {
|
||||
Self {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
id: Some(serde_json::Value::Number(id.into())),
|
||||
method: Some(method),
|
||||
params: Some(params),
|
||||
result: None,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a notification message (no ID).
|
||||
pub fn notification(method: String, params: serde_json::Value) -> Self {
|
||||
Self {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
id: None,
|
||||
method: Some(method),
|
||||
params: Some(params),
|
||||
result: None,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a success response.
|
||||
pub fn response(id: u64, result: serde_json::Value) -> Self {
|
||||
Self {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
id: Some(serde_json::Value::Number(id.into())),
|
||||
method: None,
|
||||
params: None,
|
||||
result: Some(result),
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an error response.
|
||||
pub fn error_response(
|
||||
id: u64,
|
||||
code: i32,
|
||||
message: String,
|
||||
data: Option<serde_json::Value>,
|
||||
) -> Self {
|
||||
Self {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
id: Some(serde_json::Value::Number(id.into())),
|
||||
method: None,
|
||||
params: None,
|
||||
result: None,
|
||||
error: Some(JsonRpcError { code, message, data }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_request_serialization() {
|
||||
let msg = JsonRpcMessage::request(
|
||||
1,
|
||||
"textDocument/hover".to_string(),
|
||||
serde_json::json!({"position": {}}),
|
||||
);
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert!(json.contains("2.0"));
|
||||
assert!(json.contains("textDocument/hover"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_notification_serialization() {
|
||||
let msg = JsonRpcMessage::notification("initialized".to_string(), serde_json::json!({}));
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert!(json.contains("initialized"));
|
||||
assert!(!json.contains("\"id\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_response_serialization() {
|
||||
let msg = JsonRpcMessage::response(1, serde_json::json!({"contents": "test"}));
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert!(json.contains("test"));
|
||||
assert!(!json.contains("method"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_response_serialization() {
|
||||
let msg = JsonRpcMessage::error_response(1, -32600, "Invalid Request".to_string(), None);
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert!(json.contains("-32600"));
|
||||
assert!(json.contains("Invalid Request"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_roundtrip_request() {
|
||||
let original = JsonRpcMessage::request(
|
||||
1,
|
||||
"textDocument/hover".to_string(),
|
||||
serde_json::json!({"position": {}}),
|
||||
);
|
||||
let json = serde_json::to_string(&original).unwrap();
|
||||
let deserialized: JsonRpcMessage = serde_json::from_str(&json).unwrap();
|
||||
assert_eq!(deserialized.id, original.id);
|
||||
assert_eq!(deserialized.method, original.method);
|
||||
}
|
||||
}
|
||||
401
crates/luxe-lsp/src/protocol.rs
Normal file
401
crates/luxe-lsp/src/protocol.rs
Normal file
@@ -0,0 +1,401 @@
|
||||
// LSP protocol types and constants.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Text document sync kind.
|
||||
///
|
||||
/// Wire format is an integer per the LSP spec (`TextDocumentSyncKind = 0|1|2`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(try_from = "u8", into = "u8")]
|
||||
pub enum TextDocumentSyncKind {
|
||||
None = 0,
|
||||
Full = 1,
|
||||
Incremental = 2,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for TextDocumentSyncKind {
|
||||
type Error = String;
|
||||
fn try_from(v: u8) -> Result<Self, String> {
|
||||
match v {
|
||||
0 => Ok(Self::None),
|
||||
1 => Ok(Self::Full),
|
||||
2 => Ok(Self::Incremental),
|
||||
_ => Err(format!("invalid TextDocumentSyncKind: {}", v)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TextDocumentSyncKind> for u8 {
|
||||
fn from(v: TextDocumentSyncKind) -> u8 {
|
||||
v as u8
|
||||
}
|
||||
}
|
||||
|
||||
/// Diagnostic severity levels.
|
||||
///
|
||||
/// Wire format is an integer per the LSP spec (3.17: `severity?: number`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(try_from = "u8", into = "u8")]
|
||||
pub enum DiagnosticSeverity {
|
||||
Error = 1,
|
||||
Warning = 2,
|
||||
Information = 3,
|
||||
Hint = 4,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for DiagnosticSeverity {
|
||||
type Error = String;
|
||||
fn try_from(v: u8) -> Result<Self, String> {
|
||||
match v {
|
||||
1 => Ok(Self::Error),
|
||||
2 => Ok(Self::Warning),
|
||||
3 => Ok(Self::Information),
|
||||
4 => Ok(Self::Hint),
|
||||
_ => Err(format!("invalid DiagnosticSeverity: {}", v)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DiagnosticSeverity> for u8 {
|
||||
fn from(v: DiagnosticSeverity) -> u8 {
|
||||
v as u8
|
||||
}
|
||||
}
|
||||
|
||||
/// Diagnostic tags.
|
||||
///
|
||||
/// Wire format is an integer per the LSP spec (`DiagnosticTag = 1|2`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(try_from = "u8", into = "u8")]
|
||||
pub enum DiagnosticTag {
|
||||
Unnecessary = 1,
|
||||
Deprecated = 2,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for DiagnosticTag {
|
||||
type Error = String;
|
||||
fn try_from(v: u8) -> Result<Self, String> {
|
||||
match v {
|
||||
1 => Ok(Self::Unnecessary),
|
||||
2 => Ok(Self::Deprecated),
|
||||
_ => Err(format!("invalid DiagnosticTag: {}", v)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DiagnosticTag> for u8 {
|
||||
fn from(v: DiagnosticTag) -> u8 {
|
||||
v as u8
|
||||
}
|
||||
}
|
||||
|
||||
/// A location in a text document.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Location {
|
||||
/// URI of the text document.
|
||||
pub uri: String,
|
||||
|
||||
/// Range within the document.
|
||||
pub range: Range,
|
||||
}
|
||||
|
||||
/// A range in a text document.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Range {
|
||||
/// Start position.
|
||||
pub start: Position,
|
||||
|
||||
/// End position.
|
||||
pub end: Position,
|
||||
}
|
||||
|
||||
/// A position in a text document.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Position {
|
||||
/// Line number (0-indexed).
|
||||
pub line: u32,
|
||||
|
||||
/// Character offset (0-indexed).
|
||||
pub character: u32,
|
||||
}
|
||||
|
||||
/// A diagnostic message.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Diagnostic {
|
||||
/// Range within the document.
|
||||
pub range: Range,
|
||||
|
||||
/// Severity level.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub severity: Option<DiagnosticSeverity>,
|
||||
|
||||
/// Source of the diagnostic (e.g., "clippy").
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub source: Option<String>,
|
||||
|
||||
/// Diagnostic code.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub code: Option<serde_json::Value>,
|
||||
|
||||
/// Message text.
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
/// A hover result with markup content.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Hover {
|
||||
/// Markup content (markdown).
|
||||
pub contents: MarkupContent,
|
||||
|
||||
/// Range to highlight (optional).
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub range: Option<Range>,
|
||||
}
|
||||
|
||||
/// Markup content for hover results.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct MarkupContent {
|
||||
/// Markup language (e.g., "markdown").
|
||||
pub kind: String,
|
||||
|
||||
/// Value (markdown text).
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
/// A symbol information object.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SymbolInformation {
|
||||
/// Name of the symbol.
|
||||
pub name: String,
|
||||
|
||||
/// Kind of symbol (e.g., "function", "class").
|
||||
pub kind: u32,
|
||||
|
||||
/// Tags for the symbol.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub tags: Option<Vec<SymbolTag>>,
|
||||
|
||||
/// Location of the symbol.
|
||||
pub location: Location,
|
||||
|
||||
/// Optional parent name.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub container_name: Option<String>,
|
||||
}
|
||||
|
||||
/// Symbol kind tags.
|
||||
///
|
||||
/// Wire format is an integer per the LSP spec (`SymbolTag = 1`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(try_from = "u8", into = "u8")]
|
||||
pub enum SymbolTag {
|
||||
Deprecated = 1,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for SymbolTag {
|
||||
type Error = String;
|
||||
fn try_from(v: u8) -> Result<Self, String> {
|
||||
match v {
|
||||
1 => Ok(Self::Deprecated),
|
||||
_ => Err(format!("invalid SymbolTag: {}", v)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SymbolTag> for u8 {
|
||||
fn from(v: SymbolTag) -> u8 {
|
||||
v as u8
|
||||
}
|
||||
}
|
||||
|
||||
/// A normalized symbol result, flattened from either `SymbolInformation` (flat, has `location`) or
|
||||
/// `DocumentSymbol` (hierarchical, has `range`/`selectionRange`/`children`) so callers don't care
|
||||
/// which shape a server returns. `uri` is empty for document-scoped results.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct FlatSymbol {
|
||||
/// Symbol name.
|
||||
pub name: String,
|
||||
/// LSP `SymbolKind` integer.
|
||||
pub kind: u32,
|
||||
/// Document URI (empty for `documentSymbol` results).
|
||||
pub uri: String,
|
||||
/// 0-based line of the symbol's selection (name) start — the navigation position.
|
||||
pub line: u32,
|
||||
/// 0-based character of the symbol's selection (name) start.
|
||||
pub character: u32,
|
||||
/// 0-based first line of the symbol's full extent (its body).
|
||||
pub start_line: u32,
|
||||
/// 0-based last line of the symbol's full extent (its body).
|
||||
pub end_line: u32,
|
||||
/// Enclosing container/parent name, if any.
|
||||
pub container: Option<String>,
|
||||
}
|
||||
|
||||
/// Human-readable name for an LSP `SymbolKind` integer.
|
||||
pub fn symbol_kind_name(kind: u32) -> &'static str {
|
||||
use symbol_kinds as k;
|
||||
match kind {
|
||||
k::FILE => "file",
|
||||
k::MODULE => "module",
|
||||
k::NAMESPACE => "namespace",
|
||||
k::PACKAGE => "package",
|
||||
k::CLASS => "class",
|
||||
k::METHOD => "method",
|
||||
k::PROPERTY => "property",
|
||||
k::FIELD => "field",
|
||||
k::CONSTRUCTOR => "constructor",
|
||||
k::ENUM => "enum",
|
||||
k::INTERFACE => "interface",
|
||||
k::FUNCTION => "function",
|
||||
k::VARIABLE => "variable",
|
||||
k::CONSTANT => "constant",
|
||||
k::STRUCT => "struct",
|
||||
k::ENUM_MEMBER => "enum-member",
|
||||
k::TYPE_PARAMETER => "type-param",
|
||||
k::OPERATOR => "operator",
|
||||
k::EVENT => "event",
|
||||
_ => "symbol",
|
||||
}
|
||||
}
|
||||
|
||||
/// Symbol kinds (from LSP spec), minus the value kinds 15–21 that nothing here labels.
|
||||
pub(crate) mod symbol_kinds {
|
||||
pub(crate) const FILE: u32 = 1;
|
||||
pub(crate) const MODULE: u32 = 2;
|
||||
pub(crate) const NAMESPACE: u32 = 3;
|
||||
pub(crate) const PACKAGE: u32 = 4;
|
||||
pub(crate) const CLASS: u32 = 5;
|
||||
pub(crate) const METHOD: u32 = 6;
|
||||
pub(crate) const PROPERTY: u32 = 7;
|
||||
pub(crate) const FIELD: u32 = 8;
|
||||
pub(crate) const CONSTRUCTOR: u32 = 9;
|
||||
pub(crate) const ENUM: u32 = 10;
|
||||
pub(crate) const INTERFACE: u32 = 11;
|
||||
pub(crate) const FUNCTION: u32 = 12;
|
||||
pub(crate) const VARIABLE: u32 = 13;
|
||||
pub(crate) const CONSTANT: u32 = 14;
|
||||
pub(crate) const ENUM_MEMBER: u32 = 22;
|
||||
pub(crate) const STRUCT: u32 = 23;
|
||||
pub(crate) const EVENT: u32 = 24;
|
||||
pub(crate) const OPERATOR: u32 = 25;
|
||||
pub(crate) const TYPE_PARAMETER: u32 = 26;
|
||||
}
|
||||
|
||||
/// Document highlight kind.
|
||||
///
|
||||
/// Wire format is an integer per the LSP spec (`DocumentHighlightKind = 1|2|3`).
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(try_from = "u8", into = "u8")]
|
||||
pub enum DocumentHighlightKind {
|
||||
Text = 1,
|
||||
Read = 2,
|
||||
Write = 3,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for DocumentHighlightKind {
|
||||
type Error = String;
|
||||
fn try_from(v: u8) -> Result<Self, String> {
|
||||
match v {
|
||||
1 => Ok(Self::Text),
|
||||
2 => Ok(Self::Read),
|
||||
3 => Ok(Self::Write),
|
||||
_ => Err(format!("invalid DocumentHighlightKind: {}", v)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DocumentHighlightKind> for u8 {
|
||||
fn from(v: DocumentHighlightKind) -> u8 {
|
||||
v as u8
|
||||
}
|
||||
}
|
||||
|
||||
/// A document highlight.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DocumentHighlight {
|
||||
/// Range of the highlight.
|
||||
pub range: Range,
|
||||
|
||||
/// Kind of highlight.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub kind: Option<DocumentHighlightKind>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_position_serialization() {
|
||||
let pos = Position { line: 10, character: 5 };
|
||||
let json = serde_json::to_string(&pos).unwrap();
|
||||
assert!(json.contains("10"));
|
||||
assert!(json.contains("5"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_range_serialization() {
|
||||
let range = Range {
|
||||
start: Position { line: 0, character: 0 },
|
||||
end: Position { line: 1, character: 10 },
|
||||
};
|
||||
let json = serde_json::to_string(&range).unwrap();
|
||||
assert!(json.contains("0"));
|
||||
assert!(json.contains("10"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diagnostic_serialization() {
|
||||
let diag = Diagnostic {
|
||||
range: Range {
|
||||
start: Position { line: 5, character: 0 },
|
||||
end: Position { line: 5, character: 10 },
|
||||
},
|
||||
severity: Some(DiagnosticSeverity::Error),
|
||||
source: Some("clippy".to_string()),
|
||||
code: Some(serde_json::json!("clippy::unnecessary_clone")),
|
||||
message: "This clone is unnecessary".to_string(),
|
||||
};
|
||||
let json = serde_json::to_string(&diag).unwrap();
|
||||
assert!(json.contains("1")); // Error severity = 1
|
||||
assert!(json.contains("clippy"));
|
||||
assert!(json.contains("This clone is unnecessary"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_symbol_kinds() {
|
||||
assert_eq!(symbol_kinds::FUNCTION, 12);
|
||||
assert_eq!(symbol_kinds::CLASS, 5);
|
||||
assert_eq!(symbol_kinds::VARIABLE, 13);
|
||||
}
|
||||
|
||||
/// Every LSP enum here must (de)serialize as an integer — real servers send
|
||||
/// numbers, not strings. A regression to string repr silently drops diagnostics.
|
||||
#[test]
|
||||
fn test_enums_use_integer_wire_format() {
|
||||
assert_eq!(serde_json::to_string(&DiagnosticSeverity::Warning).unwrap(), "2");
|
||||
assert_eq!(serde_json::to_string(&TextDocumentSyncKind::Incremental).unwrap(), "2");
|
||||
assert_eq!(serde_json::to_string(&DiagnosticTag::Deprecated).unwrap(), "2");
|
||||
assert_eq!(serde_json::to_string(&SymbolTag::Deprecated).unwrap(), "1");
|
||||
assert_eq!(serde_json::to_string(&DocumentHighlightKind::Write).unwrap(), "3");
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_str::<DiagnosticSeverity>("1").unwrap(),
|
||||
DiagnosticSeverity::Error
|
||||
);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<TextDocumentSyncKind>("0").unwrap(),
|
||||
TextDocumentSyncKind::None
|
||||
);
|
||||
assert_eq!(serde_json::from_str::<DiagnosticTag>("1").unwrap(), DiagnosticTag::Unnecessary);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<DocumentHighlightKind>("2").unwrap(),
|
||||
DocumentHighlightKind::Read
|
||||
);
|
||||
|
||||
// Strings (the old buggy wire format) and out-of-range ints must be rejected.
|
||||
assert!(serde_json::from_str::<DiagnosticSeverity>("\"error\"").is_err());
|
||||
assert!(serde_json::from_str::<DiagnosticSeverity>("99").is_err());
|
||||
assert!(serde_json::from_str::<DocumentHighlightKind>("\"write\"").is_err());
|
||||
}
|
||||
}
|
||||
224
crates/luxe-lsp/src/registry.rs
Normal file
224
crates/luxe-lsp/src/registry.rs
Normal file
@@ -0,0 +1,224 @@
|
||||
//! LSP server registry — language id → server command and config.
|
||||
//!
|
||||
//! Detects a file's language from its extension and finds its workspace root by marker files
|
||||
//! (`Cargo.toml`, `package.json`, …).
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// One way to launch a server for a language.
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct ServerCommand {
|
||||
/// Program name, resolved against `PATH` and the well-known bin directories.
|
||||
pub command: String,
|
||||
/// Arguments to pass it.
|
||||
#[serde(default)]
|
||||
pub args: Vec<String>,
|
||||
}
|
||||
|
||||
/// Configuration for a language server entry.
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct ServerEntry {
|
||||
/// Command to launch the language server (e.g., "rust-analyzer").
|
||||
pub command: String,
|
||||
|
||||
/// Arguments to pass to the language server.
|
||||
#[serde(default)]
|
||||
pub args: Vec<String>,
|
||||
|
||||
/// Further ways to launch this language, tried in order after [`Self::command`].
|
||||
///
|
||||
/// A language is not a server, and the two drift apart. TypeScript 7 is the native port: it
|
||||
/// ships no `tsserver.js`, so `typescript-language-server` — at every version — fails its
|
||||
/// handshake with "Could not find a valid TypeScript installation", and the language service
|
||||
/// moved to `tsc --lsp`. One hardcoded command turns that into "Luxe has no TypeScript
|
||||
/// support"; a list turns it into a fallback nobody has to notice.
|
||||
///
|
||||
/// Ordered most-preferred first, and tried on a *failed handshake*, not merely a missing
|
||||
/// binary — `tsc` is on `PATH` for both TypeScript 6 and 7 and only one of them answers.
|
||||
#[serde(default)]
|
||||
pub alternatives: Vec<ServerCommand>,
|
||||
|
||||
/// Root marker files/directories to detect workspace root (e.g., "Cargo.toml").
|
||||
#[serde(default)]
|
||||
pub root_markers: Vec<String>,
|
||||
|
||||
/// File extensions (no leading dot) this server handles, e.g. `["rs"]`. Drives language
|
||||
/// detection, so a config-added server auto-routes by extension.
|
||||
#[serde(default)]
|
||||
pub extensions: Vec<String>,
|
||||
|
||||
/// Additional initialization options.
|
||||
#[serde(default)]
|
||||
pub init_options: serde_json::Value,
|
||||
|
||||
/// How to install the server, surfaced when it isn't found (e.g. `"gem install ruby-lsp"`).
|
||||
#[serde(default)]
|
||||
pub install_hint: Option<String>,
|
||||
}
|
||||
|
||||
impl ServerEntry {
|
||||
/// Every way to launch this language, most preferred first.
|
||||
pub fn candidates(&self) -> Vec<ServerCommand> {
|
||||
std::iter::once(ServerCommand { command: self.command.clone(), args: self.args.clone() })
|
||||
.chain(self.alternatives.iter().cloned())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// A user's `lsp-servers.json` entry: every field optional.
|
||||
///
|
||||
/// The distinction that matters is *absent* versus *empty*. [`ServerEntry`] cannot express it —
|
||||
/// its fields are `#[serde(default)]`, so `{"rust": {"command": "rust-analyzer-nightly"}}`
|
||||
/// deserializes to an entry with no extensions and no root markers, and merging that over the
|
||||
/// built-in would silently remove Rust support: `.rs` would stop detecting as a language. A patch
|
||||
/// inherits every field the user did not mention, which also means a field added to `ServerEntry`
|
||||
/// later starts life inherited rather than blanked out of every existing config.
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct ServerEntryPatch {
|
||||
/// Program to launch; required when the language has no built-in to inherit from.
|
||||
pub command: Option<String>,
|
||||
pub args: Option<Vec<String>>,
|
||||
pub alternatives: Option<Vec<ServerCommand>>,
|
||||
pub root_markers: Option<Vec<String>>,
|
||||
pub extensions: Option<Vec<String>>,
|
||||
pub init_options: Option<serde_json::Value>,
|
||||
pub install_hint: Option<String>,
|
||||
}
|
||||
|
||||
impl ServerEntryPatch {
|
||||
/// Apply this patch over the shipped entry for the same language, if there is one.
|
||||
///
|
||||
/// Returns `None` for a patch that names a language Luxe does not ship *and* omits `command`,
|
||||
/// which is the one combination that cannot produce a launchable server.
|
||||
pub fn apply(self, base: Option<&ServerEntry>) -> Option<ServerEntry> {
|
||||
let base = base.cloned().unwrap_or_default();
|
||||
let command =
|
||||
self.command.or_else(|| (!base.command.is_empty()).then_some(base.command))?;
|
||||
Some(ServerEntry {
|
||||
command,
|
||||
args: self.args.unwrap_or(base.args),
|
||||
alternatives: self.alternatives.unwrap_or(base.alternatives),
|
||||
root_markers: self.root_markers.unwrap_or(base.root_markers),
|
||||
extensions: self.extensions.unwrap_or(base.extensions),
|
||||
init_options: self.init_options.unwrap_or(base.init_options),
|
||||
install_hint: self.install_hint.or(base.install_hint),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The server registry: language_id → ServerEntry.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ServerRegistry {
|
||||
entries: HashMap<String, ServerEntry>,
|
||||
}
|
||||
|
||||
impl ServerRegistry {
|
||||
/// Create a new registry with built-in entries (rust-analyzer, etc.).
|
||||
pub fn new() -> Self {
|
||||
let mut registry = Self::default();
|
||||
registry.add_defaults();
|
||||
registry
|
||||
}
|
||||
|
||||
/// Add default server entries for the common languages from the bundled `default-lsp-servers
|
||||
/// .json`. Shipping them as DATA (not hardcoded) lets users inspect + copy the defaults, and
|
||||
/// built-in + user config share one schema and code path. Servers are external binaries the
|
||||
/// user installs; Luxe only knows how to launch + detect them.
|
||||
fn add_defaults(&mut self) {
|
||||
// Compile-time-embedded and covered by `default_servers_bundle_parses` — a genuine
|
||||
// invariant, so a parse failure is a build-time bug, never a runtime path.
|
||||
let defaults: HashMap<String, ServerEntry> = serde_json::from_str(DEFAULT_SERVERS_JSON)
|
||||
.expect("bundled default-lsp-servers.json must be valid");
|
||||
self.entries.extend(defaults);
|
||||
}
|
||||
|
||||
/// Register a custom server entry.
|
||||
pub fn register(&mut self, language_id: String, entry: ServerEntry) {
|
||||
self.entries.insert(language_id, entry);
|
||||
}
|
||||
|
||||
/// Merge user-configured servers (from `lsp-servers.json`), overriding built-ins by language id.
|
||||
///
|
||||
/// Whole-entry replacement: use it for an entry you built programmatically and mean in full.
|
||||
/// User config goes through [`Self::merge_patches`], which inherits what it does not mention.
|
||||
pub fn merge_custom(&mut self, custom: HashMap<String, ServerEntry>) {
|
||||
self.entries.extend(custom);
|
||||
}
|
||||
|
||||
/// Merge user config, inheriting every field a patch leaves out from the shipped entry.
|
||||
///
|
||||
/// Returns the language ids of patches that could not be applied — a new language with no
|
||||
/// `command` — so a caller can say so rather than drop them silently.
|
||||
pub fn merge_patches(&mut self, custom: HashMap<String, ServerEntryPatch>) -> Vec<String> {
|
||||
let mut rejected = Vec::new();
|
||||
for (language_id, patch) in custom {
|
||||
match patch.apply(self.entries.get(&language_id)) {
|
||||
Some(entry) => {
|
||||
self.entries.insert(language_id, entry);
|
||||
}
|
||||
None => rejected.push(language_id),
|
||||
}
|
||||
}
|
||||
rejected.sort();
|
||||
rejected
|
||||
}
|
||||
|
||||
/// The install hint for a language's server, when known.
|
||||
pub fn install_hint(&self, language_id: &str) -> Option<&str> {
|
||||
self.entries.get(language_id).and_then(|e| e.install_hint.as_deref())
|
||||
}
|
||||
|
||||
/// Detect the language ID for a file from its extension, driven by the registered entries so
|
||||
/// config-added servers route automatically.
|
||||
pub fn detect_language(&self, path: &Path) -> Option<String> {
|
||||
let ext = path.extension()?.to_str()?;
|
||||
self.entries
|
||||
.iter()
|
||||
.find(|(_, entry)| entry.extensions.iter().any(|candidate| candidate == ext))
|
||||
.map(|(lang, _)| lang.clone())
|
||||
}
|
||||
|
||||
/// Find the workspace root for a file by searching up the directory tree.
|
||||
pub fn find_root(&self, path: &Path, markers: &[String]) -> Option<PathBuf> {
|
||||
let mut current = path.parent()?;
|
||||
|
||||
loop {
|
||||
for marker in markers {
|
||||
if current.join(marker).exists() {
|
||||
return Some(current.to_path_buf());
|
||||
}
|
||||
}
|
||||
|
||||
match current.parent() {
|
||||
Some(parent) => current = parent,
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Get the server entry for a language ID.
|
||||
pub fn get(&self, language_id: &str) -> Option<&ServerEntry> {
|
||||
self.entries.get(language_id)
|
||||
}
|
||||
|
||||
/// Check if a language is supported.
|
||||
pub fn is_supported(&self, language_id: &str) -> bool {
|
||||
self.entries.contains_key(language_id)
|
||||
}
|
||||
|
||||
/// Iterate over `(language_id, entry)` for every registered server (built-in + merged config),
|
||||
/// for status/introspection surfaces like the `/lsp` command.
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&String, &ServerEntry)> {
|
||||
self.entries.iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// The shipped default LSP-server registry (common languages), merged first in every registry.
|
||||
const DEFAULT_SERVERS_JSON: &str = include_str!("default-lsp-servers.json");
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
169
crates/luxe-lsp/src/registry/tests.rs
Normal file
169
crates/luxe-lsp/src/registry/tests.rs
Normal file
@@ -0,0 +1,169 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_registry_creation() {
|
||||
let registry = ServerRegistry::new();
|
||||
assert!(registry.is_supported("rust"));
|
||||
assert!(registry.is_supported("typescript"));
|
||||
assert!(registry.is_supported("python"));
|
||||
assert!(registry.is_supported("go"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_detect_language() {
|
||||
let registry = ServerRegistry::new();
|
||||
assert_eq!(registry.detect_language(Path::new("main.rs")), Some("rust".to_string()));
|
||||
assert_eq!(registry.detect_language(Path::new("app.ts")), Some("typescript".to_string()));
|
||||
assert_eq!(registry.detect_language(Path::new("script.py")), Some("python".to_string()));
|
||||
assert_eq!(registry.detect_language(Path::new("main.go")), Some("go".to_string()));
|
||||
assert_eq!(registry.detect_language(Path::new("readme.md")), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_register_custom() {
|
||||
let mut registry = ServerRegistry::default();
|
||||
registry.register(
|
||||
"custom".to_string(),
|
||||
ServerEntry {
|
||||
command: "custom-ls".to_string(),
|
||||
root_markers: vec![".customrc".to_string()],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
assert!(registry.is_supported("custom"));
|
||||
assert_eq!(registry.get("custom").unwrap().command, "custom-ls");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_servers_bundle_parses() {
|
||||
let defaults: HashMap<String, ServerEntry> =
|
||||
serde_json::from_str(DEFAULT_SERVERS_JSON).expect("bundled defaults parse");
|
||||
for lang in ["rust", "typescript", "python", "go", "cpp", "bash", "svelte"] {
|
||||
let entry = defaults.get(lang).unwrap_or_else(|| panic!("missing {lang}"));
|
||||
assert!(!entry.command.is_empty(), "{lang} needs a command");
|
||||
assert!(!entry.extensions.is_empty(), "{lang} needs extensions");
|
||||
assert!(entry.install_hint.is_some(), "{lang} needs an install hint");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_expanded_builtins() {
|
||||
let r = ServerRegistry::new();
|
||||
assert_eq!(r.detect_language(Path::new("a.js")), Some("typescript".to_string()));
|
||||
assert_eq!(r.detect_language(Path::new("a.jsx")), Some("typescript".to_string()));
|
||||
assert_eq!(r.detect_language(Path::new("a.cpp")), Some("cpp".to_string()));
|
||||
assert_eq!(r.detect_language(Path::new("a.h")), Some("cpp".to_string()));
|
||||
assert_eq!(r.detect_language(Path::new("a.sh")), Some("bash".to_string()));
|
||||
assert!(r.install_hint("go").unwrap().contains("gopls"));
|
||||
}
|
||||
|
||||
/// A component is not TypeScript, however much of it is TypeScript.
|
||||
///
|
||||
/// `.svelte` has to reach `svelteserver` rather than the TypeScript entry: a `tsserver` handed
|
||||
/// a component parses the markup as expressions and reports errors on every line of it. The
|
||||
/// two extension sets must therefore stay disjoint, which is also what keeps
|
||||
/// [`ServerRegistry::detect_language`] — a scan for the first entry that claims the extension —
|
||||
/// from depending on which entry it happens to reach first.
|
||||
#[test]
|
||||
fn a_svelte_component_routes_to_its_own_server() {
|
||||
let registry = ServerRegistry::new();
|
||||
assert_eq!(registry.detect_language(Path::new("src/App.svelte")), Some("svelte".to_string()));
|
||||
|
||||
let typescript = registry.get("typescript").expect("typescript is registered");
|
||||
assert!(
|
||||
!typescript.extensions.iter().any(|ext| ext == "svelte"),
|
||||
"typescript must not also claim .svelte: {:?}",
|
||||
typescript.extensions
|
||||
);
|
||||
|
||||
let svelte = registry.get("svelte").expect("svelte is registered");
|
||||
assert!(
|
||||
svelte.root_markers.iter().any(|marker| marker == "package.json"),
|
||||
"a component's root is its package, and not every project has a svelte.config: {:?}",
|
||||
svelte.root_markers
|
||||
);
|
||||
assert!(
|
||||
svelte.candidates().iter().any(|c| c.command == "svelte-language-server"),
|
||||
"the package installs both names, and which one lands has changed between releases"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_servers_merge_and_route_by_extension() {
|
||||
let mut r = ServerRegistry::new();
|
||||
let json = r#"{"ruby":{"command":"ruby-lsp","extensions":["rb"],
|
||||
"root_markers":["Gemfile"],"install_hint":"gem install ruby-lsp"}}"#;
|
||||
let custom: HashMap<String, ServerEntry> = serde_json::from_str(json).unwrap();
|
||||
r.merge_custom(custom);
|
||||
assert_eq!(r.detect_language(Path::new("app.rb")), Some("ruby".to_string()));
|
||||
assert_eq!(r.install_hint("ruby"), Some("gem install ruby-lsp"));
|
||||
}
|
||||
|
||||
/// The most natural thing a user writes must not delete the language.
|
||||
///
|
||||
/// `{"rust": {"command": "rust-analyzer-nightly"}}` means "same as yours, different binary".
|
||||
/// Merged as a whole entry it meant "no extensions, no root markers", and `.rs` stopped being
|
||||
/// detected as a language at all — the LSP tools went quiet with nothing to explain why.
|
||||
#[test]
|
||||
fn a_config_that_names_only_a_command_keeps_the_rest_of_the_language() {
|
||||
let mut registry = ServerRegistry::new();
|
||||
let custom: HashMap<String, ServerEntryPatch> =
|
||||
serde_json::from_str(r#"{"rust":{"command":"rust-analyzer-nightly"}}"#).unwrap();
|
||||
assert!(registry.merge_patches(custom).is_empty(), "the patch applies");
|
||||
|
||||
let entry = registry.get("rust").expect("rust is still registered");
|
||||
assert_eq!(entry.command, "rust-analyzer-nightly", "the override takes effect");
|
||||
assert!(
|
||||
entry.extensions.iter().any(|e| e == "rs"),
|
||||
"extensions are inherited, not blanked: {:?}",
|
||||
entry.extensions
|
||||
);
|
||||
assert!(
|
||||
entry.root_markers.iter().any(|m| m == "Cargo.toml"),
|
||||
"root markers are inherited: {:?}",
|
||||
entry.root_markers
|
||||
);
|
||||
assert_eq!(
|
||||
registry.detect_language(Path::new("src/main.rs")).as_deref(),
|
||||
Some("rust"),
|
||||
"the language is still detected from its extension"
|
||||
);
|
||||
}
|
||||
|
||||
/// A patch may still say "nothing", as long as it says so.
|
||||
#[test]
|
||||
fn an_explicitly_empty_field_overrides_rather_than_inherits() {
|
||||
let mut registry = ServerRegistry::new();
|
||||
let custom: HashMap<String, ServerEntryPatch> =
|
||||
serde_json::from_str(r#"{"rust":{"args":[],"root_markers":[]}}"#).unwrap();
|
||||
registry.merge_patches(custom);
|
||||
let entry = registry.get("rust").unwrap();
|
||||
assert!(entry.root_markers.is_empty(), "an explicit [] means []");
|
||||
assert_eq!(entry.command, "rust-analyzer", "unmentioned fields still inherit");
|
||||
}
|
||||
|
||||
/// A brand-new language works, and a patch that cannot launch anything is reported.
|
||||
#[test]
|
||||
fn a_new_language_needs_a_command_and_says_so_when_it_lacks_one() {
|
||||
let mut registry = ServerRegistry::new();
|
||||
// Languages Luxe does not ship, so there is nothing for a patch to inherit a command from.
|
||||
// Asserted rather than assumed: this test read `lua` as unshipped until the day it was.
|
||||
for unshipped in ["elixir", "cobol"] {
|
||||
assert!(!registry.is_supported(unshipped), "{unshipped} must not be a built-in here");
|
||||
}
|
||||
let custom: HashMap<String, ServerEntryPatch> = serde_json::from_str(
|
||||
r#"{"elixir":{"command":"elixir-ls","extensions":["ex","exs"]},
|
||||
"cobol":{"extensions":["cbl"]}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let rejected = registry.merge_patches(custom);
|
||||
|
||||
assert_eq!(rejected, vec!["cobol".to_string()], "a new language with no command is named");
|
||||
assert!(registry.is_supported("elixir"), "a complete new language registers");
|
||||
assert_eq!(
|
||||
registry.detect_language(Path::new("app.exs")).as_deref(),
|
||||
Some("elixir"),
|
||||
"and routes by extension with no code change"
|
||||
);
|
||||
assert!(!registry.is_supported("cobol"), "the rejected one is not half-registered");
|
||||
}
|
||||
267
crates/luxe-lsp/src/symbol_index.rs
Normal file
267
crates/luxe-lsp/src/symbol_index.rs
Normal file
@@ -0,0 +1,267 @@
|
||||
//! Offline BM25 symbol index.
|
||||
//!
|
||||
//! Walks a project, extracts symbols with the tree-sitter [`crate::syntax`] outliner, and ranks
|
||||
//! them for `symbol_search` **without a language server** — across every tree-sitter-supported
|
||||
//! language at once. Names are tokenized on camelCase / snake_case / non-alphanumeric boundaries;
|
||||
//! ranking is exact → prefix → BM25 over the name tokens.
|
||||
//!
|
||||
//! The files come from [`luxe_core::walk`], the walk `glob` and `grep` use, so the index honors the
|
||||
//! same ignore files, sees them in sorted order, and says when it stopped short.
|
||||
|
||||
use crate::protocol::symbol_kind_name;
|
||||
use crate::syntax;
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
const K1: f64 = 1.2;
|
||||
const B: f64 = 0.75;
|
||||
/// Directories never descended into, ignore files or not: vendored and generated trees. Hidden
|
||||
/// ones (`.venv`, `.luxe`, `.yarn`) the walk already skips.
|
||||
const IGNORED_DIRS: &[&str] = &["target", "node_modules", "dist", "build", "vendor"];
|
||||
/// Safety caps so a pathological repo can't blow up the walk.
|
||||
pub(crate) const MAX_FILES: usize = 5000;
|
||||
const MAX_SYMBOLS: usize = 50_000;
|
||||
|
||||
/// One indexed symbol (project-relative).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IndexedSymbol {
|
||||
/// Symbol name.
|
||||
pub name: String,
|
||||
/// LSP `SymbolKind` integer.
|
||||
pub kind: u32,
|
||||
/// Project-relative file path.
|
||||
pub path: String,
|
||||
/// 1-based line of the symbol's name.
|
||||
pub line: u32,
|
||||
/// Enclosing container/parent name, if any.
|
||||
pub container: Option<String>,
|
||||
}
|
||||
|
||||
/// A built BM25 index over a project's symbols.
|
||||
pub struct SymbolIndex {
|
||||
symbols: Vec<IndexedSymbol>,
|
||||
tokens: Vec<Vec<String>>,
|
||||
df: HashMap<String, usize>,
|
||||
avgdl: f64,
|
||||
capped: bool,
|
||||
}
|
||||
|
||||
impl SymbolIndex {
|
||||
/// Build the index by walking `root` and outlining every supported source file.
|
||||
///
|
||||
/// Blocking (filesystem + parsing); call from `spawn_blocking`.
|
||||
pub fn build(root: &Path) -> Self {
|
||||
let SourceFiles { files, capped } = source_files(root);
|
||||
|
||||
let mut symbols = Vec::new();
|
||||
for file in files {
|
||||
if symbols.len() >= MAX_SYMBOLS {
|
||||
break;
|
||||
}
|
||||
let Ok(source) = std::fs::read_to_string(&file) else { continue };
|
||||
let Some(outline) = syntax::outline(&file, &source) else { continue };
|
||||
let rel = luxe_core::paths::display(file.strip_prefix(root).unwrap_or(&file));
|
||||
for sym in outline {
|
||||
symbols.push(IndexedSymbol {
|
||||
name: sym.name,
|
||||
kind: sym.kind,
|
||||
path: rel.clone(),
|
||||
line: sym.line + 1,
|
||||
container: sym.container,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let tokens: Vec<Vec<String>> = symbols.iter().map(|s| tokenize(&s.name)).collect();
|
||||
let mut df: HashMap<String, usize> = HashMap::new();
|
||||
for doc in &tokens {
|
||||
for term in doc.iter().collect::<std::collections::HashSet<_>>() {
|
||||
*df.entry(term.clone()).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
let total: usize = tokens.iter().map(Vec::len).sum();
|
||||
let avgdl = if tokens.is_empty() { 1.0 } else { total as f64 / tokens.len() as f64 };
|
||||
|
||||
Self { symbols, tokens, df, avgdl, capped }
|
||||
}
|
||||
|
||||
/// The line a search adds when the walk stopped at its file limit, so a symbol past it is
|
||||
/// missing, not absent; or nothing.
|
||||
pub fn capped_note(&self) -> String {
|
||||
if !self.capped {
|
||||
return String::new();
|
||||
}
|
||||
format!(" (index capped at {MAX_FILES} files; a symbol past it is missing, not absent)\n")
|
||||
}
|
||||
|
||||
/// Whether the index found no symbols (e.g. no tree-sitter-supported files).
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.symbols.is_empty()
|
||||
}
|
||||
|
||||
/// Total indexed symbols.
|
||||
pub fn len(&self) -> usize {
|
||||
self.symbols.len()
|
||||
}
|
||||
|
||||
/// Ranked search: exact name → prefix → BM25 over name tokens. Returns up to `limit` hits.
|
||||
pub fn search(&self, query: &str, limit: usize) -> Vec<&IndexedSymbol> {
|
||||
self.search_with_prior(query, limit, |_| 1.0)
|
||||
}
|
||||
|
||||
/// Like [`SymbolIndex::search`] but multiplies each hit's score by `prior(symbol)` (clamped to
|
||||
/// `>= 1.0`) before ranking — so a caller can fold in a graph-centrality prior (rank central
|
||||
/// symbols higher) without the index having to know about the graph. Tiers (exact → prefix →
|
||||
/// token) still dominate; the prior only reorders within a tier.
|
||||
pub fn search_with_prior(
|
||||
&self,
|
||||
query: &str,
|
||||
limit: usize,
|
||||
prior: impl Fn(&IndexedSymbol) -> f64,
|
||||
) -> Vec<&IndexedSymbol> {
|
||||
let query_lower = query.to_lowercase();
|
||||
let query_tokens = unique(tokenize(query));
|
||||
let n = self.symbols.len() as f64;
|
||||
|
||||
let mut scored: Vec<(u8, f64, usize)> = self
|
||||
.symbols
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, sym)| {
|
||||
let name_lower = sym.name.to_lowercase();
|
||||
let tier = if name_lower == query_lower {
|
||||
0
|
||||
} else if name_lower.starts_with(&query_lower) {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
};
|
||||
let score = self.bm25(&query_tokens, index, n);
|
||||
// Keep exact/prefix hits regardless; keep token hits only if they scored. The prior
|
||||
// (>= 1.0) can't resurrect a zero BM25 score, so token filtering is unchanged.
|
||||
(tier < 2 || score > 0.0).then_some((tier, score * prior(sym).max(1.0), index))
|
||||
})
|
||||
.collect();
|
||||
|
||||
scored.sort_by(|a, b| {
|
||||
a.0.cmp(&b.0)
|
||||
.then(b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal))
|
||||
.then_with(|| self.symbols[a.2].name.cmp(&self.symbols[b.2].name))
|
||||
});
|
||||
scored.into_iter().take(limit).map(|(_, _, index)| &self.symbols[index]).collect()
|
||||
}
|
||||
|
||||
fn bm25(&self, query_tokens: &[String], doc: usize, n: f64) -> f64 {
|
||||
let doc_tokens = &self.tokens[doc];
|
||||
let dl = doc_tokens.len() as f64;
|
||||
let mut score = 0.0;
|
||||
for term in query_tokens {
|
||||
let tf = doc_tokens.iter().filter(|t| *t == term).count() as f64;
|
||||
if tf == 0.0 {
|
||||
continue;
|
||||
}
|
||||
let df = self.df.get(term).copied().unwrap_or(0) as f64;
|
||||
let idf = ((n - df + 0.5) / (df + 0.5) + 1.0).ln();
|
||||
score += idf * (tf * (K1 + 1.0)) / (tf + K1 * (1.0 - B + B * dl / self.avgdl));
|
||||
}
|
||||
score
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn format_hit(sym: &IndexedSymbol) -> String {
|
||||
let container = sym.container.as_deref().map(|c| format!(" in {c}")).unwrap_or_default();
|
||||
format!("{} ({}) {}:{}{container}", sym.name, symbol_kind_name(sym.kind), sym.path, sym.line)
|
||||
}
|
||||
|
||||
/// The source files an index or graph reads, and whether there were more than it would take.
|
||||
pub(crate) struct SourceFiles {
|
||||
pub(crate) files: Vec<PathBuf>,
|
||||
pub(crate) capped: bool,
|
||||
}
|
||||
|
||||
/// Tree-sitter-supported source files under `root`, sorted, at most [`MAX_FILES`] of them. Shared
|
||||
/// with the code graph so both see the same file set.
|
||||
pub(crate) fn source_files(root: &Path) -> SourceFiles {
|
||||
let walked = luxe_core::walk::index_files(root, IGNORED_DIRS, syntax::supported, MAX_FILES);
|
||||
let files = walked.files.into_iter().map(|path| root.join(path)).collect();
|
||||
SourceFiles { files, capped: walked.capped }
|
||||
}
|
||||
|
||||
/// Tokenize an identifier on camelCase / snake_case / non-alphanumeric boundaries, lowercased.
|
||||
fn tokenize(text: &str) -> Vec<String> {
|
||||
let mut tokens = Vec::new();
|
||||
let mut current = String::new();
|
||||
let mut prev_lower_or_digit = false;
|
||||
for ch in text.chars() {
|
||||
if ch.is_alphanumeric() {
|
||||
if ch.is_uppercase() && prev_lower_or_digit && !current.is_empty() {
|
||||
tokens.push(std::mem::take(&mut current).to_lowercase());
|
||||
}
|
||||
current.push(ch);
|
||||
prev_lower_or_digit = ch.is_lowercase() || ch.is_numeric();
|
||||
} else {
|
||||
if !current.is_empty() {
|
||||
tokens.push(std::mem::take(&mut current).to_lowercase());
|
||||
}
|
||||
prev_lower_or_digit = false;
|
||||
}
|
||||
}
|
||||
if !current.is_empty() {
|
||||
tokens.push(current.to_lowercase());
|
||||
}
|
||||
tokens
|
||||
}
|
||||
|
||||
fn unique(mut tokens: Vec<String>) -> Vec<String> {
|
||||
tokens.sort();
|
||||
tokens.dedup();
|
||||
tokens
|
||||
}
|
||||
|
||||
/// Why there is no index to search.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum IndexError {
|
||||
/// The build panicked or was cancelled.
|
||||
#[error("the symbol index could not be built: {0}")]
|
||||
Build(#[source] tokio::task::JoinError),
|
||||
}
|
||||
|
||||
/// The warm index for one project root, rebuilt when the root changes or a file does.
|
||||
///
|
||||
/// Shared between the `symbol_search` tool and the file-changed hook, which is what keeps a symbol
|
||||
/// the agent just wrote findable on the next search.
|
||||
#[derive(Default)]
|
||||
pub struct SymbolIndexCache {
|
||||
built: tokio::sync::Mutex<Option<(PathBuf, std::sync::Arc<SymbolIndex>)>>,
|
||||
}
|
||||
|
||||
impl SymbolIndexCache {
|
||||
/// The index for `root`, built if there is none for it yet or `refresh` asks.
|
||||
pub async fn get(
|
||||
&self,
|
||||
root: &Path,
|
||||
refresh: bool,
|
||||
) -> Result<std::sync::Arc<SymbolIndex>, IndexError> {
|
||||
let mut built = self.built.lock().await;
|
||||
if let Some((indexed, index)) = built.as_ref().filter(|_| !refresh) {
|
||||
if indexed == root {
|
||||
return Ok(std::sync::Arc::clone(index));
|
||||
}
|
||||
}
|
||||
let owned = root.to_path_buf();
|
||||
let index = tokio::task::spawn_blocking(move || SymbolIndex::build(&owned))
|
||||
.await
|
||||
.map_err(IndexError::Build)?;
|
||||
let index = std::sync::Arc::new(index);
|
||||
*built = Some((root.to_path_buf(), std::sync::Arc::clone(&index)));
|
||||
Ok(index)
|
||||
}
|
||||
|
||||
/// Forgets the index, so the next search rebuilds it from what is on disk now.
|
||||
pub async fn invalidate(&self) {
|
||||
*self.built.lock().await = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
137
crates/luxe-lsp/src/symbol_index/tests.rs
Normal file
137
crates/luxe-lsp/src/symbol_index/tests.rs
Normal file
@@ -0,0 +1,137 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn tokenize_splits_camel_snake_and_kebab() {
|
||||
assert_eq!(tokenize("computeTotal"), vec!["compute", "total"]);
|
||||
assert_eq!(tokenize("compute_total"), vec!["compute", "total"]);
|
||||
assert_eq!(tokenize("HTTPServer"), vec!["httpserver"]);
|
||||
assert_eq!(tokenize("read_symbol2"), vec!["read", "symbol2"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_and_search_ranks_exact_prefix_then_bm25() {
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(dir.join("src")).unwrap();
|
||||
std::fs::write(
|
||||
dir.join("src/lib.rs"),
|
||||
"pub fn compute_total() {}\npub fn compute_subtotal() {}\npub struct Total;\n",
|
||||
)
|
||||
.unwrap();
|
||||
// Ignored dir must be skipped.
|
||||
std::fs::create_dir_all(dir.join("target/x")).unwrap();
|
||||
std::fs::write(dir.join("target/x/gen.rs"), "pub fn compute_total() {}\n").unwrap();
|
||||
|
||||
let index = SymbolIndex::build(&dir);
|
||||
assert_eq!(index.len(), 3, "target/ must be ignored");
|
||||
|
||||
// Exact "Total" wins over token matches.
|
||||
let hits = index.search("Total", 10);
|
||||
assert_eq!(hits[0].name, "Total");
|
||||
|
||||
// "compute total" (two tokens) matches both compute_* via BM25.
|
||||
let names: Vec<&str> =
|
||||
index.search("compute total", 10).iter().map(|s| s.name.as_str()).collect();
|
||||
assert!(names.contains(&"compute_total"));
|
||||
assert!(names.contains(&"compute_subtotal"));
|
||||
|
||||
// Prefix beats a mid-name token match.
|
||||
let hits = index.search("compute", 10);
|
||||
assert!(hits[0].name.starts_with("compute"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prior_reorders_within_a_tier() {
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(dir.join("src")).unwrap();
|
||||
std::fs::write(
|
||||
dir.join("src/lib.rs"),
|
||||
"pub fn compute_total() {}\npub fn compute_subtotal() {}\n",
|
||||
)
|
||||
.unwrap();
|
||||
let index = SymbolIndex::build(&dir);
|
||||
|
||||
// Both are prefix-tier matches for "compute"; a prior favoring subtotal lifts it to the top,
|
||||
// while a neutral prior leaves the baseline (name-ordered) ranking untouched.
|
||||
let boosted: Vec<&str> =
|
||||
index
|
||||
.search_with_prior("compute", 10, |sym| {
|
||||
if sym.name == "compute_subtotal" {
|
||||
3.0
|
||||
} else {
|
||||
1.0
|
||||
}
|
||||
})
|
||||
.iter()
|
||||
.map(|sym| sym.name.as_str())
|
||||
.collect();
|
||||
assert_eq!(boosted.first(), Some(&"compute_subtotal"));
|
||||
|
||||
let neutral: Vec<&str> =
|
||||
index.search_with_prior("compute", 10, |_| 1.0).iter().map(|s| s.name.as_str()).collect();
|
||||
let plain: Vec<&str> = index.search("compute", 10).iter().map(|s| s.name.as_str()).collect();
|
||||
assert_eq!(neutral, plain, "a neutral prior is identical to plain search");
|
||||
}
|
||||
|
||||
/// A symbol written after the first search is found on the next one, once the file-changed
|
||||
/// hook has had its say, and a search from another root reads that root.
|
||||
///
|
||||
/// The break this catches: the index was built once and served for the session, so what the
|
||||
/// agent wrote was "No symbols matching", and a new project answered from the old tree.
|
||||
#[tokio::test]
|
||||
async fn the_index_follows_new_symbols_and_other_roots() {
|
||||
let (one, two) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
|
||||
std::fs::write(one.path().join("lib.rs"), "pub fn first() {}\n").unwrap();
|
||||
std::fs::write(two.path().join("lib.rs"), "pub fn elsewhere() {}\n").unwrap();
|
||||
let cache = SymbolIndexCache::default();
|
||||
assert_eq!(cache.get(one.path(), false).await.unwrap().search("first", 5).len(), 1);
|
||||
|
||||
std::fs::write(one.path().join("more.rs"), "pub fn second() {}\n").unwrap();
|
||||
cache.invalidate().await;
|
||||
assert_eq!(cache.get(one.path(), false).await.unwrap().search("second", 5).len(), 1);
|
||||
|
||||
let other = cache.get(two.path(), false).await.unwrap();
|
||||
assert_eq!(other.search("elsewhere", 5).len(), 1);
|
||||
assert!(other.search("first", 5).is_empty(), "not the previous project's symbols");
|
||||
}
|
||||
|
||||
/// A tree with more source files than the index takes says it stopped, and keeps the first
|
||||
/// ones in sorted order rather than whichever the filesystem listed.
|
||||
#[test]
|
||||
fn a_capped_walk_says_so_and_keeps_sorted_order() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for index in 0..=MAX_FILES {
|
||||
std::fs::write(dir.path().join(format!("f{index:05}.rs")), "").unwrap();
|
||||
}
|
||||
let SourceFiles { files, capped } = source_files(dir.path());
|
||||
assert!(capped);
|
||||
assert_eq!(files.len(), MAX_FILES);
|
||||
assert!(files.windows(2).all(|pair| pair[0] < pair[1]), "sorted");
|
||||
assert!(files[0].ends_with("f00000.rs"));
|
||||
assert!(files.last().unwrap().ends_with(format!("f{:05}.rs", MAX_FILES - 1)), "the first ones");
|
||||
assert!(
|
||||
SymbolIndex::build(dir.path()).capped_note().contains("capped"),
|
||||
"and a search says so"
|
||||
);
|
||||
|
||||
std::fs::remove_file(dir.path().join(format!("f{MAX_FILES:05}.rs"))).unwrap();
|
||||
assert!(!source_files(dir.path()).capped, "exactly at the cap is complete");
|
||||
}
|
||||
|
||||
/// The walk honors a `.gitignore` now, as `glob` and `grep` do, and still keeps out of hidden
|
||||
/// directories, which would sort first and spend the budget.
|
||||
#[test]
|
||||
fn ignored_files_stay_out_of_the_index() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::fs::create_dir_all(dir.path().join(".yarn/releases")).unwrap();
|
||||
std::fs::write(dir.path().join(".yarn/releases/yarn.js"), "function vendored() {}\n").unwrap();
|
||||
std::fs::write(dir.path().join(".gitignore"), "generated/\n").unwrap();
|
||||
std::fs::create_dir_all(dir.path().join("generated")).unwrap();
|
||||
std::fs::write(dir.path().join("generated/out.rs"), "pub fn made() {}\n").unwrap();
|
||||
std::fs::write(dir.path().join("lib.rs"), "pub fn written() {}\n").unwrap();
|
||||
let index = SymbolIndex::build(dir.path());
|
||||
assert!(index.search("made", 5).is_empty());
|
||||
assert!(index.search("vendored", 5).is_empty(), "nothing from a hidden directory");
|
||||
assert_eq!(index.search("written", 5).len(), 1);
|
||||
}
|
||||
361
crates/luxe-lsp/src/syntax.rs
Normal file
361
crates/luxe-lsp/src/syntax.rs
Normal file
@@ -0,0 +1,361 @@
|
||||
//! Tree-sitter offline outlines.
|
||||
//!
|
||||
//! A fast, server-free path to a file's symbol outline (functions, types, methods, fields, …) for
|
||||
//! the discovery tools and the BM25 symbol index. Works without a running language server and
|
||||
//! produces the same normalized [`FlatSymbol`] shape as the LSP `documentSymbol` path, so
|
||||
//! `module_report`/`read_symbol` prefer it and fall back to LSP only for unsupported languages.
|
||||
|
||||
use crate::protocol::{symbol_kinds as kinds, FlatSymbol};
|
||||
use std::path::Path;
|
||||
use std::sync::OnceLock;
|
||||
use streaming_iterator::StreamingIterator;
|
||||
use tree_sitter::{Language, Node, Parser, Query, QueryCursor, Tree};
|
||||
|
||||
/// A language this module can outline offline.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum Lang {
|
||||
Rust,
|
||||
Python,
|
||||
TypeScript,
|
||||
Tsx,
|
||||
JavaScript,
|
||||
Go,
|
||||
}
|
||||
|
||||
/// Definition node kinds to capture, per language (`@def` = full extent, `@name` = the name).
|
||||
const RUST_QUERY: &str = r#"
|
||||
(function_item name: (identifier) @name) @def
|
||||
(struct_item name: (type_identifier) @name) @def
|
||||
(enum_item name: (type_identifier) @name) @def
|
||||
(union_item name: (type_identifier) @name) @def
|
||||
(trait_item name: (type_identifier) @name) @def
|
||||
(mod_item name: (identifier) @name) @def
|
||||
(type_item name: (type_identifier) @name) @def
|
||||
(const_item name: (identifier) @name) @def
|
||||
(static_item name: (identifier) @name) @def
|
||||
(macro_definition name: (identifier) @name) @def
|
||||
(impl_item type: (type_identifier) @name) @def
|
||||
(field_declaration name: (field_identifier) @name) @def
|
||||
(enum_variant name: (identifier) @name) @def
|
||||
"#;
|
||||
|
||||
const PYTHON_QUERY: &str = r#"
|
||||
(function_definition name: (identifier) @name) @def
|
||||
(class_definition name: (identifier) @name) @def
|
||||
"#;
|
||||
|
||||
const TS_QUERY: &str = r#"
|
||||
(function_declaration name: (identifier) @name) @def
|
||||
(class_declaration name: (type_identifier) @name) @def
|
||||
(method_definition name: (property_identifier) @name) @def
|
||||
(public_field_definition name: (property_identifier) @name) @def
|
||||
(interface_declaration name: (type_identifier) @name) @def
|
||||
(type_alias_declaration name: (type_identifier) @name) @def
|
||||
(enum_declaration name: (identifier) @name) @def
|
||||
"#;
|
||||
|
||||
const JS_QUERY: &str = r#"
|
||||
(function_declaration name: (identifier) @name) @def
|
||||
(class_declaration name: (identifier) @name) @def
|
||||
(method_definition name: (property_identifier) @name) @def
|
||||
"#;
|
||||
|
||||
const GO_QUERY: &str = r#"
|
||||
(function_declaration name: (identifier) @name) @def
|
||||
(method_declaration name: (field_identifier) @name) @def
|
||||
(type_spec name: (type_identifier) @name) @def
|
||||
(field_declaration name: (field_identifier) @name) @def
|
||||
"#;
|
||||
|
||||
/// Whether tree-sitter can outline this path's language.
|
||||
pub fn supported(path: &Path) -> bool {
|
||||
detect(path).is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn detect(path: &Path) -> Option<Lang> {
|
||||
match path.extension()?.to_str()? {
|
||||
"rs" => Some(Lang::Rust),
|
||||
"py" | "pyi" => Some(Lang::Python),
|
||||
"ts" | "mts" | "cts" => Some(Lang::TypeScript),
|
||||
"tsx" => Some(Lang::Tsx),
|
||||
"js" | "jsx" | "mjs" | "cjs" => Some(Lang::JavaScript),
|
||||
"go" => Some(Lang::Go),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The tree-sitter `Language` for a detected `Lang` — for callers (e.g. the graph extractor) that
|
||||
/// run their own queries against the same grammars.
|
||||
pub(crate) fn ts_language(lang: Lang) -> Language {
|
||||
language_and_query(lang).0
|
||||
}
|
||||
|
||||
fn language_and_query(lang: Lang) -> (Language, &'static str) {
|
||||
match lang {
|
||||
Lang::Rust => (tree_sitter_rust::LANGUAGE.into(), RUST_QUERY),
|
||||
Lang::Python => (tree_sitter_python::LANGUAGE.into(), PYTHON_QUERY),
|
||||
Lang::TypeScript => (tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(), TS_QUERY),
|
||||
Lang::Tsx => (tree_sitter_typescript::LANGUAGE_TSX.into(), TS_QUERY),
|
||||
Lang::JavaScript => (tree_sitter_javascript::LANGUAGE.into(), JS_QUERY),
|
||||
Lang::Go => (tree_sitter_go::LANGUAGE.into(), GO_QUERY),
|
||||
}
|
||||
}
|
||||
|
||||
/// Cached compiled outline query for `lang`. Query compilation dominates a whole-project walk, so
|
||||
/// it's built once per language and reused for every file (the same trick the graph extractor uses).
|
||||
fn outline_query(lang: Lang) -> Option<&'static Query> {
|
||||
fn compile(lang: Lang) -> Option<Query> {
|
||||
let (language, src) = language_and_query(lang);
|
||||
Query::new(&language, src).ok()
|
||||
}
|
||||
match lang {
|
||||
Lang::Rust => {
|
||||
static Q: OnceLock<Option<Query>> = OnceLock::new();
|
||||
Q.get_or_init(|| compile(Lang::Rust)).as_ref()
|
||||
}
|
||||
Lang::Python => {
|
||||
static Q: OnceLock<Option<Query>> = OnceLock::new();
|
||||
Q.get_or_init(|| compile(Lang::Python)).as_ref()
|
||||
}
|
||||
Lang::TypeScript => {
|
||||
static Q: OnceLock<Option<Query>> = OnceLock::new();
|
||||
Q.get_or_init(|| compile(Lang::TypeScript)).as_ref()
|
||||
}
|
||||
Lang::Tsx => {
|
||||
static Q: OnceLock<Option<Query>> = OnceLock::new();
|
||||
Q.get_or_init(|| compile(Lang::Tsx)).as_ref()
|
||||
}
|
||||
Lang::JavaScript => {
|
||||
static Q: OnceLock<Option<Query>> = OnceLock::new();
|
||||
Q.get_or_init(|| compile(Lang::JavaScript)).as_ref()
|
||||
}
|
||||
Lang::Go => {
|
||||
static Q: OnceLock<Option<Query>> = OnceLock::new();
|
||||
Q.get_or_init(|| compile(Lang::Go)).as_ref()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Produce a symbol outline for `source`, or `None` for an unsupported language / parse failure.
|
||||
pub fn outline(path: &Path, source: &str) -> Option<Vec<FlatSymbol>> {
|
||||
let lang = detect(path)?;
|
||||
let language = ts_language(lang);
|
||||
let mut parser = Parser::new();
|
||||
parser.set_language(&language).ok()?;
|
||||
let tree = parser.parse(source, None)?;
|
||||
Some(outline_from_tree(lang, &tree, source.as_bytes()))
|
||||
}
|
||||
|
||||
/// Build the symbol outline from an already-parsed tree, so a caller that also runs its own queries
|
||||
/// (the graph extractor) can reuse a single parse. Uses the cached outline query.
|
||||
pub(crate) fn outline_from_tree(lang: Lang, tree: &Tree, bytes: &[u8]) -> Vec<FlatSymbol> {
|
||||
let Some(query) = outline_query(lang) else { return Vec::new() };
|
||||
let Some(def_index) = query.capture_index_for_name("def") else { return Vec::new() };
|
||||
let Some(name_index) = query.capture_index_for_name("name") else { return Vec::new() };
|
||||
|
||||
let mut defs: Vec<(Node, Node)> = Vec::new();
|
||||
let mut cursor = QueryCursor::new();
|
||||
let mut matches = cursor.matches(query, tree.root_node(), bytes);
|
||||
while let Some(m) = matches.next() {
|
||||
let mut def = None;
|
||||
let mut name = None;
|
||||
for capture in m.captures {
|
||||
if capture.index == def_index {
|
||||
def = Some(capture.node);
|
||||
} else if capture.index == name_index {
|
||||
name = Some(capture.node);
|
||||
}
|
||||
}
|
||||
if let (Some(def), Some(name)) = (def, name) {
|
||||
defs.push((def, name));
|
||||
}
|
||||
}
|
||||
|
||||
let mut symbols: Vec<FlatSymbol> = defs
|
||||
.iter()
|
||||
.filter_map(|(def, name)| {
|
||||
let name_text = name.utf8_text(bytes).ok()?.to_string();
|
||||
let container_def = innermost_container(&defs, def);
|
||||
let container = container_def
|
||||
.and_then(|(_, cname)| cname.utf8_text(bytes).ok())
|
||||
.map(str::to_string);
|
||||
let kind = symbol_kind(def.kind(), container_def.map(|(d, _)| d.kind()));
|
||||
let name_pos = name.start_position();
|
||||
Some(FlatSymbol {
|
||||
name: name_text,
|
||||
kind,
|
||||
uri: String::new(),
|
||||
line: name_pos.row as u32,
|
||||
character: name_pos.column as u32,
|
||||
start_line: def.start_position().row as u32,
|
||||
end_line: def.end_position().row as u32,
|
||||
container,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
symbols.sort_by_key(|s| (s.start_line, s.line));
|
||||
symbols
|
||||
}
|
||||
|
||||
/// The innermost other definition that strictly encloses `def` (its container/parent).
|
||||
fn innermost_container<'a>(
|
||||
defs: &'a [(Node, Node)],
|
||||
def: &Node,
|
||||
) -> Option<&'a (Node<'a>, Node<'a>)> {
|
||||
defs.iter()
|
||||
.filter(|(other, _)| other.id() != def.id())
|
||||
.filter(|(other, _)| {
|
||||
other.start_byte() <= def.start_byte()
|
||||
&& other.end_byte() >= def.end_byte()
|
||||
&& (other.start_byte() < def.start_byte() || other.end_byte() > def.end_byte())
|
||||
})
|
||||
.min_by_key(|(other, _)| other.end_byte() - other.start_byte())
|
||||
}
|
||||
|
||||
/// Map a tree-sitter node kind (and its container's kind) to an LSP `SymbolKind` integer.
|
||||
fn symbol_kind(def_kind: &str, container_kind: Option<&str>) -> u32 {
|
||||
let nested_in_type = matches!(
|
||||
container_kind,
|
||||
Some(
|
||||
"impl_item"
|
||||
| "trait_item"
|
||||
| "class_definition"
|
||||
| "class_declaration"
|
||||
| "interface_declaration"
|
||||
)
|
||||
);
|
||||
match def_kind {
|
||||
"method_definition" | "method_declaration" => kinds::METHOD,
|
||||
"function_item" | "function_definition" | "function_declaration" => {
|
||||
if nested_in_type {
|
||||
kinds::METHOD
|
||||
} else {
|
||||
kinds::FUNCTION
|
||||
}
|
||||
}
|
||||
"struct_item" | "union_item" => kinds::STRUCT,
|
||||
"enum_item" | "enum_declaration" => kinds::ENUM,
|
||||
"trait_item" | "interface_declaration" => kinds::INTERFACE,
|
||||
"impl_item" | "class_definition" | "class_declaration" => kinds::CLASS,
|
||||
"mod_item" => kinds::MODULE,
|
||||
"type_item" | "type_alias_declaration" | "type_spec" => kinds::CLASS,
|
||||
"const_item" | "static_item" => kinds::CONSTANT,
|
||||
"macro_definition" => kinds::FUNCTION,
|
||||
"field_declaration" | "public_field_definition" => kinds::FIELD,
|
||||
"enum_variant" => kinds::ENUM_MEMBER,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Function lengths for the size standards the edit hook holds, from the same outline
|
||||
/// `module_report` draws.
|
||||
pub struct SyntaxOutline;
|
||||
|
||||
impl luxe_core::hooks::file_changed::OutlineSource for SyntaxOutline {
|
||||
fn functions(
|
||||
&self,
|
||||
path: &Path,
|
||||
source: &str,
|
||||
) -> Option<Vec<luxe_core::hooks::file_changed::FunctionSize>> {
|
||||
let symbols = outline(path, source)?;
|
||||
Some(
|
||||
symbols
|
||||
.into_iter()
|
||||
.filter(|symbol| matches!(symbol.kind, kinds::FUNCTION | kinds::METHOD))
|
||||
.map(|symbol| luxe_core::hooks::file_changed::FunctionSize {
|
||||
name: match &symbol.container {
|
||||
Some(container) => format!("{container}.{}", symbol.name),
|
||||
None => symbol.name.clone(),
|
||||
},
|
||||
lines: (symbol.end_line - symbol.start_line + 1) as usize,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn names(out: &[FlatSymbol]) -> Vec<&str> {
|
||||
out.iter().map(|s| s.name.as_str()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_outline_captures_defs_fields_methods_and_nesting() {
|
||||
let src = "pub fn add(a: i32, b: i32) -> i32 {\n a + b\n}\n\npub struct Point {\n x: i32,\n}\n\nimpl Point {\n pub fn origin() -> Self {\n Point { x: 0 }\n }\n}\n";
|
||||
let out = outline(Path::new("lib.rs"), src).expect("rust outline");
|
||||
assert!(names(&out).contains(&"add"));
|
||||
assert!(names(&out).contains(&"Point"));
|
||||
assert!(names(&out).contains(&"origin"));
|
||||
|
||||
let add = out.iter().find(|s| s.name == "add").unwrap();
|
||||
assert_eq!(add.kind, kinds::FUNCTION);
|
||||
assert_eq!((add.start_line, add.end_line), (0, 2));
|
||||
|
||||
let field = out.iter().find(|s| s.name == "x").unwrap();
|
||||
assert_eq!(field.kind, kinds::FIELD);
|
||||
assert_eq!(field.container.as_deref(), Some("Point"));
|
||||
|
||||
let origin = out.iter().find(|s| s.name == "origin").unwrap();
|
||||
assert_eq!(origin.kind, kinds::METHOD);
|
||||
assert_eq!(origin.container.as_deref(), Some("Point"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn python_outline_marks_methods_inside_classes() {
|
||||
let src =
|
||||
"def top():\n pass\n\nclass Widget:\n def render(self):\n return 1\n";
|
||||
let out = outline(Path::new("app.py"), src).expect("python outline");
|
||||
assert_eq!(out.iter().find(|s| s.name == "top").unwrap().kind, kinds::FUNCTION);
|
||||
assert_eq!(out.iter().find(|s| s.name == "Widget").unwrap().kind, kinds::CLASS);
|
||||
let render = out.iter().find(|s| s.name == "render").unwrap();
|
||||
assert_eq!(render.kind, kinds::METHOD);
|
||||
assert_eq!(render.container.as_deref(), Some("Widget"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typescript_outline_captures_classes_interfaces_and_methods() {
|
||||
let src = "export function greet(n: string): string { return n; }\n\ninterface Named { name: string; }\n\nexport class Service {\n url: string = \"\";\n fetch(): number { return 1; }\n}\n";
|
||||
let out = outline(Path::new("svc.ts"), src).expect("ts outline");
|
||||
assert_eq!(out.iter().find(|s| s.name == "greet").unwrap().kind, kinds::FUNCTION);
|
||||
assert_eq!(out.iter().find(|s| s.name == "Named").unwrap().kind, kinds::INTERFACE);
|
||||
assert_eq!(out.iter().find(|s| s.name == "Service").unwrap().kind, kinds::CLASS);
|
||||
let method = out.iter().find(|s| s.name == "fetch").unwrap();
|
||||
assert_eq!(method.kind, kinds::METHOD);
|
||||
assert_eq!(method.container.as_deref(), Some("Service"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn go_outline_captures_functions_types_and_methods() {
|
||||
let src = "package main\n\nfunc Add(a, b int) int { return a + b }\n\ntype Point struct {\n\tX int\n}\n\nfunc (p Point) Norm() int { return p.X }\n";
|
||||
let out = outline(Path::new("main.go"), src).expect("go outline");
|
||||
assert_eq!(out.iter().find(|s| s.name == "Add").unwrap().kind, kinds::FUNCTION);
|
||||
assert!(names(&out).contains(&"Point"));
|
||||
let method = out.iter().find(|s| s.name == "Norm").unwrap();
|
||||
assert_eq!(method.kind, kinds::METHOD);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_language_returns_none() {
|
||||
assert!(outline(Path::new("notes.md"), "# hi").is_none());
|
||||
assert!(!supported(Path::new("notes.md")));
|
||||
assert!(supported(Path::new("x.rs")));
|
||||
assert!(supported(Path::new("x.ts")));
|
||||
assert!(supported(Path::new("x.go")));
|
||||
}
|
||||
|
||||
/// A method is named by its class, so two `__init__`s are two functions, and its length counts
|
||||
/// its first and last lines.
|
||||
#[test]
|
||||
fn the_size_outline_names_methods_by_their_class() {
|
||||
use luxe_core::hooks::file_changed::OutlineSource;
|
||||
let source = "class A:\n def __init__(self):\n x = 1\n y = 2\n\ndef top():\n pass\n";
|
||||
let functions = SyntaxOutline.functions(Path::new("m.py"), source).expect("python parses");
|
||||
let named: Vec<(&str, usize)> =
|
||||
functions.iter().map(|function| (function.name.as_str(), function.lines)).collect();
|
||||
assert_eq!(named, [("A.__init__", 3), ("top", 2)]);
|
||||
assert!(SyntaxOutline.functions(Path::new("notes.txt"), "x").is_none());
|
||||
}
|
||||
}
|
||||
99
crates/luxe-lsp/src/tools.rs
Normal file
99
crates/luxe-lsp/src/tools.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
//! The language-server tools — diagnostics, navigation, symbol discovery and impact — over the
|
||||
//! shared workspace service, which opens documents, waits for diagnostics and answers navigation.
|
||||
|
||||
use crate::{DiagnosticSeverity, LspClient, Position};
|
||||
use async_trait::async_trait;
|
||||
use luxe_core::{ContentBlock, Tool, ToolContext, ToolResultData, ToolSchema};
|
||||
use serde_json::json;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod diagnostics;
|
||||
mod discovery;
|
||||
mod impact;
|
||||
mod navigation;
|
||||
|
||||
pub use diagnostics::LspDiagnosticsTool;
|
||||
pub use discovery::{ModuleReportTool, ReadSymbolTool, SymbolSearchTool};
|
||||
pub use impact::ImpactTool;
|
||||
pub use navigation::LspNavigationTool;
|
||||
|
||||
/// The spellings [`file_arg`] reads, for [`luxe_core::Tool::argument_aliases`].
|
||||
const FILE_ALIASES: &[&str] = &["path", "file", "file_path", "filePath"];
|
||||
|
||||
/// The file a call names.
|
||||
///
|
||||
/// `path` is the word every tool in the set advertises, so nothing here is a dialect to remember.
|
||||
/// The rest are tolerated because they were once advertised (`file`, until these tools were brought
|
||||
/// in line) or because another harness trained a model on them — the same recovery path `edit`
|
||||
/// keeps, for the same reason: rejecting a spelling costs a turn and teaches nothing.
|
||||
fn file_arg(args: &serde_json::Value) -> Option<&str> {
|
||||
FILE_ALIASES
|
||||
.iter()
|
||||
.find_map(|key| args.get(*key).and_then(|value| value.as_str()))
|
||||
.filter(|text| !text.is_empty())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
fn test_context(tmp_dir: &std::path::Path) -> ToolContext {
|
||||
let (event_tx, _event_rx) = mpsc::channel(64);
|
||||
let (_cancel_tx, cancel_rx) = watch::channel(false);
|
||||
ToolContext {
|
||||
cwd: tmp_dir.to_path_buf(),
|
||||
session_id: "test-session".to_string(),
|
||||
cancelled: Arc::new(cancel_rx),
|
||||
event_tx,
|
||||
hook_ctx: None,
|
||||
question_broker: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn lsp_diagnostics_requires_paths() {
|
||||
let tmp_dir = tempfile::tempdir().unwrap();
|
||||
let service = Arc::new(crate::WorkspaceLspClient::new());
|
||||
let tool = LspDiagnosticsTool::new(service);
|
||||
let cx = test_context(tmp_dir.path());
|
||||
let result = tool.run(json!({}), &cx).await;
|
||||
assert!(result.is_error);
|
||||
assert_eq!(
|
||||
result.content,
|
||||
vec![ContentBlock::Text("Error: provide `file` or `paths`".into())]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn lsp_diagnostics_unconfigured_language() {
|
||||
let tmp_dir = tempfile::tempdir().unwrap();
|
||||
let service = Arc::new(crate::WorkspaceLspClient::new());
|
||||
let tool = LspDiagnosticsTool::new(service);
|
||||
let cx = test_context(tmp_dir.path());
|
||||
let result = tool.run(json!({"file": "test.unconfigured"}), &cx).await;
|
||||
assert!(result.is_error);
|
||||
let ContentBlock::Text(text) = &result.content[0] else {
|
||||
panic!("expected Text block");
|
||||
};
|
||||
assert!(text.contains("no language server configured for .unconfigured"), "{text}");
|
||||
assert!(text.contains("lsp-servers.json"), "points at configuring one: {text}");
|
||||
}
|
||||
|
||||
/// A model that has just called `read(path=…)` reaches for `read_symbol(path=…)` next. That is
|
||||
/// the same call, so it must not be an error.
|
||||
#[test]
|
||||
fn the_file_argument_answers_to_the_spelling_its_neighbors_use() {
|
||||
for key in ["path", "file", "file_path", "filePath"] {
|
||||
let args = json!({ key: "src/lib.rs" });
|
||||
assert_eq!(super::file_arg(&args), Some("src/lib.rs"), "{key}");
|
||||
}
|
||||
assert_eq!(super::file_arg(&json!({})), None);
|
||||
assert_eq!(super::file_arg(&json!({"path": ""})), None, "empty is not a path");
|
||||
// The advertised spelling wins when a call somehow carries both.
|
||||
let both = json!({"path": "canonical.rs", "file": "alias.rs"});
|
||||
assert_eq!(super::file_arg(&both), Some("canonical.rs"));
|
||||
}
|
||||
}
|
||||
285
crates/luxe-lsp/src/tools/diagnostics.rs
Normal file
285
crates/luxe-lsp/src/tools/diagnostics.rs
Normal file
@@ -0,0 +1,285 @@
|
||||
//! The `lsp_diagnostics` tool — warm, batchable, severity-filterable diagnostics.
|
||||
//!
|
||||
//! Uses the shared warm [`WorkspaceLspClient`] service for reused servers and settle-aware reads.
|
||||
|
||||
use super::*;
|
||||
use crate::protocol::Diagnostic;
|
||||
use crate::workspace::WorkspaceLspClient;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Default settle budget when the caller doesn't pass `waitMs`.
|
||||
const DEFAULT_WAIT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// JSON schema for the `lsp_diagnostics` tool arguments.
|
||||
fn lsp_diagnostics_schema() -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "lsp_diagnostics".to_string(),
|
||||
description: "Fetch diagnostics via a warm language server (rust-analyzer, pyright, \
|
||||
tsserver, gopls). Reuses a running server and waits for it to settle, so \
|
||||
results are real, not empty pre-index snapshots. Check one file (`path`) or \
|
||||
a batch (`paths`), optionally filtered by `severity`."
|
||||
.to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": { "type": "string", "description": "Path to a file to check (relative to cwd)" },
|
||||
"paths": {
|
||||
"type": "array",
|
||||
"items": { "type": "string" },
|
||||
"description": "Batch of files to check (relative to cwd). Use instead of `path`."
|
||||
},
|
||||
"severity": {
|
||||
"type": "string",
|
||||
"enum": ["error", "warning", "information", "hint", "all"],
|
||||
"description": "Minimum severity to include (default: all)."
|
||||
},
|
||||
"waitMs": {
|
||||
"type": "integer",
|
||||
"description": "Max time to wait for the server to settle per file (default 10000)."
|
||||
}
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// The `lsp_diagnostics` tool.
|
||||
pub struct LspDiagnosticsTool {
|
||||
service: Arc<WorkspaceLspClient>,
|
||||
}
|
||||
|
||||
impl LspDiagnosticsTool {
|
||||
/// Creates a diagnostics tool backed by the shared warm service.
|
||||
pub fn new(service: Arc<WorkspaceLspClient>) -> Self {
|
||||
Self { service }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for LspDiagnosticsTool {
|
||||
fn name(&self) -> &str {
|
||||
"lsp_diagnostics"
|
||||
}
|
||||
|
||||
fn argument_aliases(&self) -> &'static [&'static str] {
|
||||
super::FILE_ALIASES
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
lsp_diagnostics_schema()
|
||||
}
|
||||
|
||||
async fn run(&self, args: serde_json::Value, cx: &ToolContext) -> ToolResultData {
|
||||
let requested = requested_paths(&args);
|
||||
if requested.is_empty() {
|
||||
return ToolResultData::error("Error: provide `file` or `paths`");
|
||||
}
|
||||
let min_severity = min_severity(args.get("severity").and_then(|v| v.as_str()));
|
||||
let wait = args
|
||||
.get("waitMs")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
.map_or(DEFAULT_WAIT, Duration::from_millis);
|
||||
|
||||
let mut output = String::new();
|
||||
let mut errored = 0usize;
|
||||
for rel in &requested {
|
||||
let full = cx.cwd.join(rel);
|
||||
// Named the way every other tool names a file, relative to the project
|
||||
let shown = full.strip_prefix(&cx.cwd).unwrap_or(&full);
|
||||
match self.diagnostics_for(&full, wait).await {
|
||||
Ok((diags, notices)) => {
|
||||
output.push_str(&format_file(shown, &diags, min_severity, ¬ices))
|
||||
}
|
||||
Err(e) => {
|
||||
errored += 1;
|
||||
output.push_str(&format!("{}: {e}\n", luxe_core::paths::display(shown)));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Only a total failure (every requested file errored) is a tool error; a mixed batch
|
||||
// still reports per-file.
|
||||
ToolResultData {
|
||||
content: vec![ContentBlock::Text(output)],
|
||||
is_error: errored == requested.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LspDiagnosticsTool {
|
||||
/// Fetches diagnostics for one file via the warm service.
|
||||
async fn diagnostics_for(
|
||||
&self,
|
||||
full_path: &Path,
|
||||
wait: Duration,
|
||||
) -> Result<(Vec<Diagnostic>, Vec<String>), String> {
|
||||
let uri = crate::uri::from_path(full_path);
|
||||
|
||||
let client = { self.service.client_for_path(full_path).await };
|
||||
let Some(client) = client else {
|
||||
return Err(self.service.missing_server_hint(full_path));
|
||||
};
|
||||
let content = std::fs::read_to_string(full_path)
|
||||
.map_err(|e| format!("read {}: {e}", luxe_core::paths::display(full_path)))?;
|
||||
let mut guard = client.lock().await;
|
||||
let diags = guard
|
||||
.get_diagnostics_with_wait(&uri, &content, wait)
|
||||
.await
|
||||
.map_err(|e| format!("diagnostics failed: {e}"))?;
|
||||
// Explain an empty result: a misconfigured server often says why via a notice.
|
||||
let notices = if diags.is_empty() { guard.server_notices() } else { Vec::new() };
|
||||
Ok((diags, notices))
|
||||
}
|
||||
}
|
||||
|
||||
/// Collects the requested paths from `paths` (batch) or `file` (single).
|
||||
fn requested_paths(args: &serde_json::Value) -> Vec<String> {
|
||||
if let Some(paths) = args.get("paths").and_then(|v| v.as_array()) {
|
||||
return paths.iter().filter_map(|p| p.as_str().map(str::to_string)).collect();
|
||||
}
|
||||
super::file_arg(args).map(|s| vec![s.to_string()]).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Maps a `severity` argument to the highest numeric severity to include (1=error … 4=hint).
|
||||
fn min_severity(arg: Option<&str>) -> u8 {
|
||||
match arg {
|
||||
Some("error") => 1,
|
||||
Some("warning") => 2,
|
||||
Some("information") => 3,
|
||||
_ => 4, // "hint" or "all" or unset — include everything
|
||||
}
|
||||
}
|
||||
|
||||
/// Most diagnostics listed for one file before the remainder is summarized.
|
||||
const MAX_DIAGNOSTICS: usize = 50;
|
||||
|
||||
fn format_file(
|
||||
full_path: &Path,
|
||||
diagnostics: &[Diagnostic],
|
||||
min_severity: u8,
|
||||
notices: &[String],
|
||||
) -> String {
|
||||
let filtered: Vec<&Diagnostic> =
|
||||
diagnostics.iter().filter(|d| severity_rank(d.severity) <= min_severity).collect();
|
||||
if filtered.is_empty() {
|
||||
let mut out = format!("No diagnostics for {}\n", luxe_core::paths::display(full_path));
|
||||
// A server that can't do its job usually said so — surface it rather than imply "clean".
|
||||
for notice in notices {
|
||||
out.push_str(&format!(" note: language server reported {notice}\n"));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
let mut out = format!(
|
||||
"Diagnostics for {} ({} found):\n",
|
||||
luxe_core::paths::display(full_path),
|
||||
filtered.len()
|
||||
);
|
||||
// One bad refactor or one generated file yields hundreds of diagnostics that are overwhelmingly
|
||||
// the same error repeated. The header already carries the true count, so bound the list itself.
|
||||
for (i, diag) in filtered.iter().take(MAX_DIAGNOSTICS).enumerate() {
|
||||
out.push_str(&format!(
|
||||
"{}. [{}] {}:{}: {}\n",
|
||||
i + 1,
|
||||
severity_str(diag.severity),
|
||||
diag.range.start.line + 1,
|
||||
diag.range.start.character + 1,
|
||||
diag.message
|
||||
));
|
||||
}
|
||||
if filtered.len() > MAX_DIAGNOSTICS {
|
||||
out.push_str(&format!(
|
||||
"… and {} more. Fix these first, then re-run — repeats usually share one cause.\n",
|
||||
filtered.len() - MAX_DIAGNOSTICS
|
||||
));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn severity_rank(severity: Option<DiagnosticSeverity>) -> u8 {
|
||||
match severity {
|
||||
Some(DiagnosticSeverity::Error) => 1,
|
||||
Some(DiagnosticSeverity::Warning) => 2,
|
||||
Some(DiagnosticSeverity::Information) => 3,
|
||||
Some(DiagnosticSeverity::Hint) | None => 4,
|
||||
}
|
||||
}
|
||||
|
||||
fn severity_str(severity: Option<DiagnosticSeverity>) -> &'static str {
|
||||
match severity {
|
||||
Some(DiagnosticSeverity::Error) => "ERROR",
|
||||
Some(DiagnosticSeverity::Warning) => "WARN",
|
||||
Some(DiagnosticSeverity::Information) => "INFO",
|
||||
Some(DiagnosticSeverity::Hint) => "HINT",
|
||||
None => "UNKNOWN",
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::protocol::{Position as ProtoPosition, Range};
|
||||
|
||||
fn diag(severity: DiagnosticSeverity, line: u32, message: &str) -> Diagnostic {
|
||||
Diagnostic {
|
||||
severity: Some(severity),
|
||||
range: Range {
|
||||
start: ProtoPosition { line, character: 0 },
|
||||
end: ProtoPosition { line, character: 0 },
|
||||
},
|
||||
message: message.to_string(),
|
||||
source: None,
|
||||
code: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requested_paths_prefers_batch_then_single() {
|
||||
assert_eq!(
|
||||
requested_paths(&json!({ "paths": ["a.rs", "b.rs"] })),
|
||||
vec!["a.rs".to_string(), "b.rs".to_string()]
|
||||
);
|
||||
assert_eq!(requested_paths(&json!({ "file": "c.rs" })), vec!["c.rs".to_string()]);
|
||||
assert!(requested_paths(&json!({})).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn min_severity_maps_the_threshold() {
|
||||
assert_eq!(min_severity(Some("error")), 1);
|
||||
assert_eq!(min_severity(Some("warning")), 2);
|
||||
assert_eq!(min_severity(None), 4);
|
||||
assert_eq!(min_severity(Some("all")), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_file_filters_by_severity_and_counts() {
|
||||
let path = Path::new("/w/src/lib.rs");
|
||||
let diags = vec![
|
||||
diag(DiagnosticSeverity::Error, 0, "boom"),
|
||||
diag(DiagnosticSeverity::Warning, 4, "careful"),
|
||||
diag(DiagnosticSeverity::Hint, 9, "tidy"),
|
||||
];
|
||||
let errors = format_file(path, &diags, min_severity(Some("error")), &[]);
|
||||
assert!(errors.contains("(1 found)"), "{errors}");
|
||||
assert!(errors.contains("[ERROR] 1:1: boom"), "{errors}");
|
||||
assert!(!errors.contains("careful"));
|
||||
let warns = format_file(path, &diags, min_severity(Some("warning")), &[]);
|
||||
assert!(warns.contains("(2 found)"), "{warns}");
|
||||
assert!(warns.contains("[WARN] 5:1: careful"), "{warns}");
|
||||
assert!(format_file(path, &diags, min_severity(None), &[]).contains("(3 found)"));
|
||||
assert!(format_file(path, &[], 4, &[]).contains("No diagnostics"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn format_file_surfaces_server_notices_only_when_empty() {
|
||||
let path = Path::new("/w/index.ts");
|
||||
let notices = vec!["[warning] bundled 1.0.0".to_string()];
|
||||
// Empty diagnostics -> the notice is surfaced so the result isn't a silent "clean".
|
||||
let empty = format_file(path, &[], 4, ¬ices);
|
||||
assert!(empty.contains("No diagnostics"), "{empty}");
|
||||
assert!(
|
||||
empty.contains("note: language server reported [warning] bundled 1.0.0"),
|
||||
"{empty}"
|
||||
);
|
||||
// With real diagnostics present, notices are not appended (they'd be noise).
|
||||
let with = format_file(path, &[diag(DiagnosticSeverity::Error, 0, "boom")], 4, ¬ices);
|
||||
assert!(with.contains("boom") && !with.contains("note:"), "{with}");
|
||||
}
|
||||
}
|
||||
115
crates/luxe-lsp/src/tools/discovery.rs
Normal file
115
crates/luxe-lsp/src/tools/discovery.rs
Normal file
@@ -0,0 +1,115 @@
|
||||
//! Discovery funnel — `symbol_search` → `module_report` → `read_symbol`.
|
||||
//!
|
||||
//! A token-efficient path from "which symbols matter" (workspace search) to "what's in this file"
|
||||
//! (outline) to "show me this one symbol's body" (verbatim read). Outlines prefer a **tree-sitter**
|
||||
//! parse (fast, offline, no running server) and fall back to the warm LSP service's
|
||||
//! `documentSymbol`; `symbol_search` uses `workspace/symbol`.
|
||||
|
||||
use super::*;
|
||||
use crate::protocol::{symbol_kind_name, FlatSymbol};
|
||||
use crate::syntax;
|
||||
use crate::workspace::WorkspaceLspClient;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
/// Shared warm service handle used by every discovery tool.
|
||||
pub(super) type Service = Arc<WorkspaceLspClient>;
|
||||
|
||||
/// Records a successful funnel read against the shared tracker (no-op when unwired), so the
|
||||
/// `edit`/`write` guards accept a subsequent mutation of the same file.
|
||||
pub(super) fn record_read(
|
||||
tracker: Option<&Arc<luxe_core::read_tracker::ReadTracker>>,
|
||||
cx: &ToolContext,
|
||||
file: &str,
|
||||
) {
|
||||
if let Some(tracker) = tracker {
|
||||
tracker.record(&cx.session_id, &cx.cwd.join(file));
|
||||
}
|
||||
}
|
||||
|
||||
/// A file outline plus the state needed to enrich it (content for reads, a client for usages).
|
||||
pub(super) struct Outline {
|
||||
symbols: Vec<FlatSymbol>,
|
||||
content: Option<String>,
|
||||
/// Warm LSP client + URI, when one was resolved (always for the LSP path; best-effort for the
|
||||
/// tree-sitter path when usages are requested).
|
||||
client: Option<Arc<Mutex<LspClient>>>,
|
||||
uri: String,
|
||||
/// `"tree-sitter"` or `"lsp"`.
|
||||
source: &'static str,
|
||||
}
|
||||
|
||||
/// Build a file outline, preferring tree-sitter and falling back to the LSP `documentSymbol`.
|
||||
///
|
||||
/// `need_client` requests a warm LSP client for follow-up work (who-uses-this); the tree-sitter
|
||||
/// path only resolves one when asked, since it doesn't otherwise need a server.
|
||||
pub(super) async fn build_outline(
|
||||
service: &Service,
|
||||
cx: &ToolContext,
|
||||
file: &str,
|
||||
need_client: bool,
|
||||
) -> Result<Outline, String> {
|
||||
let full = cx.cwd.join(file);
|
||||
// A typo'd path must not fall through to "no symbols", which reads like an empty file rather
|
||||
// than a wrong path. Say which it is.
|
||||
if !full.exists() {
|
||||
return Err(format!(
|
||||
"file not found: {file} (resolved to {})",
|
||||
luxe_core::paths::display(&full)
|
||||
));
|
||||
}
|
||||
if full.is_dir() {
|
||||
return Err(format!("{file} is a directory — pass a file path"));
|
||||
}
|
||||
let uri = crate::uri::from_path(&full);
|
||||
let content = std::fs::read_to_string(&full).ok();
|
||||
|
||||
// Tree-sitter path: offline, no server needed.
|
||||
if syntax::supported(&full) {
|
||||
if let Some(source) = &content {
|
||||
let symbols = syntax::outline(&full, source).unwrap_or_default();
|
||||
let client =
|
||||
if need_client { open_client(service, &full, &uri, source).await } else { None };
|
||||
return Ok(Outline { symbols, content, client, uri, source: "tree-sitter" });
|
||||
}
|
||||
}
|
||||
|
||||
// LSP path: needs a running server.
|
||||
let client =
|
||||
{ service.client_for_path(&full).await }.ok_or("no language server for this file")?;
|
||||
if let Some(source) = &content {
|
||||
let _ = client.lock().await.open_document(&uri, source).await;
|
||||
}
|
||||
let symbols = client
|
||||
.lock()
|
||||
.await
|
||||
.document_symbols(&uri)
|
||||
.await
|
||||
.map_err(|e| format!("outline failed: {e}"))?;
|
||||
Ok(Outline { symbols, content, client: Some(client), uri, source: "lsp" })
|
||||
}
|
||||
|
||||
/// Best-effort: resolve + warm an LSP client for a file (opening the document).
|
||||
pub(super) async fn open_client(
|
||||
service: &Service,
|
||||
full: &Path,
|
||||
uri: &str,
|
||||
content: &str,
|
||||
) -> Option<Arc<Mutex<LspClient>>> {
|
||||
let client = { service.client_for_path(full).await }?;
|
||||
let _ = client.lock().await.open_document(uri, content).await;
|
||||
Some(client)
|
||||
}
|
||||
|
||||
mod module_report;
|
||||
mod read_symbol;
|
||||
mod symbol_search;
|
||||
|
||||
pub use module_report::ModuleReportTool;
|
||||
pub use read_symbol::ReadSymbolTool;
|
||||
pub use symbol_search::SymbolSearchTool;
|
||||
|
||||
/// Max symbols listed by `symbol_search` / `module_report`.
|
||||
pub(super) const MAX_SYMBOLS: usize = 60;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
171
crates/luxe-lsp/src/tools/discovery/module_report.rs
Normal file
171
crates/luxe-lsp/src/tools/discovery/module_report.rs
Normal file
@@ -0,0 +1,171 @@
|
||||
//! `module_report` — a file's outline, with who uses each symbol.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Returns a navigable outline of a file's symbols.
|
||||
pub struct ModuleReportTool {
|
||||
service: Service,
|
||||
tracker: Option<Arc<luxe_core::read_tracker::ReadTracker>>,
|
||||
graph: Option<Arc<crate::graph::GraphService>>,
|
||||
}
|
||||
|
||||
impl ModuleReportTool {
|
||||
/// Create the tool over the shared warm service.
|
||||
pub fn new(service: Service) -> Self {
|
||||
Self { service, tracker: None, graph: None }
|
||||
}
|
||||
|
||||
/// Attach the shared read tracker so a `module_report` counts as having seen the file for the
|
||||
/// `edit`/`write` guards.
|
||||
pub fn with_tracker(mut self, tracker: Arc<luxe_core::read_tracker::ReadTracker>) -> Self {
|
||||
self.tracker = Some(tracker);
|
||||
self
|
||||
}
|
||||
|
||||
/// Attach the warm code graph so `module_report` can report a real blast radius on request.
|
||||
pub fn with_graph(mut self, graph: Arc<crate::graph::GraphService>) -> Self {
|
||||
self.graph = Some(graph);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for ModuleReportTool {
|
||||
fn name(&self) -> &str {
|
||||
"module_report"
|
||||
}
|
||||
|
||||
fn argument_aliases(&self) -> &'static [&'static str] {
|
||||
super::FILE_ALIASES
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "module_report".to_string(),
|
||||
description:
|
||||
"Outline a file's symbols (name, kind, line range, nesting) via tree-sitter \
|
||||
or the language server — a token-efficient substitute for reading the \
|
||||
whole file. Follow up with read_symbol to read one symbol's body."
|
||||
.to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": { "type": "string", "description": "Path to the file (relative to cwd)" },
|
||||
"usages": { "type": "boolean", "description": "Also count references (who-uses-this) for top-level symbols (needs a language server; slower)" },
|
||||
"blastRadius": { "type": "boolean", "description": "Also report the blast radius (transitive callers/dependents + covering tests) from the offline code graph" }
|
||||
},
|
||||
"required": ["path"]
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self, args: serde_json::Value, cx: &ToolContext) -> ToolResultData {
|
||||
let Some(file) = super::file_arg(&args) else {
|
||||
return ToolResultData::error("Error: 'path' is required");
|
||||
};
|
||||
let want_usages = args.get("usages").and_then(serde_json::Value::as_bool).unwrap_or(false);
|
||||
let want_blast = luxe_core::tool::arg(&args, &["blastRadius", "blast_radius"])
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
.unwrap_or(false);
|
||||
let outline = match build_outline(&self.service, cx, file, want_usages).await {
|
||||
Ok(outline) => outline,
|
||||
Err(e) => return ToolResultData::error(format!("Error: {e}")),
|
||||
};
|
||||
// Outlining the file reads it, which counts as having seen it for the mutation guards.
|
||||
record_read(self.tracker.as_ref(), cx, file);
|
||||
if outline.symbols.is_empty() {
|
||||
return ToolResultData::text(format!("{file}: no symbols"));
|
||||
}
|
||||
|
||||
// who-uses-this: bounded references from each top-level symbol's name position.
|
||||
let usages = match (want_usages, &outline.client) {
|
||||
(true, Some(client)) => reference_counts(client, &outline.uri, &outline.symbols).await,
|
||||
_ => Vec::new(),
|
||||
};
|
||||
|
||||
let mut out = format!(
|
||||
"{file} — {} symbol(s) [{}]:\n",
|
||||
outline.symbols.len().min(MAX_SYMBOLS),
|
||||
outline.source
|
||||
);
|
||||
for (index, sym) in outline.symbols.iter().take(MAX_SYMBOLS).enumerate() {
|
||||
let indent = if sym.container.is_some() { " " } else { "" };
|
||||
let uses = usages
|
||||
.get(index)
|
||||
.copied()
|
||||
.flatten()
|
||||
.map(|count| format!(" · used by {count}"))
|
||||
.unwrap_or_default();
|
||||
out.push_str(&format!(
|
||||
"{indent}[{}] {} L{}-{}{uses}\n",
|
||||
symbol_kind_name(sym.kind),
|
||||
sym.name,
|
||||
sym.start_line + 1,
|
||||
sym.end_line + 1,
|
||||
));
|
||||
}
|
||||
if outline.symbols.len() > MAX_SYMBOLS {
|
||||
out.push_str(&format!("… {} more\n", outline.symbols.len() - MAX_SYMBOLS));
|
||||
}
|
||||
if want_usages && outline.client.is_none() {
|
||||
out.push_str("(who-uses-this needs a language server; none available)\n");
|
||||
}
|
||||
if want_blast {
|
||||
append_blast_radius(&mut out, self.graph.as_ref(), file).await;
|
||||
}
|
||||
ToolResultData::text(out)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reference counts per shown symbol (aligned by index): `Some(n)` for a queried top-level symbol,
|
||||
/// `None` for members or beyond the query budget. One `references` request per top-level symbol,
|
||||
/// capped so a big file can't fan out unboundedly.
|
||||
async fn reference_counts(
|
||||
client: &Arc<Mutex<LspClient>>,
|
||||
uri: &str,
|
||||
symbols: &[FlatSymbol],
|
||||
) -> Vec<Option<usize>> {
|
||||
const MAX_QUERIES: usize = 25;
|
||||
let mut counts = vec![None; symbols.len().min(MAX_SYMBOLS)];
|
||||
let mut queried = 0;
|
||||
let mut guard = client.lock().await;
|
||||
for (index, sym) in symbols.iter().take(MAX_SYMBOLS).enumerate() {
|
||||
if sym.container.is_some() || queried >= MAX_QUERIES {
|
||||
continue;
|
||||
}
|
||||
queried += 1;
|
||||
let position = Position { line: sym.line, character: sym.character };
|
||||
if let Ok(locations) = guard.find_references(uri, position).await {
|
||||
counts[index] = Some(locations.len());
|
||||
}
|
||||
}
|
||||
counts
|
||||
}
|
||||
|
||||
/// Append a compact blast-radius section to a `module_report`, using the warm code graph.
|
||||
async fn append_blast_radius(
|
||||
out: &mut String,
|
||||
graph: Option<&Arc<crate::graph::GraphService>>,
|
||||
file: &str,
|
||||
) {
|
||||
let Some(service) = graph else {
|
||||
out.push_str("(blastRadius needs the code graph; not available)\n");
|
||||
return;
|
||||
};
|
||||
let graph = match service.graph(false).await {
|
||||
Ok(graph) => graph,
|
||||
Err(error) => return out.push_str(&format!("\n(blastRadius unavailable: {error})\n")),
|
||||
};
|
||||
let br = crate::graph::blast_radius(&graph, file, crate::graph::DEFAULT_MAX_DEPTH);
|
||||
out.push_str(&format!("\nblast radius: {}\n", br.summary_line()));
|
||||
for impacted in br.dependents.iter().take(5) {
|
||||
out.push_str(&format!(
|
||||
" affected: {} · {} [depth {}]\n",
|
||||
impacted.file, impacted.reason, impacted.min_depth
|
||||
));
|
||||
}
|
||||
if !br.untested_seeds.is_empty() {
|
||||
let names: Vec<&str> = br.untested_seeds.iter().map(|s| s.base_name()).collect();
|
||||
out.push_str(&format!(" untested: {}\n", names.join(", ")));
|
||||
}
|
||||
}
|
||||
177
crates/luxe-lsp/src/tools/discovery/read_symbol.rs
Normal file
177
crates/luxe-lsp/src/tools/discovery/read_symbol.rs
Normal file
@@ -0,0 +1,177 @@
|
||||
//! `read_symbol` — one symbol's verbatim source, with what a method needs around it.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Max lines returned by `read_symbol`.
|
||||
const MAX_BODY_LINES: usize = 400;
|
||||
|
||||
/// Most field-assignment lines carried alongside a method body.
|
||||
const MAX_CONTEXT_LINES: usize = 8;
|
||||
|
||||
/// Returns the verbatim source of one named symbol in a file.
|
||||
pub struct ReadSymbolTool {
|
||||
service: Service,
|
||||
tracker: Option<Arc<luxe_core::read_tracker::ReadTracker>>,
|
||||
}
|
||||
|
||||
impl ReadSymbolTool {
|
||||
/// Create the tool over the shared warm service.
|
||||
pub fn new(service: Service) -> Self {
|
||||
Self { service, tracker: None }
|
||||
}
|
||||
|
||||
/// Attach the shared read tracker so a `read_symbol` counts as having seen the file for the
|
||||
/// `edit`/`write` guards (it returns verbatim source, so a subsequent edit is grounded).
|
||||
pub fn with_tracker(mut self, tracker: Arc<luxe_core::read_tracker::ReadTracker>) -> Self {
|
||||
self.tracker = Some(tracker);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for ReadSymbolTool {
|
||||
fn name(&self) -> &str {
|
||||
"read_symbol"
|
||||
}
|
||||
|
||||
fn argument_aliases(&self) -> &'static [&'static str] {
|
||||
super::FILE_ALIASES
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "read_symbol".to_string(),
|
||||
description: "Read the verbatim source of one named symbol (function/type/etc.) in a \
|
||||
file — a cheap alternative to reading the whole file. Resolves the symbol \
|
||||
via a tree-sitter outline (offline) or the language server."
|
||||
.to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": { "type": "string", "description": "Path to the file (relative to cwd)" },
|
||||
"symbol": { "type": "string", "description": "Exact (or substring) symbol name to read" }
|
||||
},
|
||||
"required": ["path", "symbol"]
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self, args: serde_json::Value, cx: &ToolContext) -> ToolResultData {
|
||||
let Some(file) = super::file_arg(&args) else {
|
||||
return ToolResultData::error("Error: 'path' is required");
|
||||
};
|
||||
let Some(symbol) = args.get("symbol").and_then(|v| v.as_str()) else {
|
||||
return ToolResultData::error("Error: 'symbol' is required");
|
||||
};
|
||||
let outline = match build_outline(&self.service, cx, file, false).await {
|
||||
Ok(outline) => outline,
|
||||
Err(e) => return ToolResultData::error(format!("Error: {e}")),
|
||||
};
|
||||
let Some(found) = pick_symbol(&outline.symbols, symbol) else {
|
||||
return ToolResultData::error(format!("Error: symbol '{symbol}' not found in {file}"));
|
||||
};
|
||||
let Some(content) = outline.content else {
|
||||
return ToolResultData::error(format!("Error: could not read {file}"));
|
||||
};
|
||||
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
let start = found.start_line as usize;
|
||||
if start >= lines.len() {
|
||||
return ToolResultData::error(format!(
|
||||
"Error: symbol '{symbol}' range is out of bounds"
|
||||
));
|
||||
}
|
||||
let end = (found.end_line as usize).min(lines.len().saturating_sub(1));
|
||||
let shown = end.saturating_sub(start) + 1;
|
||||
let body =
|
||||
lines[start..=end].iter().take(MAX_BODY_LINES).copied().collect::<Vec<_>>().join("\n");
|
||||
let truncated = if shown > MAX_BODY_LINES {
|
||||
format!("\n… ({} more lines)", shown - MAX_BODY_LINES)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
// Reading a symbol's verbatim body counts as having seen the file for the mutation guards.
|
||||
record_read(self.tracker.as_ref(), cx, file);
|
||||
let context = enclosing_context(found, &outline.symbols, &lines, &body);
|
||||
ToolResultData::text(format!(
|
||||
"{} ({}) — {}:{}-{}\n\n{body}{truncated}{context}",
|
||||
found.name,
|
||||
symbol_kind_name(found.kind),
|
||||
file,
|
||||
start + 1,
|
||||
end + 1,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// What a method's body needs to be *interpretable*: the class it lives on, and where the fields
|
||||
/// it touches were assigned.
|
||||
///
|
||||
/// A symbol-scoped read is cheaper than the whole file right up until the answer lives just
|
||||
/// outside the window. Measured failure: `get_connection` returns
|
||||
/// `self.connection_repo.get_by_user_id(...)`, and a model shown only those five lines answered
|
||||
/// with the enclosing *service* rather than the repository class assigned in `__init__` — twice,
|
||||
/// on two different model families. Carrying the field assignments closes that gap without
|
||||
/// giving up the cheapness.
|
||||
pub(super) fn enclosing_context(
|
||||
found: &FlatSymbol,
|
||||
symbols: &[FlatSymbol],
|
||||
lines: &[&str],
|
||||
body: &str,
|
||||
) -> String {
|
||||
let Some(container) = found.container.as_deref().filter(|name| !name.is_empty()) else {
|
||||
return String::new();
|
||||
};
|
||||
|
||||
// Fields the body actually reaches for: `self.x` / `this.x`.
|
||||
let referenced: std::collections::BTreeSet<&str> = body
|
||||
.match_indices("self.")
|
||||
.chain(body.match_indices("this."))
|
||||
.filter_map(|(at, prefix)| {
|
||||
let rest = &body[at + prefix.len()..];
|
||||
let end = rest.find(|c: char| !c.is_alphanumeric() && c != '_').unwrap_or(rest.len());
|
||||
(end > 0).then(|| &rest[..end])
|
||||
})
|
||||
.collect();
|
||||
if referenced.is_empty() {
|
||||
return format!("\n\n(defined on `{container}`)");
|
||||
}
|
||||
|
||||
// Where those fields are assigned, anywhere inside the enclosing container.
|
||||
let scope = symbols.iter().find(|s| s.name == container);
|
||||
let (from, to) = match scope {
|
||||
Some(scope) => (scope.start_line as usize, (scope.end_line as usize).min(lines.len())),
|
||||
None => (0, lines.len()),
|
||||
};
|
||||
let assignments: Vec<String> = lines
|
||||
.get(from..to)
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, line)| {
|
||||
referenced.iter().any(|field| {
|
||||
line.contains(&format!("self.{field} ="))
|
||||
|| line.contains(&format!("this.{field} ="))
|
||||
})
|
||||
})
|
||||
.take(MAX_CONTEXT_LINES)
|
||||
.map(|(offset, line)| format!(" {}: {}", from + offset + 1, line.trim()))
|
||||
.collect();
|
||||
|
||||
if assignments.is_empty() {
|
||||
format!("\n\n(defined on `{container}`)")
|
||||
} else {
|
||||
format!(
|
||||
"\n\nDefined on `{container}`; the fields this uses are assigned at:\n{}",
|
||||
assignments.join("\n")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The symbol `name` asks for: an exact name first, else the first whose name contains it.
|
||||
pub(super) fn pick_symbol<'a>(symbols: &'a [FlatSymbol], name: &str) -> Option<&'a FlatSymbol> {
|
||||
symbols
|
||||
.iter()
|
||||
.find(|s| s.name == name)
|
||||
.or_else(|| symbols.iter().find(|s| s.name.contains(name)))
|
||||
}
|
||||
192
crates/luxe-lsp/src/tools/discovery/symbol_search.rs
Normal file
192
crates/luxe-lsp/src/tools/discovery/symbol_search.rs
Normal file
@@ -0,0 +1,192 @@
|
||||
//! `symbol_search` — the workspace's symbols that match a name, best first.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Ranked project symbol search. Prefers an **offline BM25 index** (all tree-sitter languages);
|
||||
/// falls back to the language server's `workspace/symbol` when the index is empty.
|
||||
pub struct SymbolSearchTool {
|
||||
service: Service,
|
||||
index: Arc<crate::symbol_index::SymbolIndexCache>,
|
||||
graph: Option<Arc<crate::graph::GraphService>>,
|
||||
}
|
||||
|
||||
impl SymbolSearchTool {
|
||||
/// Create the tool over the shared warm service.
|
||||
pub fn new(service: Service) -> Self {
|
||||
Self { service, index: Arc::default(), graph: None }
|
||||
}
|
||||
|
||||
/// Share `index` with whatever invalidates it when a file changes.
|
||||
pub fn with_index(mut self, index: Arc<crate::symbol_index::SymbolIndexCache>) -> Self {
|
||||
self.index = index;
|
||||
self
|
||||
}
|
||||
|
||||
/// Attach the warm code graph so results are ranked with a degree-centrality prior.
|
||||
pub fn with_graph(mut self, graph: Arc<crate::graph::GraphService>) -> Self {
|
||||
self.graph = Some(graph);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for SymbolSearchTool {
|
||||
fn name(&self) -> &str {
|
||||
"symbol_search"
|
||||
}
|
||||
|
||||
fn argument_aliases(&self) -> &'static [&'static str] {
|
||||
super::FILE_ALIASES
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "symbol_search".to_string(),
|
||||
description: "Find symbols across the project by name, ranked exact → prefix → BM25. \
|
||||
Uses a fast offline tree-sitter index (all supported languages); falls \
|
||||
back to the language server when the index is empty. First step of the \
|
||||
discovery funnel; follow up with module_report / read_symbol."
|
||||
.to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": { "type": "string", "description": "Symbol name to search for" },
|
||||
"path": { "type": "string", "description": "A file in the target workspace (only needed for the language-server fallback)" },
|
||||
"refresh": { "type": "boolean", "description": "Rebuild the offline index before searching" }
|
||||
},
|
||||
"required": ["query"]
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self, args: serde_json::Value, cx: &ToolContext) -> ToolResultData {
|
||||
let Some(query) = args.get("query").and_then(|v| v.as_str()) else {
|
||||
return ToolResultData::error("Error: 'query' is required");
|
||||
};
|
||||
let refresh = args.get("refresh").and_then(serde_json::Value::as_bool).unwrap_or(false);
|
||||
|
||||
// Preferred path: the offline BM25 index over tree-sitter symbols.
|
||||
match self.index.get(&cx.cwd, refresh).await {
|
||||
Ok(index) if !index.is_empty() => {
|
||||
// Fold in a graph-centrality prior when the code graph is available, so central
|
||||
// symbols (many callers/dependents) outrank equally-matched peripheral ones.
|
||||
// the prior only reorders, so a graph that cannot be built costs the ranking alone
|
||||
let graph = match &self.graph {
|
||||
Some(service) => service.graph(refresh).await.ok(),
|
||||
None => None,
|
||||
};
|
||||
let hits = match &graph {
|
||||
Some(graph) => index.search_with_prior(query, MAX_SYMBOLS, |sym| {
|
||||
centrality_prior(graph.as_ref(), sym)
|
||||
}),
|
||||
None => index.search(query, MAX_SYMBOLS),
|
||||
};
|
||||
let capped = index.capped_note();
|
||||
if hits.is_empty() {
|
||||
return ToolResultData::text(format!(
|
||||
"No symbols matching '{query}'\n{capped}"
|
||||
));
|
||||
}
|
||||
let (kept, looser_dropped) = tighten(hits, query);
|
||||
let mut out = format!("{} match(es) for '{query}':\n", kept.len());
|
||||
for hit in kept {
|
||||
out.push_str(&format!(" {}\n", crate::symbol_index::format_hit(hit)));
|
||||
}
|
||||
if looser_dropped {
|
||||
out.push_str(
|
||||
" Looser matches left out. Search part of a name to see more, or \
|
||||
`grep` for text.\n",
|
||||
);
|
||||
}
|
||||
out.push_str(&capped);
|
||||
return ToolResultData::text(out);
|
||||
}
|
||||
Ok(_) => {} // empty index → fall back to the language server
|
||||
Err(e) => return ToolResultData::error(format!("Error: {e}")),
|
||||
}
|
||||
|
||||
// Fallback: language-server `workspace/symbol` (needs a `file` to anchor).
|
||||
let Some(file) = super::file_arg(&args) else {
|
||||
return ToolResultData::error(
|
||||
"Error: no indexable files found; pass `path` for a language-server search",
|
||||
);
|
||||
};
|
||||
let full = cx.cwd.join(file);
|
||||
let client = { self.service.client_for_path(&full).await };
|
||||
let Some(client) = client else {
|
||||
return ToolResultData::error("Error: no language server for this file");
|
||||
};
|
||||
let mut symbols = match client.lock().await.workspace_symbol(query).await {
|
||||
Ok(symbols) => symbols,
|
||||
Err(e) => return ToolResultData::error(format!("Error: symbol_search failed: {e}")),
|
||||
};
|
||||
if symbols.is_empty() {
|
||||
return ToolResultData::text(format!("No symbols matching '{query}'"));
|
||||
}
|
||||
symbols.sort_by_key(|s| (rank(&s.name, query), s.name.clone()));
|
||||
let mut out = format!("{} match(es) for '{query}':\n", symbols.len().min(MAX_SYMBOLS));
|
||||
for sym in symbols.iter().take(MAX_SYMBOLS) {
|
||||
out.push_str(&format!(
|
||||
" {} ({}) {}:{}\n",
|
||||
sym.name,
|
||||
symbol_kind_name(sym.kind),
|
||||
crate::uri::short_display(&sym.uri),
|
||||
sym.line + 1,
|
||||
));
|
||||
}
|
||||
ToolResultData::text(out)
|
||||
}
|
||||
}
|
||||
|
||||
/// Most looser matches a search returns when nothing matched the name itself.
|
||||
pub(super) const MAX_LOOSE_SYMBOLS: usize = 20;
|
||||
|
||||
/// The hits worth sending, and whether looser ones were left out.
|
||||
///
|
||||
/// The index ranks exact names, then prefixes, then token matches, and when a name matched there is
|
||||
/// nothing to gain from the rest: `set_cancelled` came back with 59 lookalikes, `set` methods and
|
||||
/// every `Cancelled` variant, 1,200 tokens where `rg` answers in one line. So an exact or prefix
|
||||
/// match is the whole answer, and token matches stand alone only when nothing better exists, and
|
||||
/// then at most [`MAX_LOOSE_SYMBOLS`] of them.
|
||||
pub(super) fn tighten<'a>(
|
||||
hits: Vec<&'a crate::symbol_index::IndexedSymbol>,
|
||||
query: &str,
|
||||
) -> (Vec<&'a crate::symbol_index::IndexedSymbol>, bool) {
|
||||
let query = query.to_lowercase();
|
||||
let named =
|
||||
|hit: &&crate::symbol_index::IndexedSymbol| hit.name.to_lowercase().starts_with(&query);
|
||||
let total = hits.len();
|
||||
let kept: Vec<_> = if hits.iter().any(named) {
|
||||
hits.into_iter().filter(named).collect()
|
||||
} else {
|
||||
hits.into_iter().take(MAX_LOOSE_SYMBOLS).collect()
|
||||
};
|
||||
let dropped = kept.len() < total;
|
||||
(kept, dropped)
|
||||
}
|
||||
|
||||
/// Degree-centrality prior for a symbol: 1.0 for an isolated/unknown symbol, rising toward 1.5 for
|
||||
/// a hub (many callers/dependents). Boosts central symbols within their match tier without
|
||||
/// overriding exact/prefix ranking.
|
||||
fn centrality_prior(
|
||||
graph: &crate::graph::InMemoryGraph,
|
||||
sym: &crate::symbol_index::IndexedSymbol,
|
||||
) -> f64 {
|
||||
use crate::graph::{GraphStore, QualifiedName};
|
||||
let qn = QualifiedName::symbol(&sym.path, sym.container.as_deref(), &sym.name);
|
||||
let degree = graph.degree(&qn) as f64;
|
||||
1.0 + 0.5 * (degree / (degree + 5.0))
|
||||
}
|
||||
|
||||
/// Rank a symbol name against a query: exact (0) → prefix (1) → substring (2) → other (3).
|
||||
pub(super) fn rank(name: &str, query: &str) -> u8 {
|
||||
if name == query {
|
||||
0
|
||||
} else if name.starts_with(query) {
|
||||
1
|
||||
} else if name.contains(query) {
|
||||
2
|
||||
} else {
|
||||
3
|
||||
}
|
||||
}
|
||||
154
crates/luxe-lsp/src/tools/discovery/tests.rs
Normal file
154
crates/luxe-lsp/src/tools/discovery/tests.rs
Normal file
@@ -0,0 +1,154 @@
|
||||
use super::read_symbol::{enclosing_context, pick_symbol};
|
||||
use super::symbol_search::{rank, tighten, MAX_LOOSE_SYMBOLS};
|
||||
use super::*;
|
||||
|
||||
fn sym(name: &str) -> FlatSymbol {
|
||||
FlatSymbol {
|
||||
name: name.to_string(),
|
||||
kind: 12,
|
||||
uri: String::new(),
|
||||
line: 0,
|
||||
character: 0,
|
||||
start_line: 0,
|
||||
end_line: 0,
|
||||
container: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A name that matched is the answer: its lookalikes stay out, and looser matches are returned,
|
||||
/// capped, only when nothing matched the name.
|
||||
#[test]
|
||||
fn a_named_match_leaves_the_lookalikes_out() {
|
||||
let symbol = |name: &str| crate::symbol_index::IndexedSymbol {
|
||||
name: name.into(),
|
||||
kind: 6,
|
||||
path: "a.rs".into(),
|
||||
line: 1,
|
||||
container: None,
|
||||
};
|
||||
let pool: Vec<_> = ["set_cancelled", "set_cancelled_flag", "set", "Cancelled", "cancel_tx"]
|
||||
.into_iter()
|
||||
.map(symbol)
|
||||
.collect();
|
||||
let names = |hits: Vec<&crate::symbol_index::IndexedSymbol>| -> Vec<String> {
|
||||
hits.into_iter().map(|hit| hit.name.clone()).collect()
|
||||
};
|
||||
|
||||
let (kept, dropped) = tighten(pool.iter().collect(), "set_cancelled");
|
||||
assert_eq!(names(kept), ["set_cancelled", "set_cancelled_flag"]);
|
||||
assert!(dropped);
|
||||
|
||||
let (kept, dropped) = tighten(pool.iter().collect(), "SET_CANCELLED");
|
||||
assert_eq!(names(kept).len(), 2, "case does not matter");
|
||||
assert!(dropped);
|
||||
|
||||
let (kept, dropped) = tighten(pool[..1].iter().collect(), "set_cancelled");
|
||||
assert_eq!(names(kept), ["set_cancelled"]);
|
||||
assert!(!dropped, "nothing left out, so nothing said");
|
||||
|
||||
let many: Vec<_> = (0..30).map(|n| symbol(&format!("render_{n}"))).collect();
|
||||
let (kept, dropped) = tighten(many.iter().collect(), "footer");
|
||||
assert_eq!(kept.len(), MAX_LOOSE_SYMBOLS);
|
||||
assert!(dropped);
|
||||
let (kept, dropped) = tighten(many[..3].iter().collect(), "footer");
|
||||
assert_eq!(kept.len(), 3);
|
||||
assert!(!dropped);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rank_orders_exact_prefix_substring() {
|
||||
assert_eq!(rank("foo", "foo"), 0);
|
||||
assert_eq!(rank("foobar", "foo"), 1);
|
||||
assert_eq!(rank("do_foo", "foo"), 2);
|
||||
assert_eq!(rank("bar", "foo"), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pick_prefers_exact_then_substring() {
|
||||
let symbols = [sym("compute_total"), sym("total"), sym("subtotal")];
|
||||
assert_eq!(pick_symbol(&symbols, "total").unwrap().name, "total");
|
||||
assert_eq!(pick_symbol(&symbols, "compute").unwrap().name, "compute_total");
|
||||
assert!(pick_symbol(&symbols, "missing").is_none());
|
||||
}
|
||||
|
||||
/// A typo'd path must say "not found", not "no symbols" — the latter reads like an empty file.
|
||||
#[tokio::test]
|
||||
async fn a_missing_file_is_reported_as_missing() {
|
||||
let service = std::sync::Arc::new(crate::workspace::WorkspaceLspClient::new());
|
||||
let (event_tx, _event_rx) = tokio::sync::mpsc::channel(4);
|
||||
let (_cancel_tx, cancel_rx) = tokio::sync::watch::channel(false);
|
||||
let cx = ToolContext {
|
||||
cwd: std::env::temp_dir(),
|
||||
session_id: "test-session".to_string(),
|
||||
cancelled: std::sync::Arc::new(cancel_rx),
|
||||
event_tx,
|
||||
hook_ctx: None,
|
||||
question_broker: None,
|
||||
};
|
||||
let Err(error) = build_outline(&service, &cx, "definitely/not/here.rs", false).await else {
|
||||
panic!("a missing path must be an error");
|
||||
};
|
||||
assert!(error.contains("file not found"), "{error}");
|
||||
|
||||
let Err(directory) = build_outline(&service, &cx, ".", false).await else {
|
||||
panic!("a directory must be an error");
|
||||
};
|
||||
assert!(directory.contains("is a directory"), "{directory}");
|
||||
}
|
||||
|
||||
fn scoped(name: &str, container: Option<&str>, start: u32, end: u32) -> FlatSymbol {
|
||||
FlatSymbol {
|
||||
name: name.to_string(),
|
||||
kind: 6,
|
||||
uri: String::new(),
|
||||
line: start,
|
||||
character: 0,
|
||||
start_line: start,
|
||||
end_line: end,
|
||||
container: container.map(str::to_string),
|
||||
}
|
||||
}
|
||||
|
||||
/// The measured failure: a five-line method body that answers nothing on its own because the
|
||||
/// field it returns from is assigned in `__init__`.
|
||||
#[test]
|
||||
fn a_method_body_carries_the_fields_it_depends_on() {
|
||||
let source = vec![
|
||||
"class GoogleAuthService:",
|
||||
" def __init__(self, session):",
|
||||
" self.connection_repo = GoogleConnectionRepository(session)",
|
||||
" self.unused = Other()",
|
||||
"",
|
||||
" async def get_connection(self, *, user_id: int):",
|
||||
" return await self.connection_repo.get_by_user_id(user_id)",
|
||||
];
|
||||
let symbols = vec![
|
||||
scoped("GoogleAuthService", None, 0, 6),
|
||||
scoped("get_connection", Some("GoogleAuthService"), 5, 6),
|
||||
];
|
||||
let body = source[5..=6].join("\n");
|
||||
|
||||
let context = enclosing_context(&symbols[1], &symbols, &source, &body);
|
||||
|
||||
assert!(context.contains("GoogleAuthService"), "names the class: {context}");
|
||||
assert!(
|
||||
context.contains("GoogleConnectionRepository"),
|
||||
"and the field's real type, which is the actual answer: {context}"
|
||||
);
|
||||
assert!(!context.contains("self.unused"), "only fields the body uses: {context}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_free_function_gets_no_class_context() {
|
||||
let source = vec!["def helper():", " return 1"];
|
||||
let symbols = vec![scoped("helper", None, 0, 1)];
|
||||
assert_eq!(enclosing_context(&symbols[0], &symbols, &source, "return 1"), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_method_touching_no_fields_just_names_its_class() {
|
||||
let source = vec!["class C:", " def f(self):", " return 1"];
|
||||
let symbols = vec![scoped("C", None, 0, 2), scoped("f", Some("C"), 1, 2)];
|
||||
let context = enclosing_context(&symbols[1], &symbols, &source, "return 1");
|
||||
assert_eq!(context, "\n\n(defined on `C`)");
|
||||
}
|
||||
222
crates/luxe-lsp/src/tools/impact.rs
Normal file
222
crates/luxe-lsp/src/tools/impact.rs
Normal file
@@ -0,0 +1,222 @@
|
||||
//! `impact` tool — the blast radius of a change (design-code-graph.md §8).
|
||||
//!
|
||||
//! Answers "if I change X, what else is affected?" from the offline structural code graph: the
|
||||
//! transitive callers, subclasses, and covering tests of a symbol or file, plus what it depends on
|
||||
//! and any missing test coverage. It reads the warm [`GraphService`] (built lazily, cached, and
|
||||
//! kept fresh by the `FileChanged` hook), so it's fast and needs no language server.
|
||||
|
||||
use super::*;
|
||||
use crate::graph::{
|
||||
blast_radius, verify_callers, BlastRadius, GraphService, InMemoryGraph, LspReferenceSource,
|
||||
DEFAULT_MAX_DEPTH,
|
||||
};
|
||||
|
||||
/// Reports the transitive impact of changing a symbol or file.
|
||||
pub struct ImpactTool {
|
||||
graph: Arc<GraphService>,
|
||||
lsp: Option<Arc<crate::WorkspaceLspClient>>,
|
||||
}
|
||||
|
||||
impl ImpactTool {
|
||||
/// Create the tool over the shared warm graph service.
|
||||
pub fn new(graph: Arc<GraphService>) -> Self {
|
||||
Self { graph, lsp: None }
|
||||
}
|
||||
|
||||
/// Attach the warm LSP service so `verify: true` can confirm heuristic callers against the
|
||||
/// server's precise references (degrades gracefully when no server answers).
|
||||
pub fn with_lsp(mut self, lsp: Arc<crate::WorkspaceLspClient>) -> Self {
|
||||
self.lsp = Some(lsp);
|
||||
self
|
||||
}
|
||||
|
||||
/// LSP verification of the blast radius's seed callers, appended to the output. `None` when no
|
||||
/// LSP service is attached.
|
||||
async fn verify_section(
|
||||
&self,
|
||||
graph: &InMemoryGraph,
|
||||
br: &BlastRadius,
|
||||
cx: &ToolContext,
|
||||
) -> Option<String> {
|
||||
const MAX_VERIFY_SEEDS: usize = 10;
|
||||
let lsp = self.lsp.as_ref()?;
|
||||
let source = LspReferenceSource::new(lsp.clone(), cx.cwd.clone());
|
||||
|
||||
let mut verified = std::collections::BTreeSet::new();
|
||||
let mut recovered = std::collections::BTreeSet::new();
|
||||
let mut answered = false;
|
||||
for seed in br.seeds.iter().take(MAX_VERIFY_SEEDS) {
|
||||
if let Some(callers) = verify_callers(graph, seed, &source).await {
|
||||
answered = true;
|
||||
for caller in callers {
|
||||
if caller.in_lsp {
|
||||
verified.insert(caller.file.clone());
|
||||
if !caller.in_graph {
|
||||
recovered.insert(caller.file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !answered {
|
||||
return Some("\nVerification: no language server answered (heuristic only).\n".into());
|
||||
}
|
||||
let mut out = String::from("\nLSP-verified callers (rust-analyzer):\n");
|
||||
if verified.is_empty() {
|
||||
out.push_str(" (none confirmed)\n");
|
||||
}
|
||||
for file in &verified {
|
||||
out.push_str(&format!(" {file}\n"));
|
||||
}
|
||||
if !recovered.is_empty() {
|
||||
out.push_str("LSP recovered (missed by the heuristic graph):\n");
|
||||
for file in &recovered {
|
||||
out.push_str(&format!(" {file}\n"));
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for ImpactTool {
|
||||
fn name(&self) -> &str {
|
||||
"impact"
|
||||
}
|
||||
|
||||
fn argument_aliases(&self) -> &'static [&'static str] {
|
||||
&["symbol", "file", "path"]
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "impact".to_string(),
|
||||
description:
|
||||
"Compute the blast radius of a change from a fast offline code graph: the \
|
||||
transitive callers, dependents, subclasses, and covering tests of a \
|
||||
symbol or file (\"if I change X, what else is affected?\"), plus what it \
|
||||
relies on and any seed with no covering test. Give a symbol name, a \
|
||||
qualified name (file::Symbol), or a file path (relative to cwd)."
|
||||
.to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"target": { "type": "string", "description": "A symbol name, qualified name (file::Symbol), or file path relative to cwd" },
|
||||
"depth": { "type": "integer", "description": "Max transitive depth to walk (default 2, max 5)" },
|
||||
"refresh": { "type": "boolean", "description": "Rebuild the graph before querying" },
|
||||
"verify": { "type": "boolean", "description": "Confirm heuristic callers against the language server's precise references (slower; needs a warm server)" }
|
||||
},
|
||||
"required": ["target"]
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self, args: serde_json::Value, cx: &ToolContext) -> ToolResultData {
|
||||
// `target` is the documented spelling; a model arriving from `read`/`read_symbol` holds
|
||||
// the word it just used, and that call means the same thing.
|
||||
let target = ["target", "symbol", "file", "path"]
|
||||
.iter()
|
||||
.find_map(|key| args.get(*key).and_then(|value| value.as_str()))
|
||||
.filter(|text| !text.is_empty());
|
||||
let Some(target) = target else {
|
||||
return ToolResultData::error("Error: 'target' is required");
|
||||
};
|
||||
let depth = args
|
||||
.get("depth")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
.map(|d| d as u32)
|
||||
.unwrap_or(DEFAULT_MAX_DEPTH)
|
||||
.clamp(1, 5);
|
||||
let refresh = args.get("refresh").and_then(serde_json::Value::as_bool).unwrap_or(false);
|
||||
|
||||
let verify = args.get("verify").and_then(serde_json::Value::as_bool).unwrap_or(false);
|
||||
|
||||
let target = project_relative(target, cx);
|
||||
let graph = match self.graph.graph(refresh).await {
|
||||
Ok(graph) => graph,
|
||||
Err(error) => return ToolResultData::error(format!("Error: {error}")),
|
||||
};
|
||||
let br = blast_radius(&graph, &target, depth);
|
||||
let mut out = br.render(&target);
|
||||
if verify {
|
||||
if let Some(section) = self.verify_section(&graph, &br, cx).await {
|
||||
out.push_str(§ion);
|
||||
}
|
||||
}
|
||||
ToolResultData::text(out)
|
||||
}
|
||||
}
|
||||
|
||||
/// Make an absolute path under `cwd` project-relative (graph keys are project-relative). A symbol
|
||||
/// or already-relative path passes through unchanged.
|
||||
fn project_relative(target: &str, cx: &ToolContext) -> String {
|
||||
Path::new(target)
|
||||
.strip_prefix(&cx.cwd)
|
||||
.map(luxe_core::paths::display)
|
||||
.unwrap_or_else(|_| target.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
fn context(cwd: &Path) -> ToolContext {
|
||||
let (event_tx, _rx) = mpsc::channel(8);
|
||||
let (_cancel_tx, cancel_rx) = watch::channel(false);
|
||||
ToolContext {
|
||||
cwd: cwd.to_path_buf(),
|
||||
session_id: "test".to_string(),
|
||||
cancelled: Arc::new(cancel_rx),
|
||||
event_tx,
|
||||
hook_ctx: None,
|
||||
question_broker: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A throwaway project, owned by the test that asked for it.
|
||||
///
|
||||
/// One directory per test: a shared one deleted at the start and end of each test is a race
|
||||
/// under `cargo test`, where one test deletes the tree the other is walking.
|
||||
fn scratch() -> tempfile::TempDir {
|
||||
let dir = tempfile::tempdir().expect("temp dir");
|
||||
let root = dir.path();
|
||||
std::fs::create_dir_all(root.join("src")).unwrap();
|
||||
std::fs::write(root.join("src/auth.rs"), "pub fn login() {}\npub fn logout() {}\n")
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
root.join("src/session.rs"),
|
||||
"use crate::auth::login;\npub fn start() { login() }\n",
|
||||
)
|
||||
.unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn impact_reports_callers_and_test_gap() {
|
||||
let dir = scratch();
|
||||
let tool = ImpactTool::new(Arc::new(GraphService::new(dir.path())));
|
||||
let cx = context(dir.path());
|
||||
|
||||
let result = tool.run(json!({ "target": "login" }), &cx).await;
|
||||
assert!(!result.is_error);
|
||||
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
|
||||
// The caller file appears as a dependent, and login has no covering test. Forward slashes
|
||||
// on every platform: this line is read by the model and handed straight back to `read`.
|
||||
assert!(text.contains("src/session.rs"), "caller listed:\n{text}");
|
||||
assert!(text.contains("no covering test"), "test-gap flagged:\n{text}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn impact_unknown_target_is_not_an_error() {
|
||||
let dir = scratch();
|
||||
let tool = ImpactTool::new(Arc::new(GraphService::new(dir.path())));
|
||||
let cx = context(dir.path());
|
||||
|
||||
let result = tool.run(json!({ "target": "nope_missing" }), &cx).await;
|
||||
assert!(!result.is_error);
|
||||
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
|
||||
assert!(text.contains("No graph node found"));
|
||||
}
|
||||
}
|
||||
373
crates/luxe-lsp/src/tools/navigation.rs
Normal file
373
crates/luxe-lsp/src/tools/navigation.rs
Normal file
@@ -0,0 +1,373 @@
|
||||
//! The `lsp_navigation` tool — IDE navigation over a warm, multi-language server.
|
||||
//!
|
||||
//! Supports definition/type-definition/declaration/implementation, references, hover,
|
||||
//! document/workspace symbols, signature help, call hierarchy, code actions, and a rename
|
||||
//! *preview*. Positions may be given as `line`/`character` (1-based) or resolved from a `symbol`
|
||||
//! name via `documentSymbol`. Uses the shared warm [`WorkspaceLspClient`] service.
|
||||
|
||||
use super::*;
|
||||
use crate::client::LspClient;
|
||||
use crate::edit_apply::Writes;
|
||||
use crate::protocol::{symbol_kind_name, FlatSymbol, Location, Range};
|
||||
use crate::workspace::WorkspaceLspClient;
|
||||
use luxe_core::read_tracker::ReadTracker;
|
||||
use serde_json::Value;
|
||||
use std::time::Duration;
|
||||
|
||||
const POSITION_ACTIONS: &str = "go_to_definition, type_definition, declaration, implementation, \
|
||||
find_references, hover, signature_help, call_hierarchy, \
|
||||
code_action, rename";
|
||||
|
||||
fn lsp_navigation_schema() -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "lsp_navigation".to_string(),
|
||||
description: "Navigate code via a warm language server. Give a position as line+character \
|
||||
(1-based) OR a `symbol` name — every position-based action accepts either. \
|
||||
Params by action: \
|
||||
go_to_definition|type_definition|declaration|implementation|find_references|\
|
||||
hover|signature_help{path, position}; document_symbols{path}; \
|
||||
workspace_symbol{path, query}; call_hierarchy{path, position, direction?}; \
|
||||
rename{path, position, newName, apply?}; \
|
||||
code_action{path, position, apply?, title? | index?}. rename and code_action \
|
||||
preview by default; pass apply:true to write the edits."
|
||||
.to_string(),
|
||||
parameters: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": { "type": "string", "description": "Path to the file (relative to cwd)" },
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"go_to_definition", "type_definition", "declaration", "implementation",
|
||||
"find_references", "hover", "document_symbols", "workspace_symbol",
|
||||
"signature_help", "call_hierarchy", "code_action", "rename"
|
||||
],
|
||||
"description": "Navigation action to perform"
|
||||
},
|
||||
"line": { "type": "integer", "description": "1-based line for position-based actions" },
|
||||
"character": { "type": "integer", "description": "1-based column for position-based actions" },
|
||||
"symbol": { "type": "string", "description": "Resolve the position from this symbol name (instead of line/character)" },
|
||||
"query": { "type": "string", "description": "workspace_symbol: symbol name to search for" },
|
||||
"direction": { "type": "string", "enum": ["incoming", "outgoing"], "description": "call_hierarchy direction (default incoming)" },
|
||||
"newName": { "type": "string", "description": "rename: the new symbol name" },
|
||||
"apply": { "type": "boolean", "description": "rename/code_action: apply the edits to disk (default false = preview)" },
|
||||
"title": { "type": "string", "description": "code_action apply: title of the action to apply" },
|
||||
"index": { "type": "integer", "description": "code_action apply: 0-based index of the action to apply (default 0)" }
|
||||
},
|
||||
"required": ["path", "action"]
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// The `lsp_navigation` tool.
|
||||
pub struct LspNavigationTool {
|
||||
service: Arc<WorkspaceLspClient>,
|
||||
tracker: Option<Arc<ReadTracker>>,
|
||||
}
|
||||
|
||||
impl LspNavigationTool {
|
||||
/// Creates a navigation tool backed by the shared warm service.
|
||||
pub fn new(service: Arc<WorkspaceLspClient>) -> Self {
|
||||
Self { service, tracker: None }
|
||||
}
|
||||
|
||||
/// Attach the shared read tracker, so a file an applied edit rewrote stays editable.
|
||||
pub fn with_tracker(mut self, tracker: Arc<ReadTracker>) -> Self {
|
||||
self.tracker = Some(tracker);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for LspNavigationTool {
|
||||
fn name(&self) -> &str {
|
||||
"lsp_navigation"
|
||||
}
|
||||
|
||||
fn argument_aliases(&self) -> &'static [&'static str] {
|
||||
super::FILE_ALIASES
|
||||
}
|
||||
|
||||
fn schema(&self) -> ToolSchema {
|
||||
lsp_navigation_schema()
|
||||
}
|
||||
|
||||
async fn run(&self, args: serde_json::Value, cx: &ToolContext) -> ToolResultData {
|
||||
let Some(file_str) = super::file_arg(&args) else {
|
||||
return ToolResultData::error("Error: 'path' argument is required");
|
||||
};
|
||||
let Some(action) = args.get("action").and_then(|v| v.as_str()).map(str::to_string) else {
|
||||
return ToolResultData::error("Error: 'action' argument is required");
|
||||
};
|
||||
let full_path = cx.cwd.join(file_str);
|
||||
let uri = crate::uri::from_path(&full_path);
|
||||
// Read the file once: opening the document lets a cold server answer, and the content backs
|
||||
// tree-sitter symbol-name resolution.
|
||||
let content = std::fs::read_to_string(&full_path).ok();
|
||||
|
||||
let client = { self.service.client_for_path(&full_path).await };
|
||||
let Some(client) = client else {
|
||||
return ToolResultData::error(format!(
|
||||
"Error: {}",
|
||||
self.service.missing_server_hint(&full_path)
|
||||
));
|
||||
};
|
||||
let mut guard = client.lock().await;
|
||||
if let Some(source) = &content {
|
||||
let _ = guard.open_document(&uri, source).await;
|
||||
}
|
||||
// Navigation/symbol queries need the crate indexed; a warm server returns instantly.
|
||||
guard.wait_until_idle(NAV_READY_WAIT).await;
|
||||
let writes = Writes { cx, tracker: self.tracker.as_deref() };
|
||||
dispatch(&mut guard, &uri, &action, &args, writes, &full_path, content.as_deref()).await
|
||||
}
|
||||
}
|
||||
|
||||
/// How long a navigation call waits for a cold server to finish indexing before querying.
|
||||
const NAV_READY_WAIT: Duration = Duration::from_secs(45);
|
||||
|
||||
/// Runs one navigation action and formats the result.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn dispatch(
|
||||
client: &mut LspClient,
|
||||
uri: &str,
|
||||
action: &str,
|
||||
args: &serde_json::Value,
|
||||
writes: Writes<'_>,
|
||||
full_path: &Path,
|
||||
content: Option<&str>,
|
||||
) -> ToolResultData {
|
||||
// Actions that don't take a position first.
|
||||
match action {
|
||||
"workspace_symbol" => {
|
||||
let Some(query) = args.get("query").and_then(Value::as_str) else {
|
||||
return ToolResultData::error("Error: 'query' is required for workspace_symbol");
|
||||
};
|
||||
return match client.workspace_symbol(query).await {
|
||||
Ok(symbols) => ToolResultData::text(format_symbols(&symbols, true)),
|
||||
Err(e) => ToolResultData::error(format!("Error: workspace_symbol failed: {e}")),
|
||||
};
|
||||
}
|
||||
"document_symbols" => {
|
||||
return match client.document_symbols(uri).await {
|
||||
Ok(symbols) => ToolResultData::text(format_symbols(&symbols, false)),
|
||||
Err(e) => ToolResultData::error(format!("Error: document_symbols failed: {e}")),
|
||||
};
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Everything else needs a position (from line/character or a symbol name).
|
||||
let position = match resolve_position(client, uri, args, full_path, content).await {
|
||||
Ok(position) => position,
|
||||
Err(message) => return ToolResultData::error(&message),
|
||||
};
|
||||
|
||||
match action {
|
||||
"go_to_definition" => match client.goto_definition(uri, position).await {
|
||||
Ok(Some(loc)) => ToolResultData::text(format!("Definition: {}", format_location(&loc))),
|
||||
Ok(None) => ToolResultData::text(nothing_found("No definition found", client)),
|
||||
Err(e) => ToolResultData::error(format!("Error: definition failed: {e}")),
|
||||
},
|
||||
"type_definition" => locations_reply(
|
||||
client.goto_type_definition(uri, position).await,
|
||||
"type definition",
|
||||
client,
|
||||
),
|
||||
"declaration" => {
|
||||
locations_reply(client.goto_declaration(uri, position).await, "declaration", client)
|
||||
}
|
||||
"implementation" => locations_reply(
|
||||
client.goto_implementation(uri, position).await,
|
||||
"implementation",
|
||||
client,
|
||||
),
|
||||
"find_references" => {
|
||||
locations_reply(client.find_references(uri, position).await, "reference", client)
|
||||
}
|
||||
"hover" => match client.hover(uri, position).await {
|
||||
Ok(Some(markup)) => ToolResultData::text(markup.value),
|
||||
Ok(None) => ToolResultData::text(nothing_found("No hover information", client)),
|
||||
Err(e) => ToolResultData::error(format!("Error: hover failed: {e}")),
|
||||
},
|
||||
"signature_help" => match client.signature_help(uri, position).await {
|
||||
Ok(value) => ToolResultData::text(format_signature_help(&value)),
|
||||
Err(e) => ToolResultData::error(format!("Error: signature_help failed: {e}")),
|
||||
},
|
||||
"call_hierarchy" => call_hierarchy(client, uri, position, args).await,
|
||||
"code_action" => {
|
||||
let range = Range { start: position.clone(), end: position };
|
||||
let actions = match client.code_action(uri, range).await {
|
||||
Ok(actions) => actions,
|
||||
Err(e) => return ToolResultData::error(format!("Error: code_action failed: {e}")),
|
||||
};
|
||||
if !args.get("apply").and_then(Value::as_bool).unwrap_or(false) {
|
||||
return ToolResultData::text(format_code_actions(&actions));
|
||||
}
|
||||
apply_code_action(client, &actions, args, writes).await
|
||||
}
|
||||
"rename" => {
|
||||
let Some(new_name) = args.get("newName").and_then(Value::as_str) else {
|
||||
return ToolResultData::error("Error: 'newName' is required for rename");
|
||||
};
|
||||
let edit = match client.rename(uri, position, new_name).await {
|
||||
Ok(edit) => edit,
|
||||
Err(e) => return ToolResultData::error(format!("Error: rename failed: {e}")),
|
||||
};
|
||||
if args.get("apply").and_then(Value::as_bool).unwrap_or(false) {
|
||||
crate::edit_apply::apply_workspace_edit(&edit, writes).await
|
||||
} else {
|
||||
ToolResultData::text(format_rename_preview(&edit))
|
||||
}
|
||||
}
|
||||
other => ToolResultData::error(format!(
|
||||
"Error: unknown action '{other}'. Position actions: {POSITION_ACTIONS}."
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a position from `line`/`character` (1-based) or a `symbol` name.
|
||||
///
|
||||
/// Symbol names resolve via **tree-sitter** first (offline, reliable even on a cold server), then
|
||||
/// fall back to the server's `documentSymbol`.
|
||||
async fn resolve_position(
|
||||
client: &mut LspClient,
|
||||
uri: &str,
|
||||
args: &serde_json::Value,
|
||||
full_path: &Path,
|
||||
content: Option<&str>,
|
||||
) -> Result<Position, String> {
|
||||
if let Some(line) = args.get("line").and_then(Value::as_u64) {
|
||||
let character = args.get("character").and_then(Value::as_u64).unwrap_or(1);
|
||||
return Ok(Position {
|
||||
line: (line.saturating_sub(1)) as u32,
|
||||
character: (character.saturating_sub(1)) as u32,
|
||||
});
|
||||
}
|
||||
let Some(name) = args.get("symbol").and_then(Value::as_str) else {
|
||||
return Err("Error: provide a `line` (1-based) or a `symbol` name".to_string());
|
||||
};
|
||||
|
||||
// Tree-sitter first.
|
||||
if let Some(source) = content {
|
||||
if let Some(symbols) = crate::syntax::outline(full_path, source) {
|
||||
if let Some(found) = symbols
|
||||
.iter()
|
||||
.find(|s| s.name == name)
|
||||
.or_else(|| symbols.iter().find(|s| s.name.contains(name)))
|
||||
{
|
||||
return Ok(Position { line: found.line, character: found.character });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to the language server's document outline.
|
||||
let symbols =
|
||||
client.document_symbols(uri).await.map_err(|e| format!("symbol lookup failed: {e}"))?;
|
||||
symbols
|
||||
.iter()
|
||||
.find(|s| s.name == name)
|
||||
.or_else(|| symbols.iter().find(|s| s.name.contains(name)))
|
||||
.map(|found| Position { line: found.line, character: found.character })
|
||||
.ok_or_else(|| format!("symbol '{name}' not found in this file"))
|
||||
}
|
||||
|
||||
async fn call_hierarchy(
|
||||
client: &mut LspClient,
|
||||
uri: &str,
|
||||
position: Position,
|
||||
args: &serde_json::Value,
|
||||
) -> ToolResultData {
|
||||
let items = match client.prepare_call_hierarchy(uri, position).await {
|
||||
Ok(items) => items,
|
||||
Err(e) => return ToolResultData::error(format!("Error: call_hierarchy failed: {e}")),
|
||||
};
|
||||
let Some(item) = items.into_iter().next() else {
|
||||
return ToolResultData::text("No call hierarchy at this position");
|
||||
};
|
||||
let outgoing = args.get("direction").and_then(Value::as_str) == Some("outgoing");
|
||||
let (calls, verb, side) = if outgoing {
|
||||
(client.outgoing_calls(item).await, "calls out to", "to")
|
||||
} else {
|
||||
(client.incoming_calls(item).await, "called by", "from")
|
||||
};
|
||||
match calls {
|
||||
Ok(calls) if calls.is_empty() => ToolResultData::text(format!("No {verb} entries")),
|
||||
Ok(calls) => {
|
||||
let mut out = format!("{} {} entry(ies):\n", calls.len(), verb);
|
||||
for call in &calls {
|
||||
if let Some(node) = call.get(side) {
|
||||
out.push_str(&format!(" {}\n", format_hierarchy_node(node)));
|
||||
}
|
||||
}
|
||||
ToolResultData::text(out)
|
||||
}
|
||||
Err(e) => ToolResultData::error(format!("Error: call_hierarchy failed: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Select + apply one code action (inline `edit`, else `codeAction/resolve`; commands unsupported).
|
||||
async fn apply_code_action(
|
||||
client: &mut LspClient,
|
||||
actions: &[Value],
|
||||
args: &serde_json::Value,
|
||||
writes: Writes<'_>,
|
||||
) -> ToolResultData {
|
||||
if actions.is_empty() {
|
||||
return ToolResultData::text("No code actions available");
|
||||
}
|
||||
let chosen = if let Some(title) = args.get("title").and_then(Value::as_str) {
|
||||
actions.iter().find(|a| a.get("title").and_then(Value::as_str) == Some(title))
|
||||
} else {
|
||||
let index = args.get("index").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
actions.get(index)
|
||||
};
|
||||
let Some(action) = chosen else {
|
||||
return ToolResultData::error(
|
||||
"Error: no matching code action; list them first (omit apply)",
|
||||
);
|
||||
};
|
||||
|
||||
// Prefer the inline edit; otherwise resolve it; commands aren't executed here.
|
||||
let edit = if let Some(edit) = action.get("edit") {
|
||||
Some(edit.clone())
|
||||
} else {
|
||||
client
|
||||
.send_request("codeAction/resolve", action.clone())
|
||||
.await
|
||||
.ok()
|
||||
.and_then(|resolved| resolved.get("edit").cloned())
|
||||
};
|
||||
match edit {
|
||||
Some(edit) => crate::edit_apply::apply_workspace_edit(&edit, writes).await,
|
||||
None => ToolResultData::error(
|
||||
"Error: this code action runs a command, not an inline edit; command execution is not \
|
||||
supported yet",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
mod format;
|
||||
|
||||
use format::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Adding host-file excerpts to server locations must not expose the target's contents.
|
||||
#[test]
|
||||
fn server_locations_do_not_expand_target_contents() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let secret = dir.path().join(".env");
|
||||
std::fs::write(&secret, "LOCATION_CONTENT_MUST_NOT_BE_RETURNED").unwrap();
|
||||
let location: Location = serde_json::from_value(serde_json::json!({
|
||||
"uri": crate::uri::from_path(&secret),
|
||||
"range": {"start": {"line": 2, "character": 4}, "end": {"line": 2, "character": 7}}
|
||||
}))
|
||||
.unwrap();
|
||||
let formatted = format_location(&location);
|
||||
assert!(formatted.ends_with(".env:3:5"), "{formatted}");
|
||||
assert!(!formatted.contains("LOCATION_CONTENT_MUST_NOT_BE_RETURNED"), "{formatted}");
|
||||
}
|
||||
}
|
||||
170
crates/luxe-lsp/src/tools/navigation/format.rs
Normal file
170
crates/luxe-lsp/src/tools/navigation/format.rs
Normal file
@@ -0,0 +1,170 @@
|
||||
//! Formatting — locations, symbols and previews as the lines a model reads.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Most locations listed before the tail is summarized.
|
||||
pub(super) const MAX_LOCATIONS: usize = 60;
|
||||
|
||||
/// Most symbols listed before the tail is summarized.
|
||||
pub(super) const MAX_SYMBOLS: usize = 60;
|
||||
|
||||
pub(super) fn locations_reply(
|
||||
result: Result<Vec<Location>, crate::client::LspError>,
|
||||
label: &str,
|
||||
client: &LspClient,
|
||||
) -> ToolResultData {
|
||||
match result {
|
||||
Ok(locs) if locs.is_empty() => {
|
||||
ToolResultData::text(nothing_found(&format!("No {label}s found"), client))
|
||||
}
|
||||
Ok(locs) => {
|
||||
let mut out = format!("{} {label}(s):\n", locs.len());
|
||||
// `find_references` on a hot symbol answers with hundreds of locations, and the count
|
||||
// in the header is the part that decides what to do next — a caller reading every entry
|
||||
// is already better served by `impact`.
|
||||
for loc in locs.iter().take(MAX_LOCATIONS) {
|
||||
out.push_str(&format!(" {}\n", format_location(loc)));
|
||||
}
|
||||
if locs.len() > MAX_LOCATIONS {
|
||||
out.push_str(&format!(
|
||||
"… and {} more. Use `impact` for a ranked view of what a change here reaches.\n",
|
||||
locs.len() - MAX_LOCATIONS
|
||||
));
|
||||
}
|
||||
ToolResultData::text(out)
|
||||
}
|
||||
Err(e) => ToolResultData::error(format!("Error: {label} failed: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn format_location(loc: &Location) -> String {
|
||||
format!(
|
||||
"{}:{}:{}",
|
||||
crate::uri::short_display(&loc.uri),
|
||||
loc.range.start.line + 1,
|
||||
loc.range.start.character + 1
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn format_symbols(symbols: &[FlatSymbol], with_uri: bool) -> String {
|
||||
if symbols.is_empty() {
|
||||
return String::from("No symbols found");
|
||||
}
|
||||
let mut out = format!("{} symbol(s):\n", symbols.len());
|
||||
// A broad `workspace_symbol` query matches most of a codebase; the header carries the count and
|
||||
// a narrower query beats scrolling the tail.
|
||||
for sym in symbols.iter().take(MAX_SYMBOLS) {
|
||||
let kind = symbol_kind_name(sym.kind);
|
||||
let loc = if with_uri {
|
||||
format!("{}:{}", crate::uri::short_display(&sym.uri), sym.line + 1)
|
||||
} else {
|
||||
format!("{}", sym.line + 1)
|
||||
};
|
||||
out.push_str(&format!(" {} ({kind}) {loc}", sym.name));
|
||||
if let Some(container) = &sym.container {
|
||||
out.push_str(&format!(" in {container}"));
|
||||
}
|
||||
out.push('\n');
|
||||
}
|
||||
if symbols.len() > MAX_SYMBOLS {
|
||||
out.push_str(&format!(
|
||||
"… and {} more. Narrow the query for the rest.\n",
|
||||
symbols.len() - MAX_SYMBOLS
|
||||
));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub(super) fn format_signature_help(value: &Value) -> String {
|
||||
let signatures = value.get("signatures").and_then(Value::as_array);
|
||||
let Some(signatures) = signatures.filter(|s| !s.is_empty()) else {
|
||||
return "No signature help".to_string();
|
||||
};
|
||||
let active = value.get("activeSignature").and_then(Value::as_u64).unwrap_or(0) as usize;
|
||||
let chosen = signatures.get(active).or_else(|| signatures.first());
|
||||
chosen
|
||||
.and_then(|s| s.get("label"))
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| "No signature help".to_string())
|
||||
}
|
||||
|
||||
pub(super) fn format_code_actions(actions: &[Value]) -> String {
|
||||
if actions.is_empty() {
|
||||
return "No code actions available".to_string();
|
||||
}
|
||||
let mut out = format!("{} code action(s):\n", actions.len());
|
||||
for action in actions {
|
||||
let title = action.get("title").and_then(Value::as_str).unwrap_or("(command)");
|
||||
let kind = action
|
||||
.get("kind")
|
||||
.and_then(Value::as_str)
|
||||
.map(|k| format!(" [{k}]"))
|
||||
.unwrap_or_default();
|
||||
out.push_str(&format!(" {title}{kind}\n"));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub(super) fn format_rename_preview(edit: &Value) -> String {
|
||||
// WorkspaceEdit: `changes` (uri → TextEdit[]) or `documentChanges` (array of {textDocument, edits}).
|
||||
let mut files: Vec<(String, usize)> = Vec::new();
|
||||
if let Some(changes) = edit.get("changes").and_then(Value::as_object) {
|
||||
for (uri, edits) in changes {
|
||||
files.push((uri.clone(), edits.as_array().map_or(0, Vec::len)));
|
||||
}
|
||||
}
|
||||
if let Some(document_changes) = edit.get("documentChanges").and_then(Value::as_array) {
|
||||
for change in document_changes {
|
||||
if let Some(uri) =
|
||||
change.get("textDocument").and_then(|d| d.get("uri")).and_then(Value::as_str)
|
||||
{
|
||||
let count = change.get("edits").and_then(Value::as_array).map_or(0, Vec::len);
|
||||
files.push((uri.to_string(), count));
|
||||
}
|
||||
}
|
||||
}
|
||||
if files.is_empty() {
|
||||
return "Rename produced no edits".to_string();
|
||||
}
|
||||
let total: usize = files.iter().map(|(_, n)| n).sum();
|
||||
files.sort();
|
||||
let mut out = format!("Rename preview — {total} edit(s) across {} file(s):\n", files.len());
|
||||
for (uri, count) in files {
|
||||
out.push_str(&format!(" {} ({count} edit(s))\n", crate::uri::short_display(&uri)));
|
||||
}
|
||||
out.push_str("(preview only — apply the change with the edit tool)");
|
||||
out
|
||||
}
|
||||
|
||||
pub(super) fn format_hierarchy_node(node: &Value) -> String {
|
||||
let name = node.get("name").and_then(Value::as_str).unwrap_or("?");
|
||||
let uri = node.get("uri").and_then(Value::as_str).unwrap_or("");
|
||||
let line = node
|
||||
.get("selectionRange")
|
||||
.or_else(|| node.get("range"))
|
||||
.and_then(|r| r.get("start"))
|
||||
.and_then(|s| s.get("line"))
|
||||
.and_then(Value::as_u64)
|
||||
.unwrap_or(0);
|
||||
format!("{name} — {}:{}", crate::uri::short_display(uri), line + 1)
|
||||
}
|
||||
|
||||
/// An empty answer, plus whatever the server said about why it cannot answer.
|
||||
///
|
||||
/// "No definition found" and "this server cannot resolve anything" are the same sentence, and the
|
||||
/// difference matters entirely: the first is information about the code, the second is a broken
|
||||
/// setup, and a server that cannot do its job usually says so. A `typescript-language-server` with
|
||||
/// no `typescript` package to drive answers every request with nothing; without its notices the
|
||||
/// reader is told their symbol has no definition.
|
||||
pub(super) fn nothing_found(answer: &str, client: &LspClient) -> String {
|
||||
let notices = client.server_notices();
|
||||
if notices.is_empty() {
|
||||
return answer.to_string();
|
||||
}
|
||||
let mut out = answer.to_string();
|
||||
for notice in notices {
|
||||
out.push_str(&format!("\n note: language server reported {notice}"));
|
||||
}
|
||||
out
|
||||
}
|
||||
128
crates/luxe-lsp/src/uri.rs
Normal file
128
crates/luxe-lsp/src/uri.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
//! uri — the `file://` URIs the language-server protocol speaks, and the paths behind them.
|
||||
//!
|
||||
//! Build and take apart these URIs here, never with `format!("file://{}", …)` or
|
||||
//! `strip_prefix("file://")`. That works for one shape of path only: an absolute unix path with no
|
||||
//! character a URI reserves. On Windows it produces `file://C:\src\main.rs`, whose authority is
|
||||
//! `C:` and whose path is nothing, which every server rejects or misresolves; and on every platform
|
||||
//! an unencoded space or `#` (`C:\Program Files`, `~/My Notes`) truncates the path.
|
||||
//!
|
||||
//! The correct spelling is `file:///C:/src/main.rs` with percent-encoding, and `url` knows all of
|
||||
//! it (drive letters, UNC shares, the encoding table), so this is a thin wrapper around it.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use url::Url;
|
||||
|
||||
/// The `file://` URI for an absolute path.
|
||||
///
|
||||
/// A relative path has no URI, and a server would reject one anyway; rather than refuse mid-request
|
||||
/// it degrades to the naive spelling with forward slashes, which is what every caller did before and
|
||||
/// is at least parseable by the [`to_path`] on the other side.
|
||||
pub(crate) fn from_path(path: &Path) -> String {
|
||||
match Url::from_file_path(path) {
|
||||
Ok(url) => url.into(),
|
||||
Err(()) => format!("file://{}", luxe_core::paths::display(path)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The path a `file://` URI names.
|
||||
///
|
||||
/// Lenient on the way back: servers are inconsistent about encoding (VS Code sends `file:///c%3A/`,
|
||||
/// others send `file:///C:/`), and some send a bare path where a URI was expected. Anything that
|
||||
/// does not parse as a URI is treated as a path already.
|
||||
pub(crate) fn to_path(uri: &str) -> PathBuf {
|
||||
if let Ok(url) = Url::parse(uri) {
|
||||
if url.scheme() == "file" {
|
||||
if let Ok(path) = url.to_file_path() {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
PathBuf::from(uri.strip_prefix("file://").unwrap_or(uri))
|
||||
}
|
||||
|
||||
/// The path a `file://` URI names, rendered for display with forward slashes.
|
||||
fn to_display_path(uri: &str) -> String {
|
||||
luxe_core::paths::display(&to_path(uri))
|
||||
}
|
||||
|
||||
/// The last two path segments of a URI — enough to recognize a file, short enough for a result row.
|
||||
pub(crate) fn short_display(uri: &str) -> String {
|
||||
let path = to_display_path(uri);
|
||||
let parts: Vec<&str> = path.rsplit('/').take(2).collect();
|
||||
match parts.as_slice() {
|
||||
[file, parent] => format!("{parent}/{file}"),
|
||||
_ => path,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A URI is not `"file://" + the path`. Three authorities' worth of difference: the empty one
|
||||
/// after `file://`, the encoding, and (on Windows) the leading slash before the drive.
|
||||
#[test]
|
||||
fn a_uri_round_trips_back_to_the_path_it_came_from() {
|
||||
let path = if cfg!(windows) {
|
||||
PathBuf::from(r"C:\projects\luxe\src\main.rs")
|
||||
} else {
|
||||
PathBuf::from("/projects/luxe/src/main.rs")
|
||||
};
|
||||
let uri = from_path(&path);
|
||||
assert!(uri.starts_with("file:///"), "three slashes, empty authority: {uri}");
|
||||
assert!(!uri.contains('\\'), "a URI has no backslashes: {uri}");
|
||||
assert_eq!(to_path(&uri), path);
|
||||
}
|
||||
|
||||
/// The bug that was live on every platform, not just Windows: a space is not a URI character,
|
||||
/// and an unencoded one truncates the path at the server.
|
||||
#[test]
|
||||
fn reserved_characters_are_encoded_and_decode_back() {
|
||||
let path = if cfg!(windows) {
|
||||
PathBuf::from(r"C:\Program Files\my app\a b#c.rs")
|
||||
} else {
|
||||
PathBuf::from("/opt/Program Files/my app/a b#c.rs")
|
||||
};
|
||||
let uri = from_path(&path);
|
||||
assert!(!uri.contains(' '), "spaces are encoded: {uri}");
|
||||
assert!(uri.contains("%20"), "{uri}");
|
||||
assert_eq!(to_path(&uri), path, "and survive the trip back");
|
||||
}
|
||||
|
||||
/// Servers disagree about how to spell a drive letter; both readings have to work.
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn either_spelling_of_a_drive_letter_is_understood() {
|
||||
let expected = PathBuf::from(r"C:\src\main.rs");
|
||||
assert_eq!(to_path("file:///C:/src/main.rs"), expected);
|
||||
assert_eq!(to_path("file:///c%3A/src/main.rs"), expected);
|
||||
}
|
||||
|
||||
/// Some servers answer with a bare path where the protocol says URI. Treat it as a path rather
|
||||
/// than losing the location.
|
||||
#[test]
|
||||
fn a_bare_path_is_accepted_where_a_uri_was_expected() {
|
||||
let bare = if cfg!(windows) { r"C:\src\main.rs" } else { "/src/main.rs" };
|
||||
assert_eq!(to_path(bare), PathBuf::from(bare));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_display_path_uses_forward_slashes_whatever_the_platform() {
|
||||
let path = if cfg!(windows) { r"C:\src\main.rs" } else { "/src/main.rs" };
|
||||
let rendered = to_display_path(&from_path(Path::new(path)));
|
||||
assert!(!rendered.contains('\\'), "{rendered}");
|
||||
assert!(rendered.ends_with("/src/main.rs"), "{rendered}");
|
||||
}
|
||||
|
||||
/// A result row shows the file and its directory — decoded, so a path with a space reads as one
|
||||
/// rather than as `%20`. The fixtures carry a drive letter on Windows because a URI without one
|
||||
/// names no file there, and `Url` rightly refuses to invent it.
|
||||
#[test]
|
||||
fn a_short_display_keeps_two_segments_and_decodes_them() {
|
||||
let root = if cfg!(windows) { "file:///C:" } else { "file://" };
|
||||
assert_eq!(short_display(&format!("{root}/a/b/c/main.rs")), "c/main.rs");
|
||||
assert_eq!(short_display(&format!("{root}/my%20app/main.rs")), "my app/main.rs");
|
||||
assert_eq!(short_display("main.rs"), "main.rs", "nothing to shorten");
|
||||
}
|
||||
}
|
||||
438
crates/luxe-lsp/src/workspace.rs
Normal file
438
crates/luxe-lsp/src/workspace.rs
Normal file
@@ -0,0 +1,438 @@
|
||||
//! Workspace LSP multiplexer — one server per (language, workspace root).
|
||||
//!
|
||||
//! Implements [`DiagnosticsProvider`] for the `file_changed` hook: on each changed file it
|
||||
//! detects the language, finds the workspace root, lazily starts (and caches) the right
|
||||
//! language server, and returns diagnostics. Anything unsupported or unavailable degrades
|
||||
//! to "no diagnostics" so editing is never blocked by a missing server.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use luxe_core::lsp::{Diagnostic, DiagnosticsError, DiagnosticsProvider, Severity};
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
use crate::client::{LspClient, LspConfig};
|
||||
use crate::registry::{ServerEntryPatch, ServerRegistry};
|
||||
|
||||
/// Base cooldown after a failed handshake; doubles per consecutive failure.
|
||||
const RETRY_BASE_COOLDOWN: Duration = Duration::from_secs(30);
|
||||
/// Longest a failing (language, root) is skipped before it gets another chance.
|
||||
const RETRY_MAX_COOLDOWN: Duration = Duration::from_secs(600);
|
||||
|
||||
/// Why a file in a fetched repository has no language server.
|
||||
const FETCHED_REPOSITORY: &str = "language servers do not start inside a fetched repository \
|
||||
(`.luxe/gh-cache/`), because starting one runs that repository's build scripts; read it with \
|
||||
read, grep and module_report's outline instead";
|
||||
|
||||
/// A failed handshake for one (language, root), and when it may be tried again.
|
||||
struct StartFailure {
|
||||
attempts: u32,
|
||||
retry_after: Instant,
|
||||
/// What went wrong, kept so the next caller is told *that* rather than "install it".
|
||||
///
|
||||
/// The server is usually installed. It is a shim with no version set, a runtime it cannot
|
||||
/// find, a config it will not accept — and being sent to install what is already there is the
|
||||
/// one instruction guaranteed not to help.
|
||||
reason: String,
|
||||
}
|
||||
|
||||
/// A language and the workspace root its server runs for.
|
||||
type Workspace = (String, PathBuf);
|
||||
|
||||
/// What is warm and what failed, which is all the service's lock guards.
|
||||
#[derive(Default)]
|
||||
struct Warm {
|
||||
/// (language, root) -> live client. Reused across edits.
|
||||
clients: HashMap<Workspace, Arc<Mutex<LspClient>>>,
|
||||
/// Failed handshakes, keyed like `clients`.
|
||||
///
|
||||
/// Keyed by *(language, root)* rather than language: a cold-start timeout on one huge or
|
||||
/// malformed root must not disable that language for every other root in the session. Each
|
||||
/// entry carries a backoff, so a recoverable failure (a slow first index, a server installed a
|
||||
/// minute later) is retried instead of cached forever.
|
||||
unavailable: HashMap<Workspace, StartFailure>,
|
||||
/// How many times the service has been shut down. A start that began before one must not add
|
||||
/// its server to the map afterwards: nothing would ever stop it.
|
||||
epoch: u64,
|
||||
}
|
||||
|
||||
/// What a lookup found for a workspace.
|
||||
enum Lookup {
|
||||
Warm(Arc<Mutex<LspClient>>),
|
||||
/// A start failed lately and its cooldown has not run out
|
||||
CoolingDown,
|
||||
Cold,
|
||||
}
|
||||
|
||||
/// A hold on one workspace's start gate, which takes the gate out of the map when the last holder
|
||||
/// lets go, so `starting` holds only starts in flight.
|
||||
struct GateLease<'a> {
|
||||
starting: &'a std::sync::Mutex<HashMap<Workspace, Arc<Mutex<()>>>>,
|
||||
key: Workspace,
|
||||
/// `None` only inside `drop`
|
||||
gate: Option<Arc<Mutex<()>>>,
|
||||
}
|
||||
|
||||
impl GateLease<'_> {
|
||||
fn gate(&self) -> &Mutex<()> {
|
||||
self.gate.as_ref().expect("a lease holds its gate until it drops")
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for GateLease<'_> {
|
||||
fn drop(&mut self) {
|
||||
let mut starting = self.starting.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
// let go before counting, so two leases dropped together cannot each count the other
|
||||
drop(self.gate.take());
|
||||
// only the map's handle left: nobody holds or waits on the gate, and nobody can take it
|
||||
// while the map is locked
|
||||
if starting.get(&self.key).is_some_and(|gate| Arc::strong_count(gate) == 1) {
|
||||
starting.remove(&self.key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Routes diagnostics for a changed file to a per-workspace language server.
|
||||
///
|
||||
/// Shared by every tool and the file-changed hook, so nothing here is held across slow work: the
|
||||
/// maps sit behind a lock taken for a lookup or an insert and never across an `.await`, each client
|
||||
/// has its own lock, and a start is serialized only against another start of the same workspace.
|
||||
/// A cold server taking twenty seconds does not hold up `/lsp`, another language, or an edit.
|
||||
pub struct WorkspaceLspClient {
|
||||
registry: ServerRegistry,
|
||||
warm: std::sync::Mutex<Warm>,
|
||||
/// One gate per workspace, held for the whole of its start, so two requests for a cold root
|
||||
/// start one server and the second takes what the first made; gone once nobody holds it
|
||||
starting: std::sync::Mutex<HashMap<Workspace, Arc<Mutex<()>>>>,
|
||||
/// Handshake timeout for a freshly started server.
|
||||
init_timeout: Duration,
|
||||
/// Whether a server the *project* supplies may be launched.
|
||||
///
|
||||
/// A decided answer, not a policy: who is watching, what the user configured, and
|
||||
/// `--trust-project` are weighed by the embedder, because they are its facts. Off by default,
|
||||
/// so a caller that says nothing gets the safe direction. What a server does once started is a
|
||||
/// separate matter and not this flag's to promise — see [`Self::client_for`].
|
||||
project_servers: bool,
|
||||
}
|
||||
|
||||
impl WorkspaceLspClient {
|
||||
/// Create a multiplexer over the default server registry (rust-analyzer, pyright, …).
|
||||
pub fn new() -> Self {
|
||||
Self::over(ServerRegistry::new())
|
||||
}
|
||||
|
||||
fn over(registry: ServerRegistry) -> Self {
|
||||
Self {
|
||||
registry,
|
||||
warm: Default::default(),
|
||||
starting: Default::default(),
|
||||
init_timeout: Duration::from_secs(20),
|
||||
project_servers: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Allow servers the project installed into `node_modules/.bin` to be launched.
|
||||
#[must_use]
|
||||
pub fn with_project_servers(mut self, allowed: bool) -> Self {
|
||||
self.project_servers = allowed;
|
||||
self
|
||||
}
|
||||
|
||||
/// Create a multiplexer whose registry is the built-ins plus user-configured servers (from
|
||||
/// `~/.config/luxe/lsp-servers.json`), so any LSP is plug-and-play by extension.
|
||||
pub fn with_servers(custom: HashMap<String, ServerEntryPatch>) -> Self {
|
||||
let mut registry = ServerRegistry::new();
|
||||
for rejected in registry.merge_patches(custom) {
|
||||
tracing::warn!(
|
||||
language = %rejected,
|
||||
"lsp-servers.json entry needs a `command`: Luxe ships no default for this language"
|
||||
);
|
||||
}
|
||||
Self::over(registry)
|
||||
}
|
||||
|
||||
/// An actionable message when no server handles a file: the install hint for a known-but-
|
||||
/// missing server, or a pointer to `lsp-servers.json` for an unconfigured extension.
|
||||
pub fn missing_server_hint(&self, path: &Path) -> String {
|
||||
if luxe_core::fetched::contains(path) {
|
||||
return FETCHED_REPOSITORY.to_string();
|
||||
}
|
||||
if let Some(reason) = self.start_failure(path) {
|
||||
return reason;
|
||||
}
|
||||
match self.registry.detect_language(path) {
|
||||
Some(lang) => match self.registry.install_hint(&lang) {
|
||||
Some(hint) => format!(
|
||||
"no {lang} language server running — install it (`{hint}`) or check it's on PATH"
|
||||
),
|
||||
None => format!("no {lang} language server available (check it's installed + on PATH)"),
|
||||
},
|
||||
None => {
|
||||
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("(none)");
|
||||
format!(
|
||||
"no language server configured for .{ext} files — add one to {}",
|
||||
luxe_core::user_dirs::config_path_display("lsp-servers.json")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why this file's server is not running, when it was tried this session and refused.
|
||||
///
|
||||
/// Looked up the same way `client_for_path` resolves a client, so the answer describes the
|
||||
/// server that would actually have served this file.
|
||||
fn start_failure(&self, path: &Path) -> Option<String> {
|
||||
let workspace = self.workspace_of(path)?;
|
||||
self.warm().unavailable.get(&workspace).map(|failure| failure.reason.clone())
|
||||
}
|
||||
|
||||
/// The `(language, workspace_root)` a file belongs to: its detected language, and the nearest
|
||||
/// directory holding one of that language's root markers, else the file's own directory.
|
||||
fn workspace_of(&self, path: &Path) -> Option<Workspace> {
|
||||
let language = self.registry.detect_language(path)?;
|
||||
let markers =
|
||||
self.registry.get(&language).map(|e| e.root_markers.clone()).unwrap_or_default();
|
||||
let root = self
|
||||
.registry
|
||||
.find_root(path, &markers)
|
||||
.or_else(|| path.parent().map(Path::to_path_buf))
|
||||
.unwrap_or_else(|| PathBuf::from("."));
|
||||
Some((language, root))
|
||||
}
|
||||
|
||||
/// The `(language, workspace_root)` of every warm language-server client, for status surfaces
|
||||
/// like `/lsp`. A server appears here once it has been lazily started for a file this session.
|
||||
pub fn running_servers(&self) -> Vec<(String, PathBuf)> {
|
||||
let mut out: Vec<Workspace> = self.warm().clients.keys().cloned().collect();
|
||||
out.sort();
|
||||
out
|
||||
}
|
||||
|
||||
/// Gracefully shut down every warm language-server client, then forget them.
|
||||
///
|
||||
/// Each server gets a bounded window to handle `shutdown` + `exit`; if it stalls (some servers
|
||||
/// answer `shutdown` with requests of their own before exiting), we move on and `kill_on_drop`
|
||||
/// reaps the child. Called when leaving a project so its servers don't linger into the next one.
|
||||
pub async fn shutdown_all(&self) {
|
||||
let clients = {
|
||||
let mut warm = self.warm();
|
||||
warm.unavailable.clear();
|
||||
warm.epoch += 1;
|
||||
std::mem::take(&mut warm.clients)
|
||||
};
|
||||
for ((language, _root), client) in clients {
|
||||
let bounded = tokio::time::timeout(Duration::from_secs(3), async {
|
||||
client.lock().await.shutdown().await
|
||||
})
|
||||
.await;
|
||||
if bounded.is_err() {
|
||||
warn!(%language, "language server slow to shut down; killed on drop");
|
||||
}
|
||||
// `client` drops here — kill_on_drop reaps the child if shutdown didn't finish.
|
||||
}
|
||||
}
|
||||
|
||||
/// Get (or lazily start) the warm client for a file, keyed by (detected language, workspace
|
||||
/// root). Returns `None` for unsupported languages, servers that failed to start this session,
|
||||
/// and files inside a fetched repository. The returned handle is reused across tool calls and
|
||||
/// edits.
|
||||
pub async fn client_for_path(&self, path: &Path) -> Option<Arc<Mutex<LspClient>>> {
|
||||
// The root would be the clone's own, and starting a server there runs its build scripts
|
||||
if luxe_core::fetched::contains(path) {
|
||||
return None;
|
||||
}
|
||||
let (language, root) = self.workspace_of(path)?;
|
||||
self.client_for(&language, root).await
|
||||
}
|
||||
|
||||
/// The maps, for a lookup or an insert. A panic elsewhere must not take the service down with it.
|
||||
fn warm(&self) -> std::sync::MutexGuard<'_, Warm> {
|
||||
self.warm.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
fn lookup(&self, key: &Workspace) -> Lookup {
|
||||
let warm = self.warm();
|
||||
if let Some(client) = warm.clients.get(key) {
|
||||
return Lookup::Warm(client.clone());
|
||||
}
|
||||
match warm.unavailable.get(key) {
|
||||
Some(failure) if Instant::now() < failure.retry_after => Lookup::CoolingDown,
|
||||
_ => Lookup::Cold,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get (or lazily start) the client for a language + root, caching the result.
|
||||
///
|
||||
/// A previous failure for this exact (language, root) is honored only until its cooldown
|
||||
/// expires; other roots of the same language are unaffected. A start holds only its own
|
||||
/// workspace's gate, so a second caller for the same root waits for it and shares the result.
|
||||
async fn client_for(&self, language: &str, root: PathBuf) -> Option<Arc<Mutex<LspClient>>> {
|
||||
let key = (language.to_string(), root.clone());
|
||||
let epoch = self.warm().epoch;
|
||||
match self.lookup(&key) {
|
||||
Lookup::Warm(client) => return Some(client),
|
||||
Lookup::CoolingDown => return None,
|
||||
Lookup::Cold => {}
|
||||
}
|
||||
let lease = self.gate(&key);
|
||||
let _starting = lease.gate().lock().await;
|
||||
// Whoever held the gate before us may have settled it either way
|
||||
match self.lookup(&key) {
|
||||
Lookup::Warm(client) => return Some(client),
|
||||
Lookup::CoolingDown => return None,
|
||||
Lookup::Cold => {}
|
||||
}
|
||||
if self.warm().unavailable.contains_key(&key) {
|
||||
debug!(language, root = %root.display(), "retrying a language server that failed earlier");
|
||||
}
|
||||
self.start(language, root, key, epoch).await
|
||||
}
|
||||
|
||||
/// This workspace's start gate, made if no start holds one.
|
||||
fn gate(&self, key: &Workspace) -> GateLease<'_> {
|
||||
let mut starting = self.starting.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let gate = Arc::clone(starting.entry(key.clone()).or_default());
|
||||
GateLease { starting: &self.starting, key: key.clone(), gate: Some(gate) }
|
||||
}
|
||||
|
||||
/// Launches `language`'s server for `root`, trying each way of launching it in order.
|
||||
async fn start(
|
||||
&self,
|
||||
language: &str,
|
||||
root: PathBuf,
|
||||
key: Workspace,
|
||||
epoch: u64,
|
||||
) -> Option<Arc<Mutex<LspClient>>> {
|
||||
let entry = self.registry.get(language)?.clone();
|
||||
// Try each way of launching this language, in order, falling through on a failed
|
||||
// *handshake* rather than only a missing binary: `tsc` is on PATH for both TypeScript 6
|
||||
// and 7 and only one of them answers, so resolution alone cannot choose. Cheap now that a
|
||||
// server which refuses reports it immediately instead of costing a 30-second timeout.
|
||||
let mut refusals: Vec<String> = Vec::new();
|
||||
let mut unpermitted: Vec<String> = Vec::new();
|
||||
for candidate in entry.candidates() {
|
||||
if self.warm().epoch != epoch {
|
||||
return None;
|
||||
}
|
||||
match self.try_candidate(language, &root, &candidate).await {
|
||||
Tried::Started(client) => {
|
||||
let handle = Arc::new(Mutex::new(*client));
|
||||
let mut warm = self.warm();
|
||||
if warm.epoch != epoch {
|
||||
// Shut down while it was starting: dropping it kills the process
|
||||
debug!(
|
||||
language,
|
||||
"a language server finished starting after shutdown; stopped"
|
||||
);
|
||||
return None;
|
||||
}
|
||||
warm.unavailable.remove(&key);
|
||||
warm.clients.insert(key, handle.clone());
|
||||
return Some(handle);
|
||||
}
|
||||
Tried::Refused(refusal) => refusals.push(refusal),
|
||||
Tried::NotPermitted(local) => unpermitted.push(local),
|
||||
Tried::Absent => {}
|
||||
}
|
||||
}
|
||||
|
||||
let reason = self.failure_reason(language, &refusals, unpermitted.first());
|
||||
let (attempts, cooldown) = {
|
||||
let mut warm = self.warm();
|
||||
// Nothing is recorded for a start the service was shut down under: it would be a
|
||||
// cooldown for a project that is gone. Checked in the lock that records, so a shutdown
|
||||
// cannot land between the two.
|
||||
if warm.epoch != epoch {
|
||||
return None;
|
||||
}
|
||||
let attempts =
|
||||
warm.unavailable.get(&key).map_or(1, |failure| failure.attempts.saturating_add(1));
|
||||
let cooldown = next_cooldown(attempts);
|
||||
let failure = StartFailure {
|
||||
attempts,
|
||||
retry_after: Instant::now() + cooldown,
|
||||
reason: reason.clone(),
|
||||
};
|
||||
warm.unavailable.insert(key, failure);
|
||||
(attempts, cooldown)
|
||||
};
|
||||
warn!(
|
||||
language,
|
||||
root = %root.display(),
|
||||
attempts,
|
||||
retry_in_secs = cooldown.as_secs(),
|
||||
%reason,
|
||||
"no language server could be started for this workspace root; retrying after a cooldown"
|
||||
);
|
||||
None
|
||||
}
|
||||
|
||||
/// Forget every recorded start failure so the next request retries immediately.
|
||||
///
|
||||
/// The manual escape hatch for an embedder that knows a server was just installed or fixed and
|
||||
/// shouldn't have to wait out its cooldown.
|
||||
pub fn clear_start_failures(&self) -> usize {
|
||||
let mut warm = self.warm();
|
||||
let cleared = warm.unavailable.len();
|
||||
warm.unavailable.clear();
|
||||
cleared
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for WorkspaceLspClient {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// The core severity for a protocol one; the numeric LSP mapping stops here.
|
||||
fn severity_of(severity: crate::protocol::DiagnosticSeverity) -> Severity {
|
||||
use crate::protocol::DiagnosticSeverity;
|
||||
match severity {
|
||||
DiagnosticSeverity::Error => Severity::Error,
|
||||
DiagnosticSeverity::Warning => Severity::Warning,
|
||||
DiagnosticSeverity::Information => Severity::Information,
|
||||
DiagnosticSeverity::Hint => Severity::Hint,
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl DiagnosticsProvider for WorkspaceLspClient {
|
||||
async fn run_diagnostics(&self, path: &Path) -> Result<Vec<Diagnostic>, DiagnosticsError> {
|
||||
let Some(client) = self.client_for_path(path).await else {
|
||||
return Ok(Vec::new()); // unsupported language or unavailable server
|
||||
};
|
||||
let uri = crate::uri::from_path(path);
|
||||
let content = std::fs::read_to_string(path).map_err(|source| DiagnosticsError::Read {
|
||||
path: luxe_core::paths::display(path),
|
||||
error: source,
|
||||
})?;
|
||||
|
||||
let mut guard = client.lock().await;
|
||||
match guard.get_diagnostics(&uri, &content).await {
|
||||
Ok(diags) => Ok(diags
|
||||
.into_iter()
|
||||
.map(|d| Diagnostic {
|
||||
severity: d.severity.map(severity_of),
|
||||
line: d.range.start.line,
|
||||
character: d.range.start.character,
|
||||
message: d.message,
|
||||
})
|
||||
.collect()),
|
||||
Err(e) => {
|
||||
debug!(uri, error = %e, "workspace lsp: diagnostics failed");
|
||||
Ok(Vec::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod start;
|
||||
|
||||
use start::{next_cooldown, Tried};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
116
crates/luxe-lsp/src/workspace/start.rs
Normal file
116
crates/luxe-lsp/src/workspace/start.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
//! Starting a server — each way of launching a language, tried in order, and why none worked.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// What launching one way of running a language's server came to.
|
||||
pub(super) enum Tried {
|
||||
Started(Box<LspClient>),
|
||||
/// It ran and did not complete the handshake, with the reason
|
||||
Refused(String),
|
||||
/// The repository ships one, and this run may not launch it: where it is
|
||||
NotPermitted(String),
|
||||
/// Nothing by that name resolved
|
||||
Absent,
|
||||
}
|
||||
|
||||
/// How long a (language, root) is skipped after its `attempts`th consecutive failure: the base,
|
||||
/// doubling each time, up to the cap.
|
||||
pub(super) fn next_cooldown(attempts: u32) -> Duration {
|
||||
RETRY_BASE_COOLDOWN
|
||||
.saturating_mul(1u32 << attempts.saturating_sub(1).min(5))
|
||||
.min(RETRY_MAX_COOLDOWN)
|
||||
}
|
||||
|
||||
impl WorkspaceLspClient {
|
||||
/// One way of launching `language`'s server, resolved, started and handshaken.
|
||||
pub(super) async fn try_candidate(
|
||||
&self,
|
||||
language: &str,
|
||||
root: &Path,
|
||||
candidate: &crate::registry::ServerCommand,
|
||||
) -> Tried {
|
||||
// Resolve the binary rather than trusting `PATH`: language servers arrive via
|
||||
// `cargo install`, `pipx`, `npm -g`, or an editor extension, and those bin directories
|
||||
// are routinely missing from the environment an app is launched with. Where project
|
||||
// servers are allowed the copy this repository installed for itself wins, so a project
|
||||
// can pin the version its own toolchain is built around.
|
||||
let Some(program) =
|
||||
luxe_core::program::resolve_in(root, &candidate.command, self.project_servers)
|
||||
else {
|
||||
// Present but not permitted is a different answer from absent, and the one a
|
||||
// reader can act on: "no server found" while the server sits in
|
||||
// `node_modules/.bin` is the least useful true statement available
|
||||
return match luxe_core::program::project_local(root, &candidate.command) {
|
||||
Some(local) => Tried::NotPermitted(local.to_string_lossy().into_owned()),
|
||||
None => Tried::Absent,
|
||||
};
|
||||
};
|
||||
// Worth an `info`: it is the one case where the program Luxe ran came from the tree
|
||||
// being edited rather than from the machine, and a reader should be able to find that
|
||||
// out from a normal run rather than by reasoning about it.
|
||||
if luxe_core::program::project_local(root, &candidate.command).as_deref()
|
||||
== Some(program.as_path())
|
||||
{
|
||||
tracing::info!(
|
||||
language,
|
||||
server = %luxe_core::paths::display(&program),
|
||||
"using the language server this project installed"
|
||||
);
|
||||
}
|
||||
let mut command = vec![program.to_string_lossy().into_owned()];
|
||||
command.extend(candidate.args.clone());
|
||||
let config = LspConfig {
|
||||
command,
|
||||
server_id: language.to_string(),
|
||||
root_path: root.to_path_buf(),
|
||||
init_timeout: self.init_timeout,
|
||||
};
|
||||
|
||||
let mut client = LspClient::new(config);
|
||||
match client.initialize().await {
|
||||
Ok(()) => {
|
||||
debug!(language, command = %candidate.command, "language server started");
|
||||
Tried::Started(Box::new(client))
|
||||
}
|
||||
Err(error) => {
|
||||
debug!(
|
||||
language,
|
||||
command = %candidate.command,
|
||||
%error,
|
||||
"language server candidate refused; trying the next"
|
||||
);
|
||||
Tried::Refused(format!("`{}`: {error}", candidate.command))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why no server could be started: what refused, what was found but not allowed, or what is
|
||||
/// simply not installed.
|
||||
pub(super) fn failure_reason(
|
||||
&self,
|
||||
language: &str,
|
||||
refusals: &[String],
|
||||
unpermitted: Option<&String>,
|
||||
) -> String {
|
||||
// Nothing resolved at all is a different situation from everything refusing, and the
|
||||
// install hint only helps with the first.
|
||||
match (refusals.is_empty(), unpermitted) {
|
||||
(false, _) => {
|
||||
format!("{language} language server did not start — {}", refusals.join("; "))
|
||||
}
|
||||
// The one case where the install hint would be actively wrong: it is installed.
|
||||
(true, Some(local)) => format!(
|
||||
"this project supplies a {language} language server at {local}, and this run does \
|
||||
not launch servers a repository brought with it — pass `--trust-project`, set \
|
||||
`lsp.project_servers` to `always`, or install the server globally"
|
||||
),
|
||||
(true, None) => format!(
|
||||
"no {language} language server found{}",
|
||||
self.registry
|
||||
.install_hint(language)
|
||||
.map(|hint| format!(" — install one (`{hint}`) or check it is on PATH"))
|
||||
.unwrap_or_default()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
603
crates/luxe-lsp/src/workspace/tests.rs
Normal file
603
crates/luxe-lsp/src/workspace/tests.rs
Normal file
@@ -0,0 +1,603 @@
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn unsupported_language_yields_no_diagnostics() {
|
||||
let client = WorkspaceLspClient::new();
|
||||
let diags = client.run_diagnostics(Path::new("notes.md")).await.unwrap();
|
||||
assert!(diags.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn shutdown_all_clears_state_and_is_a_noop_when_empty() {
|
||||
let client = WorkspaceLspClient::new();
|
||||
// No warm servers yet: shutdown_all must not panic and leaves nothing running.
|
||||
client.shutdown_all().await;
|
||||
assert!(client.running_servers().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn missing_server_degrades_gracefully() {
|
||||
// A .rs file with a bogus server binary: init fails fast, so we get no diagnostics
|
||||
// and never error, and the second call short-circuits via the unavailable set.
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let file = dir.join("x.rs");
|
||||
std::fs::write(&file, "fn main() {}\n").unwrap();
|
||||
let mut client = WorkspaceLspClient::new();
|
||||
// Force the language server command to a nonexistent binary so init fails fast.
|
||||
client.registry.register(
|
||||
"rust".to_string(),
|
||||
crate::registry::ServerEntry {
|
||||
command: "definitely-not-a-real-lsp-binary".to_string(),
|
||||
root_markers: vec!["Cargo.toml".to_string()],
|
||||
extensions: vec!["rs".to_string()],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let diags = client.run_diagnostics(&file).await.unwrap();
|
||||
assert!(diags.is_empty());
|
||||
// Second call short-circuits via the unavailable set.
|
||||
let diags = client.run_diagnostics(&file).await.unwrap();
|
||||
assert!(diags.is_empty());
|
||||
}
|
||||
|
||||
/// A fetched repository brings its own `Cargo.toml`, so a file in it would root a server there
|
||||
/// and run that repository's build scripts. Nothing is even attempted.
|
||||
#[tokio::test]
|
||||
async fn no_server_is_started_inside_a_fetched_repository() {
|
||||
let base_guard = tempfile::tempdir().expect("temp dir");
|
||||
let base = base_guard.path().to_path_buf();
|
||||
let clone = luxe_core::fetched::cache_dir(&base).join("o").join("r");
|
||||
std::fs::create_dir_all(&clone).unwrap();
|
||||
std::fs::write(clone.join("Cargo.toml"), "[package]\nname=\"x\"\n").unwrap();
|
||||
std::fs::write(clone.join("build.rs"), "fn main() {}\n").unwrap();
|
||||
|
||||
let mut client = WorkspaceLspClient::new();
|
||||
client.registry.register(
|
||||
"rust".to_string(),
|
||||
crate::registry::ServerEntry {
|
||||
command: "definitely-not-a-real-lsp-binary".to_string(),
|
||||
root_markers: vec!["Cargo.toml".to_string()],
|
||||
extensions: vec!["rs".to_string()],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let file = clone.join("build.rs");
|
||||
assert!(client.client_for_path(&file).await.is_none());
|
||||
assert!(client.warm().unavailable.is_empty(), "refused before any launch was tried");
|
||||
assert!(client.missing_server_hint(&file).contains("fetched repository"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_failed_root_does_not_disable_the_language_everywhere() {
|
||||
// One unusable root must not take the language down with it for the whole session.
|
||||
let base_guard = tempfile::tempdir().expect("temp dir");
|
||||
let base = base_guard.path().to_path_buf();
|
||||
let bad = base.join("bad");
|
||||
let good = base.join("good");
|
||||
for root in [&bad, &good] {
|
||||
std::fs::create_dir_all(root).unwrap();
|
||||
std::fs::write(root.join("Cargo.toml"), "[package]\nname=\"x\"\n").unwrap();
|
||||
std::fs::write(root.join("x.rs"), "fn main() {}\n").unwrap();
|
||||
}
|
||||
|
||||
let mut client = WorkspaceLspClient::new();
|
||||
client.registry.register(
|
||||
"rust".to_string(),
|
||||
crate::registry::ServerEntry {
|
||||
command: "definitely-not-a-real-lsp-binary".to_string(),
|
||||
root_markers: vec!["Cargo.toml".to_string()],
|
||||
extensions: vec!["rs".to_string()],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
// Fail on the first root.
|
||||
assert!(client.client_for_path(&bad.join("x.rs")).await.is_none());
|
||||
assert_eq!(client.warm().unavailable.len(), 1, "the failure is recorded for that root only");
|
||||
assert!(
|
||||
client.warm().unavailable.contains_key(&("rust".to_string(), bad.clone())),
|
||||
"keyed by (language, root)"
|
||||
);
|
||||
|
||||
// A different root of the same language is still attempted (it fails here too, because the
|
||||
// binary is bogus — the point is that it produced its *own* entry rather than being skipped).
|
||||
assert!(client.client_for_path(&good.join("x.rs")).await.is_none());
|
||||
assert_eq!(client.warm().unavailable.len(), 2, "each root tracks its own failure");
|
||||
|
||||
// A repeat on the failed root is skipped (cooldown) and backs off further.
|
||||
let first_attempts = client.warm().unavailable[&("rust".to_string(), bad.clone())].attempts;
|
||||
assert!(client.client_for_path(&bad.join("x.rs")).await.is_none());
|
||||
assert_eq!(
|
||||
client.warm().unavailable[&("rust".to_string(), bad.clone())].attempts,
|
||||
first_attempts,
|
||||
"a cooled-down root isn't retried yet, so its attempt count holds"
|
||||
);
|
||||
|
||||
// The escape hatch clears every cooldown.
|
||||
assert_eq!(client.clear_start_failures(), 2);
|
||||
assert!(client.warm().unavailable.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_expired_cooldown_lets_the_next_request_retry() {
|
||||
let dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
let dir = dir_guard.path().to_path_buf();
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::write(dir.join("Cargo.toml"), "[package]\nname=\"x\"\n").unwrap();
|
||||
let file = dir.join("x.rs");
|
||||
std::fs::write(&file, "fn main() {}\n").unwrap();
|
||||
|
||||
let mut client = WorkspaceLspClient::new();
|
||||
client.registry.register(
|
||||
"rust".to_string(),
|
||||
crate::registry::ServerEntry {
|
||||
command: "definitely-not-a-real-lsp-binary".to_string(),
|
||||
root_markers: vec!["Cargo.toml".to_string()],
|
||||
extensions: vec!["rs".to_string()],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
assert!(client.client_for_path(&file).await.is_none());
|
||||
let key = ("rust".to_string(), dir.clone());
|
||||
assert_eq!(client.warm().unavailable[&key].attempts, 1);
|
||||
|
||||
// Pretend the cooldown elapsed: the next request retries and backs off further.
|
||||
client.warm().unavailable.get_mut(&key).unwrap().retry_after = Instant::now();
|
||||
assert!(client.client_for_path(&file).await.is_none());
|
||||
assert_eq!(client.warm().unavailable[&key].attempts, 2, "a retry happened and backed off");
|
||||
}
|
||||
|
||||
/// A failed start's gate goes once the start is done, so the map does not grow with every root a
|
||||
/// session touches. A successful one is held to the same in the two-caller test below.
|
||||
#[tokio::test]
|
||||
async fn a_finished_start_leaves_no_gate_behind() {
|
||||
let base = tempfile::tempdir().expect("temp dir");
|
||||
let mut client = WorkspaceLspClient::new();
|
||||
client.registry.register(
|
||||
"rust".to_string(),
|
||||
crate::registry::ServerEntry {
|
||||
command: "definitely-not-a-real-lsp-binary".to_string(),
|
||||
root_markers: vec!["Cargo.toml".to_string()],
|
||||
extensions: vec!["rs".to_string()],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
for name in ["one", "two", "three"] {
|
||||
let root = base.path().join(name);
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
std::fs::write(root.join("Cargo.toml"), "[package]\nname=\"x\"\n").unwrap();
|
||||
std::fs::write(root.join("x.rs"), "fn main() {}\n").unwrap();
|
||||
assert!(client.client_for_path(&root.join("x.rs")).await.is_none());
|
||||
}
|
||||
assert_eq!(gates(&client), 0, "every start is over, so no gate is left");
|
||||
}
|
||||
|
||||
/// Callers that let go of one gate at the same moment still take it out of the map.
|
||||
///
|
||||
/// The break this catches: each checked the count while its own handle was still alive, so two
|
||||
/// letting go together each saw the other and both left the gate behind.
|
||||
#[test]
|
||||
fn a_gate_let_go_of_by_several_callers_at_once_is_removed() {
|
||||
let client = WorkspaceLspClient::new();
|
||||
let key = ("rust".to_string(), PathBuf::from("/project"));
|
||||
for _ in 0..2000 {
|
||||
let leases: Vec<_> = (0..8).map(|_| client.gate(&key)).collect();
|
||||
let together = std::sync::Barrier::new(leases.len());
|
||||
std::thread::scope(|scope| {
|
||||
for lease in leases {
|
||||
let together = &together;
|
||||
scope.spawn(move || {
|
||||
together.wait();
|
||||
drop(lease);
|
||||
});
|
||||
}
|
||||
});
|
||||
assert_eq!(gates(&client), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// How many workspaces have a start gate in the map.
|
||||
fn gates(client: &WorkspaceLspClient) -> usize {
|
||||
client.starting.lock().unwrap_or_else(std::sync::PoisonError::into_inner).len()
|
||||
}
|
||||
|
||||
/// A directory of this test's own, named for what it is, gone when it is dropped.
|
||||
fn project_dir(tag: &str) -> tempfile::TempDir {
|
||||
tempfile::Builder::new().prefix(&format!("luxe-lsp-{tag}-")).tempdir().expect("temp dir")
|
||||
}
|
||||
|
||||
/// The binary name the fixtures below install into a project.
|
||||
///
|
||||
/// Deliberately not a real server's: the host running these tests may well have `svelteserver`
|
||||
/// on `PATH`, and then the untrusted case would fall through to *that* and pass for the wrong
|
||||
/// reason. A name nothing can satisfy globally makes the project copy the only way to launch.
|
||||
const FIXTURE_SERVER: &str = "luxe-fixture-project-server";
|
||||
|
||||
/// A project that installed its own language server, as npm leaves one.
|
||||
///
|
||||
/// The binary is a shell script that exits immediately, so the handshake always fails — which
|
||||
/// is the point: what is under test is which path Luxe *chose to launch*, and the recorded
|
||||
/// failure names it.
|
||||
fn project_with_its_own_server(tag: &str) -> tempfile::TempDir {
|
||||
let dir = project_dir(tag);
|
||||
let root = dir.path().to_path_buf();
|
||||
let bin = root.join("node_modules").join(".bin");
|
||||
std::fs::create_dir_all(&bin).unwrap();
|
||||
std::fs::write(root.join("package.json"), "{\"name\":\"p\"}\n").unwrap();
|
||||
std::fs::write(root.join("App.svelte"), "<p>hi</p>\n").unwrap();
|
||||
// npm's Windows shim is a `.cmd`, and a bare name is not runnable there — the same shape
|
||||
// the real dev dependency has on each platform.
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let server = bin.join(FIXTURE_SERVER);
|
||||
std::fs::write(&server, "#!/bin/sh\nexit 0\n").unwrap();
|
||||
let mut perms = std::fs::metadata(&server).unwrap().permissions();
|
||||
perms.set_mode(0o755);
|
||||
std::fs::set_permissions(&server, perms).unwrap();
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
std::fs::write(bin.join(format!("{FIXTURE_SERVER}.cmd")), "@echo off\r\nexit /b 0\r\n")
|
||||
.unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// A client whose `svelte` entry names [`FIXTURE_SERVER`] and nothing else.
|
||||
fn client_for_the_fixture_server(project_servers: bool) -> WorkspaceLspClient {
|
||||
let mut client = WorkspaceLspClient::new().with_project_servers(project_servers);
|
||||
client.registry.register(
|
||||
"svelte".to_string(),
|
||||
crate::registry::ServerEntry {
|
||||
command: FIXTURE_SERVER.to_string(),
|
||||
root_markers: vec!["package.json".to_string()],
|
||||
extensions: vec!["svelte".to_string()],
|
||||
install_hint: Some("npm i -g svelte-language-server".to_string()),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
client
|
||||
}
|
||||
|
||||
/// Refused, a server the repository shipped is not launched — and the refusal says so.
|
||||
///
|
||||
/// The message is half the feature. "No svelte language server found" while one sits in
|
||||
/// `node_modules/.bin` is true, useless, and sends the reader off to install what they already
|
||||
/// have.
|
||||
#[tokio::test]
|
||||
async fn a_project_server_is_refused_by_name_when_it_is_not_allowed() {
|
||||
let root_dir = project_with_its_own_server("refused");
|
||||
let root = root_dir.path().to_path_buf();
|
||||
let client = client_for_the_fixture_server(false);
|
||||
|
||||
assert!(client.client_for_path(&root.join("App.svelte")).await.is_none());
|
||||
let reason = client.start_failure(&root.join("App.svelte")).expect("a recorded failure");
|
||||
assert!(
|
||||
reason.contains("node_modules") && reason.contains("--trust-project"),
|
||||
"the refusal has to name what it found and how to allow it: {reason}"
|
||||
);
|
||||
assert!(
|
||||
!reason.contains("npm i -g"),
|
||||
"the install hint is the one wrong answer here — it is installed: {reason}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Allowed, the same binary is the one launched.
|
||||
///
|
||||
/// It fails its handshake, which is what the fixture is for: the failure quotes the command,
|
||||
/// so it is evidence of *which* copy Luxe ran rather than of the server working.
|
||||
#[tokio::test]
|
||||
async fn an_allowed_project_server_is_the_one_launched() {
|
||||
let root_dir = project_with_its_own_server("allowed");
|
||||
let root = root_dir.path().to_path_buf();
|
||||
let client = client_for_the_fixture_server(true);
|
||||
|
||||
assert!(client.client_for_path(&root.join("App.svelte")).await.is_none());
|
||||
let reason = client.start_failure(&root.join("App.svelte")).expect("a recorded failure");
|
||||
assert!(
|
||||
reason.contains("did not start"),
|
||||
"allowed, it is launched and fails its handshake rather than being refused: {reason}"
|
||||
);
|
||||
assert!(!reason.contains("--trust-project"), "nothing left to ask permission for: {reason}");
|
||||
}
|
||||
|
||||
/// A directory with a `Cargo.toml` and one Rust file.
|
||||
#[cfg(unix)]
|
||||
fn rust_project(tag: &str) -> tempfile::TempDir {
|
||||
let dir = project_dir(tag);
|
||||
std::fs::write(dir.path().join("Cargo.toml"), "[package]\nname=\"x\"\n").unwrap();
|
||||
std::fs::write(dir.path().join("x.rs"), "fn main() {}\n").unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// A project with a `Cargo.toml` and one Rust file, and the language server stub for it: it logs
|
||||
/// each start to `starts`, and answers `initialize` after `delay_secs`.
|
||||
#[cfg(unix)]
|
||||
struct SlowProject {
|
||||
_dir: tempfile::TempDir,
|
||||
root: PathBuf,
|
||||
starts: PathBuf,
|
||||
patches: HashMap<String, ServerEntryPatch>,
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
impl SlowProject {
|
||||
fn new(tag: &str, delay_secs: f64) -> Option<Self> {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::process::Command::new("python3").arg("--version").output().ok()?;
|
||||
let root_dir = rust_project(tag);
|
||||
let root = root_dir.path().to_path_buf();
|
||||
let starts = root.join("starts.log");
|
||||
let server = root.join("slow-server.py");
|
||||
std::fs::write(
|
||||
&server,
|
||||
format!(
|
||||
r#"#!/usr/bin/env python3
|
||||
import json, os, sys, time
|
||||
open({starts:?}, "a").write("start\n")
|
||||
def read_msg():
|
||||
header = b""
|
||||
while b"\r\n\r\n" not in header:
|
||||
b = sys.stdin.buffer.read(1)
|
||||
if not b: return None
|
||||
header += b
|
||||
n = 0
|
||||
for line in header.split(b"\r\n"):
|
||||
if line.lower().startswith(b"content-length:"):
|
||||
n = int(line.split(b":")[1].strip())
|
||||
return json.loads(sys.stdin.buffer.read(n))
|
||||
def write_msg(obj):
|
||||
data = json.dumps(obj).encode()
|
||||
sys.stdout.buffer.write(b"Content-Length: %d\r\n\r\n" % len(data) + data)
|
||||
sys.stdout.buffer.flush()
|
||||
while True:
|
||||
msg = read_msg()
|
||||
if msg is None: break
|
||||
if msg.get("method") == "initialize":
|
||||
# how long this root's server takes is written beside it, so one script serves two roots
|
||||
time.sleep(float(open(os.path.join(os.getcwd(), "delay")).read()))
|
||||
write_msg({{"jsonrpc": "2.0", "id": msg["id"], "result": {{"capabilities": {{}}}}}})
|
||||
elif msg.get("method") == "shutdown":
|
||||
write_msg({{"jsonrpc": "2.0", "id": msg["id"], "result": None}})
|
||||
elif msg.get("method") == "exit":
|
||||
break
|
||||
"#,
|
||||
starts = starts.display().to_string(),
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(root.join("delay"), delay_secs.to_string()).unwrap();
|
||||
std::fs::set_permissions(&server, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let patch =
|
||||
ServerEntryPatch { command: Some(server.display().to_string()), ..Default::default() };
|
||||
Some(Self {
|
||||
_dir: root_dir,
|
||||
root,
|
||||
starts,
|
||||
patches: HashMap::from([("rust".to_string(), patch)]),
|
||||
})
|
||||
}
|
||||
|
||||
fn file(&self) -> PathBuf {
|
||||
self.root.join("x.rs")
|
||||
}
|
||||
|
||||
fn take_client(&self) -> WorkspaceLspClient {
|
||||
WorkspaceLspClient::with_servers(self.patches.clone())
|
||||
}
|
||||
|
||||
/// How many times the server has been started.
|
||||
fn started(&self) -> usize {
|
||||
std::fs::read_to_string(&self.starts).map_or(0, |log| log.lines().count())
|
||||
}
|
||||
|
||||
/// Waits for the server to have been launched at least once.
|
||||
async fn until_started(&self) {
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
while self.started() == 0 && Instant::now() < deadline {
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
assert!(self.started() > 0, "the server never started");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
|
||||
/// While one language server is slow to start, the rest of the service answers.
|
||||
///
|
||||
/// The break this catches: the service sat behind one lock held across the whole start, so `/lsp`,
|
||||
/// every other language's tools and the diagnostics hook after an edit all waited out the 20 seconds
|
||||
/// a cold server may take.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_slow_start_does_not_hold_up_a_status_read() {
|
||||
let Some(project) = SlowProject::new("slow", 2.0) else { return };
|
||||
let file = project.file();
|
||||
let service = Arc::new(project.take_client());
|
||||
let starting = tokio::spawn({
|
||||
let service = Arc::clone(&service);
|
||||
async move { service.client_for_path(&file).await.is_some() }
|
||||
});
|
||||
project.until_started().await;
|
||||
|
||||
let asked = Instant::now();
|
||||
let running = service.running_servers();
|
||||
assert!(running.is_empty(), "nothing is warm until the handshake ends");
|
||||
assert!(asked.elapsed() < Duration::from_millis(500), "waited {:?}", asked.elapsed());
|
||||
assert!(starting.await.unwrap(), "the slow server did come up");
|
||||
service.shutdown_all().await;
|
||||
}
|
||||
|
||||
/// Two requests for a cold workspace start one server between them, and share it.
|
||||
///
|
||||
/// The break this would be, without the per-workspace gate: with the service shared and no lock
|
||||
/// across the start, both callers find the root cold and each launches a server for it.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn two_requests_for_one_cold_root_start_one_server() {
|
||||
let Some(project) = SlowProject::new("shared", 0.5) else { return };
|
||||
let file = project.file();
|
||||
let service = Arc::new(project.take_client());
|
||||
|
||||
let (first, second) =
|
||||
tokio::join!(service.client_for_path(&file), service.client_for_path(&file));
|
||||
let (first, second) = (first.expect("a server"), second.expect("a server"));
|
||||
|
||||
assert!(Arc::ptr_eq(&first, &second), "the second took what the first started");
|
||||
assert_eq!(project.started(), 1, "one process was launched");
|
||||
assert_eq!(service.running_servers().len(), 1);
|
||||
assert_eq!(gates(&service), 0, "the gate goes with the last caller that waited on it");
|
||||
service.shutdown_all().await;
|
||||
}
|
||||
|
||||
/// A server that fails to start tells both of two concurrent callers so, and is tried once.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn two_requests_for_a_failing_root_try_it_once() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let root_dir = rust_project("failing");
|
||||
let root = root_dir.path().to_path_buf();
|
||||
// exits at once, so the handshake fails; each launch is logged
|
||||
let bad = root.join("fails.sh");
|
||||
std::fs::write(&bad, "#!/bin/sh\necho start >> \"$0.log\"\nexit 0\n").unwrap();
|
||||
std::fs::set_permissions(&bad, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let patch = ServerEntryPatch { command: Some(bad.display().to_string()), ..Default::default() };
|
||||
let service =
|
||||
Arc::new(WorkspaceLspClient::with_servers(HashMap::from([("rust".to_string(), patch)])));
|
||||
let file = root.join("x.rs");
|
||||
|
||||
let (first, second) =
|
||||
tokio::join!(service.client_for_path(&file), service.client_for_path(&file));
|
||||
assert!(first.is_none() && second.is_none());
|
||||
let log = std::fs::read_to_string(root.join("fails.sh.log")).unwrap_or_default();
|
||||
assert_eq!(
|
||||
log.lines().count(),
|
||||
1,
|
||||
"the second caller saw the failure rather than repeating it"
|
||||
);
|
||||
assert_eq!(gates(&service), 0);
|
||||
}
|
||||
|
||||
/// A slow start in one workspace does not hold up another's.
|
||||
///
|
||||
/// What the per-workspace gate is for: one gate for the whole service would pass every other test
|
||||
/// here, and bring back the stall for every language and root behind a cold one.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_slow_start_in_one_root_does_not_hold_up_another() {
|
||||
let Some(slow) = SlowProject::new("slow-root", 2.0) else { return };
|
||||
// another root of the same language, whose server answers at once
|
||||
let quick_dir = rust_project("quick-root");
|
||||
let quick = quick_dir.path().to_path_buf();
|
||||
std::fs::write(quick.join("delay"), "0").unwrap();
|
||||
let service = Arc::new(slow.take_client());
|
||||
let slow_file = slow.file();
|
||||
let starting = tokio::spawn({
|
||||
let service = Arc::clone(&service);
|
||||
async move { service.client_for_path(&slow_file).await.is_some() }
|
||||
});
|
||||
slow.until_started().await;
|
||||
|
||||
let asked = Instant::now();
|
||||
let there = service.client_for_path(&quick.join("x.rs")).await;
|
||||
assert!(there.is_some(), "the quick root came up");
|
||||
assert!(asked.elapsed() < Duration::from_secs(1), "waited {:?}", asked.elapsed());
|
||||
assert!(starting.await.unwrap());
|
||||
service.shutdown_all().await;
|
||||
}
|
||||
|
||||
/// Shutting the service down while a server is still starting stops that server too.
|
||||
///
|
||||
/// The break this catches: the start used to hold the lock the shutdown waited on, so it finished
|
||||
/// first and was shut down with the rest; once starts stopped holding it, the late server went into
|
||||
/// the emptied map and lived on into the next project.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_server_that_finishes_starting_after_shutdown_is_not_kept() {
|
||||
let Some(project) = SlowProject::new("late", 1.0) else { return };
|
||||
let file = project.file();
|
||||
let service = Arc::new(project.take_client());
|
||||
let starting = tokio::spawn({
|
||||
let service = Arc::clone(&service);
|
||||
async move { service.client_for_path(&file).await.is_some() }
|
||||
});
|
||||
project.until_started().await;
|
||||
|
||||
service.shutdown_all().await;
|
||||
assert!(!starting.await.unwrap(), "the late server is not handed out");
|
||||
assert!(service.running_servers().is_empty(), "and is not kept");
|
||||
}
|
||||
|
||||
/// A second caller waiting on a start that shutdown overtook does not launch a server of its own.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_caller_queued_behind_a_start_does_not_outlive_a_shutdown() {
|
||||
let Some(project) = SlowProject::new("queued", 1.0) else { return };
|
||||
let service = Arc::new(project.take_client());
|
||||
let callers: Vec<_> = (0..2)
|
||||
.map(|_| {
|
||||
let (service, file) = (Arc::clone(&service), project.file());
|
||||
tokio::spawn(async move { service.client_for_path(&file).await.is_some() })
|
||||
})
|
||||
.collect();
|
||||
project.until_started().await;
|
||||
|
||||
service.shutdown_all().await;
|
||||
for caller in callers {
|
||||
assert!(!caller.await.unwrap(), "neither is handed a server from before the shutdown");
|
||||
}
|
||||
assert!(service.running_servers().is_empty());
|
||||
assert_eq!(project.started(), 1, "the queued caller launched nothing");
|
||||
}
|
||||
|
||||
/// A start that fails after shutdown leaves no cooldown behind: the next request tries again.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_start_that_fails_after_shutdown_records_nothing() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let root_dir = rust_project("fails-late");
|
||||
let root = root_dir.path().to_path_buf();
|
||||
// outlasts the shutdown, then dies without answering
|
||||
let slow_failure = root.join("dies.sh");
|
||||
std::fs::write(&slow_failure, "#!/bin/sh\necho start >> \"$0.log\"\nsleep 1\nexit 0\n")
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&slow_failure, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let patch = ServerEntryPatch {
|
||||
command: Some(slow_failure.display().to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
let service =
|
||||
Arc::new(WorkspaceLspClient::with_servers(HashMap::from([("rust".to_string(), patch)])));
|
||||
let file = root.join("x.rs");
|
||||
let log =
|
||||
|| std::fs::read_to_string(root.join("dies.sh.log")).unwrap_or_default().lines().count();
|
||||
let starting = tokio::spawn({
|
||||
let (service, file) = (Arc::clone(&service), file.clone());
|
||||
async move { service.client_for_path(&file).await.is_some() }
|
||||
});
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
while log() == 0 && Instant::now() < deadline {
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
|
||||
service.shutdown_all().await;
|
||||
assert!(!starting.await.unwrap());
|
||||
assert!(service.warm().unavailable.is_empty(), "no cooldown for a project that was left");
|
||||
|
||||
assert!(service.client_for_path(&file).await.is_none());
|
||||
assert_eq!(log(), 2, "the next request launched the server again");
|
||||
}
|
||||
|
||||
/// The LSP's integers become the core's severities in the spec's order: a swap here would relabel
|
||||
/// every error as a warning.
|
||||
#[test]
|
||||
fn the_lsp_severity_numbers_map_to_the_core_severities_in_the_specs_order() {
|
||||
let decoded = |wire: &str| {
|
||||
severity_of(serde_json::from_str::<crate::protocol::DiagnosticSeverity>(wire).unwrap())
|
||||
};
|
||||
assert_eq!(decoded("1"), Severity::Error);
|
||||
assert_eq!(decoded("2"), Severity::Warning);
|
||||
assert_eq!(decoded("3"), Severity::Information);
|
||||
assert_eq!(decoded("4"), Severity::Hint);
|
||||
}
|
||||
Reference in New Issue
Block a user