mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
[ty] Lightly refactor document symbols AST visitor
This makes use of early continue/return to keep rightward drift under control. (I also find it easier to read.)
This commit is contained in:
parent
205eae14d2
commit
e7237652a9
1 changed files with 47 additions and 47 deletions
|
@ -456,60 +456,60 @@ impl SourceOrderVisitor<'_> for SymbolVisitor {
|
|||
|
||||
Stmt::Assign(assign) => {
|
||||
// Include assignments only when we're in global or class scope
|
||||
if !self.in_function {
|
||||
for target in &assign.targets {
|
||||
if let Expr::Name(name) = target {
|
||||
let kind = if Self::is_constant_name(name.id.as_str()) {
|
||||
SymbolKind::Constant
|
||||
} else if self
|
||||
.iter_symbol_stack()
|
||||
.any(|s| s.kind == SymbolKind::Class)
|
||||
{
|
||||
SymbolKind::Field
|
||||
} else {
|
||||
SymbolKind::Variable
|
||||
};
|
||||
if self.in_function {
|
||||
return;
|
||||
}
|
||||
for target in &assign.targets {
|
||||
let Expr::Name(name) = target else { continue };
|
||||
let kind = if Self::is_constant_name(name.id.as_str()) {
|
||||
SymbolKind::Constant
|
||||
} else if self
|
||||
.iter_symbol_stack()
|
||||
.any(|s| s.kind == SymbolKind::Class)
|
||||
{
|
||||
SymbolKind::Field
|
||||
} else {
|
||||
SymbolKind::Variable
|
||||
};
|
||||
|
||||
let symbol = SymbolTree {
|
||||
parent: None,
|
||||
name: name.id.to_string(),
|
||||
kind,
|
||||
name_range: name.range(),
|
||||
full_range: stmt.range(),
|
||||
};
|
||||
|
||||
self.add_symbol(symbol);
|
||||
}
|
||||
}
|
||||
let symbol = SymbolTree {
|
||||
parent: None,
|
||||
name: name.id.to_string(),
|
||||
kind,
|
||||
name_range: name.range(),
|
||||
full_range: stmt.range(),
|
||||
};
|
||||
self.add_symbol(symbol);
|
||||
}
|
||||
}
|
||||
|
||||
Stmt::AnnAssign(ann_assign) => {
|
||||
// Include assignments only when we're in global or class scope
|
||||
if !self.in_function {
|
||||
if let Expr::Name(name) = &*ann_assign.target {
|
||||
let kind = if Self::is_constant_name(name.id.as_str()) {
|
||||
SymbolKind::Constant
|
||||
} else if self
|
||||
.iter_symbol_stack()
|
||||
.any(|s| s.kind == SymbolKind::Class)
|
||||
{
|
||||
SymbolKind::Field
|
||||
} else {
|
||||
SymbolKind::Variable
|
||||
};
|
||||
|
||||
let symbol = SymbolTree {
|
||||
parent: None,
|
||||
name: name.id.to_string(),
|
||||
kind,
|
||||
name_range: name.range(),
|
||||
full_range: stmt.range(),
|
||||
};
|
||||
|
||||
self.add_symbol(symbol);
|
||||
}
|
||||
if self.in_function {
|
||||
return;
|
||||
}
|
||||
let Expr::Name(name) = &*ann_assign.target else {
|
||||
return;
|
||||
};
|
||||
let kind = if Self::is_constant_name(name.id.as_str()) {
|
||||
SymbolKind::Constant
|
||||
} else if self
|
||||
.iter_symbol_stack()
|
||||
.any(|s| s.kind == SymbolKind::Class)
|
||||
{
|
||||
SymbolKind::Field
|
||||
} else {
|
||||
SymbolKind::Variable
|
||||
};
|
||||
|
||||
let symbol = SymbolTree {
|
||||
parent: None,
|
||||
name: name.id.to_string(),
|
||||
kind,
|
||||
name_range: name.range(),
|
||||
full_range: stmt.range(),
|
||||
};
|
||||
self.add_symbol(symbol);
|
||||
}
|
||||
|
||||
_ => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue