[ty] Handle `Definition`s in `SemanticModel::scope` (#21919)

This commit is contained in:
Micha Reiser 2025-12-11 19:04:57 +01:00 committed by GitHub
parent c9fe4e2703
commit 34f7a04ef7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 35 additions and 1 deletions

View File

@ -239,11 +239,45 @@ impl<'db> SemanticModel<'db> {
completions
}
/// Get the scope of the given node (handles string annotations)
/// Returns the scope in which `node` is defined (handles string annotations).
pub fn scope(&self, node: ast::AnyNodeRef<'_>) -> Option<FileScopeId> {
let index = semantic_index(self.db, self.file);
match self.node_in_ast(node) {
ast::AnyNodeRef::Identifier(identifier) => index.try_expression_scope_id(identifier),
// Nodes implementing `HasDefinition`
ast::AnyNodeRef::StmtFunctionDef(function) => Some(
function
.definition(self)
.scope(self.db)
.file_scope_id(self.db),
),
ast::AnyNodeRef::StmtClassDef(class) => {
Some(class.definition(self).scope(self.db).file_scope_id(self.db))
}
ast::AnyNodeRef::Parameter(parameter) => Some(
parameter
.definition(self)
.scope(self.db)
.file_scope_id(self.db),
),
ast::AnyNodeRef::ParameterWithDefault(parameter) => Some(
parameter
.definition(self)
.scope(self.db)
.file_scope_id(self.db),
),
ast::AnyNodeRef::ExceptHandlerExceptHandler(handler) => Some(
handler
.definition(self)
.scope(self.db)
.file_scope_id(self.db),
),
ast::AnyNodeRef::TypeParamTypeVar(var) => {
Some(var.definition(self).scope(self.db).file_scope_id(self.db))
}
// Fallback
node => match node.as_expr_ref() {
// If we couldn't identify a specific
// expression that we're in, then just