Skip to main content

NotificationCache

Struct NotificationCache 

Source
pub struct NotificationCache { /* private fields */ }
Expand description

Cache for LSP server notifications.

Implementations§

Source§

impl NotificationCache

Source

pub fn new() -> Self

Create a new notification cache.

Source

pub fn set_diagnostics_route_count(&mut self, count: usize)

Configure how many diagnostics-route servers share the global MAX_DIAGNOSTIC_ENTRIES budget.

Each server’s fair share becomes MAX_DIAGNOSTIC_ENTRIES / count (minimum 1). This does not cap any server’s entries by itself – the aggregate cache is only ever trimmed once it reaches MAX_DIAGNOSTIC_ENTRIES total – it only decides, at that point, which server’s oldest entry is the one that gets evicted. Call once after server registration completes and before diagnostics start flowing. Defaults to 1 if never called (a single implicit server owns the whole budget).

Source

pub fn store_diagnostics( &mut self, server_id: &ServerId, uri: &Uri, version: Option<i32>, diagnostics: Vec<LspDiagnostic>, )

Store diagnostics for a document published by server_id.

Each diagnostic’s message is truncated to MAX_ENTRY_TEXT_BYTES, and the whole list is bounded to MAX_DIAGNOSTICS_ENTRY_BYTES serialized bytes, before storing (#311). When that bound requires dropping diagnostics, the survivors come back sorted by severity (diagnostic_severity_rank: ERROR first), not in the original publish/file-position order – see Self::get_diagnostics.

If diagnostics already exist for the URI, they are replaced and the entry is repositioned to the back of its owner’s eviction order, so a URI republished on every edit is tracked as most-recently-written and evicted last, not first – and, since it is not a new distinct URI, never triggers eviction on its own.

Eviction is work-conserving (#276): storing diagnostics for a genuinely new URI only evicts an existing entry once the aggregate across every server reaches MAX_DIAGNOSTIC_ENTRIES, and then only the least-recently-written entry of whichever server most exceeds its fair share, or – per the fallbacks documented on server_to_evict_from – the writer’s own oldest entry when no server exceeds its share. A quieter, non-writer server that is within its fair share is never touched, outside the narrow edge case also documented there. This lets a single active server use the full aggregate budget while other registered servers are idle, instead of being capped at a static equal split regardless of how much of it they actually use.

§Examples
use mcpls_core::bridge::NotificationCache;
use mcpls_core::config::ServerId;
use lsp_types::Uri;

let mut cache = NotificationCache::new();
let server: ServerId = "rust-analyzer".into();
let uri: Uri = "file:///main.rs".parse().unwrap();
cache.store_diagnostics(&server, &uri, Some(1), vec![]);
assert!(cache.get_diagnostics(uri.as_str()).is_some());
Source

pub fn store_log(&mut self, level: LogLevel, message: String)

Store a log entry.

Maintains a maximum of MAX_LOG_ENTRIES entries, removing oldest when full. message is truncated to MAX_ENTRY_TEXT_BYTES before storing.

Source

pub fn store_message(&mut self, message_type: MessageType, message: String)

Store a server message.

Maintains a maximum of MAX_SERVER_MESSAGES entries, removing oldest when full. message is truncated to MAX_ENTRY_TEXT_BYTES before storing.

Source

pub fn get_diagnostics(&self, uri: &str) -> Option<&DiagnosticInfo>

Get diagnostics for a document URI.

If the stored list was ever truncated by store_diagnostics’s MAX_DIAGNOSTICS_ENTRY_BYTES cap (#311), the diagnostics here are in severity order (ERROR first), not the original publish/file-position order – callers that assume file-position order should not rely on it after a cap-triggered truncation.

Source

pub fn diagnostics_owner(&self, uri: &str) -> Option<&ServerId>

Server that published the currently cached diagnostics for uri, if any. Used to look up that server’s negotiated position encoding for a cache-only read that has no live LSP round trip of its own to resolve one from.

Source

pub const fn logs(&self) -> &VecDeque<LogEntry>

All stored log entries.

Source

pub const fn messages(&self) -> &VecDeque<ServerMessage>

All stored server messages.

Source

pub fn clear_diagnostics(&mut self, uri: &str) -> Option<DiagnosticInfo>

Clear diagnostics for a specific document URI.

Returns the cleared diagnostics if they existed.

Source

pub fn clear_server_diagnostics(&mut self, server_id: &ServerId)

Clear all diagnostics owned by a single server.

Used when a server crashes and respawns: its own stale entries must be invalidated without disturbing any other server’s cache entries (#266), unlike Self::clear_all_diagnostics.

§Examples
use mcpls_core::bridge::NotificationCache;
use mcpls_core::config::ServerId;
use lsp_types::Uri;

let mut cache = NotificationCache::new();
let crashed: ServerId = "pyright".into();
let healthy: ServerId = "rust-analyzer".into();
let crashed_uri: Uri = "file:///main.py".parse().unwrap();
let healthy_uri: Uri = "file:///main.rs".parse().unwrap();
cache.store_diagnostics(&crashed, &crashed_uri, Some(1), vec![]);
cache.store_diagnostics(&healthy, &healthy_uri, Some(1), vec![]);

cache.clear_server_diagnostics(&crashed);

assert!(cache.get_diagnostics(crashed_uri.as_str()).is_none());
assert!(cache.get_diagnostics(healthy_uri.as_str()).is_some());
Source

pub fn clear_all_diagnostics(&mut self)

Clear all diagnostics, for every server.

Source

pub fn clear_logs(&mut self)

Clear all logs.

Source

pub fn clear_messages(&mut self)

Clear all messages.

Source

pub fn diagnostics_count(&self) -> usize

Get the number of documents with stored diagnostics.

Source

pub fn logs_count(&self) -> usize

Get the number of stored log entries.

Source

pub fn messages_count(&self) -> usize

Get the number of stored server messages.

Trait Implementations§

Source§

impl Debug for NotificationCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NotificationCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more