From 6c7986a248b053071160ae4d186da0ed8a92d52a Mon Sep 17 00:00:00 2001 From: Shashank Mittal Date: Sat, 25 May 2024 12:54:45 +0530 Subject: [PATCH 1/2] quick fix implemented for argument errors Signed-off-by: Shashank Mittal --- kclvm/sema/src/resolver/arg.rs | 64 +++++++++++++++++++++++++--- kclvm/tools/src/LSP/src/quick_fix.rs | 14 ++++-- kclvm/tools/src/LSP/src/to_lsp.rs | 9 +++- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/kclvm/sema/src/resolver/arg.rs b/kclvm/sema/src/resolver/arg.rs index 6fbebfaa8..7c8ccecbf 100644 --- a/kclvm/sema/src/resolver/arg.rs +++ b/kclvm/sema/src/resolver/arg.rs @@ -38,12 +38,17 @@ impl<'ctx> Resolver<'ctx> { let mut kwarg_types: Vec<(String, TypeRef)> = vec![]; let mut check_table: IndexSet = IndexSet::default(); for kw in kwargs { + let (suggs, msg) = self.get_arg_kw_err_suggestion(kw, func_ty); if !kw.node.arg.node.names.is_empty() { let arg_name = &kw.node.arg.node.names[0].node; if check_table.contains(arg_name) { - self.handler.add_compile_error( - &format!("{} has duplicated keyword argument {}", func_name, arg_name), + self.handler.add_compile_error_with_suggestions( + &format!( + "{} has duplicated keyword argument {}{}", + func_name, arg_name, msg + ), kw.get_span_pos(), + Some(suggs), ); } check_table.insert(arg_name.to_string()); @@ -53,7 +58,7 @@ impl<'ctx> Resolver<'ctx> { kwarg_types.push((arg_name.to_string(), arg_value_type.clone())); } else { self.handler - .add_compile_error("missing argument", kw.get_span_pos()); + .add_compile_error(&format!("missing argument{}", msg), kw.get_span_pos()); } } // Do few argument count check @@ -87,7 +92,7 @@ impl<'ctx> Resolver<'ctx> { Some(param) => param.ty.clone(), None => { if !func_ty.is_variadic { - self.handler.add_compile_error( + self.handler.add_compile_error_with_suggestions( &format!( "{} takes {} but {} were given", func_name, @@ -96,6 +101,7 @@ impl<'ctx> Resolver<'ctx> { args.len(), ), args[i].get_span_pos(), + Some(vec![]), ); } return; @@ -112,12 +118,14 @@ impl<'ctx> Resolver<'ctx> { .any(|x| x == *arg_name) && !func_ty.is_variadic { - self.handler.add_compile_error( + let (suggs, msg) = self.get_arg_kw_err_suggestion_from_name(arg_name, func_ty); + self.handler.add_compile_error_with_suggestions( &format!( - "{} got an unexpected keyword argument '{}'", - func_name, arg_name + "{} got an unexpected keyword argument '{}'{}", + func_name, arg_name, msg ), kwargs[i].get_span_pos(), + Some(suggs), ); } let expected_types: Vec = func_ty @@ -137,4 +145,46 @@ impl<'ctx> Resolver<'ctx> { }; } } + + /// Generate suggestions for keyword argument errors. + pub(crate) fn get_arg_kw_err_suggestion( + &self, + kw: &ast::NodeRef, + func_ty: &FunctionType, + ) -> (Vec, String) { + let attr = &kw.node.arg.node.names[0].node; + let valid_params: Vec<&str> = func_ty + .params + .iter() + .map(|param| param.name.as_str()) + .collect(); + let suggs = suggestions::provide_suggestions(attr, valid_params.into_iter()); + + let suggestion = if !suggs.is_empty() { + format!(", did you mean '{}'?", suggs.join(" or ")) + } else { + String::new() + }; + + (suggs, suggestion) + } + + pub(crate) fn get_arg_kw_err_suggestion_from_name( + &self, + arg_name: &str, + func_ty: &FunctionType, + ) -> (Vec, String) { + let valid_params: Vec<&str> = func_ty + .params + .iter() + .map(|param| param.name.as_str()) + .collect(); + let suggs = suggestions::provide_suggestions(arg_name, valid_params.into_iter()); + let suggestion = if !suggs.is_empty() { + format!(", did you mean '{}'?", suggs.join(" or ")) + } else { + String::new() + }; + (suggs, suggestion) + } } diff --git a/kclvm/tools/src/LSP/src/quick_fix.rs b/kclvm/tools/src/LSP/src/quick_fix.rs index c535c5026..7503e1d1f 100644 --- a/kclvm/tools/src/LSP/src/quick_fix.rs +++ b/kclvm/tools/src/LSP/src/quick_fix.rs @@ -24,10 +24,16 @@ pub(crate) fn quick_fix(uri: &Url, diags: &[Diagnostic]) -> Vec>() }) - .filter(|v| !v.is_empty()) - .map(|s| json!({ "suggested_replacement": s })); + .map(|s| { + if s.is_empty() { + json!({ "suggested_replacement": "" }) + } else { + json!({ "suggested_replacement": s }) + } + }); let related_information = if related_msg.is_empty() { None From 29bed582b4380d6dc4046a2ff1f8e2dc3a9218a7 Mon Sep 17 00:00:00 2001 From: Shashank Mittal Date: Sat, 25 May 2024 19:46:02 +0530 Subject: [PATCH 2/2] grammar test fixed Signed-off-by: Shashank Mittal --- test/grammar/schema/init/init_kwargs_fail_0/stderr.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/grammar/schema/init/init_kwargs_fail_0/stderr.golden b/test/grammar/schema/init/init_kwargs_fail_0/stderr.golden index 3a65323ff..fac3bbf6f 100644 --- a/test/grammar/schema/init/init_kwargs_fail_0/stderr.golden +++ b/test/grammar/schema/init/init_kwargs_fail_0/stderr.golden @@ -8,5 +8,5 @@ error[E2L23]: CompileError --> ${CWD}/main.k:4:17 | 4 | person = Person(_naem="Alice") {} - | ^ "Person" got an unexpected keyword argument '_naem' + | ^ "Person" got an unexpected keyword argument '_naem', did you mean '_name'? | \ No newline at end of file