diff --git a/src/document_store.zig b/src/document_store.zig index 0e7c90c..a32a870 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -327,6 +327,7 @@ fn refreshDocument(self: *DocumentStore, handle: *Handle, zig_lib_path: ?[]const var import_strs = std.ArrayList([]const u8).init(&arena.allocator); try analysis.collectImports(&import_strs, handle.tree); + // @TODO: Check final resolved import path and not just import string const still_exist = try arena.allocator.alloc(bool, handle.import_uris.items.len); for (still_exist) |*ex| { ex.* = false; @@ -576,12 +577,14 @@ pub const AnalysisContext = struct { return &self.tree().root_node.base; } - pub fn clone(self: *AnalysisContext) AnalysisContext { + pub fn clone(self: *AnalysisContext) !AnalysisContext { + // Copy the cope nodes, the rest are references + // that are not owned by the context. return AnalysisContext{ .store = self.store, .handle = self.handle, .arena = self.arena, - .scope_nodes = self.scope_nodes, + .scope_nodes = try std.mem.dupe(&self.arena.allocator, *std.zig.ast.Node, self.scope_nodes), .in_container = self.in_container, .std_uri = self.std_uri, }; diff --git a/src/main.zig b/src/main.zig index 51dc5bc..9f1cfc4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -189,8 +189,8 @@ const ResolveVarDeclFnAliasRewsult = struct { analysis_ctx: DocumentStore.AnalysisContext, }; -fn resolveVarDeclFnAlias(analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) ResolveVarDeclFnAliasRewsult { - var child_analysis_context = analysis_ctx.clone(); +fn resolveVarDeclFnAlias(analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !ResolveVarDeclFnAliasRewsult { + var child_analysis_context = try analysis_ctx.clone(); if (decl.cast(std.zig.ast.Node.VarDecl)) |var_decl| { const child_node = block: { if (var_decl.type_node) |type_node| { @@ -204,11 +204,17 @@ fn resolveVarDeclFnAlias(analysis_ctx: *DocumentStore.AnalysisContext, decl: *st if (analysis.resolveTypeOfNode(&child_analysis_context, child_node)) |resolved_node| { if (resolved_node.id == .FnProto) { - return .{ .decl = resolved_node, .analysis_ctx = child_analysis_context }; + return ResolveVarDeclFnAliasRewsult{ + .decl = resolved_node, + .analysis_ctx = child_analysis_context, + }; } } } - return .{ .decl = decl, .analysis_ctx = analysis_ctx.* }; + return ResolveVarDeclFnAliasRewsult{ + .decl = decl, + .analysis_ctx = analysis_ctx.*, + }; } fn nodeToCompletion( @@ -254,7 +260,7 @@ fn nodeToCompletion( const var_decl = node.cast(std.zig.ast.Node.VarDecl).?; const is_const = analysis_ctx.tree().token_ids[var_decl.mut_token] == .Keyword_const; - var result = resolveVarDeclFnAlias(analysis_ctx, node); + var result = try resolveVarDeclFnAlias(analysis_ctx, node); if (result.decl != node) { return try nodeToCompletion(list, &result.analysis_ctx, orig_handle, result.decl, config); } @@ -312,7 +318,7 @@ fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []cons } fn gotoDefinitionSymbol(id: i64, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !void { - const result = resolveVarDeclFnAlias(analysis_ctx, decl); + const result = try resolveVarDeclFnAlias(analysis_ctx, decl); const name_token = analysis.getDeclNameToken(result.analysis_ctx.tree(), result.decl) orelse unreachable; @@ -328,7 +334,7 @@ fn gotoDefinitionSymbol(id: i64, analysis_ctx: *DocumentStore.AnalysisContext, d } fn hoverSymbol(id: i64, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !void { - const result = resolveVarDeclFnAlias(analysis_ctx, decl); + const result = try resolveVarDeclFnAlias(analysis_ctx, decl); const str_value = switch (result.decl.id) { .VarDecl => blk: {