From 7d169eeb8faddc5878ac61f1ad4dacf172341006 Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Tue, 30 Jan 2024 00:21:56 +0100 Subject: [PATCH] add detail field to pointer deref and optional unwrap operations --- src/features/completions.zig | 9 ++++++++- tests/lsp_features/completion.zig | 27 +++++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/features/completions.zig b/src/features/completions.zig index 99c9169445..0f6f044d7e 100644 --- a/src/features/completions.zig +++ b/src/features/completions.zig @@ -37,6 +37,7 @@ fn typeToCompletion( try list.append(arena, .{ .label = "*", .kind = .Operator, + .detail = try std.fmt.allocPrint(arena, "{}", .{info.elem_ty.fmtTypeVal(analyser)}), .insertText = "*", .insertTextFormat = .PlainText, }); @@ -55,9 +56,14 @@ fn typeToCompletion( .insertText = "len", .insertTextFormat = .PlainText, }); + + var many_ptr_ty = ty; + many_ptr_ty.is_type_val = true; + many_ptr_ty.data.pointer.size = .Many; try list.append(arena, .{ .label = "ptr", .kind = .Field, + .detail = try std.fmt.allocPrint(arena, "const ptr: {}", .{many_ptr_ty.fmtTypeVal(analyser)}), .insertText = "ptr", .insertTextFormat = .PlainText, }); @@ -77,11 +83,12 @@ fn typeToCompletion( .insertTextFormat = .PlainText, }); }, - .optional => |_| { + .optional => |child_ty| { if (ty.is_type_val) return; try list.append(arena, .{ .label = "?", .kind = .Operator, + .detail = try std.fmt.allocPrint(arena, "{}", .{child_ty.fmtTypeVal(analyser)}), .insertText = "?", .insertTextFormat = .PlainText, }); diff --git a/tests/lsp_features/completion.zig b/tests/lsp_features/completion.zig index d31d9cb35c..49cc48eca4 100644 --- a/tests/lsp_features/completion.zig +++ b/tests/lsp_features/completion.zig @@ -273,8 +273,7 @@ test "completion - std.ArrayHashMap" { \\const foo = key.?. , &.{ .{ .label = "len", .kind = .Field, .detail = "const len: usize" }, - // TODO detail should be 'const ptr: [*]const u8' - .{ .label = "ptr", .kind = .Field }, + .{ .label = "ptr", .kind = .Field, .detail = "const ptr: [*]const u8" }, }); try testCompletion( \\const std = @import("std"); @@ -292,8 +291,7 @@ test "completion - std.ArrayHashMap" { \\const gop = try map.getOrPut(0); \\const foo = gop.value_ptr. , &.{ - // TODO detail should be 'u32' - .{ .label = "*", .kind = .Operator }, + .{ .label = "*", .kind = .Operator, .detail = "S" }, .{ .label = "alpha", .kind = .Field, .detail = "alpha: u32" }, }); } @@ -307,8 +305,7 @@ test "completion - std.HashMap" { \\const foo = key.?. , &.{ .{ .label = "len", .kind = .Field, .detail = "const len: usize" }, - // TODO detail should be 'const ptr: [*]const u8' - .{ .label = "ptr", .kind = .Field }, + .{ .label = "ptr", .kind = .Field, .detail = "const ptr: [*]const u8" }, }); try testCompletion( \\const std = @import("std"); @@ -326,8 +323,7 @@ test "completion - std.HashMap" { \\const gop = try map.getOrPut(0); \\const foo = gop.value_ptr. , &.{ - // TODO detail should be 'u32' - .{ .label = "*", .kind = .Operator }, + .{ .label = "*", .kind = .Operator, .detail = "S" }, .{ .label = "alpha", .kind = .Field, .detail = "alpha: u32" }, }); } @@ -387,8 +383,7 @@ test "completion - optional" { \\const foo: ?u32 = undefined; \\const bar = foo. , &.{ - // TODO detail should be 'u32' - .{ .label = "?", .kind = .Operator }, + .{ .label = "?", .kind = .Operator, .detail = "u32" }, }); try testCompletion( @@ -412,8 +407,7 @@ test "completion - pointer" { \\const foo: *u32 = undefined; \\const bar = foo. , &.{ - // TODO detail should be 'u32' - .{ .label = "*", .kind = .Operator }, + .{ .label = "*", .kind = .Operator, .detail = "u32" }, }); try testCompletion( @@ -429,8 +423,7 @@ test "completion - pointer" { \\const bar = foo. , &.{ .{ .label = "len", .kind = .Field, .detail = "const len: usize" }, - // TODO detail should be 'const ptr: [*]const u8' - .{ .label = "ptr", .kind = .Field }, + .{ .label = "ptr", .kind = .Field, .detail = "const ptr: [*]const u8" }, }); try testCompletion( @@ -446,8 +439,7 @@ test "completion - pointer" { \\const foo: *S = undefined; \\const bar = foo. , &.{ - // TODO detail should be 'S' - .{ .label = "*", .kind = .Operator }, + .{ .label = "*", .kind = .Operator, .detail = "S" }, .{ .label = "alpha", .kind = .Field, .detail = "alpha: u32" }, }); @@ -553,8 +545,7 @@ test "completion - single pointer to array" { \\const foo: *[3]u32 = undefined; \\const bar = foo. , &.{ - // TODO detail should be '[3]u32' - .{ .label = "*", .kind = .Operator }, + .{ .label = "*", .kind = .Operator, .detail = "[3]u32" }, .{ .label = "len", .kind = .Field, .detail = "const len: usize = 3" }, }); try testCompletion(