mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 03:15:44 +00:00
Leading, Dangling, and Trailing comments formatting (#4785)
This commit is contained in:
parent
b2498c576f
commit
5d939222db
47 changed files with 824 additions and 771 deletions
|
@ -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,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue