From 2755d8d8b70efa2052bc0880d325c0dadd9672fe Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Mon, 17 Oct 2022 21:20:27 +0200 Subject: [PATCH] simplify dependency collection in references.zig --- src/DocumentStore.zig | 8 ++++---- src/references.zig | 32 +++++--------------------------- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/DocumentStore.zig b/src/DocumentStore.zig index e2e85a9..7d53b46 100644 --- a/src/DocumentStore.zig +++ b/src/DocumentStore.zig @@ -258,7 +258,7 @@ fn garbageCollectionImports(self: *DocumentStore) error{OutOfMemory}!void { 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| { @@ -268,7 +268,7 @@ fn garbageCollectionImports(self: *DocumentStore) error{OutOfMemory}!void { 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; @@ -763,8 +763,8 @@ fn collectCIncludes(self: *const DocumentStore, handle: Handle) error{OutOfMemor /// collects every file uri the given handle depends on /// includes imports, cimports & packages pub fn collectDependencies( - allocator: std.mem.Allocator, store: *const DocumentStore, + allocator: std.mem.Allocator, handle: Handle, dependencies: *std.ArrayListUnmanaged(Uri), ) error{OutOfMemory}!void { @@ -879,7 +879,7 @@ fn tagStoreCompletionItems(self: DocumentStore, arena: std.mem.Allocator, handle var dependencies = std.ArrayListUnmanaged(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 var result_set = analysis.CompletionSet{}; diff --git a/src/references.zig b/src/references.zig index 6dfadb0..b55b559 100644 --- a/src/references.zig +++ b/src/references.zig @@ -483,41 +483,19 @@ pub fn symbolReferences( if (decl_handle.decl.* != .ast_node) return builder.locations; if (!workspace) return builder.locations; - var imports = std.ArrayListUnmanaged(*const DocumentStore.Handle){}; - for (store.handles.values()) |handle| { 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)) continue; } - // Check entry's transitive imports - try imports.append(arena.allocator(), handle); - 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; + var dependencies = std.ArrayListUnmanaged([]const u8){}; + try store.collectDependencies(store.allocator, handle.*, &dependencies); - if (h == curr_handle) { - // entry does import curr_handle - try symbolReferencesInternal(&builder, 0, handle); - break :blk; - } - - select: { - for (imports.items) |item| { - if (item == h) { - // already checked this import - break :select; - } - } - try imports.append(arena.allocator(), h); - } - } + for (dependencies.items) |uri| { + const hdl = store.getHandle(uri) orelse continue; + try symbolReferencesInternal(&builder, 0, hdl); } - try imports.resize(arena.allocator(), 0); } }, .param_payload => |pay| blk: {