[sourcekitd-test] Support refactoring based on a byte offset location

If an offset position but no line/column combination is given to `sourcekitd-test` when requesting a refactoring action, compute the line/column from the offset.
This commit is contained in:
Alex Hoppen
2021-06-08 11:55:50 +02:00
parent daa8aacd64
commit 80bfcf5dad
2 changed files with 22 additions and 10 deletions

View File

@@ -129,6 +129,7 @@ func hasCallToAsyncAlternative() {
// CHECK1-NEXT: ACTIONS END
// RUN: %sourcekitd-test -req=cursor -pos=1:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK2
// RUN: %sourcekitd-test -req=cursor -offset=16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK2
// RUN: %sourcekitd-test -req=cursor -pos=12:8 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK3
// RUN: %sourcekitd-test -req=cursor -pos=21:5 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK4

View File

@@ -489,6 +489,23 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
return 0;
}
static void setRefactoringFields(sourcekitd_object_t &Req, TestOptions Opts,
sourcekitd_uid_t RefactoringKind,
llvm::MemoryBuffer *SourceBuf) {
if (Opts.Offset && !Opts.Line && !Opts.Col) {
auto LineCol = resolveToLineCol(Opts.Offset, SourceBuf);
Opts.Line = LineCol.first;
Opts.Col = LineCol.second;
}
sourcekitd_request_dictionary_set_uid(Req, KeyRequest,
RequestSemanticRefactoring);
sourcekitd_request_dictionary_set_uid(Req, KeyActionUID, RefactoringKind);
sourcekitd_request_dictionary_set_string(Req, KeyName, Opts.Name.c_str());
sourcekitd_request_dictionary_set_int64(Req, KeyLine, Opts.Line);
sourcekitd_request_dictionary_set_int64(Req, KeyColumn, Opts.Col);
sourcekitd_request_dictionary_set_int64(Req, KeyLength, Opts.Length);
}
static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
if (!Opts.JsonRequestPath.empty())
return handleJsonRequestPath(Opts.JsonRequestPath, Opts);
@@ -721,16 +738,10 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
break;
}
#define SEMANTIC_REFACTORING(KIND, NAME, ID) case SourceKitRequest::KIND: \
{ \
sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestSemanticRefactoring); \
sourcekitd_request_dictionary_set_uid(Req, KeyActionUID, KindRefactoring##KIND); \
sourcekitd_request_dictionary_set_string(Req, KeyName, Opts.Name.c_str()); \
sourcekitd_request_dictionary_set_int64(Req, KeyLine, Opts.Line); \
sourcekitd_request_dictionary_set_int64(Req, KeyColumn, Opts.Col); \
sourcekitd_request_dictionary_set_int64(Req, KeyLength, Opts.Length); \
break; \
}
#define SEMANTIC_REFACTORING(KIND, NAME, ID) \
case SourceKitRequest::KIND: \
setRefactoringFields(Req, Opts, KindRefactoring##KIND, SourceBuf.get()); \
break;
#include "swift/IDE/RefactoringKinds.def"
case SourceKitRequest::MarkupToXML: {