From 26fb68ea0dfa2c442e02fabbaeabc2118df39706 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 4 Oct 2023 11:01:22 +0530 Subject: [PATCH] Use correct start location for class/function clause header --- crates/ruff_python_formatter/src/statement/clause.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/ruff_python_formatter/src/statement/clause.rs b/crates/ruff_python_formatter/src/statement/clause.rs index 449b64e2996440..92626096c1bacf 100644 --- a/crates/ruff_python_formatter/src/statement/clause.rs +++ b/crates/ruff_python_formatter/src/statement/clause.rs @@ -203,15 +203,23 @@ impl<'a> ClauseHeader<'a> { fn first_keyword_range(self, source: &str) -> FormatResult { match self { ClauseHeader::Class(header) => { - find_keyword(header.start(), SimpleTokenKind::Class, source) + let start_position = header + .decorator_list + .last() + .map_or_else(|| header.start(), |decorator| decorator.end()); + find_keyword(start_position, SimpleTokenKind::Class, source) } ClauseHeader::Function(header) => { + let start_position = header + .decorator_list + .last() + .map_or_else(|| header.start(), |decorator| decorator.end()); let keyword = if header.is_async { SimpleTokenKind::Async } else { SimpleTokenKind::Def }; - find_keyword(header.start(), keyword, source) + find_keyword(start_position, keyword, source) } ClauseHeader::If(header) => find_keyword(header.start(), SimpleTokenKind::If, source), ClauseHeader::ElifElse(ElifElseClause {