Made lookupGlobalSymbol start the search from the innermost scope that contains the source index

This commit is contained in:
Alexandros Naskos 2021-04-04 15:28:57 +03:00
parent 04372999aa
commit 4bee4e662b
No known key found for this signature in database
GPG Key ID: 02BF2E72B0EA32D2
2 changed files with 24 additions and 11 deletions

View File

@ -2250,21 +2250,26 @@ pub fn iterateSymbolsGlobal(
return try iterateSymbolsGlobalInternal(store, arena, handle, source_index, callback, context, &use_trail); return try iterateSymbolsGlobalInternal(store, arena, handle, source_index, callback, context, &use_trail);
} }
pub fn innermostScope(handle: DocumentStore.Handle, source_index: usize) ast.Node.Index { pub fn innermostBlockScopeIndex(handle: DocumentStore.Handle, source_index: usize) usize {
var current = handle.document_scope.scopes[0].data.container; if (handle.document_scope.scopes.len == 1) return 0;
if (handle.document_scope.scopes.len == 1) return current;
for (handle.document_scope.scopes[1..]) |scope| { var current: usize = 0;
for (handle.document_scope.scopes[1..]) |*scope, idx| {
if (source_index >= scope.range.start and source_index <= scope.range.end) { if (source_index >= scope.range.start and source_index <= scope.range.end) {
switch (scope.data) { switch (scope.data) {
.container, .function, .block => |node| current = node, .container, .function, .block => |node| current = idx + 1,
else => {}, else => {},
} }
} }
if (scope.range.start > source_index) break;
} }
return current; return current;
} }
pub fn innermostBlockScope(handle: DocumentStore.Handle, source_index: usize) ast.Node.Index {
return handle.document_scope.scopes[innermostBlockScopeIndex(handle, source_index)].toNodeIndex().?;
}
pub fn innermostContainer(handle: *DocumentStore.Handle, source_index: usize) TypeWithHandle { pub fn innermostContainer(handle: *DocumentStore.Handle, source_index: usize) TypeWithHandle {
var current = handle.document_scope.scopes[0].data.container; var current = handle.document_scope.scopes[0].data.container;
if (handle.document_scope.scopes.len == 1) return TypeWithHandle.typeVal(.{ .node = current, .handle = handle }); if (handle.document_scope.scopes.len == 1) return TypeWithHandle.typeVal(.{ .node = current, .handle = handle });
@ -2351,7 +2356,11 @@ fn lookupSymbolGlobalInternal(
source_index: usize, source_index: usize,
use_trail: *std.ArrayList(*const ast.Node.Index), use_trail: *std.ArrayList(*const ast.Node.Index),
) error{OutOfMemory}!?DeclWithHandle { ) error{OutOfMemory}!?DeclWithHandle {
for (handle.document_scope.scopes) |scope| { const innermost_scope_idx = innermostBlockScopeIndex(handle.*, source_index);
var curr = innermost_scope_idx;
while (curr >= 0) : (curr -= 1) {
const scope = &handle.document_scope.scopes[curr];
if (source_index >= scope.range.start and source_index <= scope.range.end) { if (source_index >= scope.range.start and source_index <= scope.range.end) {
if (scope.decls.getEntry(symbol)) |candidate| { if (scope.decls.getEntry(symbol)) |candidate| {
switch (candidate.value) { switch (candidate.value) {
@ -2366,13 +2375,10 @@ fn lookupSymbolGlobalInternal(
.handle = handle, .handle = handle,
}; };
} }
if (try resolveUse(store, arena, scope.uses, symbol, handle, use_trail)) |result| return result; if (try resolveUse(store, arena, scope.uses, symbol, handle, use_trail)) |result| return result;
} }
if (curr == 0) break;
if (scope.range.start > source_index) return null;
} }
return null; return null;
} }
@ -2521,6 +2527,13 @@ pub const Scope = struct {
uses: []const *const ast.Node.Index, uses: []const *const ast.Node.Index,
data: Data, data: Data,
pub fn toNodeIndex(self: Scope) ?ast.Node.Index {
return switch (self.data) {
.container, .function, .block => |idx| idx,
else => null,
};
}
}; };
pub fn makeDocumentScope(allocator: *std.mem.Allocator, tree: ast.Tree) !DocumentScope { pub fn makeDocumentScope(allocator: *std.mem.Allocator, tree: ast.Tree) !DocumentScope {

View File

@ -95,7 +95,7 @@ pub fn getSignatureInfo(
absolute_index: usize, absolute_index: usize,
comptime data: type, comptime data: type,
) !?types.SignatureInformation { ) !?types.SignatureInformation {
const innermost_block = analysis.innermostScope(handle.*, absolute_index); const innermost_block = analysis.innermostBlockScope(handle.*, absolute_index);
const tree = handle.tree; const tree = handle.tree;
const token_tags = tree.tokens.items(.tag); const token_tags = tree.tokens.items(.tag);
const token_starts = tree.tokens.items(.start); const token_starts = tree.tokens.items(.start);