Leading, Dangling, and Trailing comments formatting (#4785)

This commit is contained in:
Micha Reiser 2023-06-02 09:26:36 +02:00 committed by GitHub
parent b2498c576f
commit 5d939222db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 824 additions and 771 deletions

View file

@ -8,6 +8,7 @@ pub struct PyFormatContext<'a> {
options: SimpleFormatOptions,
contents: &'a str,
comments: Comments<'a>,
node_level: NodeLevel,
}
impl<'a> PyFormatContext<'a> {
@ -20,6 +21,7 @@ impl<'a> PyFormatContext<'a> {
options,
contents,
comments,
node_level: NodeLevel::TopLevel,
}
}
@ -33,6 +35,15 @@ impl<'a> PyFormatContext<'a> {
Locator::new(self.contents)
}
#[allow(unused)]
pub(crate) fn set_node_level(&mut self, level: NodeLevel) {
self.node_level = level;
}
pub(crate) fn node_level(&self) -> NodeLevel {
self.node_level
}
#[allow(unused)]
pub(crate) fn comments(&self) -> &Comments<'a> {
&self.comments
@ -56,7 +67,24 @@ impl Debug for PyFormatContext<'_> {
f.debug_struct("PyFormatContext")
.field("options", &self.options)
.field("comments", &self.comments.debug(self.source_code()))
.field("node_level", &self.node_level)
.field("source", &self.contents)
.finish()
}
}
/// What's the enclosing level of the outer node.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub(crate) enum NodeLevel {
/// Formatting statements on the module level.
#[default]
TopLevel,
/// Formatting nodes that are enclosed by a statement.
#[allow(unused)]
Statement,
/// Formatting nodes that are enclosed in a parenthesized expression.
#[allow(unused)]
Parenthesized,
}