mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Track formatting all comments
We currently don't format all comments as match statements are not yet implemented. We can work around this for the top level match statement by setting them manually formatted but the mocked-out top level match doesn't call into its children so they would still have unformatted comments
This commit is contained in:
parent
e2f7862404
commit
39beeb61f7
4 changed files with 91 additions and 25 deletions
|
@ -101,6 +101,7 @@ pub(crate) use format::{
|
||||||
};
|
};
|
||||||
use ruff_formatter::{SourceCode, SourceCodeSlice};
|
use ruff_formatter::{SourceCode, SourceCodeSlice};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
|
use ruff_python_ast::visitor::preorder::{PreorderVisitor, TraversalSignal};
|
||||||
use ruff_python_index::CommentRanges;
|
use ruff_python_index::CommentRanges;
|
||||||
|
|
||||||
use crate::comments::debug::{DebugComment, DebugComments};
|
use crate::comments::debug::{DebugComment, DebugComments};
|
||||||
|
@ -401,6 +402,24 @@ impl<'a> Comments<'a> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline(always)]
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
pub(crate) fn mark_verbatim_node_comments_formatted(&self, node: AnyNodeRef) {}
|
||||||
|
|
||||||
|
/// Marks the comments of a node printed in verbatim (suppressed) as formatted.
|
||||||
|
///
|
||||||
|
/// Marks the dangling comments and the comments of all descendants as formatted.
|
||||||
|
/// The leading and trailing comments are not marked as formatted because they are formatted
|
||||||
|
/// normally if `node` is the first or last node of a suppression range.
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
pub(crate) fn mark_verbatim_node_comments_formatted(&self, node: AnyNodeRef) {
|
||||||
|
for dangling in self.dangling_comments(node) {
|
||||||
|
dangling.mark_formatted();
|
||||||
|
}
|
||||||
|
|
||||||
|
node.visit_preorder(&mut MarkVerbatimCommentsAsFormattedVisitor(self));
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns an object that implements [Debug] for nicely printing the [`Comments`].
|
/// Returns an object that implements [Debug] for nicely printing the [`Comments`].
|
||||||
pub(crate) fn debug(&'a self, source_code: SourceCode<'a>) -> DebugComments<'a> {
|
pub(crate) fn debug(&'a self, source_code: SourceCode<'a>) -> DebugComments<'a> {
|
||||||
DebugComments::new(&self.data.comments, source_code)
|
DebugComments::new(&self.data.comments, source_code)
|
||||||
|
@ -412,6 +431,18 @@ struct CommentsData<'a> {
|
||||||
comments: CommentsMap<'a>,
|
comments: CommentsMap<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct MarkVerbatimCommentsAsFormattedVisitor<'a>(&'a Comments<'a>);
|
||||||
|
|
||||||
|
impl<'a> PreorderVisitor<'a> for MarkVerbatimCommentsAsFormattedVisitor<'a> {
|
||||||
|
fn enter_node(&mut self, node: AnyNodeRef<'a>) -> TraversalSignal {
|
||||||
|
for comment in self.0.leading_dangling_trailing_comments(node) {
|
||||||
|
comment.mark_formatted();
|
||||||
|
}
|
||||||
|
|
||||||
|
TraversalSignal::Traverse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use insta::assert_debug_snapshot;
|
use insta::assert_debug_snapshot;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::context::PyFormatContext;
|
use crate::context::PyFormatContext;
|
||||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
use crate::{FormatNodeRule, PyFormatter};
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use ruff_formatter::FormatResult;
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::ExprFormattedValue;
|
use ruff_python_ast::ExprFormattedValue;
|
||||||
|
|
||||||
|
@ -9,8 +9,8 @@ use ruff_python_ast::ExprFormattedValue;
|
||||||
pub struct FormatExprFormattedValue;
|
pub struct FormatExprFormattedValue;
|
||||||
|
|
||||||
impl FormatNodeRule<ExprFormattedValue> for FormatExprFormattedValue {
|
impl FormatNodeRule<ExprFormattedValue> for FormatExprFormattedValue {
|
||||||
fn fmt_fields(&self, item: &ExprFormattedValue, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, _item: &ExprFormattedValue, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
write!(f, [not_yet_implemented(item)])
|
unreachable!("Handled inside of `FormatExprFString");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ use ruff_formatter::{
|
||||||
PrintError,
|
PrintError,
|
||||||
};
|
};
|
||||||
use ruff_formatter::{Formatted, Printed, SourceCode};
|
use ruff_formatter::{Formatted, Printed, SourceCode};
|
||||||
use ruff_python_ast::node::{AnyNodeRef, AstNode, NodeKind};
|
use ruff_python_ast::node::{AnyNodeRef, AstNode};
|
||||||
use ruff_python_ast::{Mod, Ranged};
|
use ruff_python_ast::{Mod, Ranged};
|
||||||
use ruff_python_index::{CommentRanges, CommentRangesBuilder};
|
use ruff_python_index::{CommentRanges, CommentRangesBuilder};
|
||||||
use ruff_python_parser::lexer::{lex, LexicalError};
|
use ruff_python_parser::lexer::{lex, LexicalError};
|
||||||
|
@ -150,25 +150,30 @@ pub fn format_node<'a>(
|
||||||
|
|
||||||
let locator = Locator::new(source);
|
let locator = Locator::new(source);
|
||||||
|
|
||||||
format!(
|
let formatted = format!(
|
||||||
PyFormatContext::new(options, locator.contents(), comments),
|
PyFormatContext::new(options, locator.contents(), comments),
|
||||||
[root.format()]
|
[root.format()]
|
||||||
)
|
)?;
|
||||||
|
formatted
|
||||||
|
.context()
|
||||||
|
.comments()
|
||||||
|
.assert_formatted_all_comments(SourceCode::new(source));
|
||||||
|
Ok(formatted)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct NotYetImplemented(NodeKind);
|
pub(crate) struct NotYetImplemented<'a>(AnyNodeRef<'a>);
|
||||||
|
|
||||||
/// Formats a placeholder for nodes that have not yet been implemented
|
/// Formats a placeholder for nodes that have not yet been implemented
|
||||||
pub(crate) fn not_yet_implemented<'a, T>(node: T) -> NotYetImplemented
|
pub(crate) fn not_yet_implemented<'a, T>(node: T) -> NotYetImplemented<'a>
|
||||||
where
|
where
|
||||||
T: Into<AnyNodeRef<'a>>,
|
T: Into<AnyNodeRef<'a>>,
|
||||||
{
|
{
|
||||||
NotYetImplemented(node.into().kind())
|
NotYetImplemented(node.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Format<PyFormatContext<'_>> for NotYetImplemented {
|
impl Format<PyFormatContext<'_>> for NotYetImplemented<'_> {
|
||||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
let text = std::format!("NOT_YET_IMPLEMENTED_{:?}", self.0);
|
let text = std::format!("NOT_YET_IMPLEMENTED_{:?}", self.0.kind());
|
||||||
|
|
||||||
f.write_element(FormatElement::Tag(Tag::StartVerbatim(
|
f.write_element(FormatElement::Tag(Tag::StartVerbatim(
|
||||||
tag::VerbatimKind::Verbatim {
|
tag::VerbatimKind::Verbatim {
|
||||||
|
@ -181,31 +186,51 @@ impl Format<PyFormatContext<'_>> for NotYetImplemented {
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
f.write_element(FormatElement::Tag(Tag::EndVerbatim))?;
|
f.write_element(FormatElement::Tag(Tag::EndVerbatim))?;
|
||||||
|
|
||||||
|
f.context()
|
||||||
|
.comments()
|
||||||
|
.mark_verbatim_node_comments_formatted(self.0);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct NotYetImplementedCustomText(&'static str);
|
pub(crate) struct NotYetImplementedCustomText<'a> {
|
||||||
|
|
||||||
/// Formats a placeholder for nodes that have not yet been implemented
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub(crate) const fn not_yet_implemented_custom_text(
|
|
||||||
text: &'static str,
|
text: &'static str,
|
||||||
) -> NotYetImplementedCustomText {
|
node: AnyNodeRef<'a>,
|
||||||
NotYetImplementedCustomText(text)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Format<PyFormatContext<'_>> for NotYetImplementedCustomText {
|
/// Formats a placeholder for nodes that have not yet been implemented
|
||||||
|
pub(crate) fn not_yet_implemented_custom_text<'a, T>(
|
||||||
|
text: &'static str,
|
||||||
|
node: T,
|
||||||
|
) -> NotYetImplementedCustomText<'a>
|
||||||
|
where
|
||||||
|
T: Into<AnyNodeRef<'a>>,
|
||||||
|
{
|
||||||
|
NotYetImplementedCustomText {
|
||||||
|
text,
|
||||||
|
node: node.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Format<PyFormatContext<'_>> for NotYetImplementedCustomText<'_> {
|
||||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
f.write_element(FormatElement::Tag(Tag::StartVerbatim(
|
f.write_element(FormatElement::Tag(Tag::StartVerbatim(
|
||||||
tag::VerbatimKind::Verbatim {
|
tag::VerbatimKind::Verbatim {
|
||||||
length: self.0.text_len(),
|
length: self.text.text_len(),
|
||||||
},
|
},
|
||||||
)))?;
|
)))?;
|
||||||
|
|
||||||
text(self.0).fmt(f)?;
|
text(self.text).fmt(f)?;
|
||||||
|
|
||||||
f.write_element(FormatElement::Tag(Tag::EndVerbatim))
|
f.write_element(FormatElement::Tag(Tag::EndVerbatim))?;
|
||||||
|
|
||||||
|
f.context()
|
||||||
|
.comments()
|
||||||
|
.mark_verbatim_node_comments_formatted(self.node);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
||||||
fn fmt_fields(&self, item: &MatchCase, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, item: &MatchCase, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
let MatchCase {
|
let MatchCase {
|
||||||
range: _,
|
range: _,
|
||||||
pattern: _,
|
pattern,
|
||||||
guard,
|
guard,
|
||||||
body,
|
body,
|
||||||
} = item;
|
} = item;
|
||||||
|
@ -24,7 +24,17 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
||||||
[
|
[
|
||||||
text("case"),
|
text("case"),
|
||||||
space(),
|
space(),
|
||||||
not_yet_implemented_custom_text("NOT_YET_IMPLEMENTED_Pattern"),
|
format_with(|f: &mut PyFormatter| {
|
||||||
|
let comments = f.context().comments();
|
||||||
|
|
||||||
|
for comment in comments.leading_trailing_comments(pattern) {
|
||||||
|
// This is a lie, but let's go with it.
|
||||||
|
comment.mark_formatted();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the whole `format_with` with `pattern.format()` once pattern formatting is implemented.
|
||||||
|
not_yet_implemented_custom_text("NOT_YET_IMPLEMENTED_Pattern", pattern).fmt(f)
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue