Skip to content

Commit 449d2a9

Browse files
committed
Fix a bug that caused documentation to be added at the start of the declaration’s trivia
This meant that if there were two newlines before the declaration, the documentation would be separated to the declaration by one newline and if the declaration was at the start of a line, the declaration would be on the same line as the doc comment, effectively making the documentation part of a comment.
1 parent e3c498e commit 449d2a9

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

Sources/SourceKitLSP/Swift/CodeActions/AddDocumentation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public struct AddDocumentation: EditRefactoringProvider {
5252

5353
let newlineAndIndentation = [.newlines(1)] + (syntax.firstToken(viewMode: .sourceAccurate)?.indentationOfLine ?? [])
5454
var content: [TriviaPiece] = []
55-
content += newlineAndIndentation
5655
content.append(.docLineComment("/// A description"))
5756

5857
if let parameters = syntax.parameters?.parameters {
@@ -82,8 +81,9 @@ public struct AddDocumentation: EditRefactoringProvider {
8281
content += newlineAndIndentation
8382
content.append(.docLineComment("/// - Returns:"))
8483
}
84+
content += newlineAndIndentation
8585

86-
let insertPos = syntax.position
86+
let insertPos = syntax.positionAfterSkippingLeadingTrivia
8787
return [
8888
SourceEdit(
8989
range: insertPos..<insertPos,

Tests/SourceKitLSPTests/SyntaxRefactorTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ final class SyntaxRefactorTests: XCTestCase {
2727
provider: AddDocumentation.self,
2828
expected: [
2929
SourceEdit(
30-
range: AbsolutePosition(utf8Offset: 0)..<AbsolutePosition(utf8Offset: 0),
30+
range: AbsolutePosition(utf8Offset: 2)..<AbsolutePosition(utf8Offset: 2),
3131
replacement: """
32-
33-
/// A description
32+
/// A description
3433
/// - Parameters:
3534
/// - syntax:
3635
/// - context:
3736
///
3837
/// - Returns:
38+
\("")
3939
"""
4040
)
4141
]
@@ -51,11 +51,11 @@ final class SyntaxRefactorTests: XCTestCase {
5151
provider: AddDocumentation.self,
5252
expected: [
5353
SourceEdit(
54-
range: AbsolutePosition(utf8Offset: 0)..<AbsolutePosition(utf8Offset: 0),
54+
range: AbsolutePosition(utf8Offset: 2)..<AbsolutePosition(utf8Offset: 2),
5555
replacement: """
56-
57-
/// A description
56+
/// A description
5857
/// - Parameter syntax:
58+
\("")
5959
"""
6060
)
6161
]

0 commit comments

Comments
 (0)