Skip to content

Commit 83915cc

Browse files
committed
[IDE] Add swift-refactor option to output as plain text
Current modes are: 1. output the whole rewritten buffer, with RUN and CHECK lines removed 2. output each replacement in JSON To make each refactoring (rather than the whole file) easier to test, add a plain text output option that can be easily checked with FileCheck.
1 parent aa7d010 commit 83915cc

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

include/swift/IDE/Utils.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ class EditorConsumerInsertStream: public raw_ostream {
547547
}
548548
};
549549

550+
/// Outputs replacements as JSON, see `writeEditsInJson`
550551
class SourceEditJsonConsumer : public SourceEditConsumer {
551552
struct Implementation;
552553
Implementation &Impl;
@@ -556,6 +557,25 @@ class SourceEditJsonConsumer : public SourceEditConsumer {
556557
void accept(SourceManager &SM, RegionType RegionType, ArrayRef<Replacement> Replacements) override;
557558
};
558559

560+
/// Outputs replacements to `OS` in the form
561+
/// ```
562+
/// // </path/to/file> [startLine:startCol, endLine:endCol)
563+
/// text
564+
/// to
565+
/// replace
566+
///
567+
/// ```
568+
class SourceEditTextConsumer : public SourceEditConsumer {
569+
llvm::raw_ostream &OS;
570+
571+
public:
572+
SourceEditTextConsumer(llvm::raw_ostream &OS);
573+
574+
void accept(SourceManager &SM, RegionType RegionType,
575+
ArrayRef<Replacement> Replacements) override;
576+
};
577+
578+
/// Outputs the rewritten buffer to `OS` with RUN and CHECK lines removed
559579
class SourceEditOutputConsumer : public SourceEditConsumer {
560580
struct Implementation;
561581
Implementation &Impl;

lib/IDE/Utils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,26 @@ accept(SourceManager &SM, RegionType Type, ArrayRef<Replacement> Replacements) {
10181018
Impl.accept(SM, Replacement.Range, Replacement.Text);
10191019
}
10201020
}
1021+
1022+
swift::ide::SourceEditTextConsumer::
1023+
SourceEditTextConsumer(llvm::raw_ostream &OS) : OS(OS) { }
1024+
1025+
void swift::ide::SourceEditTextConsumer::
1026+
accept(SourceManager &SM, RegionType Type, ArrayRef<Replacement> Replacements) {
1027+
for (const auto &Replacement: Replacements) {
1028+
CharSourceRange Range = Replacement.Range;
1029+
unsigned BufID = SM.findBufferContainingLoc(Range.getStart());
1030+
auto Path(SM.getIdentifierForBuffer(BufID));
1031+
auto Start = SM.getLineAndColumnInBuffer(Range.getStart());
1032+
auto End = SM.getLineAndColumnInBuffer(Range.getEnd());
1033+
1034+
OS << "// " << Path.str() << " ";
1035+
OS << "[" << Start.first << ":" << Start.second << ", ";
1036+
OS << End.first << ":" << End.second << ")\n";
1037+
OS << Replacement.Text << "\n";
1038+
}
1039+
}
1040+
10211041
namespace {
10221042
class ClangFileRewriterHelper {
10231043
unsigned InterestedId;

tools/swift-refactor/swift-refactor.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,21 @@ static llvm::cl::opt<bool>
112112
IsNonProtocolType("is-non-protocol-type",
113113
llvm::cl::desc("The symbol being renamed is a type and not a protocol"));
114114

115-
static llvm::cl::opt<bool>
116-
DumpInJson("dump-json",
117-
llvm::cl::desc("Whether to dump refactoring edits in json"),
118-
llvm::cl::init(false));
115+
enum class DumpType {
116+
REWRITTEN,
117+
JSON,
118+
TEXT
119+
};
120+
static llvm::cl::opt<DumpType> DumpIn(
121+
llvm::cl::desc("Dump edits to stdout as:"),
122+
llvm::cl::init(DumpType::REWRITTEN),
123+
llvm::cl::values(
124+
clEnumValN(DumpType::REWRITTEN, "dump-rewritten",
125+
"rewritten file"),
126+
clEnumValN(DumpType::JSON, "dump-json",
127+
"JSON"),
128+
clEnumValN(DumpType::TEXT, "dump-text",
129+
"text")));
119130

120131
static llvm::cl::opt<bool>
121132
AvailableActions("actions",
@@ -373,12 +384,19 @@ int main(int argc, char *argv[]) {
373384
RefactoringConfig.PreferredName = options::NewName;
374385
std::string Error;
375386
std::unique_ptr<SourceEditConsumer> pConsumer;
376-
if (options::DumpInJson)
377-
pConsumer.reset(new SourceEditJsonConsumer(llvm::outs()));
378-
else
387+
switch (options::DumpIn) {
388+
case options::DumpType::REWRITTEN:
379389
pConsumer.reset(new SourceEditOutputConsumer(SF->getASTContext().SourceMgr,
380-
BufferID,
381-
llvm::outs()));
390+
BufferID,
391+
llvm::outs()));
392+
break;
393+
case options::DumpType::JSON:
394+
pConsumer.reset(new SourceEditJsonConsumer(llvm::outs()));
395+
break;
396+
case options::DumpType::TEXT:
397+
pConsumer.reset(new SourceEditTextConsumer(llvm::outs()));
398+
break;
399+
}
382400

383401
return refactorSwiftModule(CI.getMainModule(), RefactoringConfig, *pConsumer,
384402
PrintDiags);

0 commit comments

Comments
 (0)