From 13bea0ba8da01074839460fe7b8edd1c69311575 Mon Sep 17 00:00:00 2001 From: He1pa <56333845+He1pa@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:16:34 +0800 Subject: [PATCH] fix: fix config if expr pos (#1027) Signed-off-by: he1pa <18012015693@163.com> --- kclvm/parser/src/parser/expr.rs | 5 ++++- ...s__error_recovery__config_recovery_13.snap | 2 +- ...s__error_recovery__config_recovery_14.snap | 2 +- kclvm/tools/src/LSP/src/hover.rs | 20 +++++++++++++++++++ .../src/LSP/src/test_data/hover_test/hover.k | 16 +++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/kclvm/parser/src/parser/expr.rs b/kclvm/parser/src/parser/expr.rs index dce61e7b3..b300f2c42 100644 --- a/kclvm/parser/src/parser/expr.rs +++ b/kclvm/parser/src/parser/expr.rs @@ -1563,6 +1563,7 @@ impl<'a> Parser<'a> { // elif ... let mut need_skip_newlines = false; let mut elif_list = Vec::new(); + let mut last_token = self.token; loop { if !self.token.is_keyword(kw::Elif) { break; @@ -1589,6 +1590,7 @@ impl<'a> Parser<'a> { x, self.sess.struct_token_loc(token, self.prev_token), ))); + last_token = self.prev_token; } if let TokenKind::Newline = self.token.kind { @@ -1618,6 +1620,7 @@ impl<'a> Parser<'a> { Expr::Config(orelse), self.sess.struct_token_loc(token, self.prev_token), )); + last_token = self.prev_token; if_entry.node.orelse = Some(t); } @@ -1648,7 +1651,7 @@ impl<'a> Parser<'a> { Box::new(Node::node( Expr::ConfigIfEntry(if_entry.node), - self.sess.struct_token_loc(token, self.prev_token), + self.sess.struct_token_loc(token, last_token), )) } diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap index e70cbed70..858f13649 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_13.snap @@ -81,7 +81,7 @@ Node { line: 1, column: 1, end_line: 1, - end_column: 17, + end_column: 18, }, operation: Union, insert_index: -1, diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap index d759b5295..339870ddd 100644 --- a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_14.snap @@ -82,7 +82,7 @@ Node { line: 1, column: 1, end_line: 1, - end_column: 17, + end_column: 18, }, operation: Union, insert_index: -1, diff --git a/kclvm/tools/src/LSP/src/hover.rs b/kclvm/tools/src/LSP/src/hover.rs index 867eb264d..686bfb91c 100644 --- a/kclvm/tools/src/LSP/src/hover.rs +++ b/kclvm/tools/src/LSP/src/hover.rs @@ -450,6 +450,26 @@ mod tests { } } + #[test] + #[bench_test] + fn expr_after_config_if_hover() { + let (file, program, _, _, gs) = compile_test_file("src/test_data/hover_test/hover.k"); + let pos = KCLPos { + filename: file.clone(), + line: 41, + column: Some(13), + }; + let got = hover(&program, &pos, &gs).unwrap(); + match got.contents { + lsp_types::HoverContents::Scalar(marked_string) => { + if let MarkedString::String(s) = marked_string { + assert_eq!(s, "stratege: str"); + } + } + _ => unreachable!("test error"), + } + } + #[test] #[bench_test] fn schema_scope_variable_hover() { diff --git a/kclvm/tools/src/LSP/src/test_data/hover_test/hover.k b/kclvm/tools/src/LSP/src/test_data/hover_test/hover.k index db7a0f73d..e1c613e82 100644 --- a/kclvm/tools/src/LSP/src/test_data/hover_test/hover.k +++ b/kclvm/tools/src/LSP/src/test_data/hover_test/hover.k @@ -26,3 +26,19 @@ print(1) a = "".capitalize() b = a.capitalize() + +schema Deployment: + spec: DeploymentSpec + +schema DeploymentSpec: + stratege: str + replicas?: int + +d = Deployment{ + spec: DeploymentSpec { + if True: + replicas = 1 + stratege: "a" + } +} + \ No newline at end of file