Skip to content

Commit 23d6f31

Browse files
committed
refactor checker
1 parent cb6ca58 commit 23d6f31

File tree

5 files changed

+81
-105
lines changed

5 files changed

+81
-105
lines changed

crates/code_analysis/src/diagnostic/checker/type_not_found.rs renamed to crates/code_analysis/src/diagnostic/checker/analyze_error.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::DiagnosticCode;
22

33
use super::{DiagnosticContext, LuaChecker};
44

5+
const CODES: &[DiagnosticCode] = &[DiagnosticCode::TypeNotFound, DiagnosticCode::DuplicateType];
6+
57
#[derive(Debug)]
68
pub struct Checker();
79

@@ -15,16 +17,13 @@ impl LuaChecker for Checker {
1517
let mut analyze_errs = Vec::new();
1618
for error in errors {
1719
if error.kind == DiagnosticCode::TypeNotFound {
18-
analyze_errs.push((
19-
error.message.clone(),
20-
error.range.clone(),
21-
));
20+
analyze_errs.push((error.message.clone(), error.range.clone()));
2221
}
2322
}
2423

2524
analyze_errs
2625
};
27-
26+
2827
for analyze_err in errors {
2928
context.add_diagnostic(
3029
DiagnosticCode::TypeNotFound,
@@ -36,7 +35,7 @@ impl LuaChecker for Checker {
3635
Some(())
3736
}
3837

39-
fn get_code(&self) -> DiagnosticCode {
40-
DiagnosticCode::TypeNotFound
38+
fn support_codes(&self) -> &[DiagnosticCode] {
39+
CODES
4140
}
4241
}

crates/code_analysis/src/diagnostic/checker/duplicate_type.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

crates/code_analysis/src/diagnostic/checker/mod.rs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
mod syntax_error;
2-
mod type_not_found;
3-
mod duplicate_type;
2+
mod analyze_error;
43

54
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString};
65
use rowan::TextRange;
7-
use std::fmt::Debug;
6+
use std::{collections::HashSet, fmt::Debug, sync::Arc};
87

98
use crate::{db_index::DbIndex, semantic::SemanticModel, FileId};
109

11-
use super::{lua_diagnostic_code::get_default_severity, DiagnosticCode};
10+
use super::{lua_diagnostic_code::{get_default_severity, is_code_default_enable}, DiagnosticCode};
1211

1312
pub trait LuaChecker: Debug + Send + Sync {
1413
fn check(&self, context: &mut DiagnosticContext) -> Option<()>;
1514

16-
fn get_code(&self) -> DiagnosticCode;
15+
fn support_codes(&self) -> &[DiagnosticCode];
1716
}
1817

1918
macro_rules! checker {
@@ -25,21 +24,28 @@ macro_rules! checker {
2524
pub fn init_checkers() -> Vec<Box<dyn LuaChecker>> {
2625
vec![
2726
checker!(syntax_error),
28-
checker!(type_not_found),
29-
checker!(duplicate_type),
27+
checker!(analyze_error),
3028
]
3129
}
3230

3331
pub struct DiagnosticContext<'a> {
3432
semantic_model: SemanticModel<'a>,
3533
diagnostics: Vec<Diagnostic>,
34+
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
35+
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
3636
}
3737

3838
impl<'a> DiagnosticContext<'a> {
39-
pub fn new(semantic_model: SemanticModel<'a>) -> Self {
39+
pub fn new(
40+
semantic_model: SemanticModel<'a>,
41+
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
42+
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
43+
) -> Self {
4044
Self {
4145
semantic_model,
4246
diagnostics: Vec::new(),
47+
workspace_disabled,
48+
workspace_enabled
4349
}
4450
}
4551

@@ -125,4 +131,41 @@ impl<'a> DiagnosticContext<'a> {
125131
pub fn get_diagnostics(self) -> Vec<Diagnostic> {
126132
self.diagnostics
127133
}
134+
135+
pub fn is_checker_enable_by_code(
136+
&self,
137+
code: &DiagnosticCode,
138+
) -> bool {
139+
let file_id = self.get_file_id();
140+
let db = self.get_db();
141+
let diagnostic_index = db.get_diagnostic_index();
142+
// force enable
143+
if diagnostic_index.is_file_enabled(&file_id, &code) {
144+
return true;
145+
}
146+
147+
// workspace force disabled
148+
if self.workspace_disabled.contains(&code) {
149+
return false;
150+
}
151+
152+
let meta_index = db.get_meta_file();
153+
// ignore meta file diagnostic
154+
if meta_index.is_meta_file(&file_id) {
155+
return false;
156+
}
157+
158+
// is file disabled this code
159+
if diagnostic_index.is_file_disabled(&file_id, &code) {
160+
return false;
161+
}
162+
163+
// workspace force enabled
164+
if self.workspace_enabled.contains(&code) {
165+
return true;
166+
}
167+
168+
// default setting
169+
is_code_default_enable(&code)
170+
}
128171
}

crates/code_analysis/src/diagnostic/checker/syntax_error.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::DiagnosticCode;
22

33
use super::{DiagnosticContext, LuaChecker};
44

5+
const CODES: &[DiagnosticCode] = &[DiagnosticCode::SyntaxError];
6+
57
#[derive(Debug)]
68
pub struct Checker();
79

@@ -22,7 +24,7 @@ impl LuaChecker for Checker {
2224
Some(())
2325
}
2426

25-
fn get_code(&self) -> DiagnosticCode {
26-
DiagnosticCode::SyntaxError
27+
fn support_codes(&self) -> &[DiagnosticCode] {
28+
CODES
2729
}
2830
}
Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashSet, sync::Arc};
22

33
pub use super::checker::{DiagnosticContext, LuaChecker};
4-
use super::{checker::init_checkers, lua_diagnostic_code::is_code_default_enable, DiagnosticCode};
4+
use super::{checker::init_checkers, DiagnosticCode};
55
use crate::{Emmyrc, FileId, LuaCompilation};
66
use lsp_types::Diagnostic;
77
use tokio_util::sync::CancellationToken;
@@ -10,17 +10,17 @@ use tokio_util::sync::CancellationToken;
1010
pub struct LuaDiagnostic {
1111
checkers: Vec<Box<dyn LuaChecker>>,
1212
enable: bool,
13-
workspace_enabled: HashSet<DiagnosticCode>,
14-
workspace_disabled: HashSet<DiagnosticCode>,
13+
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
14+
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
1515
}
1616

1717
impl LuaDiagnostic {
1818
pub fn new() -> Self {
1919
Self {
2020
checkers: init_checkers(),
2121
enable: true,
22-
workspace_enabled: HashSet::new(),
23-
workspace_disabled: HashSet::new(),
22+
workspace_enabled: HashSet::new().into(),
23+
workspace_disabled: HashSet::new().into(),
2424
}
2525
}
2626

@@ -30,8 +30,8 @@ impl LuaDiagnostic {
3030

3131
pub fn update_config(&mut self, emmyrc: Arc<Emmyrc>) {
3232
self.enable = emmyrc.diagnostics.enable;
33-
self.workspace_disabled = emmyrc.diagnostics.disable.iter().cloned().collect();
34-
self.workspace_enabled = emmyrc.diagnostics.enables.iter().cloned().collect();
33+
self.workspace_disabled = Arc::new(emmyrc.diagnostics.disable.iter().cloned().collect());
34+
self.workspace_enabled = Arc::new(emmyrc.diagnostics.enables.iter().cloned().collect());
3535
}
3636

3737
pub async fn diagnose_file(
@@ -45,56 +45,27 @@ impl LuaDiagnostic {
4545
}
4646

4747
let model = compilation.get_semantic_model(file_id)?;
48-
let mut context = DiagnosticContext::new(model);
48+
let mut context = DiagnosticContext::new(
49+
model,
50+
self.workspace_enabled.clone(),
51+
self.workspace_disabled.clone(),
52+
);
4953
for checker in &self.checkers {
5054
if cancel_token.is_cancelled() {
5155
return None;
5256
}
5357

54-
let code = checker.get_code();
55-
if self.is_checker_enable_by_code(&context, &code) {
56-
checker.check(&mut context);
58+
let codes = checker.support_codes();
59+
let can_check = codes
60+
.iter()
61+
.any(|code| context.is_checker_enable_by_code(code));
62+
if !can_check {
63+
continue;
5764
}
58-
}
59-
60-
Some(context.get_diagnostics())
61-
}
62-
63-
fn is_checker_enable_by_code(
64-
&self,
65-
context: &DiagnosticContext,
66-
code: &DiagnosticCode,
67-
) -> bool {
68-
let file_id = context.get_file_id();
69-
let db = context.get_db();
70-
let diagnostic_index = db.get_diagnostic_index();
71-
// force enable
72-
if diagnostic_index.is_file_enabled(&file_id, &code) {
73-
return true;
74-
}
7565

76-
// workspace force disabled
77-
if self.workspace_disabled.contains(&code) {
78-
return false;
66+
checker.check(&mut context);
7967
}
8068

81-
let meta_index = db.get_meta_file();
82-
// ignore meta file diagnostic
83-
if meta_index.is_meta_file(&file_id) {
84-
return false;
85-
}
86-
87-
// is file disabled this code
88-
if diagnostic_index.is_file_disabled(&file_id, &code) {
89-
return false;
90-
}
91-
92-
// workspace force enabled
93-
if self.workspace_enabled.contains(&code) {
94-
return true;
95-
}
96-
97-
// default setting
98-
is_code_default_enable(&code)
69+
Some(context.get_diagnostics())
9970
}
10071
}

0 commit comments

Comments
 (0)