Skip to content

Commit 1b8b030

Browse files
committed
Support disable all when not code list
1 parent 500880d commit 1b8b030

File tree

3 files changed

+85
-51
lines changed

3 files changed

+85
-51
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/doc/diagnostic_tags.rs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,32 @@ fn analyze_diagnostic_disable(
4141
};
4242

4343
let diagnostic_index = analyzer.db.get_diagnostic_index_mut();
44-
let diagnostic_code_list = diagnostic.get_code_list()?;
45-
for code in diagnostic_code_list.get_codes() {
46-
let name = code.get_name_text();
47-
let diagnostic_code = if let Some(code) = DiagnosticCode::from_str(name).ok() {
48-
code
49-
} else {
50-
continue;
51-
};
52-
53-
if is_file_disable {
54-
diagnostic_index.add_file_diagnostic_disabled(analyzer.file_id, diagnostic_code);
55-
} else {
56-
diagnostic_index.add_diagnostic_action(
57-
analyzer.file_id,
58-
DiagnosticAction::new(
59-
owner_block_range,
60-
DiagnosticActionKind::Disable,
61-
diagnostic_code,
62-
),
63-
);
44+
if let Some(diagnostic_code_list) = diagnostic.get_code_list() {
45+
for code in diagnostic_code_list.get_codes() {
46+
let name = code.get_name_text();
47+
let diagnostic_code = if let Some(code) = DiagnosticCode::from_str(name).ok() {
48+
code
49+
} else {
50+
continue;
51+
};
52+
53+
if is_file_disable {
54+
diagnostic_index.add_file_diagnostic_disabled(analyzer.file_id, diagnostic_code);
55+
} else {
56+
diagnostic_index.add_diagnostic_action(
57+
analyzer.file_id,
58+
DiagnosticAction::new(
59+
owner_block_range,
60+
DiagnosticActionKind::Disable(diagnostic_code),
61+
),
62+
);
63+
}
6464
}
65+
} else {
66+
diagnostic_index.add_diagnostic_action(
67+
analyzer.file_id,
68+
DiagnosticAction::new(owner_block_range, DiagnosticActionKind::DisableAll),
69+
);
6570
}
6671

6772
Some(())
@@ -79,18 +84,24 @@ fn analyze_diagnostic_disable_next_line(
7984
let valid_range = TextRange::new(comment_range.start(), line_range.end());
8085

8186
let diagnostic_index = analyzer.db.get_diagnostic_index_mut();
82-
let diagnostic_code_list = diagnostic.get_code_list()?;
83-
for code in diagnostic_code_list.get_codes() {
84-
let name = code.get_name_text();
85-
let diagnostic_code = if let Some(code) = DiagnosticCode::from_str(name).ok() {
86-
code
87-
} else {
88-
continue;
89-
};
87+
if let Some(diagnostic_code_list) = diagnostic.get_code_list() {
88+
for code in diagnostic_code_list.get_codes() {
89+
let name = code.get_name_text();
90+
let diagnostic_code = if let Some(code) = DiagnosticCode::from_str(name).ok() {
91+
code
92+
} else {
93+
continue;
94+
};
9095

96+
diagnostic_index.add_diagnostic_action(
97+
analyzer.file_id,
98+
DiagnosticAction::new(valid_range, DiagnosticActionKind::Disable(diagnostic_code)),
99+
);
100+
}
101+
} else {
91102
diagnostic_index.add_diagnostic_action(
92103
analyzer.file_id,
93-
DiagnosticAction::new(valid_range, DiagnosticActionKind::Disable, diagnostic_code),
104+
DiagnosticAction::new(valid_range, DiagnosticActionKind::DisableAll),
94105
);
95106
}
96107

@@ -108,18 +119,24 @@ fn analyze_diagnostic_disable_line(
108119
let valid_range = document.get_line_range(comment_end_line)?;
109120

110121
let diagnostic_index = analyzer.db.get_diagnostic_index_mut();
111-
let diagnostic_code_list = diagnostic.get_code_list()?;
112-
for code in diagnostic_code_list.get_codes() {
113-
let name = code.get_name_text();
114-
let diagnostic_code = if let Some(code) = DiagnosticCode::from_str(name).ok() {
115-
code
116-
} else {
117-
continue;
118-
};
122+
if let Some(diagnostic_code_list) = diagnostic.get_code_list() {
123+
for code in diagnostic_code_list.get_codes() {
124+
let name = code.get_name_text();
125+
let diagnostic_code = if let Some(code) = DiagnosticCode::from_str(name).ok() {
126+
code
127+
} else {
128+
continue;
129+
};
119130

131+
diagnostic_index.add_diagnostic_action(
132+
analyzer.file_id,
133+
DiagnosticAction::new(valid_range, DiagnosticActionKind::Disable(diagnostic_code)),
134+
);
135+
}
136+
} else {
120137
diagnostic_index.add_diagnostic_action(
121138
analyzer.file_id,
122-
DiagnosticAction::new(valid_range, DiagnosticActionKind::Disable, diagnostic_code),
139+
DiagnosticAction::new(valid_range, DiagnosticActionKind::DisableAll),
123140
);
124141
}
125142

crates/emmylua_code_analysis/src/db_index/diagnostic/diagnostic_action.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,53 @@ use crate::DiagnosticCode;
66
pub struct DiagnosticAction {
77
range: TextRange,
88
kind: DiagnosticActionKind,
9-
code: DiagnosticCode,
109
}
1110

1211
impl DiagnosticAction {
13-
pub fn new(range: TextRange, kind: DiagnosticActionKind, code: DiagnosticCode) -> Self {
14-
Self { range, kind, code }
12+
pub fn new(range: TextRange, kind: DiagnosticActionKind) -> Self {
13+
Self { range, kind }
1514
}
1615

1716
pub fn get_range(&self) -> TextRange {
1817
self.range
1918
}
2019

2120
pub fn is_enable(&self) -> bool {
22-
matches!(self.kind, DiagnosticActionKind::Enable)
21+
matches!(self.kind, DiagnosticActionKind::Enable(_))
2322
}
2423

2524
pub fn is_disable(&self) -> bool {
26-
matches!(self.kind, DiagnosticActionKind::Disable)
25+
matches!(
26+
self.kind,
27+
DiagnosticActionKind::Disable(_) | DiagnosticActionKind::DisableAll
28+
)
2729
}
2830

29-
pub fn get_code(&self) -> DiagnosticCode {
30-
self.code
31+
pub fn get_code(&self) -> Option<DiagnosticCode> {
32+
match &self.kind {
33+
DiagnosticActionKind::Disable(code) => Some(*code),
34+
DiagnosticActionKind::Enable(code) => Some(*code),
35+
DiagnosticActionKind::DisableAll => None,
36+
}
37+
}
38+
39+
pub fn is_match(&self, is_disable: bool, range: &TextRange, code: &DiagnosticCode) -> bool {
40+
if self.range.intersect(*range).is_none() {
41+
return false;
42+
}
43+
44+
match (&self.kind, is_disable) {
45+
(DiagnosticActionKind::Disable(disable_code), true) => disable_code == code,
46+
(DiagnosticActionKind::Enable(enable_code), false) => enable_code == code,
47+
(DiagnosticActionKind::DisableAll, true) => true,
48+
_ => false,
49+
}
3150
}
3251
}
3352

3453
#[derive(Debug)]
3554
pub enum DiagnosticActionKind {
36-
Disable,
37-
Enable, // donot use this
55+
Disable(DiagnosticCode),
56+
Enable(DiagnosticCode), // donot use this
57+
DisableAll,
3858
}

crates/emmylua_code_analysis/src/db_index/diagnostic/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ impl DiagnosticIndex {
7373
) -> bool {
7474
if let Some(disabled) = self.diagnostic_actions.get(file_id) {
7575
for action in disabled {
76-
if action.get_code() == *code
77-
&& action.is_disable()
78-
&& action.get_range().intersect(*range).is_some()
79-
{
76+
if action.is_match(true, range, code) {
8077
return true;
8178
}
8279
}

0 commit comments

Comments
 (0)