Include decorators in Function and Class definition ranges (#4467)

This commit is contained in:
Micha Reiser 2023-05-22 17:50:42 +02:00 committed by GitHub
parent 9308e939f4
commit daadd24bde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 198 additions and 136 deletions

View file

@ -1,7 +1,10 @@
use std::num::TryFromIntError;
use std::ops::{Deref, Index, IndexMut};
use crate::model::SemanticModel;
use bitflags::bitflags;
use ruff_python_ast::helpers;
use ruff_python_ast::source_code::Locator;
use ruff_text_size::TextRange;
use crate::node::NodeId;
@ -108,6 +111,18 @@ impl<'a> Binding<'a> {
}
existing.is_definition()
}
/// Returns the appropriate visual range for highlighting this binding.
pub fn trimmed_range(&self, semantic_model: &SemanticModel, locator: &Locator) -> TextRange {
match self.kind {
BindingKind::ClassDefinition | BindingKind::FunctionDefinition => {
self.source.map_or(self.range, |source| {
helpers::identifier_range(semantic_model.stmts[source], locator)
})
}
_ => self.range,
}
}
}
/// ID uniquely identifying a [Binding] in a program.