[ty] Handle definitions in `SemanticModel::scope`

This commit is contained in:
Micha Reiser 2025-12-11 16:13:41 +01:00
parent 5a9d6a91ea
commit c4ba7e63a6
No known key found for this signature in database
1 changed files with 31 additions and 1 deletions

View File

@ -236,11 +236,41 @@ impl<'db> SemanticModel<'db> {
completions 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> { pub fn scope(&self, node: ast::AnyNodeRef<'_>) -> Option<FileScopeId> {
let index = semantic_index(self.db, self.file); let index = semantic_index(self.db, self.file);
match self.node_in_ast(node) { match self.node_in_ast(node) {
ast::AnyNodeRef::Identifier(identifier) => index.try_expression_scope_id(identifier), ast::AnyNodeRef::Identifier(identifier) => index.try_expression_scope_id(identifier),
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))
}
node => match node.as_expr_ref() { node => match node.as_expr_ref() {
// If we couldn't identify a specific // If we couldn't identify a specific
// expression that we're in, then just // expression that we're in, then just