diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py index f419cb9b047eb7..d7d4d9b119b173 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring.py @@ -136,6 +136,13 @@ class CommentAfterDocstring5: # This class is also the base class for pathbrowser.PathBrowser. +def f(): + """Browse module classes and functions in IDLE.""" + # ^ Do not insert a newline above here + + pass + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index a78f692d5c985f..ae84d901f36a40 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -587,23 +587,25 @@ impl Format> for DocstringStmt<'_> { ] )?; - // Comments after docstrings need a newline between the docstring and the comment. - // (https://github.com/astral-sh/ruff/issues/7948) - // ```python - // class ModuleBrowser: - // """Browse module classes and functions in IDLE.""" - // # ^ Insert a newline above here - // - // def __init__(self, master, path, *, _htest=False, _utest=False): - // pass - // ``` - if let Some(own_line) = node_comments - .trailing - .iter() - .find(|comment| comment.line_position().is_own_line()) - { - if lines_before(own_line.start(), f.context().source()) < 2 { - empty_line().fmt(f)?; + if matches!(self.0, Stmt::ClassDef(_)) { + // Comments after docstrings need a newline between the docstring and the comment. + // (https://github.com/astral-sh/ruff/issues/7948) + // ```python + // class ModuleBrowser: + // """Browse module classes and functions in IDLE.""" + // # ^ Insert a newline above here + // + // def __init__(self, master, path, *, _htest=False, _utest=False): + // pass + // ``` + if let Some(own_line) = node_comments + .trailing + .iter() + .find(|comment| comment.line_position().is_own_line()) + { + if lines_before(own_line.start(), f.context().source()) < 2 { + empty_line().fmt(f)?; + } } } diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__fmtonoff.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__fmtonoff.py.snap index ef41670ec02318..ce417c728b56be 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__fmtonoff.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__fmtonoff.py.snap @@ -225,11 +225,8 @@ d={'a':1, # fmt: on goes + here, andhere, -@@ -120,10 +121,13 @@ - - The comments between will be formatted. This is a known limitation. +@@ -122,8 +123,10 @@ """ -+ # fmt: off - # hey, that won't work @@ -240,7 +237,7 @@ d={'a':1, # fmt: on pass -@@ -138,7 +142,7 @@ +@@ -138,7 +141,7 @@ now . considers . multiple . fmt . directives . within . one . prefix # fmt: on # fmt: off @@ -249,7 +246,7 @@ d={'a':1, # fmt: on -@@ -178,14 +182,18 @@ +@@ -178,14 +181,18 @@ $ """, # fmt: off @@ -398,7 +395,6 @@ def off_and_on_without_data(): The comments between will be formatted. This is a known limitation. """ - # fmt: off diff --git a/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap index 134dee817aa372..64d76094d39872 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap @@ -142,6 +142,13 @@ class CommentAfterDocstring5: # This class is also the base class for pathbrowser.PathBrowser. +def f(): + """Browse module classes and functions in IDLE.""" + # ^ Do not insert a newline above here + + pass + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -263,7 +270,6 @@ class ByteDocstring: class CommentAfterDocstring1: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. def __init__(self): @@ -297,10 +303,16 @@ class CommentAfterDocstring4: class CommentAfterDocstring5: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. +def f(): + """Browse module classes and functions in IDLE.""" + # ^ Do not insert a newline above here + + pass + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -422,7 +434,6 @@ class ByteDocstring: class CommentAfterDocstring1: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. def __init__(self): @@ -456,10 +467,16 @@ class CommentAfterDocstring4: class CommentAfterDocstring5: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. +def f(): + """Browse module classes and functions in IDLE.""" + # ^ Do not insert a newline above here + + pass + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -581,7 +598,6 @@ class ByteDocstring: class CommentAfterDocstring1: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. def __init__(self): @@ -615,10 +631,16 @@ class CommentAfterDocstring4: class CommentAfterDocstring5: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. +def f(): + """Browse module classes and functions in IDLE.""" + # ^ Do not insert a newline above here + + pass + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting @@ -740,7 +762,6 @@ class ByteDocstring: class CommentAfterDocstring1: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. def __init__(self): @@ -774,10 +795,16 @@ class CommentAfterDocstring4: class CommentAfterDocstring5: """Browse module classes and functions in IDLE.""" - # This class is also the base class for pathbrowser.PathBrowser. +def f(): + """Browse module classes and functions in IDLE.""" + # ^ Do not insert a newline above here + + pass + + class TabbedIndent: def tabbed_indent(self): """check for correct tabbed formatting