Skip to content

Commit b24b8c3

Browse files
author
Nathan Hawes
committed
[SourceKit/CodeFormat] Get 'trailing' indentation to work in more places and fix a bug where multi-line string indentation was modified.
E.g. if/guard condition patterns are column-aligned, but hitting enter after the ',' below wasn't column-aligning the next empty line: guard let x = Optional(42), // No indentation added here after enter Also the isTargetContext() check takes a start and end token location, but wasn't accounting for the end location pointing a multiline string, so we weren't walking into such nodes. This meant we didn't realise the target location was within a multiline string in some cases, and we ended up interfering with whitespace in its content.
1 parent 3f29cf0 commit b24b8c3

25 files changed

+308
-104
lines changed

lib/IDE/Formatting.cpp

Lines changed: 198 additions & 102 deletions
Large diffs are not rendered by default.

test/SourceKit/CodeFormat/indent-trailing/trailing-closure-in.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ func foo() {
55
) in
66

77
// RUN: %sourcekitd-test -req=format -line=6 -length=1 %s | %FileCheck --strict-whitespace %s
8-
// CHECK: key.sourcetext: " "
8+
// CHECK: key.sourcetext: " "
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum Foo {
2+
case a,
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// CHECK: key.sourcetext: " "
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum Foo {
2+
case
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// CHECK: key.sourcetext: " "
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum Foo {
2+
case a
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// CHECK: key.sourcetext: " "
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct Foo<T: Equatable,
2+
3+
// RUN: %sourcekitd-test -req=format -line=2 -length=1 %s | %FileCheck --strict-whitespace %s
4+
// CHECK: key.sourcetext: " "
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct Foo<T: Equatable
2+
3+
// RUN: %sourcekitd-test -req=format -line=2 -length=1 %s | %FileCheck --strict-whitespace %s
4+
// CHECK: key.sourcetext: ""

test/SourceKit/CodeFormat/indent-trailing/trailing-guard-condition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ func foo(x: Bool) {
22
guard let number = patterns.index(where: {
33

44
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5-
// CHECK: key.sourcetext: " "
5+
// 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 = Optional(23),
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// 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
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// 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 = Optional(23)
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// 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+
if let number = Optional(23),
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// 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+
if
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// 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+
if let number = Optional(23)
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// CHECK: key.sourcetext: " "
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct Foo: Equatable,
2+
3+
// RUN: %sourcekitd-test -req=format -line=2 -length=1 %s | %FileCheck --strict-whitespace %s
4+
// CHECK: key.sourcetext: " "
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct Foo: Equatable
2+
3+
// RUN: %sourcekitd-test -req=format -line=2 -length=1 %s | %FileCheck --strict-whitespace %s
4+
// CHECK: key.sourcetext: ""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Foo {
2+
func foo(x: Int,
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// CHECK: key.sourcetext: " "
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Foo {
2+
func foo(
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+
class Foo {
2+
func foo(x: Int
3+
4+
5+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
6+
// CHECK: key.sourcetext: " "
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public let x = 10,
2+
3+
// RUN: %sourcekitd-test -req=format -line=2 -length=1 %s | %FileCheck --strict-whitespace %s
4+
// CHECK: key.sourcetext: " "
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public let x = 10
2+
3+
// RUN: %sourcekitd-test -req=format -line=2 -length=1 %s | %FileCheck --strict-whitespace %s
4+
// 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+
while let number = Optional(23),
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// 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+
while
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// 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+
while let number = Optional(23)
3+
4+
// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s | %FileCheck --strict-whitespace %s
5+
// CHECK: key.sourcetext: " "

test/swift-indent/basic.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ let s = """
308308
c
309309
"""
310310

311+
func wantsToIndentContents() {
312+
let dontLetItIndentMyValue = """
313+
a
314+
b
315+
c
316+
"""
317+
}
318+
311319
print("""
312320
foo {
313321
bar()

0 commit comments

Comments
 (0)