Skip to content

Commit c93109f

Browse files
committed
refactor some diagnostic
1 parent c092241 commit c93109f

File tree

10 files changed

+111
-40
lines changed

10 files changed

+111
-40
lines changed

crates/code_analysis/src/compilation/analyzer/doc/infer_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub fn infer_type(analyzer: &mut DocAnalyzer, node: LuaDocType) -> LuaType {
6565
return LuaType::BooleanConst(bool_token.is_true())
6666
}
6767
LuaLiteralToken::Nil(_) => return LuaType::Nil,
68+
// todo
69+
LuaLiteralToken::Dots(_) => return LuaType::Any,
6870
}
6971
}
7072
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use emmylua_parser::{LuaAstNode, LuaAstToken, LuaCallExpr, LuaGeneralToken};
1+
use emmylua_parser::{LuaAstNode, LuaAstToken, LuaCallExpr, LuaGeneralToken, LuaLiteralExpr, LuaLiteralToken};
22

33
use crate::{DiagnosticCode, SemanticModel};
44

@@ -24,6 +24,17 @@ fn check_call_expr(
2424
let params = func.get_params();
2525
let args_count = call_expr.get_args_list()?.get_args().count();
2626
if args_count < params.len() {
27+
// fix last arg is `...`
28+
if args_count != 0 {
29+
let last_arg = call_expr.get_args_list()?.child::<LuaLiteralExpr>()?;
30+
if let Some(literal_token) = last_arg.get_literal() {
31+
if let LuaLiteralToken::Dots(_) = literal_token {
32+
return Some(());
33+
}
34+
}
35+
}
36+
37+
2738
let mut miss_parameter_info = Vec::new();
2839
for i in args_count..params.len() {
2940
let param_info = params.get(i)?;

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ mod deprecated;
55
mod discard_returns;
66
mod local_const_reassign;
77
mod missing_parameter;
8+
mod param_type_check;
89
mod syntax_error;
910
mod undefined_global;
1011
mod unused;
11-
mod param_type_check;
1212

1313
use lsp_types::{Diagnostic, DiagnosticSeverity, DiagnosticTag, NumberOrString};
1414
use rowan::TextRange;
15-
use std::{collections::HashSet, sync::Arc};
15+
use std::sync::Arc;
1616

1717
use crate::{db_index::DbIndex, semantic::SemanticModel, FileId};
1818

1919
use super::{
2020
lua_diagnostic_code::{get_default_severity, is_code_default_enable},
21+
lua_diagnostic_config::LuaDiagnosticConfig,
2122
DiagnosticCode,
2223
};
2324

@@ -55,23 +56,16 @@ pub struct DiagnosticContext<'a> {
5556
file_id: FileId,
5657
db: &'a DbIndex,
5758
diagnostics: Vec<Diagnostic>,
58-
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
59-
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
59+
pub config: Arc<LuaDiagnosticConfig>,
6060
}
6161

6262
impl<'a> DiagnosticContext<'a> {
63-
pub fn new(
64-
file_id: FileId,
65-
db: &'a DbIndex,
66-
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
67-
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
68-
) -> Self {
63+
pub fn new(file_id: FileId, db: &'a DbIndex, config: Arc<LuaDiagnosticConfig>) -> Self {
6964
Self {
7065
file_id,
7166
db,
7267
diagnostics: Vec::new(),
73-
workspace_disabled,
74-
workspace_enabled,
68+
config,
7569
}
7670
}
7771

@@ -172,7 +166,7 @@ impl<'a> DiagnosticContext<'a> {
172166
}
173167

174168
// workspace force disabled
175-
if self.workspace_disabled.contains(&code) {
169+
if self.config.workspace_disabled.contains(&code) {
176170
return false;
177171
}
178172

@@ -188,7 +182,7 @@ impl<'a> DiagnosticContext<'a> {
188182
}
189183

190184
// workspace force enabled
191-
if self.workspace_enabled.contains(&code) {
185+
if self.config.workspace_enabled.contains(&code) {
192186
return true;
193187
}
194188

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,20 @@ fn check_name_expr(
5858
return Some(());
5959
}
6060

61-
let emmyrc = semantic_model.get_emmyrc();
62-
if emmyrc.diagnostics.globals.contains(&name_text) {
61+
if context
62+
.config
63+
.global_disable_set
64+
.contains(name_text.as_str())
65+
{
66+
return Some(());
67+
}
68+
69+
if context
70+
.config
71+
.global_disable_glob
72+
.iter()
73+
.any(|re| re.is_match(&name_text))
74+
{
6375
return Some(());
6476
}
6577

crates/code_analysis/src/diagnostic/lua_diagnostic.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
use std::{collections::HashSet, sync::Arc};
1+
use std::sync::Arc;
22

33
pub use super::checker::DiagnosticContext;
4-
use super::{checker::check_file, DiagnosticCode};
4+
use super::{checker::check_file, lua_diagnostic_config::LuaDiagnosticConfig};
55
use crate::{Emmyrc, FileId, LuaCompilation};
66
use lsp_types::Diagnostic;
77
use tokio_util::sync::CancellationToken;
88

99
#[derive(Debug)]
1010
pub struct LuaDiagnostic {
1111
enable: bool,
12-
workspace_enabled: Arc<HashSet<DiagnosticCode>>,
13-
workspace_disabled: Arc<HashSet<DiagnosticCode>>,
12+
config: Arc<LuaDiagnosticConfig>,
1413
}
1514

1615
impl LuaDiagnostic {
1716
pub fn new() -> Self {
1817
Self {
1918
enable: true,
20-
workspace_enabled: HashSet::new().into(),
21-
workspace_disabled: HashSet::new().into(),
19+
config: Arc::new(LuaDiagnosticConfig::default()),
2220
}
2321
}
2422

2523
pub fn update_config(&mut self, emmyrc: Arc<Emmyrc>) {
2624
self.enable = emmyrc.diagnostics.enable;
27-
self.workspace_disabled = Arc::new(emmyrc.diagnostics.disable.iter().cloned().collect());
28-
self.workspace_enabled = Arc::new(emmyrc.diagnostics.enables.iter().cloned().collect());
25+
self.config = LuaDiagnosticConfig::new(&emmyrc).into();
2926
}
3027

3128
pub async fn diagnose_file(
@@ -44,12 +41,7 @@ impl LuaDiagnostic {
4441

4542
let db = compilation.get_db();
4643
let mut semantic_model = compilation.get_semantic_model(file_id)?;
47-
let mut context = DiagnosticContext::new(
48-
file_id,
49-
db,
50-
self.workspace_enabled.clone(),
51-
self.workspace_disabled.clone(),
52-
);
44+
let mut context = DiagnosticContext::new(file_id, db, self.config.clone());
5345

5446
check_file(&mut context, &mut semantic_model);
5547

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::collections::HashSet;
2+
3+
use regex::Regex;
4+
use smol_str::SmolStr;
5+
6+
use crate::Emmyrc;
7+
8+
use super::DiagnosticCode;
9+
10+
#[derive(Debug, Clone, Default)]
11+
pub struct LuaDiagnosticConfig {
12+
pub workspace_enabled: HashSet<DiagnosticCode>,
13+
pub workspace_disabled: HashSet<DiagnosticCode>,
14+
pub global_disable_set: HashSet<SmolStr>,
15+
pub global_disable_glob: Vec<Regex>,
16+
}
17+
18+
impl LuaDiagnosticConfig {
19+
pub fn new(emmyrc: &Emmyrc) -> Self {
20+
let workspace_disabled = emmyrc.diagnostics.disable.iter().cloned().collect();
21+
let workspace_enabled = emmyrc.diagnostics.enables.iter().cloned().collect();
22+
let global_disable_set = emmyrc
23+
.diagnostics
24+
.globals
25+
.iter()
26+
.map(|s| SmolStr::new(s.as_str()))
27+
.collect();
28+
29+
let global_disable_glob = emmyrc
30+
.diagnostics
31+
.globals_regex
32+
.iter()
33+
.filter_map(|s| match Regex::new(s) {
34+
Ok(r) => Some(r),
35+
Err(e) => {
36+
log::error!("Invalid regex: {}, error: {}", s, e);
37+
None
38+
}
39+
})
40+
.collect();
41+
42+
Self {
43+
workspace_disabled,
44+
workspace_enabled,
45+
global_disable_set,
46+
global_disable_glob,
47+
}
48+
}
49+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
mod checker;
12
mod lua_diagnostic;
23
mod lua_diagnostic_code;
3-
mod checker;
4+
mod lua_diagnostic_config;
45

5-
pub use lua_diagnostic_code::DiagnosticCode;
66
pub use lua_diagnostic::LuaDiagnostic;
7+
pub use lua_diagnostic_code::DiagnosticCode;

crates/code_analysis/src/semantic/infer/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ fn infer_literal_expr(expr: LuaLiteralExpr) -> InferResult {
6666
LuaLiteralToken::Number(num) => {
6767
if num.is_int() {
6868
Some(LuaType::IntegerConst(num.get_int_value()))
69+
} else if num.is_float() {
70+
Some(LuaType::FloatConst(num.get_float_value()))
6971
} else {
7072
Some(LuaType::Number)
7173
}
7274
}
73-
LuaLiteralToken::String(str) => Some(LuaType::StringConst(SmolStr::new(str.get_value()).into())),
75+
LuaLiteralToken::String(str) => {
76+
Some(LuaType::StringConst(SmolStr::new(str.get_value()).into()))
77+
}
78+
LuaLiteralToken::Dots(_) => Some(LuaType::Any),
7479
}
7580
}
7681

crates/emmylua_ls/src/handlers/command/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub async fn on_execute_command_handler(
1818
let args = params.arguments;
1919
let command_name = params.command.as_str();
2020
commands::dispatch_command(context, command_name, args).await;
21-
None
21+
Some(Value::Null)
2222
}
2323

2424
pub fn register_capabilities(

crates/emmylua_parser/src/syntax/node/token/tokens.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ pub enum LuaLiteralToken {
363363
Number(LuaNumberToken),
364364
Bool(LuaBoolToken),
365365
Nil(LuaNilToken),
366+
Dots(LuaGeneralToken),
366367
}
367368

368369
impl LuaAstToken for LuaLiteralToken {
@@ -372,6 +373,7 @@ impl LuaAstToken for LuaLiteralToken {
372373
LuaLiteralToken::Number(token) => token.syntax(),
373374
LuaLiteralToken::Bool(token) => token.syntax(),
374375
LuaLiteralToken::Nil(token) => token.syntax(),
376+
LuaLiteralToken::Dots(token) => token.syntax(),
375377
}
376378
}
377379

@@ -380,13 +382,15 @@ impl LuaAstToken for LuaLiteralToken {
380382
Self: Sized,
381383
{
382384
match kind {
383-
LuaTokenKind::TkString
384-
| LuaTokenKind::TkLongString
385+
LuaTokenKind::TkInt
385386
| LuaTokenKind::TkFloat
386-
| LuaTokenKind::TkInt
387+
| LuaTokenKind::TkComplex
388+
| LuaTokenKind::TkNil
387389
| LuaTokenKind::TkTrue
388390
| LuaTokenKind::TkFalse
389-
| LuaTokenKind::TkNil => true,
391+
| LuaTokenKind::TkDots
392+
| LuaTokenKind::TkString
393+
| LuaTokenKind::TkLongString => true,
390394
_ => false,
391395
}
392396
}
@@ -399,13 +403,14 @@ impl LuaAstToken for LuaLiteralToken {
399403
LuaTokenKind::TkString | LuaTokenKind::TkLongString => {
400404
LuaStringToken::cast(syntax).map(LuaLiteralToken::String)
401405
}
402-
LuaTokenKind::TkFloat | LuaTokenKind::TkInt => {
406+
LuaTokenKind::TkFloat | LuaTokenKind::TkInt | LuaTokenKind::TkComplex => {
403407
LuaNumberToken::cast(syntax).map(LuaLiteralToken::Number)
404408
}
405409
LuaTokenKind::TkTrue | LuaTokenKind::TkFalse => {
406410
LuaBoolToken::cast(syntax).map(LuaLiteralToken::Bool)
407411
}
408412
LuaTokenKind::TkNil => LuaNilToken::cast(syntax).map(LuaLiteralToken::Nil),
413+
LuaTokenKind::TkDots => LuaGeneralToken::cast(syntax).map(LuaLiteralToken::Dots),
409414
_ => None,
410415
}
411416
}

0 commit comments

Comments
 (0)