Skip to content

Commit 5b5dd6d

Browse files
authored
move hover doc comments to the bottom (#2302)
If the every line in the doc comment is empty or starts with a space, one space characters will be trimmed from the beginning of every line.
1 parent 0d77561 commit 5b5dd6d

4 files changed

Lines changed: 48 additions & 31 deletions

File tree

src/analysis.zig

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,27 @@ pub fn collectDocComments(allocator: std.mem.Allocator, tree: Ast, doc_comments:
131131
var lines: std.ArrayListUnmanaged([]const u8) = .empty;
132132
defer lines.deinit(allocator);
133133

134+
var lines_start_with_space = true;
135+
134136
var curr_line_tok = doc_comments;
135137
while (true) : (curr_line_tok += 1) {
136138
const comm = tree.tokenTag(curr_line_tok);
137139
if ((container_doc and comm == .container_doc_comment) or (!container_doc and comm == .doc_comment)) {
138-
try lines.append(allocator, tree.tokenSlice(curr_line_tok)[3..]);
140+
const line = tree.tokenSlice(curr_line_tok)[3..];
141+
if (line.len > 1 and line[0] != ' ') lines_start_with_space = false;
142+
try lines.append(allocator, line);
139143
} else break;
140144
}
141145

146+
// If all of the lines that aren't empty start with a space, remove the first space
147+
if (lines_start_with_space) {
148+
for (lines.items, 0..) |line, i| {
149+
if (line.len > 1 and line[0] == ' ') {
150+
lines.items[i] = line[1..];
151+
}
152+
}
153+
}
154+
142155
return try std.mem.join(allocator, "\n", lines.items);
143156
}
144157

src/features/hover.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ fn hoverSymbolResolved(
128128
var hover_text: std.ArrayListUnmanaged(u8) = .empty;
129129
const writer = hover_text.writer(arena);
130130
if (markup_kind == .markdown) {
131-
for (doc_strings) |doc|
132-
try writer.print("{s}\n\n", .{doc});
133131
try writer.print("```zig\n{s}\n```\n```zig\n({s})\n```", .{ def_str, resolved_type_str });
134132
if (referenced_types.len > 0)
135133
try writer.print("\n\n" ++ "Go to ", .{});
@@ -141,11 +139,17 @@ fn hoverSymbolResolved(
141139
try writer.print("[{s}]({s}#L{d})", .{ ref.str, ref.handle.uri, line });
142140
}
143141
} else {
144-
for (doc_strings) |doc|
145-
try writer.print("{s}\n\n", .{doc});
146142
try writer.print("{s}\n({s})", .{ def_str, resolved_type_str });
147143
}
148144

145+
if (doc_strings.len > 0) {
146+
try writer.writeAll("\n\n");
147+
for (doc_strings, 0..) |doc, i| {
148+
try writer.writeAll(doc);
149+
if (i != doc_strings.len - 1) try writer.writeAll("\n\n");
150+
}
151+
}
152+
149153
return hover_text.items;
150154
}
151155

tests/lsp_features/completion.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,9 +3167,9 @@ test "top-level doc comment" {
31673167
.kind = .Struct,
31683168
.detail = "type",
31693169
.documentation =
3170-
\\ A
3170+
\\A
31713171
\\
3172-
\\ B
3172+
\\B
31733173
,
31743174
},
31753175
});

tests/lsp_features/hover.zig

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ test "struct" {
274274
\\ };
275275
\\};
276276
,
277-
\\ Foo doc comment
278-
\\
279277
\\```zig
280278
\\const FooStruct = struct {
281279
\\ bar: u32,
@@ -286,6 +284,8 @@ test "struct" {
286284
\\```zig
287285
\\(type)
288286
\\```
287+
\\
288+
\\Foo doc comment
289289
);
290290
try testHover(
291291
\\const Edge<cursor>Cases = struct {
@@ -691,14 +691,14 @@ test "function parameter" {
691691
\\ return a;
692692
\\}
693693
,
694-
\\ hello world
695-
\\
696694
\\```zig
697695
\\a: u32
698696
\\```
699697
\\```zig
700698
\\(u32)
701699
\\```
700+
\\
701+
\\hello world
702702
);
703703
}
704704

@@ -748,23 +748,23 @@ test "either types" {
748748
\\const either = if (undefined) A else B;
749749
\\const bar = either.<cursor>T;
750750
,
751-
\\small type
752-
\\
753751
\\```zig
754752
\\const T = u32
755753
\\```
756754
\\```zig
757755
\\(type)
758756
\\```
759757
\\
760-
\\large type
758+
\\small type
761759
\\
762760
\\```zig
763761
\\const T = u64
764762
\\```
765763
\\```zig
766764
\\(type)
767765
\\```
766+
\\
767+
\\large type
768768
);
769769
try testHoverWithOptions(
770770
\\const A = struct {
@@ -778,15 +778,15 @@ test "either types" {
778778
\\const either = if (undefined) A else B;
779779
\\const bar = either.<cursor>T;
780780
,
781-
\\small type
782-
\\
783781
\\const T = u32
784782
\\(type)
785783
\\
786-
\\large type
784+
\\small type
787785
\\
788786
\\const T = u64
789787
\\(type)
788+
\\
789+
\\large type
790790
, .{ .markup_kind = .plaintext });
791791
}
792792

@@ -795,14 +795,14 @@ test "var decl comments" {
795795
\\///this is a comment
796796
\\const f<cursor>oo = 0 + 0;
797797
,
798-
\\this is a comment
799-
\\
800798
\\```zig
801799
\\const foo = 0 + 0
802800
\\```
803801
\\```zig
804802
\\(comptime_int)
805803
\\```
804+
\\
805+
\\this is a comment
806806
);
807807
}
808808

@@ -931,16 +931,16 @@ test "combine doc comments of declaration and definition" {
931931
\\ const baz = struct {};
932932
\\};
933933
,
934-
\\ Foo
935-
\\
936-
\\ Bar
937-
\\
938934
\\```zig
939935
\\const baz = struct
940936
\\```
941937
\\```zig
942938
\\(type)
943939
\\```
940+
\\
941+
\\Foo
942+
\\
943+
\\Bar
944944
);
945945
try testHoverWithOptions(
946946
\\/// Foo
@@ -950,12 +950,12 @@ test "combine doc comments of declaration and definition" {
950950
\\ const baz = struct {};
951951
\\};
952952
,
953-
\\ Foo
954-
\\
955-
\\ Bar
956-
\\
957953
\\const baz = struct
958954
\\(type)
955+
\\
956+
\\Foo
957+
\\
958+
\\Bar
959959
, .{ .markup_kind = .plaintext });
960960
}
961961

@@ -966,16 +966,16 @@ test "top-level doc comment" {
966966
\\/// A
967967
\\const S<cursor>elf = @This();
968968
,
969-
\\ A
970-
\\
971-
\\ B
972-
\\
973969
\\```zig
974970
\\const Self = @This()
975971
\\```
976972
\\```zig
977973
\\(type)
978974
\\```
975+
\\
976+
\\A
977+
\\
978+
\\B
979979
);
980980
}
981981

0 commit comments

Comments
 (0)