Skip to content

Commit

Permalink
PrettyPrinterPerformance Optimized the PrettyPrinter for #894
Browse files Browse the repository at this point in the history
Worked to get the perfomance to be closer to where we were before the changes in #883. This code should be only about 1.5% slower rather than 7% slower.
  • Loading branch information
macshome committed Dec 13, 2024
1 parent a871b81 commit 557d19f
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions Sources/SwiftFormat/PrettyPrint/PrettyPrintBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,15 @@ struct PrettyPrintBuffer {
// In case of comments, we may get a multi-line string.
// To account for that case, we need to correct the lineNumber count.
// The new column is only the position within the last line.
let lines = text.split(separator: "\n")
lineNumber += lines.count - 1
if lines.count > 1 {
// in case we have inserted new lines, we need to reset the column
column = lines.last?.count ?? 0
} else {
// in case it is an end of line comment or a single line comment,
// we just add to the current column
column += lines.last?.count ?? 0
let lines = text.count { $0 == "\n" }
lineNumber += lines
guard lines > 1, let lastNewlineIndex = text.lastIndex(of: "\n") else {
// Handle case where only one line exists.
column += text.count
return
}
let lastLine = text[text.index(after: lastNewlineIndex)...]
column = lastLine.count
}

/// Request that the given number of spaces be printed out before the next text token.
Expand Down

0 comments on commit 557d19f

Please sign in to comment.