Skip to content

Commit f8d0c60

Browse files
committed
Use TokenSyntax.indentationOfLine from SwiftBasicFormat instead of duplicating indentation inferring logic
1 parent e71aa5d commit f8d0c60

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

Sources/SourceKitLSP/Swift/CodeActions/AddDocumentation.swift

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SwiftBasicFormat
1314
import SwiftParser
1415
import SwiftRefactor
1516
import SwiftSyntax
@@ -49,38 +50,36 @@ public struct AddDocumentation: EditRefactoringProvider {
4950
return []
5051
}
5152

52-
let indentation = [.newlines(1)] + syntax.leadingTrivia.lastLineIndentation()
53+
let newlineAndIndentation = [.newlines(1)] + (syntax.firstToken(viewMode: .sourceAccurate)?.indentationOfLine ?? [])
5354
var content: [TriviaPiece] = []
54-
content.append(contentsOf: indentation)
55+
content += newlineAndIndentation
5556
content.append(.docLineComment("/// A description"))
5657

5758
if let parameters = syntax.parameters?.parameters {
5859
if let onlyParam = parameters.only {
5960
let paramToken = onlyParam.secondName?.text ?? onlyParam.firstName.text
60-
content.append(contentsOf: indentation)
61+
content += newlineAndIndentation
6162
content.append(.docLineComment("/// - Parameter \(paramToken):"))
6263
} else {
63-
content.append(contentsOf: indentation)
64+
content += newlineAndIndentation
6465
content.append(.docLineComment("/// - Parameters:"))
65-
content.append(
66-
contentsOf: parameters.flatMap({ param in
67-
indentation + [
68-
.docLineComment("/// - \(param.secondName?.text ?? param.firstName.text):")
69-
]
70-
})
71-
)
72-
content.append(contentsOf: indentation)
66+
content += parameters.flatMap({ param in
67+
newlineAndIndentation + [
68+
.docLineComment("/// - \(param.secondName?.text ?? param.firstName.text):")
69+
]
70+
})
71+
content += newlineAndIndentation
7372
content.append(.docLineComment("///"))
7473
}
7574
}
7675

7776
if syntax.throwsKeyword != nil {
78-
content.append(contentsOf: indentation)
77+
content += newlineAndIndentation
7978
content.append(.docLineComment("/// - Throws:"))
8079
}
8180

8281
if syntax.returnType != nil {
83-
content.append(contentsOf: indentation)
82+
content += newlineAndIndentation
8483
content.append(.docLineComment("/// - Returns:"))
8584
}
8685

@@ -142,15 +141,3 @@ extension DeclSyntax {
142141
}
143142
}
144143
}
145-
146-
extension Trivia {
147-
/// Produce trivia from the last newline to the end, dropping anything
148-
/// prior to that.
149-
fileprivate func lastLineIndentation() -> Trivia {
150-
guard let lastNewline = pieces.lastIndex(where: { $0.isNewline }) else {
151-
return self
152-
}
153-
154-
return Trivia(pieces: pieces[(lastNewline + 1)...])
155-
}
156-
}

0 commit comments

Comments
 (0)