Got some aliases working

Can't figure out a bug with document changing in the original context,
which causes further analysis to break.
This commit is contained in:
Sergeeeek 2020-05-17 09:47:48 +03:00
parent 8637099d72
commit 0b433815b3
3 changed files with 49 additions and 32 deletions

View File

@ -190,25 +190,14 @@ pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
/// Resolves the type of a node /// Resolves the type of a node
pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.Node { pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.Node {
std.debug.warn("Resolving node of type {}\n{}\n", .{ node.id, analysis_ctx.tree.getNodeSource(node) }); std.debug.warn("Resolving node of type {}\n", .{node.id});
switch (node.id) { switch (node.id) {
.VarDecl => { .VarDecl => {
const vari = node.cast(ast.Node.VarDecl).?; const vari = node.cast(ast.Node.VarDecl).?;
if (vari.lib_name) |lib_name_node| {
if (nodeToString(analysis_ctx.tree, lib_name_node)) |lib_name_str| {
// if (std.mem.eql(u8, lib_name_str, "ArrayList")) {
std.debug.warn("Evaluating var decl. {}\n", .{lib_name_str});
// }
}
}
return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null; return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null;
}, },
.FnProto => { .FnProto => {
const func = node.cast(ast.Node.FnProto).?; return node;
switch (func.return_type) {
.Explicit, .InferErrorSet => |return_type| return resolveTypeOfNode(analysis_ctx, return_type),
}
}, },
.Identifier => { .Identifier => {
if (getChild(analysis_ctx.tree, &analysis_ctx.tree.root_node.base, analysis_ctx.tree.getNodeSource(node))) |child| { if (getChild(analysis_ctx.tree, &analysis_ctx.tree.root_node.base, analysis_ctx.tree.getNodeSource(node))) |child| {
@ -229,7 +218,15 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
const suffix_op = node.cast(ast.Node.SuffixOp).?; const suffix_op = node.cast(ast.Node.SuffixOp).?;
switch (suffix_op.op) { switch (suffix_op.op) {
.Call => { .Call => {
return resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node); const func_decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node) orelse return null;
if (func_decl.id == .FnProto) {
const func = node.cast(ast.Node.FnProto).?;
switch (func.return_type) {
.Explicit, .InferErrorSet => |return_type| return resolveTypeOfNode(analysis_ctx, return_type),
}
}
return null;
}, },
else => {}, else => {},
} }
@ -239,12 +236,14 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
switch (infix_op.op) { switch (infix_op.op) {
.Period => { .Period => {
std.debug.warn("\n\n\nPeriod\n\n\n", .{}); std.debug.warn("\n\n\nPeriod\n\n\n", .{});
std.debug.warn("InfixOp file = {}\n{}\n", .{ analysis_ctx.handle.document.uri, analysis_ctx.tree.getNodeSource(&analysis_ctx.tree.root_node.base) });
std.debug.warn("InfixOp full = {}\n", .{analysis_ctx.tree.getNodeSource(node)});
// Save the child string from this tree since the tree may switch when processing // Save the child string from this tree since the tree may switch when processing
// an import lhs. // an import lhs.
var rhs_str = nodeToString(analysis_ctx.tree, infix_op.rhs) orelse return null; var rhs_str = nodeToString(analysis_ctx.tree, infix_op.rhs) orelse return null;
std.debug.warn("InfixOp rhs_str = {}\n", .{rhs_str});
// Use the analysis context temporary arena to store the rhs string. // Use the analysis context temporary arena to store the rhs string.
rhs_str = std.mem.dupe(&analysis_ctx.arena.allocator, u8, rhs_str) catch return null; rhs_str = std.mem.dupe(&analysis_ctx.arena.allocator, u8, rhs_str) catch return null;
std.debug.warn("InfixOp rhs_str = {}\n", .{rhs_str});
const left = resolveTypeOfNode(analysis_ctx, infix_op.lhs) orelse return null; const left = resolveTypeOfNode(analysis_ctx, infix_op.lhs) orelse return null;
std.debug.warn("InfixOp left = {}\n", .{left}); std.debug.warn("InfixOp left = {}\n", .{left});
const child = getChild(analysis_ctx.tree, left, rhs_str) orelse return null; const child = getChild(analysis_ctx.tree, left, rhs_str) orelse return null;
@ -272,6 +271,7 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
const builtin_call = node.cast(ast.Node.BuiltinCall).?; const builtin_call = node.cast(ast.Node.BuiltinCall).?;
if (!std.mem.eql(u8, analysis_ctx.tree.tokenSlice(builtin_call.builtin_token), "@import")) return null; if (!std.mem.eql(u8, analysis_ctx.tree.tokenSlice(builtin_call.builtin_token), "@import")) return null;
if (builtin_call.params.len > 1) return null; if (builtin_call.params.len > 1) return null;
std.debug.warn("Importing {}\n", .{analysis_ctx.tree.getNodeSource(node)});
const import_param = builtin_call.params.at(0).*; const import_param = builtin_call.params.at(0).*;
if (import_param.id != .StringLiteral) return null; if (import_param.id != .StringLiteral) return null;

View File

@ -45,7 +45,7 @@ pub fn init(self: *DocumentStore, allocator: *std.mem.Allocator, zig_lib_path: ?
if (zig_lib_path) |zpath| { if (zig_lib_path) |zpath| {
const std_path = std.fs.path.resolve(allocator, &[_][]const u8{ const std_path = std.fs.path.resolve(allocator, &[_][]const u8{
zpath, "./std/std.zig" zpath, "./std/std.zig",
}) catch |err| block: { }) catch |err| block: {
std.debug.warn("Failed to resolve zig std library path, error: {}\n", .{err}); std.debug.warn("Failed to resolve zig std library path, error: {}\n", .{err});
self.std_uri = null; self.std_uri = null;
@ -207,11 +207,11 @@ pub fn applyChanges(self: *DocumentStore, handle: *Handle, content_changes: std.
if (change.Object.getValue("range")) |range| { if (change.Object.getValue("range")) |range| {
const start_pos = types.Position{ const start_pos = types.Position{
.line = range.Object.getValue("start").?.Object.getValue("line").?.Integer, .line = range.Object.getValue("start").?.Object.getValue("line").?.Integer,
.character = range.Object.getValue("start").?.Object.getValue("character").?.Integer .character = range.Object.getValue("start").?.Object.getValue("character").?.Integer,
}; };
const end_pos = types.Position{ const end_pos = types.Position{
.line = range.Object.getValue("end").?.Object.getValue("line").?.Integer, .line = range.Object.getValue("end").?.Object.getValue("line").?.Integer,
.character = range.Object.getValue("end").?.Object.getValue("character").?.Integer .character = range.Object.getValue("end").?.Object.getValue("character").?.Integer,
}; };
const change_text = change.Object.getValue("text").?.String; const change_text = change.Object.getValue("text").?.String;
@ -258,8 +258,7 @@ pub fn applyChanges(self: *DocumentStore, handle: *Handle, content_changes: std.
fn uriFromImportStr(store: *DocumentStore, handle: *Handle, import_str: []const u8) !?[]const u8 { fn uriFromImportStr(store: *DocumentStore, handle: *Handle, import_str: []const u8) !?[]const u8 {
return if (std.mem.eql(u8, import_str, "std")) return if (std.mem.eql(u8, import_str, "std"))
if (store.std_uri) |std_root_uri| try std.mem.dupe(store.allocator, u8, std_root_uri) if (store.std_uri) |std_root_uri| try std.mem.dupe(store.allocator, u8, std_root_uri) else {
else {
std.debug.warn("Cannot resolve std library import, path is null.\n", .{}); std.debug.warn("Cannot resolve std library import, path is null.\n", .{});
return null; return null;
} }
@ -270,7 +269,7 @@ fn uriFromImportStr(store: *DocumentStore, handle: *Handle, import_str: []const
const dir_path = std.fs.path.dirname(path) orelse ""; const dir_path = std.fs.path.dirname(path) orelse "";
const import_path = try std.fs.path.resolve(store.allocator, &[_][]const u8{ const import_path = try std.fs.path.resolve(store.allocator, &[_][]const u8{
dir_path, import_str dir_path, import_str,
}); });
defer store.allocator.free(import_path); defer store.allocator.free(import_path);

View File

@ -285,12 +285,17 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
var tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]); var tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]);
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| { if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| {
const initial_document = try std.mem.dupe(&arena.allocator, u8, analysis_ctx.handle.document.uri);
var index: usize = 0; var index: usize = 0;
while (node.iterate(index)) |child_node| { while (node.iterate(index)) |child_node| {
if (analysis.isNodePublic(analysis_ctx.tree, child_node)) { std.debug.warn("Document uri = {}\nInitial uri = {}\n", .{ analysis_ctx.handle.document.uri, initial_document });
std.debug.assert(std.mem.eql(u8, initial_document, analysis_ctx.handle.document.uri));
if (analysis.isNodePublic(analysis_ctx.tree, child_node)) {
// TODO: Not great to allocate it again and again inside a loop // TODO: Not great to allocate it again and again inside a loop
var node_analysis_ctx = (try document_store.analysisContext(handle, &arena)) orelse { // Creating a new context, so that we don't destroy the tree that is iterated above when resolving imports
std.debug.warn("\ncompleteFieldAccess calling resolveTypeOfNode for {}\nIn document {}\n", .{ analysis_ctx.tree.getNodeSource(child_node), analysis_ctx.handle.document.uri });
var node_analysis_ctx = (try document_store.analysisContext(analysis_ctx.handle, &arena)) orelse {
return send(types.Response{ return send(types.Response{
.id = .{ .Integer = id }, .id = .{ .Integer = id },
.result = .{ .result = .{
@ -303,9 +308,22 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
}; };
defer node_analysis_ctx.deinit(); defer node_analysis_ctx.deinit();
const resolved_node = analysis.resolveTypeOfNode(&node_analysis_ctx, child_node) orelse child_node; const resolved_node = analysis.resolveTypeOfNode(&node_analysis_ctx, child_node);
if (resolved_node) |n| {
std.debug.warn("completeFieldAccess resolveTypeOfNode result = {}\n", .{resolved_node});
}
if (try nodeToCompletion(&arena.allocator, node_analysis_ctx.tree, resolved_node, config)) |completion| { const completion_node: struct { node: *std.zig.ast.Node, context: *DocumentStore.AnalysisContext } = blk: {
if (resolved_node) |n| {
break :blk .{ .node = n, .context = &node_analysis_ctx };
}
break :blk .{ .node = child_node, .context = &analysis_ctx };
};
std.debug.warn("completeFieldAccess resolved_node = {}\n", .{completion_node.context.tree.getNodeSource(completion_node.node)});
if (try nodeToCompletion(&arena.allocator, completion_node.context.tree, completion_node.node, config)) |completion| {
try completions.append(completion); try completions.append(completion);
} }
} }