Skip to content

Commit

Permalink
Insert newline between docstring and following own line comment
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Oct 25, 2023
1 parent 31032f4 commit 3f98c27
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ def docstring_that_ends_with_quote_and_a_line_break3():
"""


class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.

def __init__(self):
pass


class CommentAfterDocstring2:
"""Browse module classes and functions in IDLE."""

# This class is also the base class for pathbrowser.PathBrowser.

def __init__(self):
pass


class CommentAfterDocstring3:
"""Browse module classes and functions in IDLE."""

# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down
33 changes: 32 additions & 1 deletion crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::cmp::Ordering;

use ruff_python_ast::whitespace::indentation;
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{self as ast, Comprehension, Expr, MatchCase, ModModule, Parameters};
use ruff_python_ast::{AnyNodeRef, Constant};
use ruff_python_trivia::{
find_only_token_in_range, indentation_at_offset, BackwardsTokenizer, CommentRanges,
SimpleToken, SimpleTokenKind, SimpleTokenizer,
Expand Down Expand Up @@ -544,6 +544,37 @@ fn handle_own_line_comment_between_statements<'a>(
return CommentPlacement::Default(comment);
}

if comment.line_position().is_end_of_line() {
return CommentPlacement::Default(comment);
}

// Comments after docstrings need a newline between the docstring and the comment, so we attach
// it as leading on the first statement after the docstring.
// (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 AnyNodeRef::StmtExpr(preceding_expr) = preceding {
// We can't use `is_docstring_stmt` here because there is no `AnyNodeRef` to `&Stmt`
// conversion
if is_first_statement_in_body(preceding, comment.enclosing_node())
&& matches!(
preceding_expr.value.as_ref(),
Expr::Constant(ast::ExprConstant {
value: Constant::Str { .. },
..
})
)
{
return CommentPlacement::leading(following, comment);
}
}

// If the comment is directly attached to the following statement; make it a leading
// comment:
// ```python
Expand Down
129 changes: 129 additions & 0 deletions crates/ruff_python_formatter/tests/snapshots/format@docstring.py.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ def docstring_that_ends_with_quote_and_a_line_break3():
"""
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring2:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring3:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -213,6 +238,32 @@ def docstring_that_ends_with_quote_and_a_line_break3():
"""he said "the news of my death have been greatly exaggerated" """
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring2:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring3:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -327,6 +378,32 @@ def docstring_that_ends_with_quote_and_a_line_break3():
"""he said "the news of my death have been greatly exaggerated" """
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring2:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring3:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -441,6 +518,32 @@ def docstring_that_ends_with_quote_and_a_line_break3():
"""he said "the news of my death have been greatly exaggerated" """
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring2:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring3:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -555,6 +658,32 @@ def docstring_that_ends_with_quote_and_a_line_break3():
"""he said "the news of my death have been greatly exaggerated" """
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring2:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class CommentAfterDocstring3:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down

0 comments on commit 3f98c27

Please sign in to comment.