More progress

This commit is contained in:
Alexandros Naskos 2020-06-10 20:48:40 +03:00
parent d6609d918b
commit 3bdb2ee444
2 changed files with 145 additions and 133 deletions

View File

@ -244,13 +244,13 @@ fn findReturnStatement(tree: *ast.Tree, fn_decl: *ast.Node.FnProto) ?*ast.Node.C
}
/// Resolves the return type of a function
fn resolveReturnType(store: *DocumentStore, fn_decl: *ast.Node.FnProto, handle: *DocumentStore.Handle) !?NodeWithHandle {
fn resolveReturnType(store: *DocumentStore, arena: *std.heap.ArenaAllocator, fn_decl: *ast.Node.FnProto, handle: *DocumentStore.Handle) !?NodeWithHandle {
if (isTypeFunction(handle.tree, fn_decl) and fn_decl.body_node != null) {
// If this is a type function and it only contains a single return statement that returns
// a container declaration, we will return that declaration.
const ret = findReturnStatement(handle.tree, fn_decl) orelse return null;
if (ret.rhs) |rhs|
if (try resolveTypeOfNode(store, .{ .node = rhs, .handle = handle })) |res_rhs| switch (res_rhs.node.id) {
if (try resolveTypeOfNode(store, arena, .{ .node = rhs, .handle = handle })) |res_rhs| switch (res_rhs.node.id) {
.ContainerDecl => {
return res_rhs;
},
@ -261,16 +261,16 @@ fn resolveReturnType(store: *DocumentStore, fn_decl: *ast.Node.FnProto, handle:
}
return switch (fn_decl.return_type) {
.Explicit, .InferErrorSet => |return_type| try resolveTypeOfNode(store, .{ .node = return_type, .handle = handle }),
.Explicit, .InferErrorSet => |return_type| try resolveTypeOfNode(store, arena, .{ .node = return_type, .handle = handle }),
.Invalid => null,
};
}
/// Resolves the child type of an optional type
fn resolveUnwrapOptionalType(store: *DocumentStore, opt: NodeWithHandle) !?NodeWithHandle {
fn resolveUnwrapOptionalType(store: *DocumentStore, arena: *std.heap.ArenaAllocator, opt: NodeWithHandle) !?NodeWithHandle {
if (opt.node.cast(ast.Node.PrefixOp)) |prefix_op| {
if (prefix_op.op == .OptionalType) {
return try resolveTypeOfNode(store, .{ .node = prefix_op.rhs, .handle = opt.handle });
return try resolveTypeOfNode(store, arena, .{ .node = prefix_op.rhs, .handle = opt.handle });
}
}
@ -278,12 +278,12 @@ fn resolveUnwrapOptionalType(store: *DocumentStore, opt: NodeWithHandle) !?NodeW
}
/// Resolves the child type of a defer type
fn resolveDerefType(store: *DocumentStore, deref: NodeWithHandle) !?NodeWithHandle {
fn resolveDerefType(store: *DocumentStore, arena: *std.heap.ArenaAllocator, deref: NodeWithHandle) !?NodeWithHandle {
if (deref.node.cast(ast.Node.PrefixOp)) |pop| {
if (pop.op == .PtrType) {
const op_token_id = deref.handle.tree.token_ids[pop.op_token];
switch (op_token_id) {
.Asterisk => return try resolveTypeOfNode(store, .{ .node = pop.rhs, .handle = deref.handle }),
.Asterisk => return try resolveTypeOfNode(store, arena, .{ .node = pop.rhs, .handle = deref.handle }),
.LBracket, .AsteriskAsterisk => return null,
else => unreachable,
}
@ -315,26 +315,26 @@ fn makeSliceType(arena: *std.heap.ArenaAllocator, child_type: *ast.Node) ?*ast.N
/// Resolves bracket access type (both slicing and array access)
fn resolveBracketAccessType(
store: *DocumentStore,
lhs: NodeWithHandle,
arena: *std.heap.ArenaAllocator,
lhs: NodeWithHandle,
rhs: enum { Single, Range },
) !?NodeWithHandle {
if (lhs.node.cast(ast.Node.PrefixOp)) |pop| {
switch (pop.op) {
.SliceType => {
if (rhs == .Single) return resolveTypeOfNode(store, .{ .node = pop.rhs, .handle = lhs.handle });
if (rhs == .Single) return try resolveTypeOfNode(store, arena, .{ .node = pop.rhs, .handle = lhs.handle });
return lhs;
},
.ArrayType => {
if (rhs == .Single) return resolveTypeOfNode(store, .{ .node = pop.rhs, .handle = lhs.handle });
return NodeWithHandle{ .node = makeSliceType(arena, pop.rhs), .handle = lhs.handle };
if (rhs == .Single) return try resolveTypeOfNode(store, arena, .{ .node = pop.rhs, .handle = lhs.handle });
return NodeWithHandle{ .node = makeSliceType(arena, pop.rhs) orelse return null, .handle = lhs.handle };
},
.PtrType => {
if (pop.rhs.cast(std.zig.ast.Node.PrefixOp)) |child_pop| {
switch (child_pop.op) {
.ArrayType => {
if (rhs == .Single) {
return resolveTypeOfNode(store, .{ .node = child_pop.rhs, .handle = lhs.handle });
return try resolveTypeOfNode(store, arena, .{ .node = child_pop.rhs, .handle = lhs.handle });
}
return lhs;
},
@ -349,39 +349,36 @@ fn resolveBracketAccessType(
}
/// Called to remove one level of pointerness before a field access
fn resolveFieldAccessLhsType(store: *DocumentStore, lhs: NodeWithHandle) !NodeWithHandle {
return resolveDerefType(store, lhs) orelse lhs;
fn resolveFieldAccessLhsType(store: *DocumentStore, arena: *std.heap.ArenaAllocator, lhs: NodeWithHandle) !NodeWithHandle {
return (try resolveDerefType(store, arena, lhs)) orelse lhs;
}
// @TODO try errors
/// Resolves the type of a node
pub fn resolveTypeOfNode(store: *DocumentStore, node_handle: NodeWithHandle) !?NodeWithHandle {
pub fn resolveTypeOfNode(store: *DocumentStore, arena: *std.heap.ArenaAllocator, node_handle: NodeWithHandle) error{OutOfMemory}!?NodeWithHandle {
const node = node_handle.node;
const handle = node_handle.handle;
switch (node.id) {
.VarDecl => {
const vari = node.cast(ast.Node.VarDecl).?;
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
return try resolveTypeOfNode(store, arena, .{ .node = vari.type_node orelse vari.init_node.?, .handle = handle });
},
.Identifier => {
if (getChildOfSlice(analysis_ctx.tree(), analysis_ctx.scope_nodes, analysis_ctx.tree().getNodeSource(node))) |child| {
if (child == node) return null;
return resolveTypeOfNode(analysis_ctx, child);
} else return null;
if (try lookupSymbolGlobal(store, handle, handle.tree.getNodeSource(node), handle.tree.token_locs[node.firstToken()].start)) |child| {
return try child.resolveType(store, arena);
}
return null;
},
.ContainerField => {
const field = node.cast(ast.Node.ContainerField).?;
return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null);
return try resolveTypeOfNode(store, arena, .{ .node = field.type_expr orelse return null, .handle = handle });
},
.Call => {
const call = node.cast(ast.Node.Call).?;
const previous_tree = analysis_ctx.tree();
const decl = resolveTypeOfNode(analysis_ctx, call.lhs) orelse return null;
if (decl.cast(ast.Node.FnProto)) |fn_decl| {
if (previous_tree != analysis_ctx.tree()) {
return resolveReturnType(analysis_ctx, fn_decl);
}
// @TODO use BoundTypeParams: ParamDecl -> NodeWithHandle or something
const decl = (try resolveTypeOfNode(store, arena, .{ .node = call.lhs, .handle = handle })) orelse return null;
if (decl.node.cast(ast.Node.FnProto)) |fn_decl| {
// Add type param values to the scope nodes
const param_len = std.math.min(call.params_len, fn_decl.params_len);
var scope_nodes = std.ArrayList(*ast.Node).fromOwnedSlice(&analysis_ctx.arena.allocator, analysis_ctx.scope_nodes);
@ -629,7 +626,7 @@ pub fn getFieldAccessTypeNode(
while (true) {
const tok = tokenizer.next();
switch (tok.id) {
.Eof => return try resolveFieldAccessLhsType(store, current_node),
.Eof => return try resolveFieldAccessLhsType(store, arena, current_node),
.Identifier => {
if (try lookupSymbolGlobal(store, current_node.handle, tokenizer.buffer[tok.loc.start..tok.loc.end], tok.loc.start)) |child| {
current_node = (try child.resolveType(store, arena)) orelse return null;
@ -638,22 +635,22 @@ pub fn getFieldAccessTypeNode(
.Period => {
const after_period = tokenizer.next();
switch (after_period.id) {
.Eof => return try resolveFieldAccessLhsType(store, current_node),
.Eof => return try resolveFieldAccessLhsType(store, arena, current_node),
.Identifier => {
if (after_period.loc.end == tokenizer.buffer.len) return try resolveFieldAccessLhsType(store, current_node);
if (after_period.loc.end == tokenizer.buffer.len) return try resolveFieldAccessLhsType(store, arena, current_node);
current_node = resolveFieldAccessLhsType(store, current_node);
current_node = try resolveFieldAccessLhsType(store, arena, current_node);
if (current_node.node.id != .ContainerDecl and current_node.node.id != .Root) {
// @TODO Is this ok?
return null;
}
if (lookupSymbolContainer(store, current_node, tokenizer.buffer[after_period.loc.start..after_period.loc.end], true)) |child| {
if (try lookupSymbolContainer(store, current_node, tokenizer.buffer[after_period.loc.start..after_period.loc.end], true)) |child| {
current_node = (try child.resolveType(store, arena)) orelse return null;
} else return null;
},
.QuestionMark => {
current_node = (try resolveUnwrapOptionalType(store, current_node)) orelse return null;
current_node = (try resolveUnwrapOptionalType(store, arena, current_node)) orelse return null;
},
else => {
std.debug.warn("Unrecognized token {} after period.\n", .{after_period.id});
@ -662,13 +659,13 @@ pub fn getFieldAccessTypeNode(
}
},
.PeriodAsterisk => {
current_node = (try resolveDerefType(store, current_node)) orelse return null;
current_node = (try resolveDerefType(store, arena, current_node)) orelse return null;
},
.LParen => {
switch (current_node.node.id) {
.FnProto => {
const func = current_node.node.cast(ast.Node.FnProto).?;
if (try resolveReturnType(store, func, current_node.handle)) |ret| {
if (try resolveReturnType(store, arena, func, current_node.handle)) |ret| {
current_node = ret;
// Skip to the right paren
var paren_count: usize = 1;
@ -701,7 +698,7 @@ pub fn getFieldAccessTypeNode(
}
} else return null;
current_node = (try resolveBracketAccessType(store, current_node, arena, if (is_range) .Range else .Single)) orelse return null;
current_node = (try resolveBracketAccessType(store, arena, current_node, if (is_range) .Range else .Single)) orelse return null;
},
else => {
std.debug.warn("Unimplemented token: {}\n", .{tok.id});
@ -710,7 +707,7 @@ pub fn getFieldAccessTypeNode(
}
}
return try resolveFieldAccessLhsType(store, current_node);
return try resolveFieldAccessLhsType(store, arena, current_node);
}
pub fn isNodePublic(tree: *ast.Tree, node: *ast.Node) bool {
@ -1092,27 +1089,27 @@ pub const DeclWithHandle = struct {
}
fn resolveType(self: DeclWithHandle, store: *DocumentStore, arena: *std.heap.ArenaAllocator) !?NodeWithHandle {
// resolveTypeOfNode(store: *DocumentStore, node_handle: NodeWithHandle)
return switch (self.decl) {
.ast_node => |node| try resolveTypeOfNode(store, .{ .node = node, .handle = self.handle }),
return switch (self.decl.*) {
.ast_node => |node| try resolveTypeOfNode(store, arena, .{ .node = node, .handle = self.handle }),
.param_decl => |param_decl| switch (param_decl.param_type) {
.type_expr => |type_node| try resolveTypeOfNode(store, .{ .node = node, .handle = self.handle }),
.type_expr => |type_node| try resolveTypeOfNode(store, arena, .{ .node = type_node, .handle = self.handle }),
else => null,
},
.pointer_payload => |pay| try resolveUnwrapOptionalType(
store,
try resolveTypeOfNode(store, .{
arena,
(try resolveTypeOfNode(store, arena, .{
.node = pay.condition,
.handle = self.handle,
}) orelse return null,
})) orelse return null,
),
.array_payload => |pay| try resolveBracketAccessType(
store,
arena,
.{
.node = pay.array_expr,
.handle = self.handle,
},
arena,
.Single,
),
// TODO Resolve switch payload types
@ -1171,18 +1168,19 @@ pub fn lookupSymbolGlobal(store: *DocumentStore, handle: *DocumentStore.Handle,
return null;
}
pub fn lookupSymbolContainer(store: *DocumentScope, container_handle: NodeWithHandle, symbol: []const u8, accept_fields: bool) !?DeclWithHandle {
pub fn lookupSymbolContainer(store: *DocumentStore, container_handle: NodeWithHandle, symbol: []const u8, accept_fields: bool) !?DeclWithHandle {
const container = container_handle.node;
const handle = container_handle.handle;
std.debug.assert(container.id == .ContainerDecl or container.id == .Root);
// Find the container scope.
var maybe_container_scope: ?*Scope = null;
for (handle.document_scope.scopes) |*scope| {
switch (scope.*) {
switch (scope.*.data) {
.container => |node| if (node == container) {
maybe_container_scope = scope;
break;
},
else => {},
}
}
@ -1194,7 +1192,7 @@ pub fn lookupSymbolContainer(store: *DocumentScope, container_handle: NodeWithHa
},
else => {},
}
return &candidate.value;
return DeclWithHandle{ .decl = &candidate.value, .handle = handle };
}
for (container_scope.uses) |use| {
@ -1206,7 +1204,7 @@ pub fn lookupSymbolContainer(store: *DocumentScope, container_handle: NodeWithHa
}
pub const DocumentScope = struct {
scopes: []const Scope,
scopes: []Scope,
pub fn debugPrint(self: DocumentScope) void {
for (self.scopes) |scope| {
@ -1338,6 +1336,7 @@ fn makeScopeInternal(
.range = nodeSourceRange(tree, node),
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .{ .function = node },
};
var scope_idx = scopes.items.len - 1;
@ -1365,6 +1364,7 @@ fn makeScopeInternal(
.range = nodeSourceRange(tree, node),
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .{ .block = node },
};
var scope_idx = scopes.items.len - 1;
@ -1410,6 +1410,7 @@ fn makeScopeInternal(
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .other,
};
errdefer scope.decls.deinit();
@ -1437,6 +1438,7 @@ fn makeScopeInternal(
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .other,
};
errdefer scope.decls.deinit();
@ -1461,6 +1463,7 @@ fn makeScopeInternal(
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .other,
};
errdefer scope.decls.deinit();
@ -1488,6 +1491,7 @@ fn makeScopeInternal(
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .other,
};
errdefer scope.decls.deinit();
@ -1514,6 +1518,7 @@ fn makeScopeInternal(
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .other,
};
errdefer scope.decls.deinit();
@ -1554,6 +1559,7 @@ fn makeScopeInternal(
},
.decls = std.StringHashMap(Declaration).init(allocator),
.uses = &[0]*ast.Node.Use{},
.tests = &[0]*ast.Node{},
.data = .other,
};
errdefer scope.decls.deinit();

View File

@ -81,7 +81,7 @@ fn respondGeneric(id: types.RequestId, response: []const u8) !void {
const json_fmt = "{{\"jsonrpc\":\"2.0\",\"id\":";
const stdout_stream = stdout.outStream();
try stdout_stream.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_len + json_fmt.len - 1 });
try stdout_stream.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{response.len + id_len + json_fmt.len - 1});
switch (id) {
.Integer => |int| try stdout_stream.print("{}", .{int}),
.String => |str| try stdout_stream.print("\"{}\"", .{str}),
@ -98,7 +98,7 @@ fn showMessage(@"type": types.MessageType, message: []const u8) !void {
.params = .{
.ShowMessageParams = .{
.@"type" = @"type",
.message = message
.message = message,
},
},
});
@ -198,31 +198,32 @@ fn publishDiagnostics(handle: DocumentStore.Handle, config: Config) !void {
fn containerToCompletion(
list: *std.ArrayList(types.CompletionItem),
analysis_ctx: *DocumentStore.AnalysisContext,
container_handle: analysis.NodeWithHandle,
orig_handle: *DocumentStore.Handle,
container: *std.zig.ast.Node,
config: Config,
) !void {
// @TODO Something like iterateSymbolsGlobal but for container to support uses.
const container = container_handle.node;
const handle = container_handle.handle;
var child_idx: usize = 0;
while (container.iterate(child_idx)) |child_node| : (child_idx += 1) {
// Declarations in the same file do not need to be public.
if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree(), child_node)) {
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config);
if (orig_handle == handle or analysis.isNodePublic(handle.tree, child_node)) {
try nodeToCompletion(list, .{ .node = child_node, .handle = handle }, orig_handle, config);
}
}
}
const ResolveVarDeclFnAliasRewsult = struct {
decl: *std.zig.ast.Node,
analysis_ctx: DocumentStore.AnalysisContext,
};
fn resolveVarDeclFnAlias(arena: *std.heap.ArenaAllocator, decl_handle: analysis.NodeWithHandle) !analysis.NodeWithHandle {
const decl = decl_handle.node;
const handle = decl_handle.handle;
fn resolveVarDeclFnAlias(analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !ResolveVarDeclFnAliasRewsult {
if (decl.cast(std.zig.ast.Node.VarDecl)) |var_decl| {
var child_analysis_context = analysis_ctx.*;
const child_node = block: {
if (var_decl.type_node) |type_node| {
if (std.mem.eql(u8, "type", analysis_ctx.tree().tokenSlice(type_node.firstToken()))) {
if (std.mem.eql(u8, "type", handle.tree.tokenSlice(type_node.firstToken()))) {
break :block var_decl.init_node orelse type_node;
}
break :block type_node;
@ -230,27 +231,28 @@ fn resolveVarDeclFnAlias(analysis_ctx: *DocumentStore.AnalysisContext, decl: *st
break :block var_decl.init_node.?;
};
if (analysis.resolveTypeOfNode(&child_analysis_context, child_node)) |resolved_node| {
if (resolved_node.id == .FnProto) {
return ResolveVarDeclFnAliasRewsult{
.decl = resolved_node,
.analysis_ctx = child_analysis_context,
};
if (try analysis.resolveTypeOfNode(&document_store, arena, .{ .node = child_node, .handle = handle })) |resolved_node| {
// TODO Just return it anyway?
// This would allow deep goto definition etc.
// Try it out.
if (resolved_node.node.id == .FnProto) {
return resolved_node;
}
}
}
return ResolveVarDeclFnAliasRewsult{
.decl = decl,
.analysis_ctx = analysis_ctx.*,
};
return decl_handle;
}
fn nodeToCompletion(
list: *std.ArrayList(types.CompletionItem),
node_handle: analysis.NodeWithHandle,
orig_handle: *DocumentStore.Handle,
config: Config,
) error{OutOfMemory}!void {
const doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree(), node)) |doc_comments|
const node = node_handle.node;
const handle = node_handle.handle;
const doc = if (try analysis.getDocComments(list.allocator, handle.tree, node)) |doc_comments|
types.MarkupContent{
.kind = .Markdown,
.value = doc_comments,
@ -260,7 +262,7 @@ fn nodeToCompletion(
switch (node.id) {
.ErrorSetDecl, .Root, .ContainerDecl => {
try containerToCompletion(list, analysis_ctx, orig_handle, node, config);
try containerToCompletion(list, node_handle, orig_handle, config);
},
.FnProto => {
const func = node.cast(std.zig.ast.Node.FnProto).?;
@ -344,12 +346,12 @@ fn nodeToCompletion(
.kind = .Field,
});
},
else => if (analysis.nodeToString(analysis_ctx.tree(), node)) |string| {
else => if (analysis.nodeToString(handle.tree, node)) |string| {
try list.append(.{
.label = string,
.kind = .Field,
.documentation = doc,
.detail = analysis_ctx.tree().getNodeSource(node)
.detail = handle.tree.getNodeSource(node),
});
},
}
@ -374,16 +376,17 @@ fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []cons
return text[start_idx + 1 .. end_idx];
}
fn gotoDefinitionSymbol(id: types.RequestId, analysis_ctx: *DocumentStore.AnalysisContext, decl_handle: analysis.DeclWithHandle) !void {
var handle = analysis_ctx.handle;
fn gotoDefinitionSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle) !void {
var handle = decl_handle.handle;
const location = switch (decl_handle.decl.*) {
.ast_node => |node| block: {
const result = try resolveVarDeclFnAlias(analysis_ctx, node);
handle = result.analysis_ctx.handle;
const name_token = analysis.getDeclNameToken(handle.tree, result.decl) orelse
const result = try resolveVarDeclFnAlias(arena, .{ .node = node, .handle = handle });
handle = result.handle;
const name_token = analysis.getDeclNameToken(result.handle.tree, result.node) orelse
return try respondGeneric(id, null_result_response);
break :block handle.tree.tokenLocation(0, name_token);
break :block result.handle.tree.tokenLocation(0, name_token);
},
else => decl_handle.location(),
};
@ -399,27 +402,29 @@ fn gotoDefinitionSymbol(id: types.RequestId, analysis_ctx: *DocumentStore.Analys
});
}
fn hoverSymbol(id: types.RequestId, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !void {
const result = try resolveVarDeclFnAlias(analysis_ctx, decl);
fn hoverSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle) !void {
switch (decl_handle.decl.*) {
.ast_node => |node| {
const result = try resolveVarDeclFnAlias(arena, .{ .node = node, .handle = decl_handle.handle });
const doc_str = if (try analysis.getDocComments(&analysis_ctx.arena.allocator, result.analysis_ctx.tree(), result.decl)) |str|
const doc_str = if (try analysis.getDocComments(&arena.allocator, result.handle.tree, result.node)) |str|
str
else
"";
const signature_str = switch (result.decl.id) {
const signature_str = switch (result.node.id) {
.VarDecl => blk: {
const var_decl = result.decl.cast(std.zig.ast.Node.VarDecl).?;
break :blk analysis.getVariableSignature(result.analysis_ctx.tree(), var_decl);
const var_decl = result.node.cast(std.zig.ast.Node.VarDecl).?;
break :blk analysis.getVariableSignature(result.handle.tree, var_decl);
},
.FnProto => blk: {
const fn_decl = result.decl.cast(std.zig.ast.Node.FnProto).?;
break :blk analysis.getFunctionSignature(result.analysis_ctx.tree(), fn_decl);
const fn_decl = result.node.cast(std.zig.ast.Node.FnProto).?;
break :blk analysis.getFunctionSignature(result.handle.tree, fn_decl);
},
else => analysis.nodeToString(result.analysis_ctx.tree(), result.decl) orelse return try respondGeneric(id, null_result_response),
else => analysis.nodeToString(result.handle.tree, result.node) orelse return try respondGeneric(id, null_result_response),
};
const md_string = try std.fmt.allocPrint(&analysis_ctx.arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str });
const md_string = try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str });
try send(types.Response{
.id = id,
.result = .{
@ -428,6 +433,10 @@ fn hoverSymbol(id: types.RequestId, analysis_ctx: *DocumentStore.AnalysisContext
},
},
});
},
// @TODO Rest of decls
else => return try respondGeneric(id, null_result_response),
}
}
fn getSymbolGlobal(arena: *std.heap.ArenaAllocator, pos_index: usize, handle: *DocumentStore.Handle) !?analysis.DeclWithHandle {
@ -442,36 +451,33 @@ fn gotoDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *Document
defer arena.deinit();
const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
var analysis_ctx = try document_store.analysisContext(decl.handle, &arena, config.zig_lib_path);
return try gotoDefinitionSymbol(id, &analysis_ctx, decl);
return try gotoDefinitionSymbol(id, &arena, decl);
}
fn hoverDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const decl = (try getSymbolGlobal(&arena, pos_index, handle.*)) orelse return try respondGeneric(id, null_result_response);
var analysis_ctx = try document_store.analysisContext(handle, &arena, pos_index, config.zig_lib_path);
return try hoverSymbol(id, &analysis_ctx, decl);
const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
return try hoverSymbol(id, &arena, decl);
}
fn getSymbolFieldAccess(
analysis_ctx: *DocumentStore.AnalysisContext,
handle: *DocumentStore.Handle,
arena: *std.heap.ArenaAllocator,
position: types.Position,
range: analysis.SourceRange,
config: Config,
) !?*std.zig.ast.Node {
const pos_index = try analysis_ctx.handle.document.positionToIndex(position);
var name = identifierFromPosition(pos_index, analysis_ctx.handle.*);
) !?analysis.DeclWithHandle {
const pos_index = try handle.document.positionToIndex(position);
const name = identifierFromPosition(pos_index, handle.*);
if (name.len == 0) return null;
const line = try analysis_ctx.handle.document.getLine(@intCast(usize, position.line));
const line = try handle.document.getLine(@intCast(usize, position.line));
var tokenizer = std.zig.Tokenizer.init(line[range.start..range.end]);
name = try std.mem.dupe(&analysis_ctx.arena.allocator, u8, name);
if (analysis.getFieldAccessTypeNode(analysis_ctx, &tokenizer)) |container| {
return analysis.getChild(analysis_ctx.tree(), container, name);
if (try analysis.getFieldAccessTypeNode(&document_store, arena, handle, &tokenizer)) |container_handle| {
return try analysis.lookupSymbolContainer(&document_store, container_handle, name, true);
}
return null;
}
@ -486,9 +492,8 @@ fn gotoDefinitionFieldAccess(
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var analysis_ctx = try document_store.analysisContext(handle, &arena, config.zig_lib_path);
const decl = (try getSymbolFieldAccess(&analysis_ctx, position, range, config)) orelse return try respondGeneric(id, null_result_response);
return try gotoDefinitionSymbol(id, &analysis_ctx, decl);
const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
return try gotoDefinitionSymbol(id, &arena, decl);
}
fn hoverDefinitionFieldAccess(
@ -501,9 +506,8 @@ fn hoverDefinitionFieldAccess(
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var analysis_ctx = try document_store.analysisContext(handle, &arena, try handle.document.positionToIndex(position), config.zig_lib_path);
const decl = (try getSymbolFieldAccess(&analysis_ctx, position, range, config)) orelse return try respondGeneric(id, null_result_response);
return try hoverSymbol(id, &analysis_ctx, decl);
const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
return try hoverSymbol(id, &arena, decl);
}
fn gotoDefinitionString(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
@ -537,12 +541,13 @@ const DeclToCompletionContext = struct {
completions: *std.ArrayList(types.CompletionItem),
config: *const Config,
arena: *std.heap.ArenaAllocator,
orig_handle: *DocumentStore.Handle,
};
fn decltoCompletion(context: DeclToCompletionContext, decl_handle: analysis.DeclWithHandle) !void {
switch (decl_handle.decl.*) {
.ast_node => |node| {
try nodeToCompletion(context.completions, .{ .node = node, .handle = decl_handle.handle }, context.config.*);
try nodeToCompletion(context.completions, .{ .node = node, .handle = decl_handle.handle }, context.orig_handle, context.config.*);
},
else => {},
// @TODO The rest
@ -560,6 +565,7 @@ fn completeGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.
.completions = &completions,
.config = &config,
.arena = &arena,
.orig_handle = handle,
};
try analysis.iterateSymbolsGlobal(&document_store, handle, pos_index, decltoCompletion, context);
@ -578,15 +584,15 @@ fn completeFieldAccess(id: types.RequestId, handle: *DocumentStore.Handle, posit
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var analysis_ctx = try document_store.analysisContext(handle, &arena, config.zig_lib_path);
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
const line = try handle.document.getLine(@intCast(usize, position.line));
var tokenizer = std.zig.Tokenizer.init(line[range.start..range.end]);
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| {
try nodeToCompletion(&completions, &analysis_ctx, handle, node, config);
if (try analysis.getFieldAccessTypeNode(&document_store, &arena, handle, &tokenizer)) |node| {
try nodeToCompletion(&completions, node, handle, config);
}
try send(types.Response{
.id = id,
.result = .{