Repository: psf/black
File: src/black/lines.py (Python)

Code before:
            )

        if (
            self.previous_line
            and self.previous_line.is_import
            and not current_line.is_import
            and not current_line.is_fmt_pass_converted(first_leaf_matches=is_import)
            and depth == self.previous_line.depth
        ):
            return (before or 1), 0

        if (
            self.previous_line
            and self.previous_line.is_class
            and current_line.is_triple_quoted_string
        ):
            if Preview.no_blank_line_before_class_docstring in current_line.mode:
                return 0, 1
            return before, 1

        # In preview mode, always allow blank lines, except right before a function
        # docstring
        is_empty_first_line_ok = (
            Preview.allow_empty_first_line_in_block in current_line.mode
            and (
                not is_docstring(current_line.leaves[0])
                or (
                    self.previous_line
                    and self.previous_line.leaves[0]
                    and self.previous_line.leaves[0].parent
                    and not is_funcdef(self.previous_line.leaves[0].parent)
                )
            )
        )

        if (
            self.previous_line
            and self.previous_line.opens_block
            and not is_empty_first_line_ok
        ):
            return 0, 0
        return before, 0

    def _maybe_empty_lines_for_class_or_def(  # noqa: C901
        self, current_line: Line, before: int, user_had_newline: bool
    ) -> Tuple[int, int]:
        if not current_line.is_decorator:
            self.previous_defs.append(current_line)
        if self.previous_line is None:
            # Don't insert empty lines before the first line in the file.
            return 0, 0

Review comment (suggestion):
Btw a few lines up we have `and current_line.is_triple_quoted_string`. Should that also use `is_docstring`? Seems preferable to have consistent behaviour for single quote docstrings as well

Code after:
                    before = 2

        if current_line.is_decorator or current_line.is_def or current_line.is_class:
            return self._maybe_empty_lines_for_class_or_def(
                current_line, before, user_had_newline
            )

        if (
            self.previous_line
            and self.previous_line.is_import
            and not current_line.is_import
            and not current_line.is_fmt_pass_converted(first_leaf_matches=is_import)
            and depth == self.previous_line.depth
        ):
            return (before or 1), 0

        if (
            self.previous_line
            and self.previous_line.is_class
            and current_line.is_triple_quoted_string
        ):
            if Preview.no_blank_line_before_class_docstring in current_line.mode:
                return 0, 1
            return before, 1

        # In preview mode, always allow blank lines, except right before a function
        # docstring
        is_empty_first_line_ok = (
            Preview.allow_empty_first_line_in_block in current_line.mode
            and (
                not is_docstring(current_line.leaves[0])
                or (
                    self.previous_line
                    and self.previous_line.leaves[0]
                    and self.previous_line.leaves[0].parent
                    and not is_funcdef(self.previous_line.leaves[0].parent)
                )
            )
        )

        if (
            self.previous_line
            and self.previous_line.opens_block
            and not is_empty_first_line_ok
        ):
            return 0, 0
        return before, 0

    def _maybe_empty_lines_for_class_or_def(  # noqa: C901
        self, current_line: Line, before: int, user_had_newline: bool
    ) -> Tuple[int, int]: