Skip to content

Commit

Permalink
add detail field to pointer deref and optional unwrap operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Jan 29, 2024
1 parent b1b4e53 commit 7d169ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
9 changes: 8 additions & 1 deletion src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -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,
});
Expand Down
27 changes: 9 additions & 18 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ test "completion - std.ArrayHashMap" {
\\const foo = key.?.<cursor>
, &.{
.{ .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");
Expand All @@ -292,8 +291,7 @@ test "completion - std.ArrayHashMap" {
\\const gop = try map.getOrPut(0);
\\const foo = gop.value_ptr.<cursor>
, &.{
// TODO detail should be 'u32'
.{ .label = "*", .kind = .Operator },
.{ .label = "*", .kind = .Operator, .detail = "S" },
.{ .label = "alpha", .kind = .Field, .detail = "alpha: u32" },
});
}
Expand All @@ -307,8 +305,7 @@ test "completion - std.HashMap" {
\\const foo = key.?.<cursor>
, &.{
.{ .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");
Expand All @@ -326,8 +323,7 @@ test "completion - std.HashMap" {
\\const gop = try map.getOrPut(0);
\\const foo = gop.value_ptr.<cursor>
, &.{
// TODO detail should be 'u32'
.{ .label = "*", .kind = .Operator },
.{ .label = "*", .kind = .Operator, .detail = "S" },
.{ .label = "alpha", .kind = .Field, .detail = "alpha: u32" },
});
}
Expand Down Expand Up @@ -387,8 +383,7 @@ test "completion - optional" {
\\const foo: ?u32 = undefined;
\\const bar = foo.<cursor>
, &.{
// TODO detail should be 'u32'
.{ .label = "?", .kind = .Operator },
.{ .label = "?", .kind = .Operator, .detail = "u32" },
});

try testCompletion(
Expand All @@ -412,8 +407,7 @@ test "completion - pointer" {
\\const foo: *u32 = undefined;
\\const bar = foo.<cursor>
, &.{
// TODO detail should be 'u32'
.{ .label = "*", .kind = .Operator },
.{ .label = "*", .kind = .Operator, .detail = "u32" },
});

try testCompletion(
Expand All @@ -429,8 +423,7 @@ test "completion - pointer" {
\\const bar = foo.<cursor>
, &.{
.{ .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(
Expand All @@ -446,8 +439,7 @@ test "completion - pointer" {
\\const foo: *S = undefined;
\\const bar = foo.<cursor>
, &.{
// TODO detail should be 'S'
.{ .label = "*", .kind = .Operator },
.{ .label = "*", .kind = .Operator, .detail = "S" },
.{ .label = "alpha", .kind = .Field, .detail = "alpha: u32" },
});

Expand Down Expand Up @@ -553,8 +545,7 @@ test "completion - single pointer to array" {
\\const foo: *[3]u32 = undefined;
\\const bar = foo.<cursor>
, &.{
// 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(
Expand Down

0 comments on commit 7d169ee

Please sign in to comment.