|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftParser |
| 14 | +import SwiftRefactor |
| 15 | +import SwiftSyntax |
| 16 | + |
| 17 | +/// Insert a documentation template associated with a function or macro. |
| 18 | +/// |
| 19 | +/// ## Before |
| 20 | +/// |
| 21 | +/// ```swift |
| 22 | +/// static func refactor(syntax: DeclSyntax, in context: Void) -> DeclSyntax? {} |
| 23 | +/// ``` |
| 24 | +/// |
| 25 | +/// ## After |
| 26 | +/// |
| 27 | +/// ```swift |
| 28 | +/// /// |
| 29 | +/// /// - Parameters: |
| 30 | +/// /// - syntax: |
| 31 | +/// /// - context: |
| 32 | +/// /// - Returns: |
| 33 | +/// static func refactor(syntax: DeclSyntax, in context: Void) -> DeclSyntax? {} |
| 34 | +/// ``` |
| 35 | +@_spi(Testing) |
| 36 | +public struct AddDocumentation: EditRefactoringProvider { |
| 37 | + @_spi(Testing) |
| 38 | + public static func textRefactor(syntax: DeclSyntax, in context: Void) -> [SourceEdit] { |
| 39 | + let hasDocumentation = syntax.leadingTrivia.contains(where: { trivia in |
| 40 | + switch trivia { |
| 41 | + case .blockComment(_), .docBlockComment(_), .lineComment(_), .docLineComment(_): |
| 42 | + return true |
| 43 | + default: |
| 44 | + return false |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + guard !hasDocumentation else { |
| 49 | + return [] |
| 50 | + } |
| 51 | + |
| 52 | + let indentation = [.newlines(1)] + syntax.leadingTrivia.lastLineIndentation() |
| 53 | + var content: [TriviaPiece] = [] |
| 54 | + content.append(contentsOf: indentation) |
| 55 | + content.append(.docLineComment("/// A description")) |
| 56 | + |
| 57 | + if let parameters = syntax.parameters?.parameters { |
| 58 | + if let onlyParam = parameters.only { |
| 59 | + let paramToken = onlyParam.secondName?.text ?? onlyParam.firstName.text |
| 60 | + content.append(contentsOf: indentation) |
| 61 | + content.append(.docLineComment("/// - Parameter \(paramToken):")) |
| 62 | + } else { |
| 63 | + content.append(contentsOf: indentation) |
| 64 | + 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) |
| 73 | + content.append(.docLineComment("///")) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if syntax.throwsKeyword != nil { |
| 78 | + content.append(contentsOf: indentation) |
| 79 | + content.append(.docLineComment("/// - Throws:")) |
| 80 | + } |
| 81 | + |
| 82 | + if syntax.returnType != nil { |
| 83 | + content.append(contentsOf: indentation) |
| 84 | + content.append(.docLineComment("/// - Returns:")) |
| 85 | + } |
| 86 | + |
| 87 | + let insertPos = syntax.position |
| 88 | + return [ |
| 89 | + SourceEdit( |
| 90 | + range: insertPos..<insertPos, |
| 91 | + replacement: Trivia(pieces: content).description |
| 92 | + ) |
| 93 | + ] |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +extension AddDocumentation: SyntaxRefactoringCodeActionProvider { |
| 98 | + static var title: String { "Add documentation" } |
| 99 | +} |
| 100 | + |
| 101 | +extension DeclSyntax { |
| 102 | + 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 |
| 112 | + default: |
| 113 | + return nil |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + 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 |
| 125 | + default: |
| 126 | + return nil |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + 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 |
| 140 | + default: |
| 141 | + return nil |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 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