Fixed crash when field access completing on a local variable from a type coming from an import

This commit is contained in:
Alexandros Naskos 2020-05-18 14:26:52 +03:00
parent cb4354e494
commit 4ad33c16f9
3 changed files with 37 additions and 51 deletions

View File

@ -234,7 +234,6 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?*
/// 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("NODE {}\n", .{node});
switch (node.id) { switch (node.id) {
.VarDecl => { .VarDecl => {
const vari = node.cast(ast.Node.VarDecl).?; const vari = node.cast(ast.Node.VarDecl).?;
@ -257,9 +256,9 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.
} }
}, },
.Identifier => { .Identifier => {
// std.debug.warn("IDENTIFIER {}\n", .{analysis_ctx.tree.getNodeSource(node)}); const identifier = std.mem.dupe(&analysis_ctx.arena.allocator, u8, analysis_ctx.tree.getNodeSource(node)) catch return null;
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| {
// std.debug.warn("CHILD {}\n", .{child}); if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, identifier)) |child| {
return resolveTypeOfNode(analysis_ctx, child); return resolveTypeOfNode(analysis_ctx, child);
} else return null; } else return null;
}, },
@ -391,9 +390,8 @@ pub fn getFieldAccessTypeNode(analysis_ctx: *AnalysisContext, tokenizer: *std.zi
return current_node; return current_node;
}, },
.Identifier => { .Identifier => {
// var root = current_node.cast(ast.Node.Root).?; const identifier = std.mem.dupe(&analysis_ctx.arena.allocator, u8, tokenizer.buffer[next.start..next.end]) catch return null;
// current_node. if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, identifier)) |child| {
if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| {
if (resolveTypeOfNode(analysis_ctx, child)) |node_type| { if (resolveTypeOfNode(analysis_ctx, child)) |node_type| {
current_node = node_type; current_node = node_type;
} else return null; } else return null;
@ -462,70 +460,46 @@ pub fn nodeToString(tree: *ast.Tree, node: *ast.Node) ?[]const u8 {
return null; return null;
} }
pub fn declsFromIndexInternal(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node, nodes: *std.ArrayList(*ast.Node)) anyerror!void { pub fn declsFromIndexInternal(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) anyerror!void {
switch (node.id) { switch (node.id) {
.FnProto => { .FnProto => {
const func = node.cast(ast.Node.FnProto).?; const func = node.cast(ast.Node.FnProto).?;
var param_index: usize = 0; var param_index: usize = 0;
while (param_index < func.params.len) : (param_index += 1) while (param_index < func.params.len) : (param_index += 1)
try declsFromIndexInternal(allocator, tree, func.params.at(param_index).*, nodes); try declsFromIndexInternal(decls, tree, func.params.at(param_index).*);
if (func.body_node) |body_node| if (func.body_node) |body_node|
try declsFromIndexInternal(allocator, tree, body_node, nodes); try declsFromIndexInternal(decls, tree, body_node);
}, },
.Block => { .Block => {
var index: usize = 0; var index: usize = 0;
while (node.iterate(index)) |inode| { while (node.iterate(index)) |inode| {
try declsFromIndexInternal(allocator, tree, inode, nodes); try declsFromIndexInternal(decls, tree, inode);
index += 1; index += 1;
} }
}, },
.VarDecl => { .VarDecl, .ParamDecl => try decls.append(node),
try nodes.append(node); else => try getCompletionsFromNode(decls, tree, node),
},
.ParamDecl => {
try nodes.append(node);
},
else => {
try nodes.appendSlice(try getCompletionsFromNode(allocator, tree, node));
},
} }
} }
pub fn getCompletionsFromNode(allocator: *std.mem.Allocator, tree: *ast.Tree, node: *ast.Node) ![]*ast.Node { pub fn getCompletionsFromNode(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, node: *ast.Node) !void {
var nodes = std.ArrayList(*ast.Node).init(allocator);
var index: usize = 0; var index: usize = 0;
while (node.iterate(index)) |child_node| { while (node.iterate(index)) |child_node| : (index += 1) {
try nodes.append(child_node); try decls.append(child_node);
}
index += 1;
} }
return nodes.items; pub fn declsFromIndex(decls: *std.ArrayList(*ast.Node), tree: *ast.Tree, index: usize) !void {
}
pub fn declsFromIndex(allocator: *std.mem.Allocator, tree: *ast.Tree, index: usize) ![]*ast.Node {
var iindex: usize = 0;
var node = &tree.root_node.base; var node = &tree.root_node.base;
var nodes = std.ArrayList(*ast.Node).init(allocator);
try nodes.appendSlice(try getCompletionsFromNode(allocator, tree, node)); try getCompletionsFromNode(decls, tree, node);
var node_index: usize = 0;
while (node.iterate(iindex)) |inode| { while (node.iterate(node_index)) |inode| : (node_index += 1) {
if (tree.tokens.at(inode.firstToken()).start < index and index < tree.tokens.at(inode.lastToken()).start) { if (tree.tokens.at(inode.firstToken()).start < index and index < tree.tokens.at(inode.lastToken()).end) {
try declsFromIndexInternal(allocator, tree, inode, &nodes); try declsFromIndexInternal(decls, tree, inode);
} }
iindex += 1;
} }
if (tree.tokens.at(node.firstToken()).start < index and index < tree.tokens.at(node.lastToken()).start) {
return nodes.items;
}
return nodes.items;
} }

View File

@ -257,6 +257,12 @@ pub const AnalysisContext = struct {
tree: *std.zig.ast.Tree, tree: *std.zig.ast.Tree,
scope_nodes: []*std.zig.ast.Node, scope_nodes: []*std.zig.ast.Node,
fn refreshScopeNodes(self: *AnalysisContext) !void {
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&self.arena.allocator);
try analysis.getCompletionsFromNode(&scope_nodes, self.tree, &self.tree.root_node.base);
self.scope_nodes = scope_nodes.items;
}
pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node { pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node {
const allocator = self.store.allocator; const allocator = self.store.allocator;
const final_uri = (try uriFromImportStr(self.store, self.handle.*, import_str)) orelse return null; const final_uri = (try uriFromImportStr(self.store, self.handle.*, import_str)) orelse return null;
@ -273,6 +279,7 @@ pub const AnalysisContext = struct {
self.tree.deinit(); self.tree.deinit();
self.tree = try self.handle.tree(allocator); self.tree = try self.handle.tree(allocator);
try self.refreshScopeNodes();
return &self.tree.root_node.base; return &self.tree.root_node.base;
} }
} }
@ -286,6 +293,7 @@ pub const AnalysisContext = struct {
self.tree.deinit(); self.tree.deinit();
self.tree = try self.handle.tree(allocator); self.tree = try self.handle.tree(allocator);
try self.refreshScopeNodes();
return &self.tree.root_node.base; return &self.tree.root_node.base;
} }
@ -325,6 +333,7 @@ pub const AnalysisContext = struct {
// If we return null, no one should access the tree. // If we return null, no one should access the tree.
self.tree.deinit(); self.tree.deinit();
self.tree = try self.handle.tree(allocator); self.tree = try self.handle.tree(allocator);
try self.refreshScopeNodes();
return &self.tree.root_node.base; return &self.tree.root_node.base;
} }
@ -335,12 +344,16 @@ pub const AnalysisContext = struct {
pub fn analysisContext(self: *DocumentStore, handle: *Handle, arena: *std.heap.ArenaAllocator, position: types.Position) !AnalysisContext { pub fn analysisContext(self: *DocumentStore, handle: *Handle, arena: *std.heap.ArenaAllocator, position: types.Position) !AnalysisContext {
const tree = try handle.tree(self.allocator); const tree = try handle.tree(self.allocator);
var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
try analysis.declsFromIndex(&scope_nodes, tree, try handle.document.positionToIndex(position));
return AnalysisContext{ return AnalysisContext{
.store = self, .store = self,
.handle = handle, .handle = handle,
.arena = arena, .arena = arena,
.tree = tree, .tree = tree,
.scope_nodes = try analysis.declsFromIndex(&arena.allocator, tree, try handle.document.positionToIndex(position)) .scope_nodes = scope_nodes.items,
}; };
} }

View File

@ -280,9 +280,9 @@ fn completeGlobal(id: i64, pos_index: usize, handle: DocumentStore.Handle, confi
// Deallocate all temporary data. // Deallocate all temporary data.
defer arena.deinit(); defer arena.deinit();
// var decls = tree.root_node.decls.iterator(0); var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
var decls = try analysis.declsFromIndex(&arena.allocator, tree, pos_index); try analysis.declsFromIndex(&decl_nodes, tree, pos_index);
for (decls) |decl_ptr| { for (decl_nodes.items) |decl_ptr| {
var decl = decl_ptr.*; var decl = decl_ptr.*;
try nodeToCompletion(&completions, tree, decl_ptr, config); try nodeToCompletion(&completions, tree, decl_ptr, config);
} }
@ -310,7 +310,6 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
const line = try handle.document.getLine(@intCast(usize, position.line)); const line = try handle.document.getLine(@intCast(usize, position.line));
var tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]); var tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]);
// var decls = try analysis.declsFromIndex(&arena.allocator, analysis_ctx.tree, try handle.document.positionToIndex(position));
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| { if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| {
try nodeToCompletion(&completions, analysis_ctx.tree, node, config); try nodeToCompletion(&completions, analysis_ctx.tree, node, config);
} }