|
| 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 | +#include "RefactoringActions.h" |
| 14 | + |
| 15 | +using namespace swift::refactoring; |
| 16 | + |
| 17 | +struct swift::ide::FindRenameRangesAnnotatingConsumer::Implementation { |
| 18 | + std::unique_ptr<SourceEditConsumer> pRewriter; |
| 19 | + Implementation(SourceManager &SM, unsigned BufferId, raw_ostream &OS) |
| 20 | + : pRewriter(new SourceEditOutputConsumer(SM, BufferId, OS)) {} |
| 21 | + static StringRef tag(RefactoringRangeKind Kind) { |
| 22 | + switch (Kind) { |
| 23 | + case RefactoringRangeKind::BaseName: |
| 24 | + return "base"; |
| 25 | + case RefactoringRangeKind::KeywordBaseName: |
| 26 | + return "keywordBase"; |
| 27 | + case RefactoringRangeKind::ParameterName: |
| 28 | + return "param"; |
| 29 | + case RefactoringRangeKind::NoncollapsibleParameterName: |
| 30 | + return "noncollapsibleparam"; |
| 31 | + case RefactoringRangeKind::DeclArgumentLabel: |
| 32 | + return "arglabel"; |
| 33 | + case RefactoringRangeKind::CallArgumentLabel: |
| 34 | + return "callarg"; |
| 35 | + case RefactoringRangeKind::CallArgumentColon: |
| 36 | + return "callcolon"; |
| 37 | + case RefactoringRangeKind::CallArgumentCombined: |
| 38 | + return "callcombo"; |
| 39 | + case RefactoringRangeKind::SelectorArgumentLabel: |
| 40 | + return "sel"; |
| 41 | + } |
| 42 | + llvm_unreachable("unhandled kind"); |
| 43 | + } |
| 44 | + void accept(SourceManager &SM, const RenameRangeDetail &Range) { |
| 45 | + std::string NewText; |
| 46 | + llvm::raw_string_ostream OS(NewText); |
| 47 | + StringRef Tag = tag(Range.RangeKind); |
| 48 | + OS << "<" << Tag; |
| 49 | + if (Range.Index.has_value()) |
| 50 | + OS << " index=" << *Range.Index; |
| 51 | + OS << ">" << Range.Range.str() << "</" << Tag << ">"; |
| 52 | + pRewriter->accept(SM, {/*Path=*/{}, Range.Range, /*BufferName=*/{}, |
| 53 | + OS.str(), /*RegionsWorthNote=*/{}}); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +swift::ide::FindRenameRangesAnnotatingConsumer:: |
| 58 | + FindRenameRangesAnnotatingConsumer(SourceManager &SM, unsigned BufferId, |
| 59 | + raw_ostream &OS) |
| 60 | + : Impl(*new Implementation(SM, BufferId, OS)) {} |
| 61 | + |
| 62 | +swift::ide::FindRenameRangesAnnotatingConsumer:: |
| 63 | + ~FindRenameRangesAnnotatingConsumer() { |
| 64 | + delete &Impl; |
| 65 | +} |
| 66 | + |
| 67 | +void swift::ide::FindRenameRangesAnnotatingConsumer::accept( |
| 68 | + SourceManager &SM, RegionType RegionType, |
| 69 | + ArrayRef<RenameRangeDetail> Ranges) { |
| 70 | + if (RegionType == RegionType::Mismatch || RegionType == RegionType::Unmatched) |
| 71 | + return; |
| 72 | + for (const auto &Range : Ranges) { |
| 73 | + Impl.accept(SM, Range); |
| 74 | + } |
| 75 | +} |
0 commit comments