From 45100d6d169d2abffdce51292308b9d459ebd409 Mon Sep 17 00:00:00 2001 From: Luyan Zhou <78394824+BlueQuantumx@users.noreply.github.com> Date: Sun, 14 Sep 2025 09:49:13 +0800 Subject: [PATCH] fix: correct the lexical hierarchy of comment group (#2116) Hierarchy of comment group is always wrong previously, causing #2021. This PR fixed it and added a test case. BTW, the changes in the lexical hierarchy module influenced a folding range test case, but it's just changed the order of items, which doesn't affect the correctness (the order is even more correct). --- .../snaps/test@symbol-after-comments.typ.snap | 25 ++++++++++++++++ .../symbol-after-comments.typ | 10 +++++++ .../folding_range/snaps/test@comment.typ.snap | 29 +++++++++---------- .../src/syntax/lexical_hierarchy.rs | 2 ++ 4 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 crates/tinymist-query/src/fixtures/document_symbols/snaps/test@symbol-after-comments.typ.snap create mode 100644 crates/tinymist-query/src/fixtures/document_symbols/symbol-after-comments.typ diff --git a/crates/tinymist-query/src/fixtures/document_symbols/snaps/test@symbol-after-comments.typ.snap b/crates/tinymist-query/src/fixtures/document_symbols/snaps/test@symbol-after-comments.typ.snap new file mode 100644 index 00000000..7098de79 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/document_symbols/snaps/test@symbol-after-comments.typ.snap @@ -0,0 +1,25 @@ +--- +source: crates/tinymist-query/src/document_symbol.rs +expression: "JsonRepr::new_redacted(result.unwrap(), &REDACT_LOC)" +input_file: crates/tinymist-query/src/fixtures/document_symbols/symbol-after-comments.typ +--- +[ + { + "kind": 13, + "name": "symbol_after_comment", + "range": "1:5:1:25", + "selectionRange": "1:5:1:25" + }, + { + "kind": 13, + "name": "symbol_after_line_comments", + "range": "5:5:5:31", + "selectionRange": "5:5:5:31" + }, + { + "kind": 13, + "name": "symbol_after_block_comment", + "range": "9:5:9:31", + "selectionRange": "9:5:9:31" + } +] diff --git a/crates/tinymist-query/src/fixtures/document_symbols/symbol-after-comments.typ b/crates/tinymist-query/src/fixtures/document_symbols/symbol-after-comments.typ new file mode 100644 index 00000000..74b740b4 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/document_symbols/symbol-after-comments.typ @@ -0,0 +1,10 @@ +// This is a comment +#let symbol_after_comment = 0 + +// This is a group +// of line comments +#let symbol_after_line_comments = 1 + +/* This is a + * block comment */ +#let symbol_after_block_comment = 2 diff --git a/crates/tinymist-query/src/fixtures/folding_range/snaps/test@comment.typ.snap b/crates/tinymist-query/src/fixtures/folding_range/snaps/test@comment.typ.snap index 59e34b0d..4aa214e6 100644 --- a/crates/tinymist-query/src/fixtures/folding_range/snaps/test@comment.typ.snap +++ b/crates/tinymist-query/src/fixtures/folding_range/snaps/test@comment.typ.snap @@ -2,18 +2,9 @@ source: crates/tinymist-query/src/folding_range.rs expression: "JsonRepr::new_pure(json!({ \"false\": result_false, \"true\": result_true, }))" input_file: crates/tinymist-query/src/fixtures/folding_range/comment.typ -snapshot_kind: text --- { "false": [ - { - "collapsedText": "", - "endCharacter": 14, - "endLine": 8, - "kind": "comment", - "startCharacter": 2, - "startLine": 3 - }, { "collapsedText": "", "endCharacter": 27, @@ -21,20 +12,28 @@ snapshot_kind: text "kind": "comment", "startCharacter": 0, "startLine": 0 + }, + { + "collapsedText": "", + "endCharacter": 14, + "endLine": 8, + "kind": "comment", + "startCharacter": 2, + "startLine": 3 } ], "true": [ - { - "collapsedText": "", - "endLine": 8, - "kind": "comment", - "startLine": 3 - }, { "collapsedText": "", "endLine": 1, "kind": "comment", "startLine": 0 + }, + { + "collapsedText": "", + "endLine": 8, + "kind": "comment", + "startLine": 3 } ] } diff --git a/crates/tinymist-query/src/syntax/lexical_hierarchy.rs b/crates/tinymist-query/src/syntax/lexical_hierarchy.rs index a4cf44c1..22343ada 100644 --- a/crates/tinymist-query/src/syntax/lexical_hierarchy.rs +++ b/crates/tinymist-query/src/syntax/lexical_hierarchy.rs @@ -297,6 +297,8 @@ impl LexicalHierarchyWorker { }, eco_vec![], )); + // Push the lexical node to the children of the current top of the stack. + self.finish_hierarchy(); } if !Self::is_plain_token(child.kind()) {