Skip to content

Commit

Permalink
restrict hint whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Koranir committed Sep 26, 2023
1 parent 281871b commit f43695f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ fn writeVariableDeclHint(builder: *Builder, decl_node: Ast.Node.Index) !void {
);
if (type_str.len == 0) return;

// Restrict whitespace to only one space at a time.
var reduced_type_str = std.ArrayList(u8).init(builder.arena);
// Overallocates by a small amount if whitespace is reduced, but it should be fine.
try reduced_type_str.ensureTotalCapacity(type_str.len);
var skip = false;
for (type_str) |char| {
if (char == '\n' or char == ' ') {
if (!skip) {
try reduced_type_str.append(' ');
}
skip = true;
continue;
}
skip = false;
try reduced_type_str.append(char);
}
type_str = reduced_type_str.items;

try builder.hints.append(builder.arena, .{
.index = offsets.tokenToLoc(tree, hint.ast.mut_token + 1).end,
.label = try std.fmt.allocPrint(builder.arena, ": {s}", .{
Expand Down
22 changes: 22 additions & 0 deletions tests/lsp_features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ test "inlayhints - var decl" {
\\ }
\\}
, .Type);
try testInlayHints(
\\ fn thing(a: u32, b: i32) struct {
\\ a: u32,
\\ b: i32,
\\ c: struct {
\\ d: usize,
\\ e: []const u8,
\\ },
\\ } {
\\ return .{
\\ .a = a,
\\ .b = b,
\\ .c = .{
\\ .d = 0,
\\ .e = "Testing",
\\ }
\\ };
\\ }
\\
\\ var a<struct { a: u32, b: i32, c: struct { d: usize, e: []const u8, }, }> = thing(10, -4);
\\ _ = a;
, .Type);
}

fn testInlayHints(source: []const u8, kind: types.InlayHintKind) !void {
Expand Down

0 comments on commit f43695f

Please sign in to comment.