Skip to content

Commit 2826871

Browse files
authored
Merge pull request #37827 from ahoppen/pr/offset-refactoring-sourcekitd-test
[sourcekitd-test] Support refactoring based on a byte offset location
2 parents 0fccbcb + 80bfcf5 commit 2826871

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

test/SourceKit/Refactoring/basic.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func hasCallToAsyncAlternative() {
129129
// CHECK1-NEXT: ACTIONS END
130130

131131
// RUN: %sourcekitd-test -req=cursor -pos=1:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK2
132+
// RUN: %sourcekitd-test -req=cursor -offset=16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK2
132133

133134
// RUN: %sourcekitd-test -req=cursor -pos=12:8 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK3
134135
// RUN: %sourcekitd-test -req=cursor -pos=21:5 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK4

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,23 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
489489
return 0;
490490
}
491491

492+
static void setRefactoringFields(sourcekitd_object_t &Req, TestOptions Opts,
493+
sourcekitd_uid_t RefactoringKind,
494+
llvm::MemoryBuffer *SourceBuf) {
495+
if (Opts.Offset && !Opts.Line && !Opts.Col) {
496+
auto LineCol = resolveToLineCol(Opts.Offset, SourceBuf);
497+
Opts.Line = LineCol.first;
498+
Opts.Col = LineCol.second;
499+
}
500+
sourcekitd_request_dictionary_set_uid(Req, KeyRequest,
501+
RequestSemanticRefactoring);
502+
sourcekitd_request_dictionary_set_uid(Req, KeyActionUID, RefactoringKind);
503+
sourcekitd_request_dictionary_set_string(Req, KeyName, Opts.Name.c_str());
504+
sourcekitd_request_dictionary_set_int64(Req, KeyLine, Opts.Line);
505+
sourcekitd_request_dictionary_set_int64(Req, KeyColumn, Opts.Col);
506+
sourcekitd_request_dictionary_set_int64(Req, KeyLength, Opts.Length);
507+
}
508+
492509
static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
493510
if (!Opts.JsonRequestPath.empty())
494511
return handleJsonRequestPath(Opts.JsonRequestPath, Opts);
@@ -721,16 +738,10 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
721738
break;
722739
}
723740

724-
#define SEMANTIC_REFACTORING(KIND, NAME, ID) case SourceKitRequest::KIND: \
725-
{ \
726-
sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestSemanticRefactoring); \
727-
sourcekitd_request_dictionary_set_uid(Req, KeyActionUID, KindRefactoring##KIND); \
728-
sourcekitd_request_dictionary_set_string(Req, KeyName, Opts.Name.c_str()); \
729-
sourcekitd_request_dictionary_set_int64(Req, KeyLine, Opts.Line); \
730-
sourcekitd_request_dictionary_set_int64(Req, KeyColumn, Opts.Col); \
731-
sourcekitd_request_dictionary_set_int64(Req, KeyLength, Opts.Length); \
732-
break; \
733-
}
741+
#define SEMANTIC_REFACTORING(KIND, NAME, ID) \
742+
case SourceKitRequest::KIND: \
743+
setRefactoringFields(Req, Opts, KindRefactoring##KIND, SourceBuf.get()); \
744+
break;
734745
#include "swift/IDE/RefactoringKinds.def"
735746

736747
case SourceKitRequest::MarkupToXML: {

0 commit comments

Comments
 (0)