From 12ecc6f3cd8dec3539397f8e9ad9135581ac8acb Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Wed, 21 Aug 2024 17:13:23 +0530 Subject: [PATCH 1/7] modify walk config expr Signed-off-by: shruti2522 --- kclvm/sema/src/advanced_resolver/node.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 73fffdba0..916b5d9d4 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -867,6 +867,28 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { } fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result { + let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); + let schema_symbol = self.ctx.schema_symbol_stack.last().unwrap_or(&None).clone(); + let kind = LocalSymbolScopeKind::Config; + + if let Some(schema_expr) = self + .gs + .get_symbols_mut() + .exprs + .get_mut(schema_symbol.unwrap().get_id()) + { + let mut expr_symbol = ExpressionSymbol::new( + schema_expr.name.to_string(), + start.clone(), + end.clone(), + None, + ); + expr_symbol.hint = Some(SymbolHint { + pos: start.clone(), + kind: SymbolHintKind::VarHint(schema_expr.name.to_string()), + }) + } + self.walk_config_entries(&config_expr.items)?; Ok(None) } From 422aa4ca9cee52ebe87346c12f5ebc6ab37e143f Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Mon, 26 Aug 2024 12:26:08 +0530 Subject: [PATCH 2/7] set local scope Signed-off-by: shruti2522 --- kclvm/sema/src/advanced_resolver/node.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 916b5d9d4..82a59a721 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -871,6 +871,18 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { let schema_symbol = self.ctx.schema_symbol_stack.last().unwrap_or(&None).clone(); let kind = LocalSymbolScopeKind::Config; + self.enter_local_scope( + &self.ctx.current_filename.as_ref().unwrap().clone(), + start.clone(), + end.clone(), + kind, + ); + + let cur_scope = *self.ctx.scopes.last().unwrap(); + self.gs + .get_scopes_mut() + .set_owner_to_scope(cur_scope, schema_symbol.unwrap()); + if let Some(schema_expr) = self .gs .get_symbols_mut() @@ -886,9 +898,11 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { expr_symbol.hint = Some(SymbolHint { pos: start.clone(), kind: SymbolHintKind::VarHint(schema_expr.name.to_string()), - }) + }); } + self.leave_scope(); + self.walk_config_entries(&config_expr.items)?; Ok(None) } From 9859903392cfca831de811ae70b303ab1a75d322 Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Tue, 27 Aug 2024 09:50:35 +0530 Subject: [PATCH 3/7] add hint Signed-off-by: shruti2522 schema name hint Signed-off-by: shruti2522 set sema type Signed-off-by: shruti2522 add expr name Signed-off-by: shruti2522 revert change in fib.k Signed-off-by: shruti2522 conditional hint Signed-off-by: shruti2522 add tests Signed-off-by: shruti2522 add config_entry Signed-off-by: shruti2522 add hints for type assign Signed-off-by: shruti2522 add hint Signed-off-by: shruti2522 --- kclvm/sema/src/advanced_resolver/node.rs | 98 ++++++++++++------- kclvm/tools/src/LSP/src/inlay_hints.rs | 5 + ...nlay_hints__tests__schema_config_hint.snap | 31 ++++++ .../schema_config_hint/schema_config_hint.k | 6 ++ 4 files changed, 104 insertions(+), 36 deletions(-) create mode 100644 kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap create mode 100644 kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index 82a59a721..ae8ea3648 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -867,43 +867,24 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { } fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result { - let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); - let schema_symbol = self.ctx.schema_symbol_stack.last().unwrap_or(&None).clone(); - let kind = LocalSymbolScopeKind::Config; - - self.enter_local_scope( - &self.ctx.current_filename.as_ref().unwrap().clone(), - start.clone(), - end.clone(), - kind, - ); - - let cur_scope = *self.ctx.scopes.last().unwrap(); - self.gs - .get_scopes_mut() - .set_owner_to_scope(cur_scope, schema_symbol.unwrap()); + let (start, _end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); - if let Some(schema_expr) = self - .gs - .get_symbols_mut() - .exprs - .get_mut(schema_symbol.unwrap().get_id()) - { - let mut expr_symbol = ExpressionSymbol::new( - schema_expr.name.to_string(), - start.clone(), - end.clone(), - None, - ); - expr_symbol.hint = Some(SymbolHint { - pos: start.clone(), - kind: SymbolHintKind::VarHint(schema_expr.name.to_string()), - }); - } + let pre_pos = Position { + filename: start.filename.clone(), + line: start.line.clone(), + column: start.column.map(|c| if c >= 1 { c - 1 } else { 0 }), + }; - self.leave_scope(); + let with_hint = if let Some(stmt) = self.ctx.program.pos_to_stmt(&pre_pos) { + match stmt.node { + ast::Stmt::Assign(ref assign_stmt) => assign_stmt.ty.is_some(), + _ => false, + } + } else { + false + }; - self.walk_config_entries(&config_expr.items)?; + self.walk_config_entries_with_hint(&config_expr.items, with_hint)?; Ok(None) } @@ -1915,6 +1896,14 @@ impl<'ctx> AdvancedResolver<'ctx> { pub(crate) fn walk_config_entries( &mut self, entries: &'ctx [ast::NodeRef], + ) -> anyhow::Result<()> { + self.walk_config_entries_with_hint(entries, false) + } + + pub(crate) fn walk_config_entries_with_hint( + &mut self, + entries: &'ctx [ast::NodeRef], + with_hint: bool, ) -> anyhow::Result<()> { let (start, end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); @@ -1923,8 +1912,8 @@ impl<'ctx> AdvancedResolver<'ctx> { self.enter_local_scope( &self.ctx.current_filename.as_ref().unwrap().clone(), - start, - end, + start.clone(), + end.clone(), kind, ); @@ -1935,6 +1924,43 @@ impl<'ctx> AdvancedResolver<'ctx> { .set_owner_to_scope(*cur_scope, owner); } + if with_hint { + if let Some(schema_symbol) = schema_symbol { + let symbol_data = self.gs.get_symbols_mut(); + if let Some(schema_data) = symbol_data.get_symbol(schema_symbol) { + if let Some(ty) = &schema_data.get_sema_info().ty { + let mut expr_symbol = ExpressionSymbol::new( + format!("@{}", ty.ty_str()), + end.clone(), + end.clone(), + None, + ); + + expr_symbol.sema_info = SymbolSemanticInfo { + ty: self + .ctx + .node_ty_map + .borrow() + .get(&self.ctx.get_node_key(&ast::AstIndex::default())) + .cloned(), + doc: None, + }; + + expr_symbol.hint = Some(SymbolHint { + pos: start.clone(), + kind: SymbolHintKind::VarHint(schema_data.get_name().to_owned()), + }); + + symbol_data.alloc_expression_symbol( + expr_symbol, + self.ctx.get_node_key(&ast::AstIndex::default()), + self.ctx.current_pkgpath.clone().unwrap(), + ); + } + } + } + } + let mut entries_range = vec![]; for entry in entries.iter() { entries_range.push(( diff --git a/kclvm/tools/src/LSP/src/inlay_hints.rs b/kclvm/tools/src/LSP/src/inlay_hints.rs index 6f7dab790..d26974b64 100644 --- a/kclvm/tools/src/LSP/src/inlay_hints.rs +++ b/kclvm/tools/src/LSP/src/inlay_hints.rs @@ -123,4 +123,9 @@ mod tests { test_schema_arg_hint, "src/test_data/inlay_hints/schema_args/schema_args_hint.k" ); + + inlay_hints_test_snapshot!( + test_schema_config_hint, + "src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k" + ); } diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap new file mode 100644 index 000000000..39aa0bb89 --- /dev/null +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap @@ -0,0 +1,31 @@ +--- +source: tools/src/LSP/src/inlay_hints.rs +assertion_line: 127 +expression: "format!(\"{:#?}\", res)" +--- +Some( + [ + InlayHint { + position: Position { + line: 3, + character: 10, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: "Name: ", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: None, + padding_right: None, + data: None, + }, + ], +) diff --git a/kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k b/kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k new file mode 100644 index 000000000..4dbefe68e --- /dev/null +++ b/kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k @@ -0,0 +1,6 @@ +schema Name: + name: str + +n: Name = { + name = "kcl" +} From 039f9ddefb9fec5c45aa1f2d2792f67a25fc4db6 Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Wed, 28 Aug 2024 15:27:23 +0530 Subject: [PATCH 4/7] add schema hint Signed-off-by: shruti2522 --- kclvm/sema/src/advanced_resolver/node.rs | 2 +- kclvm/sema/src/core/symbol.rs | 1 + kclvm/tools/src/LSP/src/inlay_hints.rs | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index ae8ea3648..fd873135d 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -1948,7 +1948,7 @@ impl<'ctx> AdvancedResolver<'ctx> { expr_symbol.hint = Some(SymbolHint { pos: start.clone(), - kind: SymbolHintKind::VarHint(schema_data.get_name().to_owned()), + kind: SymbolHintKind::SchemaHint(schema_data.get_name().to_owned()), }); symbol_data.alloc_expression_symbol( diff --git a/kclvm/sema/src/core/symbol.rs b/kclvm/sema/src/core/symbol.rs index 8b7d20a9d..573b039a3 100644 --- a/kclvm/sema/src/core/symbol.rs +++ b/kclvm/sema/src/core/symbol.rs @@ -99,6 +99,7 @@ pub struct SymbolHint { pub enum SymbolHintKind { TypeHint(String), VarHint(String), + SchemaHint(String), } impl SymbolData { diff --git a/kclvm/tools/src/LSP/src/inlay_hints.rs b/kclvm/tools/src/LSP/src/inlay_hints.rs index d26974b64..06b77acf4 100644 --- a/kclvm/tools/src/LSP/src/inlay_hints.rs +++ b/kclvm/tools/src/LSP/src/inlay_hints.rs @@ -89,6 +89,13 @@ fn get_hint_label(hint: &SymbolHint) -> (InlayHintLabelPart, LspPosition) { }, lsp_pos(&hint.pos), ), + SymbolHintKind::SchemaHint(schema) => ( + InlayHintLabelPart { + value: format!("{schema} "), + ..Default::default() + }, + lsp_pos(&hint.pos), + ), } } From 38757d59d625cad8d045e8d576f468a157780040 Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Wed, 28 Aug 2024 15:49:19 +0530 Subject: [PATCH 5/7] updated snap Signed-off-by: shruti2522 --- ...nguage_server__inlay_hints__tests__schema_config_hint.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap index 39aa0bb89..ee245f9b0 100644 --- a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap @@ -1,6 +1,6 @@ --- source: tools/src/LSP/src/inlay_hints.rs -assertion_line: 127 +assertion_line: 134 expression: "format!(\"{:#?}\", res)" --- Some( @@ -13,7 +13,7 @@ Some( label: LabelParts( [ InlayHintLabelPart { - value: "Name: ", + value: "Name ", tooltip: None, location: None, command: None, From f080459faf57ef2cac4c40e331a51503ff199590 Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Wed, 28 Aug 2024 18:10:22 +0530 Subject: [PATCH 6/7] reduce complexity of with_hint Signed-off-by: shruti2522 --- kclvm/sema/src/advanced_resolver/node.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/kclvm/sema/src/advanced_resolver/node.rs b/kclvm/sema/src/advanced_resolver/node.rs index fd873135d..46511a775 100644 --- a/kclvm/sema/src/advanced_resolver/node.rs +++ b/kclvm/sema/src/advanced_resolver/node.rs @@ -867,7 +867,7 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { } fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result { - let (start, _end) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); + let (start, _) = (self.ctx.start_pos.clone(), self.ctx.end_pos.clone()); let pre_pos = Position { filename: start.filename.clone(), @@ -875,14 +875,10 @@ impl<'ctx> MutSelfTypedResultWalker<'ctx> for AdvancedResolver<'ctx> { column: start.column.map(|c| if c >= 1 { c - 1 } else { 0 }), }; - let with_hint = if let Some(stmt) = self.ctx.program.pos_to_stmt(&pre_pos) { - match stmt.node { - ast::Stmt::Assign(ref assign_stmt) => assign_stmt.ty.is_some(), - _ => false, - } - } else { - false - }; + let with_hint = self.ctx.program.pos_to_stmt(&pre_pos).map_or( + false, + |stmt| matches!(stmt.node, ast::Stmt::Assign(assign_stmt) if assign_stmt.ty.is_some()), + ); self.walk_config_entries_with_hint(&config_expr.items, with_hint)?; Ok(None) From 6da4b754272b09056608c12c1c6d9aaaac1aebad Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Wed, 28 Aug 2024 18:31:26 +0530 Subject: [PATCH 7/7] add test for nested schema hints Signed-off-by: shruti2522 --- ...nlay_hints__tests__schema_config_hint.snap | 46 ++++++++++++++++++- .../schema_config_hint/schema_config_hint.k | 18 +++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap index ee245f9b0..9bf3f38ff 100644 --- a/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap +++ b/kclvm/tools/src/LSP/src/snapshots/kcl_language_server__inlay_hints__tests__schema_config_hint.snap @@ -8,7 +8,51 @@ Some( InlayHint { position: Position { line: 3, - character: 10, + character: 13, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: "People ", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: None, + padding_right: None, + data: None, + }, + InlayHint { + position: Position { + line: 16, + character: 13, + }, + label: LabelParts( + [ + InlayHintLabelPart { + value: "Config ", + tooltip: None, + location: None, + command: None, + }, + ], + ), + kind: None, + text_edits: None, + tooltip: None, + padding_left: None, + padding_right: None, + data: None, + }, + InlayHint { + position: Position { + line: 14, + character: 12, }, label: LabelParts( [ diff --git a/kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k b/kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k index 4dbefe68e..82d1ff36d 100644 --- a/kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k +++ b/kclvm/tools/src/LSP/src/test_data/inlay_hints/schema_config_hint/schema_config_hint.k @@ -1,6 +1,20 @@ -schema Name: +schema People: name: str -n: Name = { +n1: People = { name = "kcl" } + +schema Name: + name: str + config: Config + +schema Config: + count: int + +n2: Name = { + name = "foo" + config: { + count: 1 + } +}