doki_error/error_3rd/
for_lsp.rs

1use crate::DokiError;
2use lsp_types::{Diagnostic, DiagnosticTag, Range};
3use yggdrasil_shared::{LspTextAdaptor, TextIndex};
4
5impl DokiError {
6    /// Get the range as [`Range`]
7    #[inline]
8    pub fn get_lsp_range(&self, text: &TextIndex) -> Option<Range> {
9        self.range.as_ref().and_then(|r| text.offset_range_to_lsp_range(&r))
10    }
11    /// Get the tags as [`DiagnosticTag`]
12    #[inline]
13    pub fn get_lsp_tags(&self) -> Option<Vec<DiagnosticTag>> {
14        let mut tags = vec![];
15        if self.is_unnecessary() {
16            tags.push(DiagnosticTag::UNNECESSARY)
17        }
18        if self.is_deprecated() {
19            tags.push(DiagnosticTag::DEPRECATED)
20        }
21        return Some(tags);
22    }
23    /// Convert error to lsp [`Diagnostic`]
24    #[inline]
25    pub fn as_lsp_diagnostic(&self, text: &TextIndex) -> Diagnostic {
26        Diagnostic {
27            range: self.get_lsp_range(text).unwrap_or_default(),
28            severity: self.level.into_severity(),
29            code: None,
30            code_description: None,
31            source: None,
32            message: "".to_string(),
33            related_information: None,
34            tags: self.get_lsp_tags(),
35            data: None,
36        }
37    }
38}