Skip to content

Commit 6507937

Browse files
authored
Merge pull request #1067 from ahoppen/ahoppen/jumbled-format-response
Fix jumbled formatting results
2 parents e16f8c6 + 0ca315c commit 6507937

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Sources/SourceKitLSP/Swift/DocumentFormatting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private func edits(from original: DocumentSnapshot, to edited: String) -> [TextE
116116
// Adjust the index offset based on changes that `Collection.difference` expects to already have been applied.
117117
var adjustment: Int = 0
118118
for edit in edits {
119-
if edit.range.upperBound < change.offset {
119+
if edit.range.upperBound < change.offset + adjustment {
120120
adjustment = adjustment + edit.range.count - edit.replacement.count
121121
}
122122
}

Tests/SourceKitLSPTests/FormattingTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,36 @@ final class FormattingTests: XCTestCase {
215215
)
216216
)
217217
}
218+
219+
func testInsertAndRemove() async throws {
220+
try await SkipUnless.toolchainContainsSwiftFormat()
221+
let testClient = try await TestSourceKitLSPClient()
222+
let uri = DocumentURI.for(.swift)
223+
224+
let positions = testClient.openDocument(
225+
"""
226+
1️⃣public 2️⃣extension Example {
227+
3️⃣func function() {}
228+
}
229+
230+
""",
231+
uri: uri
232+
)
233+
234+
let response = try await testClient.send(
235+
DocumentFormattingRequest(
236+
textDocument: TextDocumentIdentifier(uri),
237+
options: FormattingOptions(tabSize: 2, insertSpaces: true)
238+
)
239+
)
240+
241+
let edits = try XCTUnwrap(response)
242+
XCTAssertEqual(
243+
edits,
244+
[
245+
TextEdit(range: positions["1️⃣"]..<positions["2️⃣"], newText: ""),
246+
TextEdit(range: Range(positions["3️⃣"]), newText: "public "),
247+
]
248+
)
249+
}
218250
}

0 commit comments

Comments
 (0)