Skip to content

Commit 25cda5f

Browse files
committed
More for formatting a selection <#297>
Do a better job of getting the whitespace right when entering/leaving a selection. For trivia, only enable/disable around comments.
1 parent 2b8d41b commit 25cda5f

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Sources/SwiftFormat/API/Selection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct Selection {
5656
return ranges.contains {
5757
let maxStart = max($0.offset, range.offset)
5858
let minEnd = (min($0.end, range.end))
59-
return maxStart < minEnd
59+
return maxStart <= minEnd
6060
}
6161
}
6262
}

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SwiftSyntax
14+
import Foundation
1415

1516
/// PrettyPrinter takes a Syntax node and outputs a well-formatted, re-indented reproduction of the
1617
/// code as a String.
@@ -245,15 +246,18 @@ public class PrettyPrinter {
245246
/// Before printing the text, this function will print any line-leading indentation or interior
246247
/// leading spaces that are required before the text itself.
247248
private func write(_ text: String) {
248-
guard writingIsEnabled else { return }
249249
if isAtStartOfLine {
250-
writeRaw(currentIndentation.indentation())
250+
if writingIsEnabled {
251+
writeRaw(currentIndentation.indentation())
252+
}
251253
spaceRemaining = maxLineLength - currentIndentation.length(in: configuration)
252254
isAtStartOfLine = false
253-
} else if pendingSpaces > 0 {
255+
} else if pendingSpaces > 0 && writingIsEnabled {
254256
writeRaw(String(repeating: " ", count: pendingSpaces))
255257
}
256-
writeRaw(text)
258+
if writingIsEnabled {
259+
writeRaw(text)
260+
}
257261
consecutiveNewlineCount = 0
258262
pendingSpaces = 0
259263
}
@@ -591,13 +595,21 @@ public class PrettyPrinter {
591595
} else {
592596
end = source.endIndex
593597
}
594-
let text = String(source[start..<end])
598+
var text = String(source[start..<end])
599+
// strip trailing whitespace so that the next formatting can add the right amount
600+
if let nonWhitespace = text.rangeOfCharacter(
601+
from: CharacterSet.whitespaces.inverted, options: .backwards) {
602+
text = String(text[..<nonWhitespace.upperBound])
603+
}
604+
595605
writeRaw(text)
596606
if text.hasSuffix("\n") {
597607
isAtStartOfLine = true
598608
consecutiveNewlineCount = 1
609+
} else {
610+
isAtStartOfLine = false
611+
consecutiveNewlineCount = 0
599612
}
600-
pendingSpaces = 0
601613
self.disabledPosition = nil
602614
}
603615

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,20 +3263,23 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
32633263
var requiresNextNewline = false
32643264

32653265
for (index, piece) in trivia.enumerated() {
3266-
generateEnable(Selection.Range(start: position, end: position + piece.sourceLength))
32673266
if let cutoff = cutoffIndex, index == cutoff { break }
32683267
switch piece {
32693268
case .lineComment(let text):
32703269
if index > 0 || isStartOfFile {
3270+
generateEnable(Selection.Range(start: position, end: position + piece.sourceLength))
32713271
appendToken(.comment(Comment(kind: .line, text: text), wasEndOfLine: false))
3272+
generateDisable(position + piece.sourceLength)
32723273
appendNewlines(.soft)
32733274
isStartOfFile = false
32743275
}
32753276
requiresNextNewline = true
32763277

32773278
case .blockComment(let text):
32783279
if index > 0 || isStartOfFile {
3280+
generateEnable(Selection.Range(start: position, end: position + piece.sourceLength))
32793281
appendToken(.comment(Comment(kind: .block, text: text), wasEndOfLine: false))
3282+
generateDisable(position + piece.sourceLength)
32803283
// There is always a break after the comment to allow a discretionary newline after it.
32813284
var breakSize = 0
32823285
if index + 1 < trivia.endIndex {
@@ -3291,13 +3294,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
32913294
requiresNextNewline = false
32923295

32933296
case .docLineComment(let text):
3297+
generateEnable(Selection.Range(start: position, end: position + piece.sourceLength))
32943298
appendToken(.comment(Comment(kind: .docLine, text: text), wasEndOfLine: false))
3299+
generateDisable(position + piece.sourceLength)
32953300
appendNewlines(.soft)
32963301
isStartOfFile = false
32973302
requiresNextNewline = true
32983303

32993304
case .docBlockComment(let text):
3305+
generateEnable(Selection.Range(start: position, end: position + piece.sourceLength))
33003306
appendToken(.comment(Comment(kind: .docBlock, text: text), wasEndOfLine: false))
3307+
generateDisable(position + piece.sourceLength)
33013308
appendNewlines(.soft)
33023309
isStartOfFile = false
33033310
requiresNextNewline = false
@@ -3337,7 +3344,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
33373344
break
33383345
}
33393346
position += piece.sourceLength
3340-
generateDisable(position)
33413347
}
33423348
}
33433349

0 commit comments

Comments
 (0)