mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Preserve trailing statement semicolons when using fmt: skip
(#8273)
This commit is contained in:
parent
e799f90782
commit
2c84f911c4
5 changed files with 85 additions and 3 deletions
|
@ -17,6 +17,7 @@ use crate::comments::{leading_comments, trailing_comments, SourceComment};
|
|||
use crate::prelude::*;
|
||||
use crate::statement::clause::ClauseHeader;
|
||||
use crate::statement::suite::SuiteChildStatement;
|
||||
use crate::statement::trailing_semicolon;
|
||||
|
||||
/// Disables formatting for all statements between the `first_suppressed` that has a leading `fmt: off` comment
|
||||
/// and the first trailing or leading `fmt: on` comment. The statements are formatted as they appear in the source code.
|
||||
|
@ -902,6 +903,15 @@ impl Format<PyFormatContext<'_>> for FormatSuppressedNode<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
// Some statements may end with a semicolon. Preserve the semicolon
|
||||
let semicolon_range = self
|
||||
.node
|
||||
.is_statement()
|
||||
.then(|| trailing_semicolon(self.node, f.context().source()))
|
||||
.flatten();
|
||||
let verbatim_range = semicolon_range.map_or(self.node.range(), |semicolon| {
|
||||
TextRange::new(self.node.start(), semicolon.end())
|
||||
});
|
||||
comments.mark_verbatim_node_comments_formatted(self.node);
|
||||
|
||||
// Write the outer comments and format the node as verbatim
|
||||
|
@ -909,7 +919,7 @@ impl Format<PyFormatContext<'_>> for FormatSuppressedNode<'_> {
|
|||
f,
|
||||
[
|
||||
leading_comments(node_comments.leading),
|
||||
verbatim_text(self.node),
|
||||
verbatim_text(verbatim_range),
|
||||
trailing_comments(node_comments.trailing)
|
||||
]
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue