Skip to content

Commit

Permalink
Branching type resolution (#1031)
Browse files Browse the repository at this point in the history
* Branching type resolution

* Add condition information to completions (borked rn i give up)

* Fix completion conditional descriptor

* Multi gotodef

* Multi hover

* Reenable references

* Fix getAllTypesWithHandles
  • Loading branch information
SuperAuguste authored Mar 7, 2023
1 parent 515a0e4 commit 2ce59a3
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 155 deletions.
191 changes: 136 additions & 55 deletions src/Server.zig

Large diffs are not rendered by default.

286 changes: 193 additions & 93 deletions src/analysis.zig

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn handleUnusedFunctionParameter(builder: *Builder, actions: *std.ArrayListUnman
const token_starts = tree.tokens.items(.start);

const decl = (try analysis.lookupSymbolGlobal(
builder.arena,
builder.document_store,
builder.handle,
identifier_name,
Expand Down Expand Up @@ -134,6 +135,7 @@ fn handleUnusedVariableOrConstant(builder: *Builder, actions: *std.ArrayListUnma
const token_starts = tree.tokens.items(.start);

const decl = (try analysis.lookupSymbolGlobal(
builder.arena,
builder.document_store,
builder.handle,
identifier_name,
Expand Down
7 changes: 4 additions & 3 deletions src/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn writeCallHint(builder: *Builder, call: Ast.full.Call, decl_handle: analysis.D
var i: usize = 0;
var it = fn_proto.iterate(&decl_tree);

if (try analysis.hasSelfParam(builder.store, decl_handle.handle, fn_proto)) {
if (try analysis.hasSelfParam(builder.arena, builder.store, decl_handle.handle, fn_proto)) {
_ = ast.nextFnParam(&it);
}

Expand Down Expand Up @@ -177,7 +177,7 @@ fn writeCallNodeHint(builder: *Builder, call: Ast.full.Call) !void {
const source_index = offsets.tokenToIndex(tree, main_tokens[call.ast.fn_expr]);
const name = offsets.tokenToSlice(tree, main_tokens[call.ast.fn_expr]);

if (try analysis.lookupSymbolGlobal(builder.store, handle, name, source_index)) |decl_handle| {
if (try analysis.lookupSymbolGlobal(builder.arena, builder.store, handle, name, source_index)) |decl_handle| {
try writeCallHint(builder, call, decl_handle);
}
},
Expand All @@ -194,11 +194,12 @@ fn writeCallNodeHint(builder: *Builder, call: Ast.full.Call) !void {

// note: we have the ast node, traversing it would probably yield better results
// than trying to re-tokenize and re-parse it
if (try analysis.getFieldAccessType(builder.store, handle, rhs_loc.end, &tokenizer)) |result| {
if (try analysis.getFieldAccessType(builder.arena, builder.store, handle, rhs_loc.end, &tokenizer)) |result| {
const container_handle = result.unwrapped orelse result.original;
switch (container_handle.type.data) {
.other => |container_handle_node| {
if (try analysis.lookupSymbolContainer(
builder.arena,
builder.store,
.{ .node = container_handle_node, .handle = container_handle.handle },
tree.tokenSlice(rhsToken),
Expand Down
4 changes: 4 additions & 0 deletions src/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const Builder = struct {
const identifier_token = main_tokens[node];

const child = (try analysis.lookupSymbolGlobal(
builder.allocator,
builder.store,
handle,
offsets.tokenToSlice(handle.tree, identifier_token),
Expand All @@ -113,8 +114,10 @@ const Builder = struct {
var bound_type_params = analysis.BoundTypeParams{};
defer bound_type_params.deinit(builder.store.allocator);
const left_type = try analysis.resolveFieldAccessLhsType(
builder.allocator,
builder.store,
(try analysis.resolveTypeOfNodeInternal(
builder.allocator,
builder.store,
.{ .node = datas[node].lhs, .handle = handle },
&bound_type_params,
Expand All @@ -128,6 +131,7 @@ const Builder = struct {
};

const child = (try analysis.lookupSymbolContainer(
self.builder.allocator,
builder.store,
.{ .node = left_type_node, .handle = left_type.handle },
offsets.tokenToSlice(handle.tree, datas[node].rhs),
Expand Down
11 changes: 8 additions & 3 deletions src/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
try writeToken(builder, var_decl.comptime_token, .keyword);
try writeToken(builder, var_decl.ast.mut_token, .keyword);

if (try analysis.resolveTypeOfNode(builder.store, .{ .node = node, .handle = handle })) |decl_type| {
if (try analysis.resolveTypeOfNode(allocator, builder.store, .{ .node = node, .handle = handle })) |decl_type| {
try colorIdentifierBasedOnType(builder, decl_type, var_decl.ast.mut_token + 1, .{ .declaration = true });
} else {
try writeTokenMod(builder, var_decl.ast.mut_token + 1, .variable, .{ .declaration = true });
Expand Down Expand Up @@ -417,6 +417,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
}

if (try analysis.lookupSymbolGlobal(
allocator,
builder.store,
handle,
name,
Expand All @@ -428,7 +429,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
var bound_type_params = analysis.BoundTypeParams{};
defer bound_type_params.deinit(builder.store.allocator);

if (try child.resolveType(builder.store, &bound_type_params)) |decl_type| {
if (try child.resolveType(allocator, builder.store, &bound_type_params)) |decl_type| {
try colorIdentifierBasedOnType(builder, decl_type, main_token, .{});
} else {
try writeTokenMod(builder, main_token, .variable, .{});
Expand Down Expand Up @@ -659,6 +660,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
try callWriteNodeTokens(allocator, .{ builder, struct_init.ast.type_expr });

field_token_type = if (try analysis.resolveTypeOfNode(
allocator,
builder.store,
.{ .node = struct_init.ast.type_expr, .handle = handle },
)) |struct_type| switch (struct_type.type.data) {
Expand Down Expand Up @@ -878,8 +880,10 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
defer bound_type_params.deinit(builder.store.allocator);

const lhs_type = try analysis.resolveFieldAccessLhsType(
allocator,
builder.store,
(try analysis.resolveTypeOfNodeInternal(
allocator,
builder.store,
.{ .node = data.lhs, .handle = handle },
&bound_type_params,
Expand All @@ -891,6 +895,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
else => return,
};
if (try analysis.lookupSymbolContainer(
allocator,
builder.store,
.{ .node = left_type_node, .handle = lhs_type.handle },
tree.tokenSlice(data.rhs),
Expand All @@ -915,7 +920,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
else => {},
}

if (try decl_type.resolveType(builder.store, &bound_type_params)) |resolved_type| {
if (try decl_type.resolveType(allocator, builder.store, &bound_type_params)) |resolved_type| {
try colorIdentifierBasedOnType(builder, resolved_type, data.rhs, .{});
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/signature_help.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn fnProtoToSignatureInfo(document_store: *DocumentStore, alloc: std.mem.Allocat
const proto_comments = (try analysis.getDocComments(alloc, tree, fn_node, .markdown)) orelse "";

const arg_idx = if (skip_self_param) blk: {
const has_self_param = try analysis.hasSelfParam(document_store, handle, proto);
const has_self_param = try analysis.hasSelfParam(alloc, document_store, handle, proto);
break :blk commas + @boolToInt(has_self_param);
} else commas;

Expand Down Expand Up @@ -257,6 +257,7 @@ pub fn getSignatureInfo(document_store: *DocumentStore, alloc: std.mem.Allocator
// Resolve the expression.
var tokenizer = std.zig.Tokenizer.init(held_expr);
if (try analysis.getFieldAccessType(
alloc,
document_store,
handle,
expr_start,
Expand Down Expand Up @@ -292,6 +293,7 @@ pub fn getSignatureInfo(document_store: *DocumentStore, alloc: std.mem.Allocator

const skip_self_param = !type_handle.type.is_type_val;
const decl_handle = (try analysis.lookupSymbolContainer(
alloc,
document_store,
.{ .node = node, .handle = type_handle.handle },
name,
Expand All @@ -310,6 +312,7 @@ pub fn getSignatureInfo(document_store: *DocumentStore, alloc: std.mem.Allocator
};

if (try analysis.resolveVarDeclAlias(
alloc,
document_store,
.{ .node = node, .handle = decl_handle.handle },
)) |resolved| {
Expand Down

0 comments on commit 2ce59a3

Please sign in to comment.