Skip to content

Commit

Permalink
completions: add parser dependent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
llogick committed Mar 1, 2024
1 parent 2317ebe commit ca60277
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,126 @@ test "insert replace behaviour - file system completions" {
// zig fmt: on
}

// These only work with the modified parser
test "parser dependent" {
try testCompletion(
\\fn alias() void {
\\ var s = Alias{.<cursor>};
\\}
\\pub const Outer = struct {
\\ pub const Inner = struct {
\\ isf1: bool = true,
\\ isf2: bool = false,
\\ };
\\};
\\const Alias0 = Outer.Inner;
\\const Alias = Alias0;
, &.{
.{ .label = "isf1", .kind = .Field, .detail = "bool = true" },
.{ .label = "isf2", .kind = .Field, .detail = "bool = false" },
});
try testCompletion(
\\const MyStruct = struct {
\\ a: bool,
\\ b: bool,
\\ fn inside() void {
\\ var s = MyStruct{.<cursor>};
\\ }
\\};
, &.{
.{ .label = "a", .kind = .Field, .detail = "bool" },
.{ .label = "b", .kind = .Field, .detail = "bool" },
});
try testCompletion(
\\const Birdie = enum {
\\ canary,
\\};
\\const E = enum {
\\ foo,
\\ bar,
\\ fn foo(e: E) void {
\\ switch (e) {.<cursor>};
\\ }
\\};
, &.{
.{ .label = "foo", .kind = .EnumMember },
.{ .label = "bar", .kind = .EnumMember },
});
try testCompletion(
\\pub const bar = struct {
\\ pub const baz = struct {
\\ pub const Foo = struct {
\\ alpha: u32 = 0,
\\ };
\\
\\ pub fn qux() u32 {
\\ return Foo{
\\ .<cursor>
\\ };
\\ }
\\ };
\\};
, &.{
.{ .label = "alpha", .kind = .Field },
});
try testCompletion(
\\pub var state: S = undefined;
\\pub const S = struct { foo: u32 };
\\
\\pub fn main() void {
\\ state.<cursor>
\\ {
\\ _ = state.foo;
\\ }
\\ state.foo;
\\}
, &.{
.{ .label = "foo", .kind = .Field },
});
try testCompletion(
\\const Birdie = enum {
\\ canary,
\\};
\\fn foo(e: Enum) void {
\\ switch (e) {.<cursor>}
\\}
\\
\\const Enum = enum {
\\ foo,
\\};
, &.{
.{ .label = "foo", .kind = .EnumMember },
});
// TODO
// Test for `fnCall(.{.})` and `fnCall(Parser.Node{. .some})` because they are handled in different places
// Test for completions after `.{` and every `,` in the following snippet
// ```
// pub fn gamma(p: Parser) void {
// return p.addNode(.{
// .tag = 1,
// .main_token = 1,
// .data = .{
// .lhs = undefined,
// .rhs = p.addNode(Parser.Node{
// .tag,
// }),
// },
// });
// }
// const Parser = struct {
// const Node = struct {
// tag: u32,
// main_token: u32,
// data: struct {
// lhs: u32,
// rhs: u32,
// },
// };
// pub fn addNode(_: Node) u32 {}
// };
// ```
}

fn testCompletion(source: []const u8, expected_completions: []const Completion) !void {
try testCompletionWithOptions(source, expected_completions, .{});
}
Expand Down

0 comments on commit ca60277

Please sign in to comment.