Change handles HashMap value type to pointers instead of values themselves
This commit is contained in:
		
							parent
							
								
									6cd98697c0
								
							
						
					
					
						commit
						d79f3bc809
					
				@ -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 => {
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
 | 
			
		||||
@ -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{
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user