Skip to content

Commit 6154e28

Browse files
committed
simplify Analyser.innermostScope* functions
1 parent 2995f08 commit 6154e28

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/analysis.zig

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3867,7 +3867,7 @@ pub const Type = struct {
38673867
return;
38683868
}
38693869
if (token >= 1 and tree.tokenTag(token - 1) == .keyword_return) blk: {
3870-
const function_scope = innermostFunctionScopeAtIndex(doc_scope, tree.tokenStart(token - 1)).unwrap() orelse break :blk;
3870+
const function_scope = innermostScopeAtIndexWithTag(doc_scope, tree.tokenStart(token - 1), .initOne(.function)).unwrap() orelse break :blk;
38713871
const function_node = doc_scope.getScopeAstNode(function_scope).?;
38723872
var buf: [1]Ast.Node.Index = undefined;
38733873
const func = tree.fullFnProto(&buf, function_node).?;
@@ -5293,36 +5293,28 @@ pub fn iterateLabels(handle: *DocumentStore.Handle, source_index: usize, comptim
52935293
}
52945294
}
52955295

5296-
pub fn innermostScopeAtIndex(document_scope: DocumentScope, source_index: usize) Scope.Index {
5297-
var scope_iterator = iterateEnclosingScopes(&document_scope, source_index);
5298-
var scope_index: Scope.Index = scope_iterator.next().unwrap().?; // the DocumentScope's root scope must exist
5299-
while (scope_iterator.next().unwrap()) |inner_scope| {
5300-
scope_index = inner_scope;
5301-
}
5302-
return scope_index;
5296+
pub fn innermostScopeAtIndex(
5297+
document_scope: DocumentScope,
5298+
source_index: usize,
5299+
) Scope.Index {
5300+
return innermostScopeAtIndexWithTag(document_scope, source_index, .initFull()).unwrap().?;
53035301
}
53045302

5305-
pub fn innermostFunctionScopeAtIndex(document_scope: DocumentScope, source_index: usize) Scope.OptionalIndex {
5303+
pub fn innermostScopeAtIndexWithTag(
5304+
document_scope: DocumentScope,
5305+
source_index: usize,
5306+
tag_filter: std.EnumSet(Scope.Tag),
5307+
) Scope.OptionalIndex {
53065308
var scope_iterator = iterateEnclosingScopes(&document_scope, source_index);
53075309
var scope_index: Scope.OptionalIndex = .none;
53085310
while (scope_iterator.next().unwrap()) |inner_scope| {
5309-
if (document_scope.getScopeTag(inner_scope) != .function) continue;
5311+
const scope_tag = document_scope.getScopeTag(inner_scope);
5312+
if (!tag_filter.contains(scope_tag)) continue;
53105313
scope_index = inner_scope.toOptional();
53115314
}
53125315
return scope_index;
53135316
}
53145317

5315-
pub fn innermostBlockScope(document_scope: DocumentScope, source_index: usize) Ast.Node.Index {
5316-
var scope_iterator = iterateEnclosingScopes(&document_scope, source_index);
5317-
var ast_node: ?Ast.Node.Index = null;
5318-
while (scope_iterator.next().unwrap()) |inner_scope| {
5319-
if (document_scope.getScopeAstNode(inner_scope)) |node| {
5320-
ast_node = node;
5321-
}
5322-
}
5323-
return ast_node.?; // the DocumentScope's root scope is guaranteed to have an Ast Node
5324-
}
5325-
53265318
pub fn innermostContainer(analyser: *Analyser, handle: *DocumentStore.Handle, source_index: usize) error{OutOfMemory}!Type {
53275319
const tree = handle.tree;
53285320
const document_scope = try handle.getDocumentScope();

src/features/signature_help.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,14 @@ pub fn getSignatureInfo(
7878
markup_kind: types.MarkupKind,
7979
) !?types.SignatureInformation {
8080
const document_scope = try handle.getDocumentScope();
81-
const innermost_block = Analyser.innermostBlockScope(document_scope, absolute_index);
81+
const innermost_block_scope = Analyser.innermostScopeAtIndexWithTag(document_scope, absolute_index, .init(.{
82+
.block = true,
83+
.container = true,
84+
.container_usingnamespace = true,
85+
.function = true,
86+
.other = false,
87+
})).unwrap().?;
88+
const innermost_block = document_scope.getScopeAstNode(innermost_block_scope).?;
8289
const tree = handle.tree;
8390

8491
// Use the innermost scope to determine the earliest token we would need

0 commit comments

Comments
 (0)