mirror of https://github.com/astral-sh/ruff
[ty] Handle definitions in `SemanticModel::scope`
This commit is contained in:
parent
5a9d6a91ea
commit
c4ba7e63a6
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue