From 7c5c7c5203b77ca42aa4d6b9865abcde5d68be18 Mon Sep 17 00:00:00 2001 From: peefy Date: Fri, 3 Nov 2023 15:26:12 +0800 Subject: [PATCH] fix: use char index in the compiler error message Signed-off-by: peefy --- kclvm/error/src/lib.rs | 7 +- kclvm/parser/src/tests/error_recovery.rs | 1 + ...s__error_recovery__config_recovery_15.snap | 65 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_15.snap diff --git a/kclvm/error/src/lib.rs b/kclvm/error/src/lib.rs index 745b86334..0c7a16f96 100644 --- a/kclvm/error/src/lib.rs +++ b/kclvm/error/src/lib.rs @@ -419,6 +419,7 @@ impl SessionDiagnostic for Diagnostic { let line = source.get_line((msg.range.0.line - 1) as usize); match line.as_ref() { Some(content) => { + let length = content.chars().count(); let snippet = Snippet { title: None, footer: vec![], @@ -428,12 +429,12 @@ impl SessionDiagnostic for Diagnostic { origin: Some(&msg.range.0.filename), annotations: vec![SourceAnnotation { range: match msg.range.0.column { - Some(column) if content.len() >= 1 => { + Some(column) if length >= 1 => { let column = column as usize; // If the position exceeds the length of the content, // put the annotation at the end of the line. - if column >= content.len() { - (content.len() - 1, content.len()) + if column >= length { + (length - 1, length) } else { (column, column + 1) } diff --git a/kclvm/parser/src/tests/error_recovery.rs b/kclvm/parser/src/tests/error_recovery.rs index dce35418b..1c3ab1553 100644 --- a/kclvm/parser/src/tests/error_recovery.rs +++ b/kclvm/parser/src/tests/error_recovery.rs @@ -70,6 +70,7 @@ parse_expr_snapshot! { config_recovery_11, "{if True: a = , b = 2}" } parse_expr_snapshot! { config_recovery_12, "{if True: *a, b = 2}" } parse_expr_snapshot! { config_recovery_13, "{if True: key: {}}" } parse_expr_snapshot! { config_recovery_14, "{if True: key: []}" } +parse_expr_snapshot! { config_recovery_15, "{你好" } parse_expr_snapshot! { comp_clause_recovery_0, "[i for i in [1,2,3]]" } parse_expr_snapshot! { comp_clause_recovery_1, "[i, j for i in [1,2,3]]" } parse_expr_snapshot! { comp_clause_recovery_2, "[for i in [1,2,3]]" } diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_15.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_15.snap new file mode 100644 index 000000000..6af1cafc7 --- /dev/null +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__config_recovery_15.snap @@ -0,0 +1,65 @@ +--- +source: parser/src/tests/error_recovery.rs +assertion_line: 73 +expression: "crate::tests::parsing_expr_string(\"{你好\")" +--- +Node { + node: Config( + ConfigExpr { + items: [ + Node { + node: ConfigEntry { + key: Some( + Node { + node: Identifier( + Identifier { + names: [ + Node { + node: "你好", + filename: "", + line: 1, + column: 1, + end_line: 1, + end_column: 3, + }, + ], + pkgpath: "", + ctx: Load, + }, + ), + filename: "", + line: 1, + column: 1, + end_line: 1, + end_column: 3, + }, + ), + value: Node { + node: Missing( + MissingExpr, + ), + filename: "", + line: 1, + column: 3, + end_line: 1, + end_column: 3, + }, + operation: Override, + insert_index: -1, + }, + filename: "", + line: 1, + column: 1, + end_line: 1, + end_column: 3, + }, + ], + }, + ), + filename: "", + line: 1, + column: 0, + end_line: 1, + end_column: 3, +} +