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

Remove empty line before raw dostrings #7944

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ class Test:
x = 1


class EmptyLineBeforeRawDocstring:

r"""Character and line based layer over a BufferedIOBase object, buffer."""


class C(): # comment
pass

Expand Down
10 changes: 5 additions & 5 deletions crates/ruff_python_formatter/src/expression/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl Format<PyFormatContext<'_>> for NormalizedString<'_> {

bitflags! {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(super) struct StringPrefix: u8 {
pub(crate) struct StringPrefix: u8 {
const UNICODE = 0b0000_0001;
/// `r"test"`
const RAW = 0b0000_0010;
Expand All @@ -508,7 +508,7 @@ bitflags! {
}

impl StringPrefix {
pub(super) fn parse(input: &str) -> StringPrefix {
pub(crate) fn parse(input: &str) -> StringPrefix {
let chars = input.chars();
let mut prefix = StringPrefix::empty();

Expand All @@ -533,15 +533,15 @@ impl StringPrefix {
prefix
}

pub(super) const fn text_len(self) -> TextSize {
pub(crate) const fn text_len(self) -> TextSize {
TextSize::new(self.bits().count_ones())
}

pub(super) const fn is_raw_string(self) -> bool {
pub(crate) const fn is_raw_string(self) -> bool {
self.contains(StringPrefix::RAW) || self.contains(StringPrefix::RAW_UPPER)
}

pub(super) const fn is_fstring(self) -> bool {
pub(crate) const fn is_fstring(self) -> bool {
self.contains(StringPrefix::F_STRING)
}
}
Expand Down
27 changes: 12 additions & 15 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::comments::{
};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::expr_constant::ExprConstantLayout;
use crate::expression::string::StringLayout;
use crate::expression::string::{StringLayout, StringPrefix};
use crate::prelude::*;
use crate::statement::stmt_expr::FormatStmtExpr;
use crate::verbatim::{
Expand Down Expand Up @@ -99,9 +99,13 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {

SuiteKind::Class => {
if let Some(docstring) = DocstringStmt::try_from_statement(first) {
let prefix =
StringPrefix::parse(f.context().locator().slice(docstring.0.range()));
if !comments.has_leading(first)
&& lines_before(first.start(), source) > 1
&& !source_type.is_stub()
// For some reason black removes the empty line before raw docstrings
&& !prefix.is_raw_string()
{
// Allow up to one empty line before a class docstring, e.g., this is
// stable formatting:
Expand Down Expand Up @@ -484,8 +488,10 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for Suite {
}

/// A statement representing a docstring.
///
/// We keep both the outer statement and the inner constant here for convenience.
#[derive(Copy, Clone)]
pub(crate) struct DocstringStmt<'a>(&'a Stmt);
pub(crate) struct DocstringStmt<'a>(&'a Stmt, &'a ExprConstant);

impl<'a> DocstringStmt<'a> {
/// Checks if the statement is a simple string that can be formatted as a docstring
Expand All @@ -494,9 +500,9 @@ impl<'a> DocstringStmt<'a> {
return None;
};

if let Expr::Constant(ExprConstant { value, .. }) = value.as_ref() {
if !value.is_implicit_concatenated() {
return Some(DocstringStmt(stmt));
if let Expr::Constant(expr_constant @ ExprConstant { value, .. }) = value.as_ref() {
if (value.is_str() || value.is_unicode_string()) && !value.is_implicit_concatenated() {
return Some(DocstringStmt(stmt, expr_constant));
}
}

Expand All @@ -512,21 +518,12 @@ impl Format<PyFormatContext<'_>> for DocstringStmt<'_> {
if FormatStmtExpr.is_suppressed(node_comments.trailing, f.context()) {
suppressed_node(self.0).fmt(f)
} else {
// SAFETY: Safe because `DocStringStmt` guarantees that it only ever wraps a `ExprStmt` containing a `ConstantExpr`.
let constant = self
.0
.as_expr_stmt()
.unwrap()
.value
.as_constant_expr()
.unwrap();

// We format the expression, but the statement carries the comments
write!(
f,
[
leading_comments(node_comments.leading),
constant
self.1
.format()
.with_options(ExprConstantLayout::String(StringLayout::DocString)),
trailing_comments(node_comments.trailing),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ class Test:
x = 1


class EmptyLineBeforeRawDocstring:

r"""Character and line based layer over a BufferedIOBase object, buffer."""


class C(): # comment
pass

Expand Down Expand Up @@ -356,6 +361,10 @@ class Test:
x = 1


class EmptyLineBeforeRawDocstring:
r"""Character and line based layer over a BufferedIOBase object, buffer."""


class C: # comment
pass

Expand Down