Skip to content

Commit

Permalink
improve perf of handle_dict_unpacking_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszotten committed Jun 19, 2023
1 parent 54b3a88 commit 7be8e80
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,46 +911,34 @@ fn handle_dict_unpacking_comment<'a>(
return CommentPlacement::Default(comment);
}
};
if let Some(following) = comment.following_node() {
let preceding_end = match comment.preceding_node() {
Some(preceding) => preceding.end(),
None => comment.enclosing_node().start(),
};
let mut tokens_before = SimpleTokenizer::new(
locator.contents(),
TextRange::new(preceding_end, comment.slice().start()),
)
.skip_trivia()
.peekable();

while let Some(it) = tokens_before.next() {
if it.kind == TokenKind::Star
&& tokens_before.peek().map(|t| t.kind) == Some(TokenKind::Star)
{
return CommentPlacement::trailing(following, comment);
}
}
}

if let Some(preceding) = comment.preceding_node() {
let following_start = match comment.following_node() {
Some(following) => following.start(),
None => comment.enclosing_node().end(),
};
let mut tokens_after = SimpleTokenizer::new(
locator.contents(),
TextRange::new(comment.slice().end(), following_start),
)
.skip_trivia()
.peekable();
let Some(following) = comment.following_node() else {
return CommentPlacement::Default(comment);
};

while let Some(it) = tokens_after.next() {
if it.kind == TokenKind::Star
&& tokens_after.peek().map(|t| t.kind) == Some(TokenKind::Star)
{
return CommentPlacement::trailing(preceding, comment);
}
let preceding_end = match comment.preceding_node() {
Some(preceding) => preceding.end(),
None => comment.enclosing_node().start(),
};
let range_start = preceding_end + TextSize::new(1);
if range_start > comment.slice().start() {
return CommentPlacement::Default(comment);
}
let tokens = SimpleTokenizer::new(
locator.contents(),
TextRange::new(range_start, comment.slice().start()),
)
.skip_trivia();

let mut count = 0;
for token in tokens {
if token.kind != TokenKind::Star {
return CommentPlacement::Default(comment);
}
count += 1;
}
if count == 2 {
return CommentPlacement::trailing(following, comment);
}

CommentPlacement::Default(comment)
Expand Down

0 comments on commit 7be8e80

Please sign in to comment.