simplify dependency collection in references.zig

This commit is contained in:
Techatrix 2022-10-17 21:20:27 +02:00
parent 8fb7379d71
commit 2755d8d8b7
2 changed files with 9 additions and 31 deletions

View File

@ -258,7 +258,7 @@ fn garbageCollectionImports(self: *DocumentStore) error{OutOfMemory}!void {
try reachable_handles.put(self.allocator, handle.uri, {}); try reachable_handles.put(self.allocator, handle.uri, {});
try collectDependencies(self.allocator, self, handle.*, &queue); try self.collectDependencies(self.allocator, handle.*, &queue);
} }
while (queue.popOrNull()) |uri| { while (queue.popOrNull()) |uri| {
@ -268,7 +268,7 @@ fn garbageCollectionImports(self: *DocumentStore) error{OutOfMemory}!void {
const handle = self.handles.get(uri) orelse continue; const handle = self.handles.get(uri) orelse continue;
try collectDependencies(self.allocator, self, handle.*, &queue); try self.collectDependencies(self.allocator, handle.*, &queue);
} }
var i: usize = 0; var i: usize = 0;
@ -763,8 +763,8 @@ fn collectCIncludes(self: *const DocumentStore, handle: Handle) error{OutOfMemor
/// collects every file uri the given handle depends on /// collects every file uri the given handle depends on
/// includes imports, cimports & packages /// includes imports, cimports & packages
pub fn collectDependencies( pub fn collectDependencies(
allocator: std.mem.Allocator,
store: *const DocumentStore, store: *const DocumentStore,
allocator: std.mem.Allocator,
handle: Handle, handle: Handle,
dependencies: *std.ArrayListUnmanaged(Uri), dependencies: *std.ArrayListUnmanaged(Uri),
) error{OutOfMemory}!void { ) error{OutOfMemory}!void {
@ -879,7 +879,7 @@ fn tagStoreCompletionItems(self: DocumentStore, arena: std.mem.Allocator, handle
var dependencies = std.ArrayListUnmanaged(Uri){}; var dependencies = std.ArrayListUnmanaged(Uri){};
try dependencies.append(self.allocator, handle.uri); try dependencies.append(self.allocator, handle.uri);
try collectDependencies(self.allocator, &self, handle, &dependencies); try self.collectDependencies(self.allocator, handle, &dependencies);
// TODO Better solution for deciding what tags to include // TODO Better solution for deciding what tags to include
var result_set = analysis.CompletionSet{}; var result_set = analysis.CompletionSet{};

View File

@ -483,41 +483,19 @@ pub fn symbolReferences(
if (decl_handle.decl.* != .ast_node) return builder.locations; if (decl_handle.decl.* != .ast_node) return builder.locations;
if (!workspace) return builder.locations; if (!workspace) return builder.locations;
var imports = std.ArrayListUnmanaged(*const DocumentStore.Handle){};
for (store.handles.values()) |handle| { for (store.handles.values()) |handle| {
if (skip_std_references and std.mem.indexOf(u8, handle.uri, "std") != null) { if (skip_std_references and std.mem.indexOf(u8, handle.uri, "std") != null) {
if (!include_decl or !std.mem.eql(u8, handle.uri, curr_handle.uri)) if (!include_decl or !std.mem.eql(u8, handle.uri, curr_handle.uri))
continue; continue;
} }
// Check entry's transitive imports var dependencies = std.ArrayListUnmanaged([]const u8){};
try imports.append(arena.allocator(), handle); try store.collectDependencies(store.allocator, handle.*, &dependencies);
var i: usize = 0;
blk: while (i < imports.items.len) : (i += 1) {
const import = imports.items[i];
// TODO handle cimports
for (import.import_uris.items) |uri| {
const h = store.getHandle(uri) orelse break;
if (h == curr_handle) { for (dependencies.items) |uri| {
// entry does import curr_handle const hdl = store.getHandle(uri) orelse continue;
try symbolReferencesInternal(&builder, 0, handle); try symbolReferencesInternal(&builder, 0, hdl);
break :blk;
} }
select: {
for (imports.items) |item| {
if (item == h) {
// already checked this import
break :select;
}
}
try imports.append(arena.allocator(), h);
}
}
}
try imports.resize(arena.allocator(), 0);
} }
}, },
.param_payload => |pay| blk: { .param_payload => |pay| blk: {