From 9ec32ac8302cd72a96da118d6118344531896ba0 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Sun, 24 May 2020 15:24:18 +0300 Subject: [PATCH] We now complete ArrayList! --- src/analysis.zig | 100 ++++++++++++++++++++++++++++++----------- src/document_store.zig | 10 +++++ src/main.zig | 13 +++--- 3 files changed, 89 insertions(+), 34 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index da5d245..1ad5422 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -234,7 +234,12 @@ pub fn getChildOfSlice(tree: *ast.Tree, nodes: []*ast.Node, name: []const u8) ?* return null; } -fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast.Node.ControlFlowExpression { +fn findReturnStatementInternal( + tree: *ast.Tree, + fn_decl: *ast.Node.FnProto, + base_node: *ast.Node, + already_found: *bool, +) ?*ast.Node.ControlFlowExpression { var result: ?*ast.Node.ControlFlowExpression = null; var idx: usize = 0; while (base_node.iterate(idx)) |child_node| : (idx += 1) { @@ -242,6 +247,19 @@ fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast .ControlFlowExpression => { const cfe = child_node.cast(ast.Node.ControlFlowExpression).?; if (cfe.kind == .Return) { + // If we are calling ourselves recursively, ignore this return. + if (cfe.rhs) |rhs| { + if (rhs.cast(ast.Node.SuffixOp)) |suffix_op| { + if (suffix_op.op == .Call) { + if (suffix_op.lhs.node.id == .Identifier) { + if (std.mem.eql(u8, getDeclName(tree, suffix_op.lhs.node).?, getDeclName(tree, &fn_decl.base).?)) { + continue; + } + } + } + } + } + if (already_found.*) return null; already_found.* = true; result = cfe; @@ -251,25 +269,28 @@ fn findReturnStatementInternal(base_node: *ast.Node, already_found: *bool) ?*ast else => {}, } - result = findReturnStatementInternal(child_node, already_found); + result = findReturnStatementInternal(tree, fn_decl, child_node, already_found); } return result; } -fn findReturnStatement(base_node: *ast.Node) ?*ast.Node.ControlFlowExpression { +fn findReturnStatement(tree: *ast.Tree, fn_decl: *ast.Node.FnProto) ?*ast.Node.ControlFlowExpression { var already_found = false; - return findReturnStatementInternal(base_node, &already_found); + return findReturnStatementInternal(tree, fn_decl, fn_decl.body_node.?, &already_found); } /// Resolves the return type of a function -fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto, current_container: *ast.Node) ?*ast.Node { +fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto) ?*ast.Node { if (isTypeFunction(analysis_ctx.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(fn_decl.body_node.?) orelse return null; + const ret = findReturnStatement(analysis_ctx.tree, fn_decl) orelse return null; if (ret.rhs) |rhs| - if (resolveTypeOfNode(analysis_ctx, rhs, current_container)) |res_rhs| switch (res_rhs.id) { - .ContainerDecl => return res_rhs, + if (resolveTypeOfNode(analysis_ctx, rhs)) |res_rhs| switch (res_rhs.id) { + .ContainerDecl => { + analysis_ctx.onContainer(res_rhs.cast(ast.Node.ContainerDecl).?) catch return null; + return res_rhs; + }, else => return null, }; @@ -277,44 +298,44 @@ fn resolveReturnType(analysis_ctx: *AnalysisContext, fn_decl: *ast.Node.FnProto, } return switch (fn_decl.return_type) { - .Explicit, .InferErrorSet => |return_type| resolveTypeOfNode(analysis_ctx, return_type, current_container), + .Explicit, .InferErrorSet => |return_type| resolveTypeOfNode(analysis_ctx, return_type), .Invalid => null, }; } /// Resolves the type of a node -pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, current_container: *ast.Node) ?*ast.Node { +pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast.Node { switch (node.id) { .VarDecl => { const vari = node.cast(ast.Node.VarDecl).?; - return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?, current_container) orelse null; + return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null; }, .ParamDecl => { const decl = node.cast(ast.Node.ParamDecl).?; switch (decl.param_type) { .var_type, .type_expr => |var_type| { - return resolveTypeOfNode(analysis_ctx, var_type, current_container) orelse null; + return resolveTypeOfNode(analysis_ctx, var_type) orelse null; }, else => {}, } }, .Identifier => { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, analysis_ctx.tree.getNodeSource(node))) |child| { - return resolveTypeOfNode(analysis_ctx, child, current_container); + return resolveTypeOfNode(analysis_ctx, child); } else return null; }, .ContainerField => { const field = node.cast(ast.Node.ContainerField).?; - return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null, current_container); + return resolveTypeOfNode(analysis_ctx, field.type_expr orelse return null); }, .SuffixOp => { const suffix_op = node.cast(ast.Node.SuffixOp).?; switch (suffix_op.op) { .Call, .StructInitializer => { - const decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node, current_container) orelse return null; + const decl = resolveTypeOfNode(analysis_ctx, suffix_op.lhs.node) orelse return null; return switch (decl.id) { - .FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?, current_container), + .FnProto => resolveReturnType(analysis_ctx, decl.cast(ast.Node.FnProto).?), else => decl, }; }, @@ -330,9 +351,9 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren var rhs_str = nodeToString(analysis_ctx.tree, infix_op.rhs) orelse return null; // 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; - const left = resolveTypeOfNode(analysis_ctx, infix_op.lhs, current_container) orelse return null; + const left = resolveTypeOfNode(analysis_ctx, infix_op.lhs) orelse return null; const child = getChild(analysis_ctx.tree, left, rhs_str) orelse return null; - return resolveTypeOfNode(analysis_ctx, child, current_container); + return resolveTypeOfNode(analysis_ctx, child); }, else => {}, } @@ -344,13 +365,13 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren .PtrType => { const op_token = analysis_ctx.tree.tokens.at(prefix_op.op_token); switch (op_token.id) { - .Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs, current_container), + .Asterisk => return resolveTypeOfNode(analysis_ctx, prefix_op.rhs), .LBracket, .AsteriskAsterisk => return null, else => unreachable, } }, .Try => { - const rhs_type = resolveTypeOfNode(analysis_ctx, prefix_op.rhs, current_container) orelse return null; + const rhs_type = resolveTypeOfNode(analysis_ctx, prefix_op.rhs) orelse return null; switch (rhs_type.id) { .InfixOp => { const infix_op = rhs_type.cast(ast.Node.InfixOp).?; @@ -368,7 +389,7 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren const call_name = analysis_ctx.tree.tokenSlice(builtin_call.builtin_token); if (std.mem.eql(u8, call_name, "@This")) { if (builtin_call.params.len != 0) return null; - return current_container; + return analysis_ctx.in_container; } if (!std.mem.eql(u8, call_name, "@import")) return null; @@ -383,7 +404,10 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node, curren break :block null; }; }, - .ContainerDecl => return node, + .ContainerDecl => { + analysis_ctx.onContainer(node.cast(ast.Node.ContainerDecl).?) catch return null; + return node; + }, .MultilineStringLiteral, .StringLiteral, .ErrorSetDecl, .FnProto => return node, else => std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}), } @@ -437,6 +461,7 @@ pub fn getFieldAccessTypeNode( line_length: usize, ) ?*ast.Node { var current_node = analysis_ctx.in_container; + var current_container = analysis_ctx.in_container; while (true) { var next = tokenizer.next(); @@ -444,7 +469,7 @@ pub fn getFieldAccessTypeNode( .Eof => return current_node, .Identifier => { if (getChildOfSlice(analysis_ctx.tree, analysis_ctx.scope_nodes, tokenizer.buffer[next.start..next.end])) |child| { - if (resolveTypeOfNode(analysis_ctx, child, current_node)) |node_type| { + if (resolveTypeOfNode(analysis_ctx, child)) |node_type| { current_node = node_type; } else return null; } else return null; @@ -458,7 +483,7 @@ pub fn getFieldAccessTypeNode( if (after_period.end == line_length) return current_node; if (getChild(analysis_ctx.tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| { - if (resolveTypeOfNode(analysis_ctx, child, current_node)) |child_type| { + if (resolveTypeOfNode(analysis_ctx, child)) |child_type| { current_node = child_type; } else return null; } else return null; @@ -468,17 +493,40 @@ pub fn getFieldAccessTypeNode( switch (current_node.id) { .FnProto => { const func = current_node.cast(ast.Node.FnProto).?; - if (resolveReturnType(analysis_ctx, func, current_node)) |ret| { + if (resolveReturnType(analysis_ctx, func)) |ret| { current_node = ret; + // Skip to the right paren + var paren_count: usize = 1; + next = tokenizer.next(); + while (next.id != .Eof) : (next = tokenizer.next()) { + if (next.id == .RParen) { + paren_count -= 1; + if (paren_count == 0) break; + } else if (next.id == .LParen) { + paren_count += 1; + } + } else return null; } else { return null; } }, - else => {} + else => {}, + } + }, + .Keyword_const, .Keyword_var => { + next = tokenizer.next(); + if (next.id == .Identifier) { + next = tokenizer.next(); + if (next.id != .Equal) return null; + continue; } }, else => std.debug.warn("Not implemented; {}\n", .{next.id}), } + + if (current_node.id == .ContainerDecl or current_node.id == .Root) { + current_container = current_node; + } } return current_node; diff --git a/src/document_store.zig b/src/document_store.zig index f2c5faf..23cd5d4 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -266,6 +266,16 @@ pub const AnalysisContext = struct { self.in_container = &self.tree.root_node.base; } + pub fn onContainer(self: *AnalysisContext, container: *std.zig.ast.Node.ContainerDecl) !void { + if (self.in_container != &container.base) { + self.in_container = &container.base; + + var scope_nodes = std.ArrayList(*std.zig.ast.Node).init(&self.arena.allocator); + try analysis.addChildrenNodes(&scope_nodes, self.tree, &container.base); + self.scope_nodes = scope_nodes.items; + } + } + pub fn onImport(self: *AnalysisContext, import_str: []const u8) !?*std.zig.ast.Node { const allocator = self.store.allocator; const final_uri = (try uriFromImportStr( diff --git a/src/main.zig b/src/main.zig index a35b7ab..720707d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -183,7 +183,7 @@ fn containerToCompletion( while (container.iterate(index)) |child_node| : (index += 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, container, config); + try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config); } } } @@ -193,7 +193,6 @@ fn nodeToCompletion( analysis_ctx: *DocumentStore.AnalysisContext, orig_handle: *DocumentStore.Handle, node: *std.zig.ast.Node, - current_container: *std.zig.ast.Node, config: Config, ) error{OutOfMemory}!void { var doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree, node)) |doc_comments| @@ -245,11 +244,11 @@ fn nodeToCompletion( break :block var_decl.init_node.?; }; - if (analysis.resolveTypeOfNode(&child_analysis_context, child_node, current_container)) |resolved_node| { + if (analysis.resolveTypeOfNode(&child_analysis_context, child_node)) |resolved_node| { // Special case for function aliases // In the future it might be used to print types of values instead of their declarations if (resolved_node.id == .FnProto) { - try nodeToCompletion(list, &child_analysis_context, orig_handle, resolved_node, current_container, config); + try nodeToCompletion(list, &child_analysis_context, orig_handle, resolved_node, config); return; } } @@ -423,7 +422,7 @@ fn completeGlobal(id: i64, pos_index: usize, handle: *DocumentStore.Handle, conf for (analysis_ctx.scope_nodes) |decl_ptr| { var decl = decl_ptr.*; - try nodeToCompletion(&completions, &analysis_ctx, handle, decl_ptr, &analysis_ctx.tree.root_node.base, config); + try nodeToCompletion(&completions, &analysis_ctx, handle, decl_ptr, config); } try send(types.Response{ @@ -451,7 +450,7 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P const line_length = line.len - line_start_idx; if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer, line_length)) |node| { - try nodeToCompletion(&completions, &analysis_ctx, handle, node, node, config); + try nodeToCompletion(&completions, &analysis_ctx, handle, node, config); } try send(types.Response{ .id = .{ .Integer = id }, @@ -796,8 +795,6 @@ fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !v const pos_index = try handle.document.positionToIndex(pos); const pos_context = documentPositionContext(handle.document, pos_index); - std.debug.warn("{}", .{pos_context}); - const this_config = configFromUriOr(uri, config); switch (pos_context) { .builtin => try send(types.Response{