sync_lsp/text_document/
publish_diagnostics.rs

1//! impl of the `textDocument/publishDiagnostics` notification
2//! 
3//! # Usage
4//! A server can publish diagnostics for a specific document via [`Connection::publish_diagnostics`]
5//! in any callback. The client will then display these diagnostics in the editor.
6
7use serde::{Serialize, Deserialize};
8use serde_repr::{Serialize_repr, Deserialize_repr};
9
10use crate::{Connection, TypeProvider};
11use crate::connection::RpcConnection;
12
13use super::{DocumentUri, Range};
14
15#[derive(Default, Clone)]
16pub(super) struct PublishDiagnostics;
17
18/// The diagnostic information.
19#[derive(Deserialize, Serialize, Debug)]
20pub struct Diagnostic {
21    /// A range in the document that contains the diagnostic message.
22    pub range: Range,
23    /// The severity of the diagnostic.
24    pub severity: Option<DiagnosticSeverity>,
25    /// A optional code to identify the diagnostic.
26    pub code: Option<String>,
27    /// A string describing the source of this diagnostic.
28    pub source: Option<String>,
29    /// A human-readable string describing the diagnostic.
30    pub message: String,
31}
32
33/// The diagnostic severity.
34#[repr(i32)]
35#[derive(Deserialize_repr, Serialize_repr, Debug)]
36pub enum DiagnosticSeverity {
37    Error = 1,
38    Warning = 2,
39    Information = 3,
40    Hint = 4
41}
42
43#[derive(Serialize)]
44struct PublishDiagnosticsParams {
45    uri: DocumentUri,
46    diagnostics: Vec<Diagnostic>
47}
48
49impl PublishDiagnostics {
50    const METHOD: &'static str = "textDocument/publishDiagnostics";
51}
52
53impl<T: TypeProvider> Connection<T> {
54
55    /// [Publishes diagnostics](self) for a specific document.
56    /// 
57    /// # Arguments
58    /// * `uri` - The [`DocumentUri`] of the document to publish diagnostics for.
59    /// * `diagnostics` - A list of diagnostics to publish.
60
61    pub fn publish_diagnostics(&mut self, uri: DocumentUri, diagnostics: Vec<Diagnostic>) {
62        self.notify(
63            PublishDiagnostics::METHOD,
64            PublishDiagnosticsParams {
65                uri,
66                diagnostics
67            }
68        );
69    }
70}