Skip to content

Commit aedafe9

Browse files
author
Nathan Hawes
committed
[SourceKit/CodeFormat] Improve documentation and fix propagation of ContextLocs.
- Rename several symbols to make it clearer whether the ranges they deal with are open or closed. - Add comments documenting the implementation of OutdentChecker::hasOutdent - Fix a bug where code after a doc coment block of the '/**' style was being indented 1 space. - Fix IsInStringLiteral not being set if the indent target was in a string segment of an interpolated multiline string. - Update OutdentChecker::hasOutdent to propagate indent contexts from parent parens/brackets/braces to child parens/brackets/braces that start later in the same line (like FormatWalker already does). This changes the braces in the example below to 'inherit' a ContextLoc from their parent square brackets, which have a ContextLoc at 'foo'. This makes the whole expression be correctly considered 'outdenting': foo(a: "hello" b: "hello")[x: { print("hello") }]
1 parent 1c729ca commit aedafe9

File tree

8 files changed

+570
-193
lines changed

8 files changed

+570
-193
lines changed

lib/IDE/Formatting.cpp

Lines changed: 445 additions & 190 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func foo() {
2+
foo { (
3+
x,
4+
y
5+
) in
6+
7+
// RUN: %sourcekitd-test -req=format -line=6 -length=1 %s | %FileCheck --strict-whitespace %s
8+
// CHECK: key.sourcetext: " "
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func foo() {
2+
bar
3+
.fooooooooooo(first: 3,
4+
second)[x: 10] {
5+
6+
// RUN: %sourcekitd-test -req=format -line=5 -length=1 %s | %FileCheck --strict-whitespace %s
7+
// CHECK: key.sourcetext: " "
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Foo {
2-
deinit {
2+
deinit {
33

44
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
55
// CHECK: key.sourcetext: " "
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func foo(x: Bool) {
2+
guard let number = patterns.index(where: {
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// CHECK: key.sourcetext: " "
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let foo = Bar.Stuff(
2+
first: path,
3+
description: "No \(thing) was found at path \(path)"
4+
5+
// RUN: %sourcekitd-test -req=format -line=4 -length=1 %s | %FileCheck --strict-whitespace %s
6+
// CHECK: key.sourcetext: ""

test/SourceKit/CodeFormat/rdar_32789463.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ $
1313
// CHECK: key.sourcetext: "struct $ {"
1414
// CHECK: key.sourcetext: " let $: <#Type#>"
1515
// CHECK: key.sourcetext: " = foo(\"foo \\($) bar\") {"
16-
// CHECK: key.sourcetext: "$"
16+
// CHECK: key.sourcetext: " $"

test/swift-indent/basic.swift

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ if #available(
115115

116116
// Trailing closures, subscript expressions and argument tuples/parens should
117117
// indent relative to the line containing the start of the last named component
118-
// of their function, or the function start if their are none.
118+
// of their function, or the function start if their are none. Any child
119+
// tuples/parens/brackets should that start after them on the same line should
120+
// do the same.
119121
//
120122
let _ = []
121123
.map {
@@ -146,6 +148,51 @@ basename
146148
fatalError()
147149
}
148150

151+
[foo(a: Int,
152+
b: Int)[z: {
153+
fatalError()
154+
}],
155+
"hello"
156+
]
157+
158+
[foo(a: Int,
159+
b: Int)[z: foo ({
160+
fatalError()
161+
}, {
162+
fatalError()
163+
})],
164+
"hello"
165+
]
166+
167+
[foo(a: Int,
168+
b: Int) {
169+
fatalError()
170+
} [y: 10,
171+
z: foo ({
172+
fatalError()
173+
}, {
174+
fatalError()
175+
})],
176+
"hello"
177+
]
178+
179+
[foo(a: Int,
180+
b: Int) [z: foo ()
181+
{
182+
fatalError()
183+
})],
184+
"hello"
185+
]
186+
187+
[foo(a: Int,
188+
b: Int) [z: foo (a: 1,
189+
b: 2)
190+
{
191+
fatalError()
192+
})],
193+
"hello"
194+
]
195+
149196

150197
// Closing arg parens shouldn't indent.
151198
//
@@ -240,6 +287,34 @@ let s = """
240287
c
241288
"""
242289

290+
291+
// Interpolations shouldn't change how multiline strings are handled.
292+
//
293+
switch self {
294+
case .first:
295+
return """
296+
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo \
297+
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo.
298+
"""
299+
case .second(let a, let b):
300+
return """
301+
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo \(bar.bar), \
302+
foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo foo \(bar.bar).
303+
"""
304+
}
305+
306+
307+
// Comments after the last item of a collection should still indent.
308+
//
309+
let x = [
310+
// hello
311+
124,
312+
// hello
313+
123
314+
// hello
315+
]
316+
317+
243318
// Pound directives aren't indentation contexts, though this should probably be
244319
// configurable.
245320
//
@@ -717,6 +792,11 @@ let myFunc: () -> (Int,
717792
Int)
718793

719794

795+
// Tuple types should align on their outer label locs (if present)
796+
//
797+
typealias Thing = (_ tmpURL: URL,
798+
_ secondURL: URL) -> (destinationURL: URL, options: Options)
799+
720800
// Type member access shouldn't indent.
721801
//
722802
func foo(
@@ -818,11 +898,13 @@ foo.bar() {
818898
return
819899
}
820900

901+
821902
// Handle invalid for each (missing 'in' location)
822903
//
823904
for query: foo
824905
bar;
825906

907+
826908
// Handle custom attributes
827909
//
828910
@Custom(foo: 3,
@@ -834,3 +916,17 @@ struct Foo {
834916
)
835917
var d: Int = 10
836918
}
919+
920+
// Ignore postfix expressions when determining context locations.
921+
//
922+
return Foo(deferred) {
923+
print("hello")
924+
}++
925+
926+
IncrementedFirst++
927+
.foo()++
928+
.bar {
929+
930+
}++
931+
.baz()
932+

0 commit comments

Comments
 (0)