pub struct ServerState {
pub documents: DashMap<Uri, DocumentState>,
pub cache: Arc<HttpCache>,
pub lockfile_cache: Arc<LockFileCache>,
pub ecosystem_registry: Arc<EcosystemRegistry>,
pub cold_start_limiter: ColdStartLimiter,
/* private fields */
}Expand description
Global LSP server state.
Manages all open documents, HTTP cache, lock file cache, and background
tasks for the server. This state is shared across all LSP handlers via
Arc and uses concurrent data structures (DashMap, RwLock) for
thread-safe access.
§Examples
use deps_lsp::document::ServerState;
use tower_lsp_server::ls_types::Uri;
let state = ServerState::new();
assert_eq!(state.document_count(), 0);Fields§
§documents: DashMap<Uri, DocumentState>Open documents by URI
cache: Arc<HttpCache>HTTP cache for registry requests
lockfile_cache: Arc<LockFileCache>Lock file cache for parsed lock files
ecosystem_registry: Arc<EcosystemRegistry>Ecosystem registry for trait-based architecture
cold_start_limiter: ColdStartLimiterCold start rate limiter
Implementations§
Source§impl ServerState
impl ServerState
Sourcepub fn get_document(&self, uri: &Uri) -> Option<Ref<'_, Uri, DocumentState>>
pub fn get_document(&self, uri: &Uri) -> Option<Ref<'_, Uri, DocumentState>>
Retrieves document state by URI.
Returns a read-only reference to the document state if it exists. The reference holds a lock on the internal map, so it should be dropped as soon as possible.
Sourcepub fn get_document_clone(&self, uri: &Uri) -> Option<DocumentState>
pub fn get_document_clone(&self, uri: &Uri) -> Option<DocumentState>
Retrieves a cloned copy of document state by URI.
This method clones the document state immediately and releases the DashMap lock, allowing concurrent access to the map while the document is being processed. Use this in hot paths where async operations are performed with the document data.
§Performance
Cloning DocumentState is relatively cheap as it only clones
String and HashMap metadata, not the underlying parse result
trait object.
§Examples
// Lock released immediately after clone
let doc = state.get_document_clone(uri);
if let Some(doc) = doc {
// Perform async operations without holding lock
let result = process_async(&doc).await;
}Sourcepub fn update_document(&self, uri: Uri, state: DocumentState)
pub fn update_document(&self, uri: Uri, state: DocumentState)
Updates or inserts document state.
If a document already exists at the given URI, it is replaced. Otherwise, a new entry is created.
Sourcepub fn remove_document(&self, uri: &Uri) -> Option<(Uri, DocumentState)>
pub fn remove_document(&self, uri: &Uri) -> Option<(Uri, DocumentState)>
Removes document state and returns the removed entry.
Returns None if no document exists at the given URI.
Sourcepub async fn spawn_background_task(&self, uri: Uri, task: JoinHandle<()>)
pub async fn spawn_background_task(&self, uri: Uri, task: JoinHandle<()>)
Spawns a background task for a document.
If a task already exists for the given URI, it is aborted before the new task is registered. This ensures only one background task runs per document.
Typical use case: fetching version data asynchronously after document open or change.
Sourcepub async fn cancel_background_task(&self, uri: &Uri)
pub async fn cancel_background_task(&self, uri: &Uri)
Cancels the background task for a document.
If no task exists, this is a no-op.
Sourcepub fn document_count(&self) -> usize
pub fn document_count(&self) -> usize
Returns the number of open documents.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for ServerState
impl !RefUnwindSafe for ServerState
impl Send for ServerState
impl Sync for ServerState
impl Unpin for ServerState
impl !UnwindSafe for ServerState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more