From 97f7fd77c61088d8e501b0927b4d8b254fda42ca Mon Sep 17 00:00:00 2001 From: Techarix <19954306+Techatrix@users.noreply.github.com> Date: Sat, 25 Feb 2023 01:30:47 +0100 Subject: [PATCH] fix folding range on structs with doc comment on first field --- src/folding_range.zig | 8 +++++++- tests/lsp_features/folding_range.zig | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/folding_range.zig b/src/folding_range.zig index 75734c7..857fac1 100644 --- a/src/folding_range.zig +++ b/src/folding_range.zig @@ -232,7 +232,13 @@ pub fn generateFoldingRanges(allocator: std.mem.Allocator, tree: Ast, encoding: const container_decl = tree.fullContainerDecl(&buffer, node).?; if (container_decl.ast.members.len != 0) { const first_member = container_decl.ast.members[0]; - const start_tok = tree.firstToken(first_member) -| 1; + var start_tok = tree.firstToken(first_member) -| 1; + while (start_tok != 0 and + (token_tags[start_tok] == .doc_comment or + token_tags[start_tok] == .container_doc_comment)) + { + start_tok -= 1; + } const end_tok = ast.lastToken(tree, node); try builder.add(null, start_tok, end_tok, .exclusive, .exclusive); } diff --git a/tests/lsp_features/folding_range.zig b/tests/lsp_features/folding_range.zig index 92be07e..8b6b63b 100644 --- a/tests/lsp_features/folding_range.zig +++ b/tests/lsp_features/folding_range.zig @@ -151,6 +151,15 @@ test "foldingRange - container decl" { , &.{ .{ .startLine = 0, .startCharacter = 20, .endLine = 3, .endCharacter = 0 }, }); + try testFoldingRange( + \\const Foo = struct { + \\ /// doc comment + \\ alpha: u32, + \\ beta: []const u8, + \\}; + , &.{ + .{ .startLine = 0, .startCharacter = 20, .endLine = 4, .endCharacter = 0 }, + }); try testFoldingRange( \\const Foo = packed struct(u32) { \\ alpha: u16,