|
10 | 10 | //
|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
| 13 | +import SwiftBasicFormat |
13 | 14 | import SwiftParser
|
14 | 15 | import SwiftRefactor
|
15 | 16 | import SwiftSyntax
|
@@ -49,38 +50,36 @@ public struct AddDocumentation: EditRefactoringProvider {
|
49 | 50 | return []
|
50 | 51 | }
|
51 | 52 |
|
52 |
| - let indentation = [.newlines(1)] + syntax.leadingTrivia.lastLineIndentation() |
| 53 | + let newlineAndIndentation = [.newlines(1)] + (syntax.firstToken(viewMode: .sourceAccurate)?.indentationOfLine ?? []) |
53 | 54 | var content: [TriviaPiece] = []
|
54 |
| - content.append(contentsOf: indentation) |
| 55 | + content += newlineAndIndentation |
55 | 56 | content.append(.docLineComment("/// A description"))
|
56 | 57 |
|
57 | 58 | if let parameters = syntax.parameters?.parameters {
|
58 | 59 | if let onlyParam = parameters.only {
|
59 | 60 | let paramToken = onlyParam.secondName?.text ?? onlyParam.firstName.text
|
60 |
| - content.append(contentsOf: indentation) |
| 61 | + content += newlineAndIndentation |
61 | 62 | content.append(.docLineComment("/// - Parameter \(paramToken):"))
|
62 | 63 | } else {
|
63 |
| - content.append(contentsOf: indentation) |
| 64 | + content += newlineAndIndentation |
64 | 65 | 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 |
73 | 72 | content.append(.docLineComment("///"))
|
74 | 73 | }
|
75 | 74 | }
|
76 | 75 |
|
77 | 76 | if syntax.throwsKeyword != nil {
|
78 |
| - content.append(contentsOf: indentation) |
| 77 | + content += newlineAndIndentation |
79 | 78 | content.append(.docLineComment("/// - Throws:"))
|
80 | 79 | }
|
81 | 80 |
|
82 | 81 | if syntax.returnType != nil {
|
83 |
| - content.append(contentsOf: indentation) |
| 82 | + content += newlineAndIndentation |
84 | 83 | content.append(.docLineComment("/// - Returns:"))
|
85 | 84 | }
|
86 | 85 |
|
@@ -142,15 +141,3 @@ extension DeclSyntax {
|
142 | 141 | }
|
143 | 142 | }
|
144 | 143 | }
|
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