Skip to content

Commit 4558882

Browse files
committed
From review: add some comments, rename some methods, and reorganize tests.
1 parent 9195d90 commit 4558882

File tree

6 files changed

+159
-147
lines changed

6 files changed

+159
-147
lines changed

Sources/SwiftFormat/API/Selection.swift

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public enum Selection {
1818
case infinite
1919
case ranges([Range<AbsolutePosition>])
2020

21+
/// Create a selection from an array of utf8 ranges. An empty array means an infinite selection.
2122
public init(offsetPairs: [Range<Int>]) {
2223
if offsetPairs.isEmpty {
2324
self = .infinite

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

+10
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,18 @@ public class PrettyPrinter {
6868
private let maxLineLength: Int
6969
private var tokens: [Token]
7070
private var source: String
71+
72+
/// keep track of where formatting was disabled in the original source
73+
///
74+
/// To format a selection, we insert `enableFormatting`/`disableFormatting` tokens into the
75+
/// stream when entering/exiting a selection range. Those tokens include utf8 offsets into the
76+
/// original source. When enabling formatting, we copy the text between `disabledPosition` and the
77+
/// current position to `outputBuffer`. From then on, we continue to format until the next
78+
/// `disableFormatting` token.
7179
private var disabledPosition: AbsolutePosition? = nil
80+
/// true if we're currently formatting
7281
private var writingIsEnabled: Bool { disabledPosition == nil }
82+
7383
private var outputBuffer: String = ""
7484

7585
/// The number of spaces remaining on the current line.

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -2735,7 +2735,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
27352735
extractLeadingTrivia(token)
27362736
closeScopeTokens.forEach(appendToken)
27372737

2738-
generateEnable(
2738+
generateEnableFormattingIfNecessary(
27392739
token.positionAfterSkippingLeadingTrivia ..< token.endPositionBeforeTrailingTrivia
27402740
)
27412741

@@ -2744,7 +2744,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
27442744
appendToken(.syntax(token.presence == .present ? token.text : ""))
27452745
}
27462746

2747-
generateDisable(token.endPositionBeforeTrailingTrivia)
2747+
generateDisableFormattingIfNecessary(token.endPositionBeforeTrailingTrivia)
27482748

27492749
appendTrailingTrivia(token)
27502750
appendAfterTokensAndTrailingComments(token)
@@ -2753,15 +2753,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
27532753
return .skipChildren
27542754
}
27552755

2756-
func generateEnable(_ range: Range<AbsolutePosition>) {
2756+
private func generateEnableFormattingIfNecessary(_ range: Range<AbsolutePosition>) {
27572757
if case .infinite = selection { return }
27582758
if !isInsideSelection && selection.overlapsOrTouches(range) {
27592759
appendToken(.enableFormatting(range.lowerBound))
27602760
isInsideSelection = true
27612761
}
27622762
}
27632763

2764-
func generateDisable(_ position: AbsolutePosition) {
2764+
private func generateDisableFormattingIfNecessary(_ position: AbsolutePosition) {
27652765
if case .infinite = selection { return }
27662766
if isInsideSelection && !selection.contains(position) {
27672767
appendToken(.disableFormatting(position))
@@ -3270,19 +3270,19 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
32703270
switch piece {
32713271
case .lineComment(let text):
32723272
if index > 0 || isStartOfFile {
3273-
generateEnable(position ..< position + piece.sourceLength)
3273+
generateEnableFormattingIfNecessary(position ..< position + piece.sourceLength)
32743274
appendToken(.comment(Comment(kind: .line, text: text), wasEndOfLine: false))
3275-
generateDisable(position + piece.sourceLength)
3275+
generateDisableFormattingIfNecessary(position + piece.sourceLength)
32763276
appendNewlines(.soft)
32773277
isStartOfFile = false
32783278
}
32793279
requiresNextNewline = true
32803280

32813281
case .blockComment(let text):
32823282
if index > 0 || isStartOfFile {
3283-
generateEnable(position ..< position + piece.sourceLength)
3283+
generateEnableFormattingIfNecessary(position ..< position + piece.sourceLength)
32843284
appendToken(.comment(Comment(kind: .block, text: text), wasEndOfLine: false))
3285-
generateDisable(position + piece.sourceLength)
3285+
generateDisableFormattingIfNecessary(position + piece.sourceLength)
32863286
// There is always a break after the comment to allow a discretionary newline after it.
32873287
var breakSize = 0
32883288
if index + 1 < trivia.endIndex {
@@ -3297,17 +3297,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
32973297
requiresNextNewline = false
32983298

32993299
case .docLineComment(let text):
3300-
generateEnable(position ..< position + piece.sourceLength)
3300+
generateEnableFormattingIfNecessary(position ..< position + piece.sourceLength)
33013301
appendToken(.comment(Comment(kind: .docLine, text: text), wasEndOfLine: false))
3302-
generateDisable(position + piece.sourceLength)
3302+
generateDisableFormattingIfNecessary(position + piece.sourceLength)
33033303
appendNewlines(.soft)
33043304
isStartOfFile = false
33053305
requiresNextNewline = true
33063306

33073307
case .docBlockComment(let text):
3308-
generateEnable(position ..< position + piece.sourceLength)
3308+
generateEnableFormattingIfNecessary(position ..< position + piece.sourceLength)
33093309
appendToken(.comment(Comment(kind: .docBlock, text: text), wasEndOfLine: false))
3310-
generateDisable(position + piece.sourceLength)
3310+
generateDisableFormattingIfNecessary(position + piece.sourceLength)
33113311
appendNewlines(.soft)
33123312
isStartOfFile = false
33133313
requiresNextNewline = false

Tests/SwiftFormatTests/PrettyPrint/AccessorTests.swift

-76
Original file line numberDiff line numberDiff line change
@@ -81,82 +81,6 @@ final class AccessorTests: PrettyPrintTestCase {
8181
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
8282
}
8383

84-
func testBasicAccessorsWithSelections() {
85-
let input =
86-
"""
87-
⏩struct MyStruct {
88-
var memberValue: Int
89-
var someValue: Int { get { return memberValue + 2 } set(newValue) { memberValue = newValue } }
90-
}⏪
91-
struct MyStruct {
92-
var memberValue: Int
93-
var someValue: Int { @objc get { return memberValue + 2 } @objc(isEnabled) set(newValue) { memberValue = newValue } }
94-
}
95-
struct MyStruct {
96-
var memberValue: Int
97-
var memberValue2: Int
98-
var someValue: Int {
99-
get {
100-
let A = 123
101-
return A
102-
}
103-
set(newValue) {
104-
memberValue = newValue && otherValue
105-
⏩memberValue2 = newValue / 2 && andableValue⏪
106-
}
107-
}
108-
}
109-
struct MyStruct {
110-
var memberValue: Int
111-
var SomeValue: Int { return 123 }
112-
var AnotherValue: Double {
113-
let out = 1.23
114-
return out
115-
}
116-
}
117-
"""
118-
119-
let expected =
120-
"""
121-
struct MyStruct {
122-
var memberValue: Int
123-
var someValue: Int {
124-
get { return memberValue + 2 }
125-
set(newValue) { memberValue = newValue }
126-
}
127-
}
128-
struct MyStruct {
129-
var memberValue: Int
130-
var someValue: Int { @objc get { return memberValue + 2 } @objc(isEnabled) set(newValue) { memberValue = newValue } }
131-
}
132-
struct MyStruct {
133-
var memberValue: Int
134-
var memberValue2: Int
135-
var someValue: Int {
136-
get {
137-
let A = 123
138-
return A
139-
}
140-
set(newValue) {
141-
memberValue = newValue && otherValue
142-
memberValue2 =
143-
newValue / 2 && andableValue
144-
}
145-
}
146-
}
147-
struct MyStruct {
148-
var memberValue: Int
149-
var SomeValue: Int { return 123 }
150-
var AnotherValue: Double {
151-
let out = 1.23
152-
return out
153-
}
154-
}
155-
"""
156-
157-
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
158-
}
159-
16084
func testEmptyAccessorList() {
16185
// The comment inside the struct prevents it from *also* being collapsed onto a single line.
16286
let input = """

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

-56
Original file line numberDiff line numberDiff line change
@@ -274,62 +274,6 @@ final class CommentTests: PrettyPrintTestCase {
274274
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
275275
}
276276

277-
func testContainerLineCommentsWithSelection() {
278-
let input =
279-
"""
280-
// Array comment
281-
let a = [⏩4⏪56, // small comment
282-
789]
283-
284-
// Dictionary comment
285-
let b = ["abc": ⏩456, // small comment
286-
"def": 789]⏪
287-
288-
// Trailing comment
289-
let c = [123, 456 // small comment
290-
]
291-
292-
⏩/* Array comment */
293-
let a = [456, /* small comment */
294-
789]
295-
296-
/* Dictionary comment */
297-
let b = ["abc": 456, /* small comment */
298-
"def": 789]⏪
299-
"""
300-
301-
let expected =
302-
"""
303-
// Array comment
304-
let a = [
305-
456, // small comment
306-
789]
307-
308-
// Dictionary comment
309-
let b = ["abc": 456, // small comment
310-
"def": 789,
311-
]
312-
313-
// Trailing comment
314-
let c = [123, 456 // small comment
315-
]
316-
317-
/* Array comment */
318-
let a = [
319-
456, /* small comment */
320-
789,
321-
]
322-
323-
/* Dictionary comment */
324-
let b = [
325-
"abc": 456, /* small comment */
326-
"def": 789,
327-
]
328-
"""
329-
330-
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
331-
}
332-
333277
func testDocumentationBlockComments() {
334278
let input =
335279
"""

0 commit comments

Comments
 (0)