Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert newline between docstring and following own line comment #8216

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,45 @@ class ByteDocstring:
b""" has leading whitespace"""
first_statement = 1

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 CommentAfterDocstring4:
"""Browse module classes and functions in IDLE."""


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


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


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down
9 changes: 7 additions & 2 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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::{
self as ast, AnyNodeRef, Comprehension, Expr, MatchCase, ModModule, Parameters,
};
use ruff_python_trivia::{
find_only_token_in_range, indentation_at_offset, BackwardsTokenizer, CommentRanges,
SimpleToken, SimpleTokenKind, SimpleTokenizer,
Expand Down Expand Up @@ -544,6 +545,10 @@ fn handle_own_line_comment_between_statements<'a>(
return CommentPlacement::Default(comment);
}

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

// If the comment is directly attached to the following statement; make it a leading
// comment:
// ```python
Expand Down
25 changes: 23 additions & 2 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,30 @@ impl Format<PyFormatContext<'_>> for DocstringStmt<'_> {
string_literal
.format()
.with_options(StringLayout::DocString),
trailing_comments(node_comments.trailing),
]
)
)?;

// 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)?;
}
}

trailing_comments(node_comments.trailing).fmt(f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only be applied to class docstrings, right? (Wouldn't it also apply to function docstrings as written?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@konstin - Sorry to ping, did this get addressed? I don't see test cases for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #8375

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,11 @@ d={'a':1,
# fmt: on
goes + here,
andhere,
@@ -122,8 +123,10 @@
@@ -120,10 +121,13 @@

The comments between will be formatted. This is a known limitation.
"""
+
# fmt: off

- # hey, that won't work
Expand All @@ -237,7 +240,7 @@ d={'a':1,
# fmt: on
pass

@@ -138,7 +141,7 @@
@@ -138,7 +142,7 @@
now . considers . multiple . fmt . directives . within . one . prefix
# fmt: on
# fmt: off
Expand All @@ -246,7 +249,7 @@ d={'a':1,
# fmt: on


@@ -178,14 +181,18 @@
@@ -178,14 +182,18 @@
$
""",
# fmt: off
Expand Down Expand Up @@ -395,6 +398,7 @@ def off_and_on_without_data():

The comments between will be formatted. This is a known limitation.
"""

# fmt: off


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,45 @@ class ByteDocstring:
b""" has leading whitespace"""
first_statement = 1

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 CommentAfterDocstring4:
"""Browse module classes and functions in IDLE."""


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


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


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -222,6 +261,46 @@ class ByteDocstring:
first_statement = 1


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 CommentAfterDocstring4:
"""Browse module classes and functions in IDLE."""

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


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

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


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -341,6 +420,46 @@ class ByteDocstring:
first_statement = 1


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 CommentAfterDocstring4:
"""Browse module classes and functions in IDLE."""

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


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

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


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -460,6 +579,46 @@ class ByteDocstring:
first_statement = 1


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 CommentAfterDocstring4:
"""Browse module classes and functions in IDLE."""

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


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

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


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -579,6 +738,46 @@ class ByteDocstring:
first_statement = 1


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 CommentAfterDocstring4:
"""Browse module classes and functions in IDLE."""

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


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

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


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down
Loading