Skip to content

Don’t add the indentation of the current line to the insert text of subsequent lines when expanding trailing closures #1160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Sources/SourceKitLSP/Swift/CodeCompletionSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,14 @@ class CodeCompletionSession {
return nil
}

let indentationOfLine = snapshot.lineTable[requestPosition.line].prefix(while: { $0.isWhitespace })

let strippedPrefix: String
let exprToExpand: String
if insertText.starts(with: "?.") {
strippedPrefix = "?."
exprToExpand = indentationOfLine + String(insertText.dropFirst(2))
exprToExpand = String(insertText.dropFirst(2))
} else {
strippedPrefix = ""
exprToExpand = indentationOfLine + insertText
exprToExpand = insertText
}

var parser = Parser(exprToExpand)
Expand All @@ -325,7 +323,7 @@ class CodeCompletionSession {
// Add any part of the expression that didn't end up being part of the function call
expandedBytes += bytesToExpand[0..<call.position.utf8Offset]
// Add the expanded function call excluding the added `indentationOfLine`
expandedBytes += expandedCall.syntaxTextBytes[indentationOfLine.utf8.count...]
expandedBytes += expandedCall.syntaxTextBytes
// Add any trailing text that didn't end up being part of the function call
expandedBytes += bytesToExpand[call.endPosition.utf8Offset...]
return String(bytes: expandedBytes, encoding: .utf8)
Expand Down
57 changes: 29 additions & 28 deletions Tests/SourceKitLSPTests/SwiftCompletionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@ final class SwiftCompletionTests: XCTestCase {
]
)
}

func testExpandClosurePlaceholder() async throws {
let testClient = try await TestSourceKitLSPClient(capabilities: snippetCapabilities)
let uri = DocumentURI.for(.swift)
Expand Down Expand Up @@ -1007,17 +1008,17 @@ final class SwiftCompletionTests: XCTestCase {
filterText: "myMap(:)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
}
${2:Bool}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
}
${2:Bool}
}
"""
)
)
Expand Down Expand Up @@ -1055,17 +1056,17 @@ final class SwiftCompletionTests: XCTestCase {
filterText: ".myMap(:)",
insertText: """
?.myMap { ${1:Int} in
${2:Bool}
}
${2:Bool}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: positions["1️⃣"]..<positions["2️⃣"],
newText: """
?.myMap { ${1:Int} in
${2:Bool}
}
${2:Bool}
}
"""
)
)
Expand Down Expand Up @@ -1103,21 +1104,21 @@ final class SwiftCompletionTests: XCTestCase {
filterText: "myMap(::)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
} _: { ${3:Int} in
${4:String}
}
${2:Bool}
} _: { ${3:Int} in
${4:String}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
} _: { ${3:Int} in
${4:String}
}
${2:Bool}
} _: { ${3:Int} in
${4:String}
}
"""
)
)
Expand Down Expand Up @@ -1155,21 +1156,21 @@ final class SwiftCompletionTests: XCTestCase {
filterText: "myMap(:second:)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
} second: { ${3:Int} in
${4:String}
}
${2:Bool}
} second: { ${3:Int} in
${4:String}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
} second: { ${3:Int} in
${4:String}
}
${2:Bool}
} second: { ${3:Int} in
${4:String}
}
"""
)
)
Expand Down Expand Up @@ -1209,17 +1210,17 @@ final class SwiftCompletionTests: XCTestCase {
filterText: "myMap(:)",
insertText: """
myMap { ${1:Int} in
${2:Bool}
}
${2:Bool}
}
""",
insertTextFormat: .snippet,
textEdit: .textEdit(
TextEdit(
range: Range(positions["1️⃣"]),
newText: """
myMap { ${1:Int} in
${2:Bool}
}
${2:Bool}
}
"""
)
)
Expand Down