Debian Codegraph @

helix-core/src/diagnostic.rs

raw symbols SCIP
1 //! LSP diagnostic utility types.
2 use std::{fmt, sync::Arc};
3
4 pub use helix_stdx::range::Range;
5 use serde::{Deserialize, Serialize};
6
7 /// Describes the severity level of a [`Diagnostic`].
8 #[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
9 #[serde(rename_all = "lowercase")]
10 #[derive(Default)]
11 pub enum Severity {
12 #[default]
13 Hint,
14 Info,
15 Warning,
16 Error,
17 }
18
19 #[derive(Debug, Eq, Hash, PartialEq, Clone, Deserialize, Serialize)]
20 pub enum NumberOrString {
21 Number(i32),
22 String(String),
23 }
24
25 #[derive(Debug, Clone)]
26 pub enum DiagnosticTag {
27 Unnecessary,
28 Deprecated,
29 }
30
31 /// Corresponds to [`lsp_types::Diagnostic`](https://docs.rs/lsp-types/0.94.0/lsp_types/struct.Diagnostic.html)
32 #[derive(Debug, Clone)]
33 pub struct Diagnostic {
34 pub range: Range,
35 // whether this diagnostic ends at the end of(or inside) a word
36 pub ends_at_word: bool,
37 pub starts_at_word: bool,
38 pub zero_width: bool,
39 pub line: usize,
40 pub message: String,
41 pub severity: Option<Severity>,
42 pub code: Option<NumberOrString>,
43 pub provider: DiagnosticProvider,
44 pub tags: Vec<DiagnosticTag>,
45 pub source: Option<String>,
46 pub data: Option<serde_json::Value>,
47 }
48
49 /// The source of a diagnostic.
50 ///
51 /// This type is cheap to clone: all data is either `Copy` or wrapped in an `Arc`.
52 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
53 pub enum DiagnosticProvider {
54 Lsp {
55 /// The ID of the language server which sent the diagnostic.
56 server_id: LanguageServerId,
57 /// An optional identifier under which diagnostics are managed by the client.
58 ///
59 /// `identifier` is a field from the LSP "Pull Diagnostics" feature meant to provide an
60 /// optional "namespace" for diagnostics: a language server can respond to a diagnostics
61 /// pull request with an identifier and these diagnostics should be treated as separate
62 /// from push diagnostics. Rust-analyzer uses this feature for example to provide Cargo
63 /// diagnostics with push and internal diagnostics with pull. The push diagnostics should
64 /// not clear the pull diagnostics and vice-versa.
65 identifier: Option<Arc<str>>,
66 },
67 // Future internal features can go here...
68 }
69
70 impl DiagnosticProvider {
71 pub fn language_server_id(&self) -> Option<LanguageServerId> {
72 match self {
73 Self::Lsp { server_id, .. } => Some(*server_id),
74 // _ => None,
75 }
76 }
77 }
78
79 // while I would prefer having this in helix-lsp that necessitates a bunch of
80 // conversions I would rather not add. I think its fine since this just a very
81 // trivial newtype wrapper and we would need something similar once we define
82 // completions in core
83 slotmap::new_key_type! {
84 pub struct LanguageServerId;
85 }
86
87 impl fmt::Display for LanguageServerId {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 write!(f, "{:?}", self.0)
90 }
91 }
92
93 impl Diagnostic {
94 #[inline]
95 pub fn severity(&self) -> Severity {
96 self.severity.unwrap_or(Severity::Warning)
97 }
98 }