From 7876e189382478e353ef9d3a912ca4341ca57812 Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Thu, 4 Jul 2024 15:04:56 +0530 Subject: [PATCH] add inlay hints for function call args Signed-off-by: shruti2522 make fmt Signed-off-by: shruti2522 add inlay hints for function call args Signed-off-by: shruti2522 --- kclvm/sema/src/advanced_resolver/node.rs | 71 ++++++++-- ...y_hints__tests__assign_stmt_type_hint.snap | 131 ++++++++++++++++++ .../assign_stmt_type_hint.k | 6 + 3 files changed, 196 insertions(+), 12 deletions(-) diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 0d263bfd2..c7bce6cf7 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -15,7 +15,7 @@ use crate::{ SymbolSemanticInfo, UnresolvedSymbol, ValueSymbol, }, }, - ty::{Type, TypeKind, SCHEMA_MEMBER_FUNCTIONS}, + ty::{Parameter, Type, TypeKind, SCHEMA_MEMBER_FUNCTIONS}, }; use super::AdvancedResolver; @@ -573,7 +573,22 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { fn walk_call_expr(&mut self, call_expr: &'ctx ast::CallExpr) -> Self::Result { self.expr(&call_expr.func)?; - self.do_arguments_symbol_resolve(&call_expr.args, &call_expr.keywords)?; + let ty = self + .ctx + .node_ty_map + .borrow() + .get(&self.ctx.get_node_key(&call_expr.func.id)) + .unwrap() + .clone(); + if let TypeKind::Function(func_ty) = &ty.kind { + let func_params = &func_ty.params; + self.do_arguments_symbol_resolve_with_hint( + &call_expr.args, + &call_expr.keywords, + true, + func_params.to_vec(), + )?; + } Ok(None) } @@ -1226,32 +1241,64 @@ impl<'ctx> AdvancedResolver<'ctx> { args: &'ctx [ast::NodeRef], kwargs: &'ctx [ast::NodeRef], ) -> anyhow::Result<()> { - for arg in args.iter() { - self.expr(arg)?; - } + self.do_arguments_symbol_resolve_with_hint(args, kwargs, false, vec![]) + } + + pub fn do_arguments_symbol_resolve_with_hint( + &mut self, + args: &'ctx [ast::NodeRef], + kwargs: &'ctx [ast::NodeRef], + with_hint: bool, + mut params: Vec, + ) -> anyhow::Result<()> { for kw in kwargs.iter() { if let Some(value) = &kw.node.value { self.expr(value)?; } + let kw_name = kw.node.arg.node.get_name(); let (start_pos, end_pos): Range = kw.get_span_pos(); let value = self.gs.get_symbols_mut().alloc_value_symbol( - ValueSymbol::new(kw.node.arg.node.get_name(), start_pos, end_pos, None, false), + ValueSymbol::new(kw_name.clone(), start_pos, end_pos, None, false), self.ctx.get_node_key(&kw.id), self.ctx.current_pkgpath.clone().unwrap(), ); if let Some(value) = self.gs.get_symbols_mut().values.get_mut(value.get_id()) { - value.sema_info = SymbolSemanticInfo { - ty: self + let ty = self + .ctx + .node_ty_map + .borrow() + .get(&self.ctx.get_node_key(&kw.id)) + .map(|ty| ty.clone()); + if with_hint { + value.hint = Some(SymbolHint::VarHint(kw_name.clone())); + params.retain(|param| param.name != kw_name); + } + value.sema_info = SymbolSemanticInfo { ty, doc: None }; + } + } + for (i, arg) in args.iter().enumerate() { + self.expr(arg)?; + if with_hint { + let (start_pos, end_pos) = arg.get_span_pos(); + let value = self.gs.get_symbols_mut().alloc_value_symbol( + ValueSymbol::new(params[i].name.clone(), start_pos, end_pos, None, false), + self.ctx.get_node_key(&arg.id), + self.ctx.current_pkgpath.clone().unwrap(), + ); + if let Some(value) = self.gs.get_symbols_mut().values.get_mut(value.get_id()) { + let ty = self .ctx .node_ty_map .borrow() - .get(&self.ctx.get_node_key(&kw.id)) - .map(|ty| ty.clone()), - doc: None, - }; + .get(&self.ctx.get_node_key(&arg.id)) + .map(|ty| ty.clone()); + value.hint = Some(SymbolHint::VarHint(params[i].name.clone())); + value.sema_info = SymbolSemanticInfo { ty, doc: None }; + } } } + Ok(()) } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap index fb56296b3..2582d2cc2 100644 --- a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__assign_stmt_type_hint.snap @@ -1,5 +1,6 @@ --- source: tools/src/LSP/src/inlay_hints.rs +assertion_line: 80 expression: "format!(\"{:#?}\", res)" --- Some( @@ -290,5 +291,135 @@ Some( ), data: None, }, + InlayHint { + position: Position { + line: 23, + character: 4, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: ": (any, any, any) -> any", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: Some( + true, + ), + padding_right: Some( + true, + ), + data: None, + }, + InlayHint { + position: Position { + line: 27, + character: 1, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: ": any", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: Some( + true, + ), + padding_right: Some( + true, + ), + data: None, + }, + InlayHint { + position: Position { + line: 27, + character: 9, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: "x: ", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: Some( + true, + ), + padding_right: Some( + true, + ), + data: None, + }, + InlayHint { + position: Position { + line: 27, + character: 12, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: "y: ", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: Some( + true, + ), + padding_right: Some( + true, + ), + data: None, + }, + InlayHint { + position: Position { + line: 27, + character: 19, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: "z: ", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: Some( + true, + ), + padding_right: Some( + true, + ), + data: None, + }, ], ) diff --git a/kclvm/tools/src/LSP/src/test_data/inlay_hints/assign_stmt_type_hint/assign_stmt_type_hint.k b/kclvm/tools/src/LSP/src/test_data/inlay_hints/assign_stmt_type_hint/assign_stmt_type_hint.k index 56b633380..60accee2e 100644 --- a/kclvm/tools/src/LSP/src/test_data/inlay_hints/assign_stmt_type_hint/assign_stmt_type_hint.k +++ b/kclvm/tools/src/LSP/src/test_data/inlay_hints/assign_stmt_type_hint/assign_stmt_type_hint.k @@ -20,3 +20,9 @@ ee = { a: "asda" } ccc = 1Ki + +func = lambda x, y, z{ + x * y + z +} + +z = func(a, y = 1, z = 2)