Skip to content

Commit

Permalink
check the skipped token type
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszotten committed Jun 20, 2023
1 parent fb8fa49 commit 8d97593
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::comments::CommentTextPosition;
use crate::trivia::{SimpleTokenizer, TokenKind};
use crate::trivia::{SimpleTokenizer, Token, TokenKind};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::whitespace;
Expand Down Expand Up @@ -909,25 +909,41 @@ fn handle_dict_unpacking_comment<'a>(
}
};

// no node after our comment so we can't be between `**` and the name (node)
let Some(following) = comment.following_node() else {
return CommentPlacement::Default(comment);
};

// we look at tokens between the previous node (or the start of the dict)
// and the comment
let preceding_end = match comment.preceding_node() {
Some(preceding) => preceding.end(),
None => comment.enclosing_node().start(),
};
if preceding_end > comment.slice().start() {
return CommentPlacement::Default(comment);
}
let tokens = SimpleTokenizer::new(
let mut tokens = SimpleTokenizer::new(
locator.contents(),
TextRange::new(preceding_end, comment.slice().start()),
)
.skip_trivia();

// we start from the preceding node but we skip its token
if let Some(first) = tokens.next() {
debug_assert!(matches!(
first,
Token {
kind: TokenKind::LBrace | TokenKind::Comma | TokenKind::Colon,
..
}
));
}

// if the remaining tokens from the previous node is exactly `**`,
// re-assign the comment to the one that follows the stars
let mut count = 0;
for token in tokens.skip(1) {
for token in tokens {
if token.kind != TokenKind::Star {
return CommentPlacement::Default(comment);
}
Expand Down

0 comments on commit 8d97593

Please sign in to comment.