Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct start location for class/function clause header #7802

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ class Test:
def test():
pass


# Regression test for https://github.com/astral-sh/ruff/issues/7735
@decorator1
@decorator2
class Foo: # fmt: skip
pass


# Regression test for https://github.com/astral-sh/ruff/issues/7735
@decorator1
@decorator2
def foo(): # fmt: skip
pass
12 changes: 10 additions & 2 deletions crates/ruff_python_formatter/src/statement/clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,23 @@ impl<'a> ClauseHeader<'a> {
fn first_keyword_range(self, source: &str) -> FormatResult<TextRange> {
match self {
ClauseHeader::Class(header) => {
find_keyword(header.start(), SimpleTokenKind::Class, source)
let start_position = header
.decorator_list
.last()
.map_or_else(|| header.start(), Ranged::end);
find_keyword(start_position, SimpleTokenKind::Class, source)
}
ClauseHeader::Function(header) => {
let start_position = header
.decorator_list
.last()
.map_or_else(|| header.start(), Ranged::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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ class Test:
def test():
pass


# Regression test for https://github.com/astral-sh/ruff/issues/7735
@decorator1
@decorator2
class Foo: # fmt: skip
pass


# Regression test for https://github.com/astral-sh/ruff/issues/7735
@decorator1
@decorator2
def foo(): # fmt: skip
pass
```

## Output
Expand All @@ -43,6 +56,20 @@ class Test:
# leading class comment
def test():
pass


# Regression test for https://github.com/astral-sh/ruff/issues/7735
@decorator1
@decorator2
class Foo: # fmt: skip
pass


# Regression test for https://github.com/astral-sh/ruff/issues/7735
@decorator1
@decorator2
def foo(): # fmt: skip
pass
```


Expand Down
Loading