Skip to content

Commit c1deedc

Browse files
committed
Don’t add the indentation of the current line to the insert text of subsequent lines when expanding trailing closures
Visual Studio Code automatically inserts the indentation of the current line to any additional lines from code completion.
1 parent 3e51f70 commit c1deedc

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

Sources/SourceKitLSP/Swift/CodeCompletionSession.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,14 @@ class CodeCompletionSession {
294294
return nil
295295
}
296296

297-
let indentationOfLine = snapshot.lineTable[requestPosition.line].prefix(while: { $0.isWhitespace })
298-
299297
let strippedPrefix: String
300298
let exprToExpand: String
301299
if insertText.starts(with: "?.") {
302300
strippedPrefix = "?."
303-
exprToExpand = indentationOfLine + String(insertText.dropFirst(2))
301+
exprToExpand = String(insertText.dropFirst(2))
304302
} else {
305303
strippedPrefix = ""
306-
exprToExpand = indentationOfLine + insertText
304+
exprToExpand = insertText
307305
}
308306

309307
var parser = Parser(exprToExpand)
@@ -325,7 +323,7 @@ class CodeCompletionSession {
325323
// Add any part of the expression that didn't end up being part of the function call
326324
expandedBytes += bytesToExpand[0..<call.position.utf8Offset]
327325
// Add the expanded function call excluding the added `indentationOfLine`
328-
expandedBytes += expandedCall.syntaxTextBytes[indentationOfLine.utf8.count...]
326+
expandedBytes += expandedCall.syntaxTextBytes
329327
// Add any trailing text that didn't end up being part of the function call
330328
expandedBytes += bytesToExpand[call.endPosition.utf8Offset...]
331329
return String(bytes: expandedBytes, encoding: .utf8)

Tests/SourceKitLSPTests/SwiftCompletionTests.swift

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,7 @@ final class SwiftCompletionTests: XCTestCase {
978978
]
979979
)
980980
}
981+
981982
func testExpandClosurePlaceholder() async throws {
982983
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
983984
let uri = DocumentURI.for(.swift)
@@ -1007,17 +1008,17 @@ final class SwiftCompletionTests: XCTestCase {
10071008
filterText: "myMap(:)",
10081009
insertText: """
10091010
myMap { ${1:Int} in
1010-
${2:Bool}
1011-
}
1011+
${2:Bool}
1012+
}
10121013
""",
10131014
insertTextFormat: .snippet,
10141015
textEdit: .textEdit(
10151016
TextEdit(
10161017
range: Range(positions["1️⃣"]),
10171018
newText: """
10181019
myMap { ${1:Int} in
1019-
${2:Bool}
1020-
}
1020+
${2:Bool}
1021+
}
10211022
"""
10221023
)
10231024
)
@@ -1055,17 +1056,17 @@ final class SwiftCompletionTests: XCTestCase {
10551056
filterText: ".myMap(:)",
10561057
insertText: """
10571058
?.myMap { ${1:Int} in
1058-
${2:Bool}
1059-
}
1059+
${2:Bool}
1060+
}
10601061
""",
10611062
insertTextFormat: .snippet,
10621063
textEdit: .textEdit(
10631064
TextEdit(
10641065
range: positions["1️⃣"]..<positions["2️⃣"],
10651066
newText: """
10661067
?.myMap { ${1:Int} in
1067-
${2:Bool}
1068-
}
1068+
${2:Bool}
1069+
}
10691070
"""
10701071
)
10711072
)
@@ -1103,21 +1104,21 @@ final class SwiftCompletionTests: XCTestCase {
11031104
filterText: "myMap(::)",
11041105
insertText: """
11051106
myMap { ${1:Int} in
1106-
${2:Bool}
1107-
} _: { ${3:Int} in
1108-
${4:String}
1109-
}
1107+
${2:Bool}
1108+
} _: { ${3:Int} in
1109+
${4:String}
1110+
}
11101111
""",
11111112
insertTextFormat: .snippet,
11121113
textEdit: .textEdit(
11131114
TextEdit(
11141115
range: Range(positions["1️⃣"]),
11151116
newText: """
11161117
myMap { ${1:Int} in
1117-
${2:Bool}
1118-
} _: { ${3:Int} in
1119-
${4:String}
1120-
}
1118+
${2:Bool}
1119+
} _: { ${3:Int} in
1120+
${4:String}
1121+
}
11211122
"""
11221123
)
11231124
)
@@ -1155,21 +1156,21 @@ final class SwiftCompletionTests: XCTestCase {
11551156
filterText: "myMap(:second:)",
11561157
insertText: """
11571158
myMap { ${1:Int} in
1158-
${2:Bool}
1159-
} second: { ${3:Int} in
1160-
${4:String}
1161-
}
1159+
${2:Bool}
1160+
} second: { ${3:Int} in
1161+
${4:String}
1162+
}
11621163
""",
11631164
insertTextFormat: .snippet,
11641165
textEdit: .textEdit(
11651166
TextEdit(
11661167
range: Range(positions["1️⃣"]),
11671168
newText: """
11681169
myMap { ${1:Int} in
1169-
${2:Bool}
1170-
} second: { ${3:Int} in
1171-
${4:String}
1172-
}
1170+
${2:Bool}
1171+
} second: { ${3:Int} in
1172+
${4:String}
1173+
}
11731174
"""
11741175
)
11751176
)
@@ -1209,17 +1210,17 @@ final class SwiftCompletionTests: XCTestCase {
12091210
filterText: "myMap(:)",
12101211
insertText: """
12111212
myMap { ${1:Int} in
1212-
${2:Bool}
1213-
}
1213+
${2:Bool}
1214+
}
12141215
""",
12151216
insertTextFormat: .snippet,
12161217
textEdit: .textEdit(
12171218
TextEdit(
12181219
range: Range(positions["1️⃣"]),
12191220
newText: """
12201221
myMap { ${1:Int} in
1221-
${2:Bool}
1222-
}
1222+
${2:Bool}
1223+
}
12231224
"""
12241225
)
12251226
)

0 commit comments

Comments
 (0)