Skip to content

Commit

Permalink
State: exclude trailing space/CR from column width
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 30, 2025
1 parent 18171fc commit 4bed184
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ class FormatWriter(formatOps: FormatOps) {
): Boolean = {
val codeStyle = style.copy(
runner = style.runner.forCodeBlock,
lineEndings = Some(LineEndings.unix),
// let's not wrap docstrings, to avoid recursion
docstrings = style.docstrings.withoutRewrites,
maxColumn = style.maxColumn - spaces.length - termIndent.length - 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ object State {
val firstLineLength = column + syntaxLen
(firstLineLength, firstLineLength)
} else {
val firstLength = column + firstNL
val firstLength = column + getLineLength(syntax, 0, firstNL)
tok match {
case _: T.Constant.String =>
val margin: Int => Int = stringMargin
Expand Down Expand Up @@ -453,6 +453,9 @@ object State {
_ => adjusted
} else identity)

def getLineLength(syntax: String, lineBeg: Int, lineEnd: Int): Int =
(syntax.lastIndexWhere(!_.isWhitespace, lineEnd - 1) + 1 - lineBeg).max(0)

private def getColumnsFromMultiline(
syntax: String,
firstNL: Int,
Expand All @@ -461,7 +464,9 @@ object State {
@tailrec
def iter(prevMaxLength: Int, lineBeg: Int): (Int, Int) = {
val nextNL = syntax.indexOf('\n', lineBeg)
val length = (if (nextNL < 0) syntax.length else nextNL) - lineBeg
val length =
if (nextNL < 0) syntax.length - lineBeg
else getLineLength(syntax, lineBeg, nextNL)
val maxLength = math.max(prevMaxLength, length)
if (nextNL < 0) (maxLength, length) else iter(maxLength, nextNL + 1)
}
Expand Down

0 comments on commit 4bed184

Please sign in to comment.