Skip to content

Commit a32423d

Browse files
committed
[IMP] server: implement NOQA
Noqas can now be used to prevent diagnostics to be raised for some part of the code. A noqa can be placed: - before the first expression to be effective on the whole file - before a class or a function to be effective on the whole class/function - at the end of a line to be effective on the current line Noqas can be written:
1 parent b8dbb3b commit a32423d

14 files changed

+393
-124
lines changed

server/src/core/evaluation.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::core::odoo::SyncOdoo;
1212
use crate::threads::SessionInfo;
1313
use crate::S;
1414

15-
use super::file_mgr::FileMgr;
15+
use super::file_mgr::{add_diagnostic, FileMgr, NoqaInfo};
1616
use super::python_validator::PythonValidator;
1717
use super::symbols::function_symbol::{Argument, ArgumentType, FunctionSymbol};
1818
use super::symbols::symbol::Symbol;
@@ -757,7 +757,7 @@ impl Evaluation {
757757
}
758758
let class_sym_weak_eval = class_sym_weak_eval.as_weak();
759759
if class_sym_weak_eval.instance.unwrap_or(false) {
760-
diagnostics.push(Diagnostic::new(
760+
add_diagnostic(&mut diagnostics, Diagnostic::new(
761761
Range::new(Position::new(expr.arguments.args[0].range().start().to_u32(), 0),
762762
Position::new(expr.arguments.args[0].range().end().to_u32(), 0)),
763763
Some(DiagnosticSeverity::ERROR),
@@ -767,7 +767,7 @@ impl Evaluation {
767767
None,
768768
None
769769
)
770-
);
770+
, &session.current_noqa);
771771
None
772772
} else {
773773
let mut is_instance = None;
@@ -793,7 +793,7 @@ impl Evaluation {
793793
} else {
794794
match parent.borrow().get_in_parents(&vec![SymType::CLASS], true){
795795
None => {
796-
diagnostics.push(Diagnostic::new(
796+
add_diagnostic(&mut diagnostics, Diagnostic::new(
797797
Range::new(Position::new(expr.range().start().to_u32(), 0),
798798
Position::new(expr.range().end().to_u32(), 0)),
799799
Some(DiagnosticSeverity::ERROR),
@@ -803,7 +803,7 @@ impl Evaluation {
803803
None,
804804
None
805805
)
806-
);
806+
, &session.current_noqa);
807807
None
808808
},
809809
Some(parent_class) => Some((parent_class.clone(), Some(true)))
@@ -929,7 +929,8 @@ impl Evaluation {
929929
expr,
930930
call_parent.clone(),
931931
module.clone(),
932-
on_instance));
932+
on_instance,
933+
));
933934
}
934935
context.as_mut().unwrap().insert(S!("base_call"), ContextValue::SYMBOL(call_parent));
935936
for eval in base_sym.borrow().evaluations().unwrap().iter() {
@@ -1213,15 +1214,15 @@ impl Evaluation {
12131214
}
12141215
}
12151216
if !pos_arg {
1216-
diagnostics.push(Diagnostic::new(
1217+
add_diagnostic(&mut diagnostics, Diagnostic::new(
12171218
Range::new(Position::new(exprCall.range().start().to_u32(), 0), Position::new(exprCall.range().end().to_u32(), 0)),
12181219
Some(DiagnosticSeverity::ERROR),
12191220
Some(NumberOrString::String(S!("OLS30315"))),
12201221
Some(EXTENSION_NAME.to_string()),
12211222
format!("{} takes 0 positional arguments, but at least 1 is given", function.name),
12221223
None,
12231224
None,
1224-
));
1225+
), &session.current_noqa);
12251226
return diagnostics;
12261227
}
12271228
arg_index += 1;
@@ -1234,15 +1235,15 @@ impl Evaluation {
12341235
//match arg with argument from function
12351236
let function_arg = function.args.get(min(arg_index, vararg_index) as usize);
12361237
if function_arg.is_none() || function_arg.unwrap().arg_type == ArgumentType::KWORD_ONLY || function_arg.unwrap().arg_type == ArgumentType::KWARG {
1237-
diagnostics.push(Diagnostic::new(
1238+
add_diagnostic(&mut diagnostics, Diagnostic::new(
12381239
Range::new(Position::new(exprCall.range().start().to_u32(), 0), Position::new(exprCall.range().end().to_u32(), 0)),
12391240
Some(DiagnosticSeverity::ERROR),
12401241
Some(NumberOrString::String(S!("OLS30315"))),
12411242
Some(EXTENSION_NAME.to_string()),
12421243
format!("{} takes {} positional arguments, but at least {} is given", function.name, number_pos_arg, arg_index + 1),
12431244
None,
12441245
None,
1245-
));
1246+
), &session.current_noqa);
12461247
return diagnostics;
12471248
}
12481249
if function_arg.unwrap().arg_type != ArgumentType::VARARG {
@@ -1268,31 +1269,31 @@ impl Evaluation {
12681269
}
12691270
}
12701271
if !found_one && kwarg_index == i32::MAX {
1271-
diagnostics.push(Diagnostic::new(
1272+
add_diagnostic(&mut diagnostics, Diagnostic::new(
12721273
Range::new(Position::new(exprCall.range().start().to_u32(), 0), Position::new(exprCall.range().end().to_u32(), 0)),
12731274
Some(DiagnosticSeverity::ERROR),
12741275
Some(NumberOrString::String(S!("OLS30316"))),
12751276
Some(EXTENSION_NAME.to_string()),
12761277
format!("{} got an unexpected keyword argument '{}'", function.name, arg_identifier.id),
12771278
None,
12781279
None,
1279-
))
1280+
), &session.current_noqa)
12801281
}
12811282
} else {
12821283
// if arg is None, it means that it is a **arg
12831284
found_pos_arg_with_kw = number_pos_arg;
12841285
}
12851286
}
12861287
if found_pos_arg_with_kw + 1 < number_pos_arg {
1287-
diagnostics.push(Diagnostic::new(
1288+
add_diagnostic(&mut diagnostics, Diagnostic::new(
12881289
Range::new(Position::new(exprCall.range().start().to_u32(), 0), Position::new(exprCall.range().end().to_u32(), 0)),
12891290
Some(DiagnosticSeverity::ERROR),
12901291
Some(NumberOrString::String(S!("OLS30315"))),
12911292
Some(EXTENSION_NAME.to_string()),
12921293
format!("{} takes {} positional arguments, but only {} is given", function.name, number_pos_arg, arg_index),
12931294
None,
12941295
None,
1295-
));
1296+
), &session.current_noqa);
12961297
return diagnostics;
12971298
}
12981299
diagnostics
@@ -1312,31 +1313,31 @@ impl Evaluation {
13121313
Expr::Tuple(t) => {
13131314
need_tuple = max(need_tuple - 1, 0);
13141315
if t.elts.len() != 3 {
1315-
diagnostics.push(Diagnostic::new(
1316+
add_diagnostic(&mut diagnostics, Diagnostic::new(
13161317
Range::new(Position::new(t.range().start().to_u32(), 0), Position::new(t.range().end().to_u32(), 0)),
13171318
Some(DiagnosticSeverity::ERROR),
13181319
Some(NumberOrString::String(S!("OLS30314"))),
13191320
Some(EXTENSION_NAME.to_string()),
13201321
format!("Domain tuple should have 3 elements"),
13211322
None,
13221323
None,
1323-
));
1324+
), &session.current_noqa);
13241325
} else {
13251326
Evaluation::validate_tuple_search_domain(session, on_object.clone(), from_module.clone(), &t.elts[0], &t.elts[1], &t.elts[2], &mut diagnostics);
13261327
}
13271328
},
13281329
Expr::List(l) => {
13291330
need_tuple = max(need_tuple - 1, 0);
13301331
if l.elts.len() != 3 {
1331-
diagnostics.push(Diagnostic::new(
1332+
add_diagnostic(&mut diagnostics, Diagnostic::new(
13321333
Range::new(Position::new(l.range().start().to_u32(), 0), Position::new(l.range().end().to_u32(), 0)),
13331334
Some(DiagnosticSeverity::ERROR),
13341335
Some(NumberOrString::String(S!("OLS30314"))),
13351336
Some(EXTENSION_NAME.to_string()),
13361337
format!("Domain tuple should have 3 elements"),
13371338
None,
13381339
None,
1339-
));
1340+
), &session.current_noqa);
13401341
} else {
13411342
Evaluation::validate_tuple_search_domain(session, on_object.clone(), from_module.clone(), &l.elts[0], &l.elts[1], &l.elts[2], &mut diagnostics);
13421343
}
@@ -1356,15 +1357,15 @@ impl Evaluation {
13561357
}
13571358
}
13581359
_ => {
1359-
diagnostics.push(Diagnostic::new(
1360+
add_diagnostic(&mut diagnostics, Diagnostic::new(
13601361
Range::new(Position::new(s.range().start().to_u32(), 0), Position::new(s.range().end().to_u32(), 0)),
13611362
Some(DiagnosticSeverity::ERROR),
13621363
Some(NumberOrString::String(S!("OLS30317"))),
13631364
Some(EXTENSION_NAME.to_string()),
13641365
format!("A String value in tuple should contains '&', '|' or '!'"),
13651366
None,
13661367
None,
1367-
));
1368+
), &session.current_noqa);
13681369
}
13691370
}
13701371
},
@@ -1373,15 +1374,15 @@ impl Evaluation {
13731374
}
13741375
}
13751376
if need_tuple > 0 {
1376-
diagnostics.push(Diagnostic::new(
1377+
add_diagnostic(&mut diagnostics, Diagnostic::new(
13771378
Range::new(Position::new(value.range().start().to_u32(), 0), Position::new(value.range().end().to_u32(), 0)),
13781379
Some(DiagnosticSeverity::ERROR),
13791380
Some(NumberOrString::String(S!("OLS30319"))),
13801381
Some(EXTENSION_NAME.to_string()),
13811382
format!("Missing tuple after a search domain operator"),
13821383
None,
13831384
None,
1384-
));
1385+
), &session.current_noqa);
13851386
}
13861387
diagnostics
13871388
}
@@ -1398,21 +1399,21 @@ impl Evaluation {
13981399
'split_name: for name in split_expr {
13991400
if date_mode {
14001401
if !["year_number", "quarter_number", "month_number", "iso_week_number", "day_of_week", "day_of_month", "day_of_year", "hour_number", "minute_number", "second_number"].contains(&name) {
1401-
diagnostics.push(Diagnostic::new(
1402+
add_diagnostic(diagnostics, Diagnostic::new(
14021403
Range::new(Position::new(s.range().start().to_u32(), 0), Position::new(s.range().end().to_u32(), 0)),
14031404
Some(DiagnosticSeverity::ERROR),
14041405
Some(NumberOrString::String(S!("OLS30321"))),
14051406
Some(EXTENSION_NAME.to_string()),
14061407
format!("Invalid search domain field: Unknown granularity for date field. Use either \"year_number\", \"quarter_number\", \"month_number\", \"iso_week_number\", \"day_of_week\", \"day_of_month\", \"day_of_year\", \"hour_number\", \"minute_number\" or \"second_number\""),
14071408
None,
14081409
None,
1409-
));
1410+
), &session.current_noqa);
14101411
}
14111412
date_mode = false;
14121413
continue;
14131414
}
14141415
if obj.is_none() {
1415-
diagnostics.push(Diagnostic::new(
1416+
add_diagnostic(diagnostics, Diagnostic::new(
14161417
Range::new(Position::new(s.range().start().to_u32(), 0), Position::new(s.range().end().to_u32(), 0)),
14171418
Some(DiagnosticSeverity::ERROR),
14181419
Some(NumberOrString::String(S!("OLS30322"))),
@@ -1422,7 +1423,7 @@ In a search domain, when using a dot separator, it should be used either on a Da
14221423
If you used a relational field and get this error, check that the comodel of this field is valid."),
14231424
None,
14241425
None,
1425-
));
1426+
), &session.current_noqa);
14261427
break;
14271428
}
14281429
if let Some(object) = &obj {
@@ -1434,15 +1435,15 @@ If you used a relational field and get this error, check that the comodel of thi
14341435
false,
14351436
false);
14361437
if symbols.is_empty() {
1437-
diagnostics.push(Diagnostic::new(
1438+
add_diagnostic(diagnostics, Diagnostic::new(
14381439
Range::new(Position::new(s.range().start().to_u32(), 0), Position::new(s.range().end().to_u32(), 0)),
14391440
Some(DiagnosticSeverity::ERROR),
14401441
Some(NumberOrString::String(S!("OLS30320"))),
14411442
Some(EXTENSION_NAME.to_string()),
14421443
format!("Invalid search domain field: {} is not a member of {}", name, object.borrow().name()),
14431444
None,
14441445
None,
1445-
));
1446+
), &session.current_noqa);
14461447
break;
14471448
}
14481449
obj = None;
@@ -1478,15 +1479,15 @@ If you used a relational field and get this error, check that the comodel of thi
14781479
"=" | "!=" | ">" | ">=" | "<" | "<=" | "=?" | "=like" | "like" | "not like" | "ilike" |
14791480
"not ilike" | "=ilike" | "in" | "not in" | "child_of" | "parent_of" | "any" | "not any" => {},
14801481
_ => {
1481-
diagnostics.push(Diagnostic::new(
1482+
add_diagnostic(diagnostics, Diagnostic::new(
14821483
Range::new(Position::new(s.range().start().to_u32(), 0), Position::new(s.range().end().to_u32(), 0)),
14831484
Some(DiagnosticSeverity::ERROR),
14841485
Some(NumberOrString::String(S!("OLS30318"))),
14851486
Some(EXTENSION_NAME.to_string()),
14861487
format!("Invalid comparison operator"),
14871488
None,
14881489
None,
1489-
));
1490+
), &session.current_noqa);
14901491
}
14911492
}
14921493
},

0 commit comments

Comments
 (0)