diff --git a/src/analysis.zig b/src/analysis.zig index c10f610..bd4062a 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -194,6 +194,7 @@ pub fn resolveTypeOfNode(analysis_ctx: *AnalysisContext, node: *ast.Node) ?*ast. switch (node.id) { .VarDecl => { const vari = node.cast(ast.Node.VarDecl).?; + return resolveTypeOfNode(analysis_ctx, vari.type_node orelse vari.init_node.?) orelse null; }, .FnProto => { diff --git a/src/document_store.zig b/src/document_store.zig index 89176b2..073dca8 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -35,12 +35,12 @@ pub const Handle = struct { }; allocator: *std.mem.Allocator, -handles: std.StringHashMap(Handle), +handles: std.StringHashMap(*Handle), std_uri: ?[]const u8, pub fn init(self: *DocumentStore, allocator: *std.mem.Allocator, zig_lib_path: ?[]const u8) !void { self.allocator = allocator; - self.handles = std.StringHashMap(Handle).init(allocator); + self.handles = std.StringHashMap(*Handle).init(allocator); errdefer self.handles.deinit(); if (zig_lib_path) |zpath| { @@ -71,7 +71,10 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) !*Handle { self.allocator.free(text); } - var handle = Handle{ + var handle = try self.allocator.create(Handle); + errdefer self.allocator.destroy(handle); + + handle.* = Handle{ .count = 1, .import_uris = std.ArrayList([]const u8).init(self.allocator), .document = .{ @@ -81,9 +84,9 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) !*Handle { .sane_text = null, }, }; - try self.checkSanity(&handle); + try self.checkSanity(handle); try self.handles.putNoClobber(uri, handle); - return &(self.handles.get(uri) orelse unreachable).value; + return (self.handles.get(uri) orelse unreachable).value; } pub fn openDocument(self: *DocumentStore, uri: []const u8, text: []const u8) !*Handle { @@ -91,7 +94,7 @@ pub fn openDocument(self: *DocumentStore, uri: []const u8, text: []const u8) !*H std.debug.warn("Document already open: {}, incrementing count\n", .{uri}); entry.value.count += 1; std.debug.warn("New count: {}\n", .{entry.value.count}); - return &entry.value; + return entry.value; } const duped_text = try std.mem.dupe(self.allocator, u8, text); @@ -124,6 +127,7 @@ fn decrementCount(self: *DocumentStore, uri: []const u8) void { const uri_key = entry.key; self.handles.removeAssertDiscard(uri); self.allocator.free(uri_key); + self.allocator.destroy(entry.value); } } @@ -133,7 +137,7 @@ pub fn closeDocument(self: *DocumentStore, uri: []const u8) void { pub fn getHandle(self: *DocumentStore, uri: []const u8) ?*Handle { if (self.handles.get(uri)) |entry| { - return &entry.value; + return entry.value; } return null; @@ -394,6 +398,7 @@ pub fn deinit(self: *DocumentStore) void { entry.value.import_uris.deinit(); self.allocator.free(entry.key); + self.allocator.destroy(entry.value); } self.handles.deinit(); diff --git a/src/main.zig b/src/main.zig index 7246159..726292d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -287,13 +287,12 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P var tokenizer = std.zig.Tokenizer.init(line_copy); 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; while (node.iterate(index)) |child_node| { if (analysis.isNodePublic(analysis_ctx.tree, child_node)) { // TODO: Not great to allocate it again and again inside a loop // Creating a new context, so that we don't destroy the tree that is iterated above when resolving imports - const initial_handle = document_store.getHandle(initial_document) orelse continue; + const initial_handle = analysis_ctx.handle; std.debug.warn("\ncompleteFieldAccess calling resolveTypeOfNode for {}\n", .{analysis_ctx.tree.getNodeSource(child_node)}); var node_analysis_ctx = (try document_store.analysisContext(initial_handle, &arena)) orelse { return send(types.Response{