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
@@ -100,57 +99,43 @@ extension AddDocumentation: SyntaxRefactoringCodeActionProvider {
100
99
101
100
extension DeclSyntax {
102
101
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
112
111
default :
113
112
return nil
114
113
}
115
114
}
116
115
117
116
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
125
122
default :
126
123
return nil
127
124
}
128
125
}
129
126
130
127
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
140
137
default :
141
138
return nil
142
139
}
143
140
}
144
141
}
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