Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rust/crates/lsp-engine/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub(crate) fn apply_indexing_to_sessions(
let order = session.lock_outbound_order()?;
if session.lock_state()?.client.initialized {
if let Some(configuration) = session.plugin_configuration() {
session.lock_state()?.hover_cache.clear();
session.send_messages_or_fail(
&order,
vec![plugin_configuration_notification(&configuration)],
Expand Down Expand Up @@ -171,7 +172,7 @@ impl RuntimeSession {
let configuration = Arc::new(configuration);
let outbound_order = self.lock_outbound_order()?;
let initialized = {
let state = self.lock_state()?;
let mut state = self.lock_state()?;
if matches!(
state.lifecycle,
LspLifecycleState::Stopping
Expand All @@ -183,6 +184,7 @@ impl RuntimeSession {
"The language session is no longer running.",
));
}
state.hover_cache.clear();
// Under `outbound_order`, an uninitialized client sends the stored
// configuration with its `initialized` notification instead.
state.client.initialized
Expand Down
6 changes: 5 additions & 1 deletion rust/crates/lsp-engine/src/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl RuntimeSession {
pub(crate) fn save_document(&self, request: SaveDocumentRequest) -> Result<bool, CoreError> {
let outbound_order = self.lock_outbound_order()?;
let message = {
let state = self.lock_state()?;
let mut state = self.lock_state()?;
ensure_not_terminal(state.lifecycle)?;
if state.lifecycle != LspLifecycleState::Ready {
return Ok(false);
Expand All @@ -24,6 +24,7 @@ impl RuntimeSession {
if *document.text != request.text {
return Ok(false);
}
state.hover_cache.clear();
let mut params = json!({"textDocument": {"uri": request.uri}});
if include_text {
params["text"] = json!(request.text);
Expand Down Expand Up @@ -66,6 +67,7 @@ impl RuntimeSession {
let message = {
let mut state = self.lock_state()?;
ensure_not_terminal(state.lifecycle)?;
state.hover_cache.clear();
if !state.client.initialized {
state.pending_workspace_file_changes.extend(normalized);
return Ok(());
Expand Down Expand Up @@ -114,6 +116,7 @@ impl RuntimeSession {
});
}
}
state.hover_cache.clear();
if state.client.initialized {
let response = if state
.client
Expand Down Expand Up @@ -197,6 +200,7 @@ impl RuntimeSession {
"Cannot close a document that is not owned by the language-server session.",
));
};
state.hover_cache.clear();
cleared = state.client.diagnostics.contains_key(uri);
if document.version == 0 {
state.client.open_documents.remove(uri);
Expand Down
2 changes: 2 additions & 0 deletions rust/crates/lsp-engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ impl LspEngine {
method: "initialize".to_string(),
document_uri: None,
document_version: None,
hover: None,
created_at: now,
deadline: now + initialize_timeout,
},
Expand All @@ -236,6 +237,7 @@ impl LspEngine {
rejected_plugin_diagnostics: false,
plugin_diagnostics_window: Instant::now(),
plugin_diagnostics_count: 0,
hover_cache: HoverCache::default(),
}),
event_signal: Condvar::new(),
process: process.handle,
Expand Down
1 change: 1 addition & 0 deletions rust/crates/lsp-engine/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ pub(crate) fn clear_runtime_state(session: &RuntimeSession, state: &mut SessionS
state.maintenance_write_deadline = None;
state.relay_write_deadline = None;
state.pending_workspace_file_changes.clear();
state.hover_cache.clear();
state.initialize_deadline = None;
state.shutdown_deadline = None;
push_features_event(session, state, Vec::new());
Expand Down
242 changes: 242 additions & 0 deletions rust/crates/lsp-engine/src/hover_cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
//! Hover answers a session already received, so resting on a symbol again answers at
//! once instead of asking the language server a second time.
//!
//! An answer is reused only for the same document version and a position the answer
//! covers. Anything that can change what the server knows clears every answer, not
//! only those of one document: an edit in one file changes the hover shown for its
//! symbols in every other file.

use super::*;

/// Most answers one session keeps; the least recently used one goes first.
pub(crate) const HOVER_CACHE_CAPACITY: usize = 32;

/// Longest hover text, in bytes, worth keeping. A longer answer is still delivered
/// but asked for again next time, which bounds a session's cache to
/// `HOVER_CACHE_CAPACITY * HOVER_CACHE_MAX_CONTENT_BYTES` of hover text.
pub(crate) const HOVER_CACHE_MAX_CONTENT_BYTES: usize = 64 * 1024;

/// Zero-based LSP line and UTF-16 column, ordered line first.
type Point = (i64, i64);

/// What a hover request in flight needs to keep its answer.
#[derive(Debug, Clone)]
pub(crate) struct HoverRequest {
/// Document version the request was sent for; the answer describes that text.
pub(crate) version: i64,
pub(crate) position: Point,
/// Cache generation when the request was sent. The answer is kept only if
/// nothing cleared the cache before it arrived, because the server may have
/// computed it from what it knew before the change.
pub(crate) generation: u64,
}

/// One kept answer.
struct HoverEntry {
uri: String,
version: i64,
position: Point,
/// Same-line half-open column span `(line, start, end)` the server gave as the
/// answer's range, when that range contains `position`. Any position inside it
/// shows this answer, the way editors keep a hover open across its range.
span: Option<(i64, i64, i64)>,
/// The `requestCompleted` result exactly as the host received it.
result: Value,
}

impl HoverEntry {
fn covers(&self, uri: &str, version: i64, position: Point) -> bool {
if self.uri != uri || self.version != version {
return false;
}
position == self.position
|| self.span.is_some_and(|(line, start, end)| {
position.0 == line && start <= position.1 && position.1 < end
})
}
}

/// Hover answers of one session, owned by its `SessionState`.
#[derive(Default)]
pub(crate) struct HoverCache {
/// Advances on every clear; see `HoverRequest::generation`.
generation: u64,
/// Least recently used first.
entries: VecDeque<HoverEntry>,
}

impl HoverCache {
/// Forgets every answer. Called whenever the server may know something it did
/// not know when it answered: a document opened, changed, saved or closed, files
/// changed on disk, settings changed, a command ran, a plugin backend was
/// notified or sent the server its own request, or the server asked the client
/// for something or finished a piece of work.
pub(crate) fn clear(&mut self) {
self.entries.clear();
self.generation = self.generation.wrapping_add(1);
}

/// The answer kept for `position` in this version of the document, marked as
/// the most recently used.
pub(crate) fn lookup(&mut self, uri: &str, version: i64, position: Point) -> Option<Value> {
let index = self
.entries
.iter()
.rposition(|entry| entry.covers(uri, version, position))?;
let entry = self.entries.remove(index)?;
let result = entry.result.clone();
self.entries.push_back(entry);
Some(result)
}

/// Describes a hover request about to be sent for `position`.
pub(crate) fn request(&self, version: i64, position: Point) -> HoverRequest {
HoverRequest {
version,
position,
generation: self.generation,
}
}

/// Keeps the answer to `request`. An empty or oversized answer, or one the
/// server computed before the cache was last cleared, is not kept: an empty
/// answer can turn into a real one once the server has loaded the project.
pub(crate) fn store(&mut self, uri: &str, request: &HoverRequest, result: &Value) {
if request.generation != self.generation {
return;
}
let Some(hover) = result.get("hover").filter(|hover| hover.is_object()) else {
return;
};
let Some(contents) = hover.get("contents").and_then(Value::as_str) else {
return;
};
if contents.trim().is_empty() || contents.len() > HOVER_CACHE_MAX_CONTENT_BYTES {
return;
}
let span = same_line_span(hover.get("range")).filter(|(line, start, end)| {
request.position.0 == *line && *start <= request.position.1 && request.position.1 < *end
});
self.entries.retain(|entry| {
!(entry.uri == uri
&& entry.version == request.version
&& entry.position == request.position)
});
self.entries.push_back(HoverEntry {
uri: uri.to_string(),
version: request.version,
position: request.position,
span,
result: result.clone(),
});
while self.entries.len() > HOVER_CACHE_CAPACITY {
self.entries.pop_front();
}
}
}

/// A hover range from `lithe_lsp_client` (`{start, end}` of `{line, utf16Column}`)
/// as `(line, start, end)`, when it is a non-empty span on one line. A range over
/// several lines is not used to answer other positions: it can enclose other
/// symbols with hovers of their own.
fn same_line_span(range: Option<&Value>) -> Option<(i64, i64, i64)> {
let range = range?;
let point = |key: &str| -> Option<Point> {
let position = range.get(key)?;
Some((
position.get("line")?.as_i64()?,
position.get("utf16Column")?.as_i64()?,
))
};
let (start, end) = (point("start")?, point("end")?);
(start.0 == end.0 && start.1 < end.1).then_some((start.0, start.1, end.1))
}

/// Whether a message the server sent may mean it knows something new: any request
/// it makes of the client (refreshes, edits, capability changes, configuration
/// pulls, progress tokens) and the end of a piece of work such as loading the
/// workspace.
pub(crate) fn server_message_clears_hovers(value: &Value) -> bool {
let Some(method) = value.get("method").and_then(Value::as_str) else {
return false;
};
value.get("id").is_some()
|| (method == "$/progress"
&& value.pointer("/params/value/kind").and_then(Value::as_str) == Some("end"))
}

#[cfg(test)]
mod tests {
use super::*;

fn answer(contents: &str) -> Value {
json!({ "hover": { "contents": contents, "isMarkdown": false, "range": null } })
}

fn keep(cache: &mut HoverCache, line: i64) {
let request = cache.request(1, (line, 0));
cache.store("file:///a.go", &request, &answer(&format!("line {line}")));
}

#[test]
fn the_least_recently_used_answer_goes_first_at_capacity() {
let mut cache = HoverCache::default();
for line in 0..HOVER_CACHE_CAPACITY as i64 {
keep(&mut cache, line);
}
// Reading line 0 makes line 1 the oldest.
assert!(cache.lookup("file:///a.go", 1, (0, 0)).is_some());
keep(&mut cache, HOVER_CACHE_CAPACITY as i64);
assert_eq!(cache.entries.len(), HOVER_CACHE_CAPACITY);
assert!(cache.lookup("file:///a.go", 1, (1, 0)).is_none());
assert!(cache.lookup("file:///a.go", 1, (0, 0)).is_some());
assert!(cache
.lookup("file:///a.go", 1, (HOVER_CACHE_CAPACITY as i64, 0))
.is_some());
}

#[test]
fn an_answer_is_kept_once_per_position_and_only_for_its_document_version() {
let mut cache = HoverCache::default();
keep(&mut cache, 3);
keep(&mut cache, 3);
assert_eq!(cache.entries.len(), 1);
assert!(cache.lookup("file:///a.go", 2, (3, 0)).is_none());
assert!(cache.lookup("file:///b.go", 1, (3, 0)).is_none());
assert_eq!(
cache.lookup("file:///a.go", 1, (3, 0)),
Some(answer("line 3"))
);
}

#[test]
fn a_clear_forgets_answers_and_refuses_ones_requested_before_it() {
let mut cache = HoverCache::default();
keep(&mut cache, 0);
let in_flight = cache.request(1, (5, 0));
cache.clear();
assert!(cache.lookup("file:///a.go", 1, (0, 0)).is_none());
cache.store("file:///a.go", &in_flight, &answer("stale"));
assert!(cache.entries.is_empty());
}

#[test]
fn only_server_requests_and_finished_work_clear_answers() {
let clears = |message: Value| server_message_clears_hovers(&message);
assert!(clears(
json!({ "id": 1, "method": "workspace/codeLens/refresh" })
));
assert!(clears(json!({
"method": "$/progress",
"params": { "token": "t", "value": { "kind": "end" } }
})));
assert!(!clears(json!({
"method": "$/progress",
"params": { "token": "t", "value": { "kind": "report" } }
})));
assert!(!clears(
json!({ "method": "textDocument/publishDiagnostics", "params": {} })
));
assert!(!clears(json!({ "id": 1, "result": null })));
}
}
2 changes: 2 additions & 0 deletions rust/crates/lsp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mod engine;
mod events;
mod function_calls;
mod host_requests;
mod hover_cache;
mod lifecycle;
mod operations;
mod plugin_server_calls;
Expand All @@ -93,6 +94,7 @@ use engine::*;
use events::*;
use function_calls::*;
use host_requests::*;
use hover_cache::*;
use lifecycle::*;
use operations::*;
use plugin_server_calls::*;
Expand Down
1 change: 1 addition & 0 deletions rust/crates/lsp-engine/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl RuntimeSession {
method: "shutdown".to_string(),
document_uri: None,
document_version: None,
hover: None,
created_at: now,
deadline: now + shutdown_timeout,
},
Expand Down
Loading
Loading