Skip to content

Commit 5e1ff2d

Browse files
authored
Merge pull request #1225 from ahoppen/add-documentation-followup
Use `TokenSyntax.indentationOfLine` from SwiftBasicFormat instead of duplicating indentation inferring logic
2 parents e71aa5d + 329e3d3 commit 5e1ff2d

File tree

1 file changed

+36
-51
lines changed

1 file changed

+36
-51
lines changed

Sources/SourceKitLSP/Swift/CodeActions/AddDocumentation.swift

Lines changed: 36 additions & 51 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

@@ -100,57 +99,43 @@ extension AddDocumentation: SyntaxRefactoringCodeActionProvider {
10099

101100
extension DeclSyntax {
102101
fileprivate var parameters: FunctionParameterClauseSyntax? {
103-
switch self.syntaxNodeType {
104-
case is FunctionDeclSyntax.Type:
105-
return self.as(FunctionDeclSyntax.self)!.signature.parameterClause
106-
case is SubscriptDeclSyntax.Type:
107-
return self.as(SubscriptDeclSyntax.self)!.parameterClause
108-
case is InitializerDeclSyntax.Type:
109-
return self.as(InitializerDeclSyntax.self)!.signature.parameterClause
110-
case is MacroDeclSyntax.Type:
111-
return self.as(MacroDeclSyntax.self)!.signature.parameterClause
102+
switch self.as(DeclSyntaxEnum.self) {
103+
case .functionDecl(let functionDecl):
104+
return functionDecl.signature.parameterClause
105+
case .subscriptDecl(let subscriptDecl):
106+
return subscriptDecl.parameterClause
107+
case .initializerDecl(let initializer):
108+
return initializer.signature.parameterClause
109+
case .macroDecl(let macro):
110+
return macro.signature.parameterClause
112111
default:
113112
return nil
114113
}
115114
}
116115

117116
fileprivate var throwsKeyword: TokenSyntax? {
118-
switch self.syntaxNodeType {
119-
case is FunctionDeclSyntax.Type:
120-
return self.as(FunctionDeclSyntax.self)!.signature.effectSpecifiers?
121-
.throwsClause?.throwsSpecifier
122-
case is InitializerDeclSyntax.Type:
123-
return self.as(InitializerDeclSyntax.self)!.signature.effectSpecifiers?
124-
.throwsClause?.throwsSpecifier
117+
switch self.as(DeclSyntaxEnum.self) {
118+
case .functionDecl(let functionDecl):
119+
return functionDecl.signature.effectSpecifiers?.throwsClause?.throwsSpecifier
120+
case .initializerDecl(let initializer):
121+
return initializer.signature.effectSpecifiers?.throwsClause?.throwsSpecifier
125122
default:
126123
return nil
127124
}
128125
}
129126

130127
fileprivate var returnType: TypeSyntax? {
131-
switch self.syntaxNodeType {
132-
case is FunctionDeclSyntax.Type:
133-
return self.as(FunctionDeclSyntax.self)!.signature.returnClause?.type
134-
case is SubscriptDeclSyntax.Type:
135-
return self.as(SubscriptDeclSyntax.self)!.returnClause.type
136-
case is InitializerDeclSyntax.Type:
137-
return self.as(InitializerDeclSyntax.self)!.signature.returnClause?.type
138-
case is MacroDeclSyntax.Type:
139-
return self.as(MacroDeclSyntax.self)!.signature.returnClause?.type
128+
switch self.as(DeclSyntaxEnum.self) {
129+
case .functionDecl(let functionDecl):
130+
return functionDecl.signature.returnClause?.type
131+
case .subscriptDecl(let subscriptDecl):
132+
return subscriptDecl.returnClause.type
133+
case .initializerDecl(let initializer):
134+
return initializer.signature.returnClause?.type
135+
case .macroDecl(let macro):
136+
return macro.signature.returnClause?.type
140137
default:
141138
return nil
142139
}
143140
}
144141
}
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)