|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 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 SwiftSyntax |
| 14 | +import SwiftDiagnostics |
| 15 | + |
| 16 | +public class FixItApplier: SyntaxRewriter { |
| 17 | + fileprivate struct Edit { |
| 18 | + let startUtf8Offset: Int |
| 19 | + let endUtf8Offset: Int |
| 20 | + let replacement: String |
| 21 | + } |
| 22 | + |
| 23 | + var changes: [FixIt.Change] |
| 24 | + |
| 25 | + private var locationConverter: SourceLocationConverter? |
| 26 | + private var edits: [Edit] = [] |
| 27 | + |
| 28 | + init(diagnostics: [Diagnostic], withMessages messages: [String]?) { |
| 29 | + let messages = messages ?? diagnostics.compactMap { $0.fixIts.first?.message.message } |
| 30 | + |
| 31 | + self.changes = |
| 32 | + diagnostics |
| 33 | + .flatMap { $0.fixIts } |
| 34 | + .filter { |
| 35 | + return messages.contains($0.message.message) |
| 36 | + } |
| 37 | + .flatMap { $0.changes } |
| 38 | + |
| 39 | + super.init(viewMode: .all) |
| 40 | + } |
| 41 | + |
| 42 | + public override func visitPre(_ node: Syntax) { |
| 43 | + if locationConverter == nil && !node.hasParent { |
| 44 | + locationConverter = SourceLocationConverter(fileName: "", tree: node) |
| 45 | + |
| 46 | + for change in self.changes { |
| 47 | + switch change { |
| 48 | + case .replace(oldNode: let oldNode, newNode: let newNode): |
| 49 | + let oldStartLocation = oldNode.startLocation(converter: locationConverter!) |
| 50 | + let oldEndLocation = oldNode.startLocation(converter: locationConverter!) |
| 51 | + |
| 52 | + edits.append(Edit(startUtf8Offset: oldStartLocation.offset, endUtf8Offset: oldEndLocation.offset, replacement: newNode.description)) |
| 53 | + |
| 54 | + default: |
| 55 | + break |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public override func visitAny(_ node: Syntax) -> Syntax? { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + public override func visit(_ node: TokenSyntax) -> TokenSyntax { |
| 66 | + var modifiedNode = node |
| 67 | + for change in changes { |
| 68 | + switch change { |
| 69 | + case .replaceLeadingTrivia(token: let changedNode, newTrivia: let newTrivia) where changedNode.id == node.id: |
| 70 | + modifiedNode = node.with(\.leadingTrivia, newTrivia) |
| 71 | + case .replaceTrailingTrivia(token: let changedNode, newTrivia: let newTrivia) where changedNode.id == node.id: |
| 72 | + modifiedNode = node.with(\.trailingTrivia, newTrivia) |
| 73 | + default: |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + return modifiedNode |
| 78 | + } |
| 79 | + |
| 80 | + /// If `messages` is `nil`, applies all Fix-Its in `diagnostics` to `tree` and returns the fixed syntax tree. |
| 81 | + /// If `messages` is not `nil`, applies only Fix-Its whose message is in `messages`. |
| 82 | + public static func applyFixes<T: SyntaxProtocol>(in diagnostics: [Diagnostic], withMessages messages: [String]?, to tree: T) -> Syntax { |
| 83 | + let applier = FixItApplier(diagnostics: diagnostics, withMessages: messages) |
| 84 | + return applier.rewrite(tree) |
| 85 | + } |
| 86 | +} |
0 commit comments