mirror of https://github.com/astral-sh/ruff
[ty] Handle `Definition`s in `SemanticModel::scope` (#21919)
This commit is contained in:
parent
c9fe4e2703
commit
34f7a04ef7
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue