Skip to main content

emmylua_code_analysis/db_index/diagnostic/
mod.rs

1mod analyze_error;
2mod diagnostic_action;
3
4use std::collections::{HashMap, HashSet};
5
6pub use analyze_error::AnalyzeError;
7pub use diagnostic_action::{DiagnosticAction, DiagnosticActionKind};
8use rowan::TextRange;
9
10use crate::{DiagnosticCode, FileId};
11
12use super::traits::LuaIndex;
13
14#[derive(Debug)]
15pub struct DiagnosticIndex {
16    diagnostic_actions: HashMap<FileId, Vec<DiagnosticAction>>,
17    diagnostics: HashMap<FileId, Vec<AnalyzeError>>,
18    file_diagnostic_disabled: HashMap<FileId, HashSet<DiagnosticCode>>,
19    file_diagnostic_enabled: HashMap<FileId, HashSet<DiagnosticCode>>,
20}
21
22impl Default for DiagnosticIndex {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl DiagnosticIndex {
29    pub fn new() -> Self {
30        Self {
31            diagnostic_actions: HashMap::new(),
32            diagnostics: HashMap::new(),
33            file_diagnostic_disabled: HashMap::new(),
34            file_diagnostic_enabled: HashMap::new(),
35        }
36    }
37
38    pub fn add_diagnostic_action(&mut self, file_id: FileId, diagnostic: DiagnosticAction) {
39        self.diagnostic_actions
40            .entry(file_id)
41            .or_default()
42            .push(diagnostic);
43    }
44
45    pub fn add_file_diagnostic_disabled(&mut self, file_id: FileId, code: DiagnosticCode) {
46        self.file_diagnostic_disabled
47            .entry(file_id)
48            .or_default()
49            .insert(code);
50    }
51
52    pub fn add_file_diagnostic_enabled(&mut self, file_id: FileId, code: DiagnosticCode) {
53        self.file_diagnostic_enabled
54            .entry(file_id)
55            .or_default()
56            .insert(code);
57    }
58
59    pub fn get_diagnostics_actions(&self, file_id: FileId) -> Option<&Vec<DiagnosticAction>> {
60        self.diagnostic_actions.get(&file_id)
61    }
62
63    pub fn add_diagnostic(&mut self, file_id: FileId, diagnostic: AnalyzeError) {
64        self.diagnostics
65            .entry(file_id)
66            .or_default()
67            .push(diagnostic);
68    }
69
70    pub fn get_diagnostics(&self, file_id: &FileId) -> Option<&Vec<AnalyzeError>> {
71        self.diagnostics.get(file_id)
72    }
73
74    pub fn is_file_diagnostic_code_disabled(
75        &self,
76        file_id: &FileId,
77        code: &DiagnosticCode,
78        range: &TextRange,
79    ) -> bool {
80        if let Some(disabled) = self.diagnostic_actions.get(file_id) {
81            for action in disabled {
82                if action.is_match(true, range, code) {
83                    return true;
84                }
85            }
86        }
87        false
88    }
89
90    pub fn is_file_disabled(&self, file_id: &FileId, code: &DiagnosticCode) -> bool {
91        if let Some(disabled) = self.file_diagnostic_disabled.get(file_id) {
92            disabled.contains(code)
93        } else {
94            false
95        }
96    }
97
98    pub fn is_file_enabled(&self, file_id: &FileId, code: &DiagnosticCode) -> bool {
99        if let Some(enabled) = self.file_diagnostic_enabled.get(file_id) {
100            enabled.contains(code)
101        } else {
102            false
103        }
104    }
105}
106
107impl LuaIndex for DiagnosticIndex {
108    fn remove(&mut self, file_id: FileId) {
109        self.diagnostic_actions.remove(&file_id);
110        self.diagnostics.remove(&file_id);
111        self.file_diagnostic_disabled.remove(&file_id);
112        self.file_diagnostic_enabled.remove(&file_id);
113    }
114
115    fn clear(&mut self) {
116        self.diagnostic_actions.clear();
117        self.diagnostics.clear();
118        self.file_diagnostic_disabled.clear();
119        self.file_diagnostic_enabled.clear();
120    }
121}