mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
Format all attribute dot comments manually (#6825)
## Summary This PR modifies our formatting of comments around the `.` in an attribute. Specifically, the goal here is to avoid _reordering_ comments, and the net effect is that we generally leave comments where-they-are when dealing with comments between around the dot (which you can also think of as comments between attributes). All comments around the dot are now treated as dangling and formatted manually, with the exception of end-of-line or parenthesized comments on the value, like those marked as trailing here, which remain trailing: ```python ( ( a # trailing end-of-line # trailing own-line ) # dangling before dot end-of-line .b # trailing end-of-line ) ``` Closes https://github.com/astral-sh/ruff/issues/6823. ## Test Plan `cargo test` Before: | project | similarity index | |--------------|------------------| | cpython | 0.76050 | | django | 0.99820 | | transformers | 0.99800 | | twine | 0.99876 | | typeshed | 0.99953 | | warehouse | 0.99615 | | zulip | 0.99729 | After: | project | similarity index | |--------------|------------------| | cpython | 0.76050 | | django | 0.99820 | | transformers | 0.99800 | | twine | 0.99876 | | typeshed | 0.99953 | | warehouse | 0.99615 | | zulip | 0.99729 |
This commit is contained in:
parent
6f23469e00
commit
474e8fbcd4
5 changed files with 216 additions and 128 deletions
|
@ -19,6 +19,24 @@ pub fn first_non_trivia_token(offset: TextSize, code: &str) -> Option<SimpleToke
|
|||
.next()
|
||||
}
|
||||
|
||||
/// Returns the only non-trivia, non-closing parenthesis token in `range`.
|
||||
///
|
||||
/// Includes debug assertions that the range only contains that single token.
|
||||
pub fn find_only_token_in_range(
|
||||
range: TextRange,
|
||||
token_kind: SimpleTokenKind,
|
||||
code: &str,
|
||||
) -> SimpleToken {
|
||||
let mut tokens = SimpleTokenizer::new(code, range)
|
||||
.skip_trivia()
|
||||
.skip_while(|token| token.kind == SimpleTokenKind::RParen);
|
||||
let token = tokens.next().expect("Expected a token");
|
||||
debug_assert_eq!(token.kind(), token_kind);
|
||||
let mut tokens = tokens.skip_while(|token| token.kind == SimpleTokenKind::LParen);
|
||||
debug_assert_eq!(tokens.next(), None);
|
||||
token
|
||||
}
|
||||
|
||||
/// Returns the number of newlines between `offset` and the first non whitespace character in the source code.
|
||||
pub fn lines_before(offset: TextSize, code: &str) -> u32 {
|
||||
let mut cursor = Cursor::new(&code[TextRange::up_to(offset)]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue