Skip to content

Commit

Permalink
perf: improve local variable reference finding (#1917)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillLillis authored Jun 18, 2024
1 parent 2f7343e commit cfea2d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3811,7 +3811,7 @@ pub const DeclWithHandle = struct {
};
}

fn isPublic(self: DeclWithHandle) bool {
pub fn isPublic(self: DeclWithHandle) bool {
return switch (self.decl) {
.ast_node => |node| isNodePublic(self.handle.tree, node),
else => true,
Expand Down
18 changes: 15 additions & 3 deletions src/features/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Ast = std.zig.Ast;
const log = std.log.scoped(.zls_references);

const Server = @import("../Server.zig");
const DocumentScope = @import("../DocumentScope.zig");
const DocumentStore = @import("../DocumentStore.zig");
const Analyser = @import("../analysis.zig");
const types = @import("../lsp.zig");
Expand Down Expand Up @@ -208,14 +209,13 @@ fn gatherReferences(
fn symbolReferences(
allocator: std.mem.Allocator,
analyser: *Analyser,
request: GeneralReferencesRequest,
decl_handle: Analyser.DeclWithHandle,
encoding: offsets.Encoding,
/// add `decl_handle` as a references
include_decl: bool,
/// exclude references from the std library
skip_std_references: bool,
/// search other files for references
workspace: bool,
) error{OutOfMemory}!std.ArrayListUnmanaged(types.Location) {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
Expand All @@ -236,6 +236,18 @@ fn symbolReferences(
switch (decl_handle.decl) {
.ast_node => {
try builder.collectReferences(curr_handle, 0);

const source_index = offsets.tokenToIndex(decl_handle.handle.tree, decl_handle.nameToken());
// highlight requests only pertain to the current document, otherwise we can try to narrow things down
const workspace = if (request == .highlight) false else blk: {
const doc_scope = try curr_handle.getDocumentScope();
const scope_index = Analyser.innermostBlockScopeIndex(doc_scope, source_index).unwrap() orelse break :blk true;
break :blk switch (doc_scope.getScopeTag(scope_index)) {
.function, .block => false,
.container, .container_usingnamespace => decl_handle.isPublic(),
.other => true,
};
};
if (workspace) {
try gatherReferences(allocator, analyser, curr_handle, skip_std_references, include_decl, &builder, .get);
}
Expand Down Expand Up @@ -464,11 +476,11 @@ pub fn referencesHandler(server: *Server, arena: std.mem.Allocator, request: Gen
try symbolReferences(
arena,
&analyser,
request,
decl,
server.offset_encoding,
include_decl,
server.config.skip_std_references,
request != .highlight, // scan the entire workspace except for highlight
);

switch (request) {
Expand Down

0 comments on commit cfea2d5

Please sign in to comment.