Skip to content

Commit c35d119

Browse files
committed
[SourceKit] Introduce RawCharSourceRange
Use this to simplify the writing of ranges into SourceKit responses.
1 parent f0d5456 commit c35d119

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,18 @@ enum class DiagnosticCategory {
215215
NoUsage
216216
};
217217

218+
struct RawCharSourceRange {
219+
unsigned Offset;
220+
unsigned Length;
221+
};
222+
218223
struct DiagnosticEntryInfoBase {
219224
struct Fixit {
220-
unsigned Offset;
221-
unsigned Length;
225+
RawCharSourceRange Range;
222226
std::string Text;
227+
228+
Fixit(RawCharSourceRange Range, std::string Text)
229+
: Range(Range), Text(std::move(Text)) {}
223230
};
224231

225232
std::string ID;
@@ -229,7 +236,7 @@ struct DiagnosticEntryInfoBase {
229236
unsigned Column = 0;
230237
std::string Filename;
231238
SmallVector<DiagnosticCategory, 1> Categories;
232-
SmallVector<std::pair<unsigned, unsigned>, 2> Ranges;
239+
SmallVector<RawCharSourceRange, 2> Ranges;
233240
SmallVector<Fixit, 2> Fixits;
234241
SmallVector<std::string, 1> EducationalNotePaths;
235242
};

tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,6 +3514,12 @@ void SKEditorConsumer::recordFormattedText(StringRef Text) {
35143514
Dict.set(KeySourceText, Text);
35153515
}
35163516

3517+
static void fillDictionaryForRange(ResponseBuilder::Dictionary Elem,
3518+
const RawCharSourceRange &R) {
3519+
Elem.set(KeyOffset, R.Offset);
3520+
Elem.set(KeyLength, R.Length);
3521+
}
3522+
35173523
static void fillDictionaryForDiagnosticInfoBase(
35183524
ResponseBuilder::Dictionary Elem, const DiagnosticEntryInfoBase &Info) {
35193525

@@ -3554,19 +3560,15 @@ static void fillDictionaryForDiagnosticInfoBase(
35543560

35553561
if (!Info.Ranges.empty()) {
35563562
auto RangesArr = Elem.setArray(KeyRanges);
3557-
for (auto R : Info.Ranges) {
3558-
auto RangeElem = RangesArr.appendDictionary();
3559-
RangeElem.set(KeyOffset, R.first);
3560-
RangeElem.set(KeyLength, R.second);
3561-
}
3563+
for (auto R : Info.Ranges)
3564+
fillDictionaryForRange(RangesArr.appendDictionary(), R);
35623565
}
35633566

35643567
if (!Info.Fixits.empty()) {
35653568
auto FixitsArr = Elem.setArray(KeyFixits);
35663569
for (auto F : Info.Fixits) {
35673570
auto FixitElem = FixitsArr.appendDictionary();
3568-
FixitElem.set(KeyOffset, F.Offset);
3569-
FixitElem.set(KeyLength, F.Length);
3571+
fillDictionaryForRange(FixitElem, F.Range);
35703572
FixitElem.set(KeySourceText, F.Text);
35713573
}
35723574
}

0 commit comments

Comments
 (0)