Skip to content

Commit bfa7185

Browse files
ahoppenCatfish-Man
authored andcommitted
[SourceKit] Allow passing cancel_on_subsequent_request: 0 for all requests that use a OncePerASTToken
We need this option for `collectVariableType` (aka inlay type hints) but since I’m at it, I’m adding an option to disable the implicit request cancellation for all requests that have it since we don’t want it in LSP at all. Prerequisite to fixing swiftlang/sourcekit-lsp#2021 / rdar://145871554, need to adopt this option in SourceKit-LSP.
1 parent c7dbd85 commit bfa7185

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ class LangSupport {
11071107
virtual void
11081108
editorOpenSwiftSourceInterface(StringRef Name, StringRef SourceName,
11091109
ArrayRef<const char *> Args,
1110+
bool CancelOnSubsequentRequest,
11101111
SourceKitCancellationToken CancellationToken,
11111112
std::shared_ptr<EditorConsumer> Consumer) = 0;
11121113

@@ -1216,12 +1217,14 @@ class LangSupport {
12161217
virtual void
12171218
findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,
12181219
unsigned Length, ArrayRef<const char *> Args,
1220+
bool CancelOnSubsequentRequest,
12191221
SourceKitCancellationToken CancellationToken,
12201222
CategorizedRenameRangesReceiver Receiver) = 0;
12211223

12221224
virtual void semanticRefactoring(StringRef PrimaryFilePath,
12231225
SemanticRefactoringInfo Info,
12241226
ArrayRef<const char *> Args,
1227+
bool CancelOnSubsequentRequest,
12251228
SourceKitCancellationToken CancellationToken,
12261229
CategorizedEditsReceiver Receiver) = 0;
12271230

@@ -1240,6 +1243,7 @@ class LangSupport {
12401243
StringRef PrimaryFilePath, StringRef InputBufferName,
12411244
ArrayRef<const char *> Args, std::optional<unsigned> Offset,
12421245
std::optional<unsigned> Length, bool FullyQualified,
1246+
bool CancelOnSubsequentRequest,
12431247
SourceKitCancellationToken CancellationToken,
12441248
std::function<void(const RequestResult<VariableTypesInFile> &)>
12451249
Receiver) = 0;

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,8 @@ SwiftLangSupport::findRenameRanges(llvm::MemoryBuffer *InputBuf,
14181418

14191419
void SwiftLangSupport::findLocalRenameRanges(
14201420
StringRef Filename, unsigned Line, unsigned Column, unsigned Length,
1421-
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
1421+
ArrayRef<const char *> Args, bool CancelOnSubsequentRequest,
1422+
SourceKitCancellationToken CancellationToken,
14221423
CategorizedRenameRangesReceiver Receiver) {
14231424
using ResultType = CancellableResult<std::vector<CategorizedRenameRanges>>;
14241425
std::string Error;
@@ -1465,8 +1466,8 @@ void SwiftLangSupport::findLocalRenameRanges(
14651466
/// FIXME: When request cancellation is implemented and Xcode adopts it,
14661467
/// don't use 'OncePerASTToken'.
14671468
static const char OncePerASTToken = 0;
1468-
getASTManager()->processASTAsync(Invok, ASTConsumer, &OncePerASTToken,
1469-
CancellationToken,
1469+
const void *Once = CancelOnSubsequentRequest ? &OncePerASTToken : nullptr;
1470+
getASTManager()->processASTAsync(Invok, ASTConsumer, Once, CancellationToken,
14701471
llvm::vfs::getRealFileSystem());
14711472
}
14721473

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ class PrimaryFileInterfaceConsumer : public SwiftASTConsumer {
853853

854854
void SwiftLangSupport::editorOpenSwiftSourceInterface(
855855
StringRef Name, StringRef SourceName, ArrayRef<const char *> Args,
856+
bool CancelOnSubsequentRequest,
856857
SourceKitCancellationToken CancellationToken,
857858
std::shared_ptr<EditorConsumer> Consumer) {
858859
std::string Error;
@@ -864,7 +865,8 @@ void SwiftLangSupport::editorOpenSwiftSourceInterface(
864865
auto AstConsumer = std::make_shared<PrimaryFileInterfaceConsumer>(Name,
865866
SourceName, IFaceGenContexts, Consumer, Invocation);
866867
static const char OncePerASTToken = 0;
867-
getASTManager()->processASTAsync(Invocation, AstConsumer, &OncePerASTToken,
868+
const void *Once = CancelOnSubsequentRequest ? &OncePerASTToken : nullptr;
869+
getASTManager()->processASTAsync(Invocation, AstConsumer, Once,
868870
CancellationToken,
869871
llvm::vfs::getRealFileSystem());
870872
}

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ class SwiftLangSupport : public LangSupport {
619619

620620
void editorOpenSwiftSourceInterface(
621621
StringRef Name, StringRef SourceName, ArrayRef<const char *> Args,
622+
bool CancelOnSubsequentRequest,
622623
SourceKitCancellationToken CancellationToken,
623624
std::shared_ptr<EditorConsumer> Consumer) override;
624625

@@ -708,6 +709,7 @@ class SwiftLangSupport : public LangSupport {
708709

709710
void findLocalRenameRanges(StringRef Filename, unsigned Line, unsigned Column,
710711
unsigned Length, ArrayRef<const char *> Args,
712+
bool CancelOnSubsequentRequest,
711713
SourceKitCancellationToken CancellationToken,
712714
CategorizedRenameRangesReceiver Receiver) override;
713715

@@ -723,13 +725,15 @@ class SwiftLangSupport : public LangSupport {
723725
StringRef PrimaryFilePath, StringRef InputBufferName,
724726
ArrayRef<const char *> Args, std::optional<unsigned> Offset,
725727
std::optional<unsigned> Length, bool FullyQualified,
728+
bool CancelOnSubsequentRequest,
726729
SourceKitCancellationToken CancellationToken,
727730
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver)
728731
override;
729732

730733
void semanticRefactoring(StringRef PrimaryFilePath,
731734
SemanticRefactoringInfo Info,
732735
ArrayRef<const char *> Args,
736+
bool CancelOnSubsequentRequest,
733737
SourceKitCancellationToken CancellationToken,
734738
CategorizedEditsReceiver Receiver) override;
735739

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,7 +2776,8 @@ static RefactoringKind getIDERefactoringKind(SemanticRefactoringInfo Info) {
27762776

27772777
void SwiftLangSupport::semanticRefactoring(
27782778
StringRef PrimaryFilePath, SemanticRefactoringInfo Info,
2779-
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
2779+
ArrayRef<const char *> Args, bool CancelOnSubsequentRequest,
2780+
SourceKitCancellationToken CancellationToken,
27802781
CategorizedEditsReceiver Receiver) {
27812782
std::string Error;
27822783
SwiftInvocationRef Invok =
@@ -2834,7 +2835,8 @@ void SwiftLangSupport::semanticRefactoring(
28342835
/// FIXME: When request cancellation is implemented and Xcode adopts it,
28352836
/// don't use 'OncePerASTToken'.
28362837
static const char OncePerASTToken = 0;
2837-
getASTManager()->processASTAsync(Invok, std::move(Consumer), &OncePerASTToken,
2838+
const void *Once = CancelOnSubsequentRequest ? &OncePerASTToken : nullptr;
2839+
getASTManager()->processASTAsync(Invok, std::move(Consumer), Once,
28382840
CancellationToken,
28392841
llvm::vfs::getRealFileSystem());
28402842
}
@@ -2918,6 +2920,7 @@ void SwiftLangSupport::collectVariableTypes(
29182920
StringRef PrimaryFilePath, StringRef InputBufferName,
29192921
ArrayRef<const char *> Args, std::optional<unsigned> Offset,
29202922
std::optional<unsigned> Length, bool FullyQualified,
2923+
bool CancelOnSubsequentRequest,
29212924
SourceKitCancellationToken CancellationToken,
29222925
std::function<void(const RequestResult<VariableTypesInFile> &)> Receiver) {
29232926
std::string Error;
@@ -2997,7 +3000,8 @@ void SwiftLangSupport::collectVariableTypes(
29973000
/// FIXME: When request cancellation is implemented and Xcode adopts it,
29983001
/// don't use 'OncePerASTToken'.
29993002
static const char OncePerASTToken = 0;
3000-
getASTManager()->processASTAsync(Invok, std::move(Collector),
3001-
&OncePerASTToken, CancellationToken,
3003+
const void *Once = CancelOnSubsequentRequest ? &OncePerASTToken : nullptr;
3004+
getASTManager()->processASTAsync(Invok, std::move(Collector), Once,
3005+
CancellationToken,
30023006
llvm::vfs::getRealFileSystem());
30033007
}

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ editorOpenHeaderInterface(StringRef Name, StringRef HeaderName,
331331
static void
332332
editorOpenSwiftSourceInterface(StringRef Name, StringRef SourceName,
333333
ArrayRef<const char *> Args,
334+
bool CancelOnSubsequentRequest,
334335
SourceKitCancellationToken CancellationToken,
335336
ResponseReceiver Rec, bool EnableDeclarations);
336337
@@ -1053,8 +1054,13 @@ static void handleRequestEditorOpenSwiftSourceInterface(
10531054
// Reporting the declarations array is off by default
10541055
bool EnableDeclarations =
10551056
Req.getOptionalInt64(KeyEnableDeclarations).value_or(false);
1057+
// For backwards compatibility, the default is 1.
1058+
int64_t CancelOnSubsequentRequest = 1;
1059+
Req.getInt64(KeyCancelOnSubsequentRequest, CancelOnSubsequentRequest,
1060+
/*isOptional=*/true);
10561061
return editorOpenSwiftSourceInterface(
1057-
*Name, *FileName, Args, CancellationToken, Rec, EnableDeclarations);
1062+
*Name, *FileName, Args, CancelOnSubsequentRequest, CancellationToken,
1063+
Rec, EnableDeclarations);
10581064
}
10591065
}
10601066
@@ -1705,8 +1711,15 @@ handleRequestSemanticRefactoring(const RequestDict &Req,
17051711
Info.Line = Line;
17061712
Info.Column = Column;
17071713
Info.Length = Length;
1714+
1715+
// For backwards compatibility, the default is 1.
1716+
int64_t CancelOnSubsequentRequest = 1;
1717+
Req.getInt64(KeyCancelOnSubsequentRequest, CancelOnSubsequentRequest,
1718+
/*isOptional=*/true);
1719+
17081720
return Lang.semanticRefactoring(
1709-
*PrimaryFilePath, Info, Args, CancellationToken,
1721+
*PrimaryFilePath, Info, Args, CancelOnSubsequentRequest,
1722+
CancellationToken,
17101723
[Rec](const RequestResult<ArrayRef<CategorizedEdits>> &Result) {
17111724
Rec(createCategorizedEditsResponse(Result));
17121725
});
@@ -1782,9 +1795,15 @@ handleRequestCollectVariableType(const RequestDict &Req,
17821795
[](int64_t v) -> unsigned { return v; });
17831796
int64_t FullyQualified = false;
17841797
Req.getInt64(KeyFullyQualified, FullyQualified, /*isOptional=*/true);
1798+
1799+
// For backwards compatibility, the default is 1.
1800+
int64_t CancelOnSubsequentRequest = 1;
1801+
Req.getInt64(KeyCancelOnSubsequentRequest, CancelOnSubsequentRequest,
1802+
/*isOptional=*/true);
1803+
17851804
return Lang.collectVariableTypes(
17861805
*PrimaryFilePath, InputBufferName, Args, Offset, Length, FullyQualified,
1787-
CancellationToken,
1806+
CancelOnSubsequentRequest, CancellationToken,
17881807
[Rec](const RequestResult<VariableTypesInFile> &Result) {
17891808
reportVariableTypeInfo(Result, Rec);
17901809
});
@@ -1814,9 +1833,15 @@ handleRequestFindLocalRenameRanges(const RequestDict &Req,
18141833
return Rec(createErrorRequestInvalid("'key.column' is required"));
18151834
Req.getInt64(KeyLength, Length, /*isOptional=*/true);
18161835
1836+
// For backwards compatibility, the default is 1.
1837+
int64_t CancelOnSubsequentRequest = 1;
1838+
Req.getInt64(KeyCancelOnSubsequentRequest, CancelOnSubsequentRequest,
1839+
/*isOptional=*/true);
1840+
18171841
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
18181842
return Lang.findLocalRenameRanges(
1819-
*PrimaryFilePath, Line, Column, Length, Args, CancellationToken,
1843+
*PrimaryFilePath, Line, Column, Length, Args, CancelOnSubsequentRequest,
1844+
CancellationToken,
18201845
[Rec](const CancellableResult<std::vector<CategorizedRenameRanges>>
18211846
&Result) {
18221847
Rec(createCategorizedRenameRangesResponse(Result));
@@ -3686,6 +3711,7 @@ static sourcekitd_response_t editorOpenInterface(
36863711
static void
36873712
editorOpenSwiftSourceInterface(StringRef Name, StringRef HeaderName,
36883713
ArrayRef<const char *> Args,
3714+
bool CancelOnSubsequentRequest,
36893715
SourceKitCancellationToken CancellationToken,
36903716
ResponseReceiver Rec, bool EnableDeclarations) {
36913717
SKEditorConsumerOptions Opts;
@@ -3694,8 +3720,9 @@ editorOpenSwiftSourceInterface(StringRef Name, StringRef HeaderName,
36943720
Opts.EnableDeclarations = EnableDeclarations;
36953721
auto EditC = std::make_shared<SKEditorConsumer>(Rec, Opts);
36963722
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
3697-
Lang.editorOpenSwiftSourceInterface(Name, HeaderName, Args, CancellationToken,
3698-
EditC);
3723+
Lang.editorOpenSwiftSourceInterface(Name, HeaderName, Args,
3724+
CancelOnSubsequentRequest,
3725+
CancellationToken, EditC);
36993726
}
37003727

37013728
static void

0 commit comments

Comments
 (0)