|
14 | 14 | #include "swift/Basic/Edit.h"
|
15 | 15 | #include "swift/Basic/SourceManager.h"
|
16 | 16 |
|
| 17 | +using namespace swift; |
| 18 | + |
| 19 | +void SourceEdits::addEdit(SourceManager &SM, CharSourceRange Range, |
| 20 | + StringRef Text) { |
| 21 | + SourceLoc Loc = Range.getStart(); |
| 22 | + unsigned BufID = SM.findBufferContainingLoc(Loc); |
| 23 | + unsigned Offset = SM.getLocOffsetInBuffer(Loc, BufID); |
| 24 | + unsigned Length = Range.getByteLength(); |
| 25 | + StringRef Path = SM.getIdentifierForBuffer(BufID); |
| 26 | + |
| 27 | + // NOTE: We cannot store SourceManager here since this logic is used by a |
| 28 | + // DiagnosticConsumer where the SourceManager may not remain valid. This is |
| 29 | + // the case when e.g build swift interfaces, we create a fresh |
| 30 | + // CompilerInstance for a limited scope, but diagnostics are passed outside of |
| 31 | + // it. |
| 32 | + Edits.push_back({Path.str(), Text.str(), Offset, Length}); |
| 33 | +} |
| 34 | + |
17 | 35 | void swift::
|
18 |
| -writeEditsInJson(ArrayRef<SingleEdit> AllEdits, llvm::raw_ostream &OS) { |
| 36 | +writeEditsInJson(const SourceEdits &AllEdits, llvm::raw_ostream &OS) { |
19 | 37 | OS << "[\n";
|
20 |
| - for (auto &Edit : AllEdits) { |
21 |
| - SourceManager &SM = Edit.SM; |
22 |
| - CharSourceRange Range = Edit.Range; |
23 |
| - StringRef Text = Edit.Text; |
24 |
| - SourceLoc Loc = Range.getStart(); |
25 |
| - unsigned BufID = SM.findBufferContainingLoc(Loc); |
26 |
| - unsigned Offset = SM.getLocOffsetInBuffer(Loc, BufID); |
27 |
| - unsigned Length = Range.getByteLength(); |
28 |
| - StringRef Path(SM.getIdentifierForBuffer(BufID)); |
29 |
| - |
| 38 | + for (auto &Edit : AllEdits.getEdits()) { |
30 | 39 | OS << " {\n";
|
31 | 40 | OS << " \"file\": \"";
|
32 |
| - OS.write_escaped(Path) << "\",\n"; |
33 |
| - OS << " \"offset\": " << Offset << ",\n"; |
34 |
| - if (Length != 0) |
35 |
| - OS << " \"remove\": " << Length << ",\n"; |
36 |
| - if (!Text.empty()) { |
| 41 | + OS.write_escaped(Edit.Path) << "\",\n"; |
| 42 | + OS << " \"offset\": " << Edit.Offset << ",\n"; |
| 43 | + if (Edit.Length != 0) |
| 44 | + OS << " \"remove\": " << Edit.Length << ",\n"; |
| 45 | + if (!Edit.Text.empty()) { |
37 | 46 | OS << " \"text\": \"";
|
38 |
| - OS.write_escaped(Text) << "\",\n"; |
| 47 | + OS.write_escaped(Edit.Text) << "\",\n"; |
39 | 48 | }
|
40 | 49 | OS << " },\n";
|
41 | 50 | }
|
|
0 commit comments