Merge branch 'master' into intern-pool

This commit is contained in:
Techarix
2023-02-08 20:10:17 +01:00
16 changed files with 427 additions and 547 deletions

View File

@@ -53,7 +53,7 @@ fn testConvertCInclude(cimport_source: []const u8, expected: []const u8) !void {
const source: [:0]u8 = try std.fmt.allocPrintZ(allocator, "const c = {s};", .{cimport_source});
defer allocator.free(source);
var ast = try std.zig.parse(allocator, source);
var ast = try Ast.parse(allocator, source, .zig);
defer ast.deinit(allocator);
const main_tokens = ast.nodes.items(.main_token);

View File

@@ -11,9 +11,6 @@ const offsets = zls.offsets;
const allocator: std.mem.Allocator = std.testing.allocator;
// TODO fix references so that we can stop skipping these tests
const skip_references_tests = true;
test "references" {
try testReferences(
\\const <0> = 0;
@@ -59,7 +56,7 @@ test "references - local scope" {
\\ return <0> + bar;
\\}
);
if (skip_references_tests) return error.SkipZigTest;
if (true) return error.SkipZigTest; // TODO
try testReferences(
\\const foo = blk: {
\\ _ = blk: {
@@ -73,6 +70,32 @@ test "references - local scope" {
);
}
test "references - struct field access" {
if (true) return error.SkipZigTest; // TODO
try testReferences(
\\const S = struct {placeholder: u32 = 3};
\\pub fn foo() bool {
\\ const s: S = .{};
\\ return s.<0> == s.<0>;
\\}
);
}
test "references - struct decl access" {
try testReferences(
\\const S = struct {
\\ fn <0>() void {}
\\};
\\pub fn foo() bool {
\\ const s: S = .{};
\\ s.<0>();
\\ s.<0>();
\\ <1>();
\\}
\\fn <1>() void {}
);
}
test "references - while continue expression" {
try testReferences(
\\ pub fn foo() void {
@@ -83,7 +106,7 @@ test "references - while continue expression" {
}
test "references - label" {
if (skip_references_tests) return error.SkipZigTest;
if (true) return error.SkipZigTest; // TODO
try testReferences(
\\const foo = <0>: {
\\ break :<0> 0;
@@ -106,6 +129,8 @@ fn testReferences(source: []const u8) !void {
try ctx.requestDidOpen(file_uri, phr.new_source);
try std.testing.expect(phr.locations.len != 0);
var i: usize = 0;
while (i < phr.locations.len) : (i += 1) {
const var_loc = phr.locations.items(.old)[i];
@@ -120,6 +145,14 @@ fn testReferences(source: []const u8) !void {
const response = try ctx.requestGetResponse(?[]types.Location, "textDocument/references", params);
var error_builder = ErrorBuilder.init(allocator, phr.new_source);
defer error_builder.deinit();
errdefer {
const note_loc = phr.locations.items(.new)[i];
error_builder.msgAtLoc("asked for references here", note_loc, .info, .{}) catch {};
error_builder.writeDebug();
}
const locations: []types.Location = response.result orelse {
std.debug.print("Server returned `null` as the result\n", .{});
return error.InvalidResponse;
@@ -150,14 +183,6 @@ fn testReferences(source: []const u8) !void {
};
defer allocator.free(expected_locs);
var error_builder = ErrorBuilder.init(allocator, phr.new_source);
defer error_builder.deinit();
errdefer {
const note_loc = phr.locations.items(.new)[i];
error_builder.msgAtLoc("asked for references here", note_loc, .info, .{}) catch {};
error_builder.writeDebug();
}
// keeps track of expected locations that have been given by the server
// used to detect double references and missing references
var visited = try std.DynamicBitSetUnmanaged.initEmpty(allocator, expected_locs.len);

View File

@@ -47,7 +47,7 @@ fn testNodesAtLoc(source: []const u8) !void {
const new_source = try allocator.dupeZ(u8, ccp.new_source);
defer allocator.free(new_source);
var tree = try std.zig.parse(allocator, new_source);
var tree = try std.zig.Ast.parse(allocator, new_source, .zig);
defer tree.deinit(allocator);
const nodes = try ast.nodesAtLoc(allocator, tree, inner_loc);

View File

@@ -4,6 +4,8 @@ const zls = @import("zls");
const types = zls.types;
const offsets = zls.offsets;
const Ast = std.zig.Ast;
test "offsets - index <-> Position" {
try testIndexPosition("", 0, 0, .{ 0, 0, 0 });
@@ -116,8 +118,8 @@ fn testIndexPosition(text: []const u8, index: usize, line: u32, characters: [3]u
try std.testing.expectEqual(index, offsets.positionToIndex(text, position32, .@"utf-32"));
}
fn testTokenToLoc(text: [:0]const u8, token_index: std.zig.Ast.TokenIndex, start: usize, end: usize) !void {
var tree = try std.zig.parse(std.testing.allocator, text);
fn testTokenToLoc(text: [:0]const u8, token_index: Ast.TokenIndex, start: usize, end: usize) !void {
var tree = try Ast.parse(std.testing.allocator, text, .zig);
defer tree.deinit(std.testing.allocator);
const actual = offsets.tokenToLoc(tree, token_index);