Skip to content

Commit 631013c

Browse files
authored
Merge pull request swiftlang#36316 from rintaro/sourcekit-editnowait-rdar74984613
[sourcekit] Response 'edit' immediately if client needs nothing
2 parents 3420058 + 40dc8e1 commit 631013c

File tree

7 files changed

+50
-1
lines changed

7 files changed

+50
-1
lines changed

test/SourceKit/Sema/edit_nowait.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: echo "" > %t/t.swift
3+
4+
// RUN: %sourcekitd-test \
5+
// RUN: -req=open %t/t.swift -- %t/t.swift == \
6+
// RUN: -req=edit -offset=0 -replace="func foo() { warn("") }" -length=0 -req-opts=enablesyntaxmap=0,enablesubstructure=0,enablediagnostics=0 %t/t.swift -print-raw-response == \
7+
// RUN: -req=edit -offset=13 -replace="print" -length=5 -req-opts=enablesyntaxmap=0,enablesubstructure=0,enablediagnostics=0 %t/t.swift -print-raw-response \
8+
// RUN: | %FileCheck --check-prefix=EDIT_NOWAIT %s
9+
10+
// EDIT_NOWAIT: {
11+
// EDIT_NOWAIT-NEXT: }
12+
// EDIT_NOWAIT-NEXT: {
13+
// EDIT_NOWAIT-NEXT: }
14+
15+
// RUN: %sourcekitd-test \
16+
// RUN: %sourcekitd-test \
17+
// RUN: -req=open %t/t.swift -- %t/t.swift == \
18+
// RUN: -req=edit -offset=0 -replace="func foo() { warn("") }" -length=0 -req-opts=enablesyntaxmap=0,enablesubstructure=0,enablediagnostics=0 %t/t.swift == \
19+
// RUN: -req=edit -offset=13 -replace="print" -length=4 -req-opts=enablesyntaxmap=0,enablesubstructure=0,enablediagnostics=0 %t/t.swift == \
20+
// RUN: -req=print-annotations %s \
21+
// RUN: | %FileCheck --check-prefix=ANNOTATION %s
22+
23+
// ANNOTATION: [
24+
// ANNOTATION-NEXT: {
25+
// ANNOTATION-NEXT: key.kind: source.lang.swift.ref.function.free,
26+
// ANNOTATION-NEXT: key.offset: 13,
27+
// ANNOTATION-NEXT: key.length: 5,
28+
// ANNOTATION-NEXT: key.is_system: 1
29+
// ANNOTATION-NEXT: }
30+
// ANNOTATION-NEXT: ]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ class EditorConsumer {
276276

277277
virtual void recordFormattedText(StringRef Text) = 0;
278278

279+
virtual bool diagnosticsEnabled() = 0;
280+
279281
virtual void setDiagnosticStage(UIdent DiagStage) = 0;
280282
virtual void handleDiagnostic(const DiagnosticEntryInfo &Info,
281283
UIdent DiagStage) = 0;

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,8 @@ void SwiftEditorDocument::readSyntaxInfo(EditorConsumer &Consumer, bool ReportDi
20162016
"same time is not supported. Use the syntax tree to compute the "
20172017
"document structure.");
20182018
}
2019-
} else {
2019+
} else if (Consumer.documentStructureEnabled() ||
2020+
Consumer.syntaxMapEnabled()) {
20202021
ide::SyntaxModelContext ModelContext(Impl.SyntaxInfo->getSourceFile());
20212022

20222023
SwiftEditorSyntaxWalker SyntaxWalker(
@@ -2500,6 +2501,14 @@ void SwiftLangSupport::editorReplaceText(StringRef Name,
25002501
SyntaxCache->addEdit(Offset, Offset + Length, Buf->getBufferSize());
25012502
}
25022503

2504+
// If client doesn't need any information, we doen't need to parse it.
2505+
if (!Consumer.documentStructureEnabled() &&
2506+
!Consumer.syntaxMapEnabled() &&
2507+
!Consumer.diagnosticsEnabled() &&
2508+
!Consumer.syntaxTreeEnabled()) {
2509+
return;
2510+
}
2511+
25032512
SyntaxParsingCache *SyntaxCachePtr = nullptr;
25042513
if (SyntaxCache.hasValue()) {
25052514
SyntaxCachePtr = SyntaxCache.getPointer();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
897897
case SourceKitRequest::Open:
898898
sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestEditorOpen);
899899
sourcekitd_request_dictionary_set_string(Req, KeyName, SemaName.c_str());
900+
addRequestOptionsDirect(Req, Opts);
900901
break;
901902

902903
case SourceKitRequest::Close:
@@ -912,6 +913,7 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
912913
sourcekitd_request_dictionary_set_int64(Req, KeyLength, Opts.Length);
913914
sourcekitd_request_dictionary_set_string(Req, KeySourceText,
914915
Opts.ReplaceText.getValue().c_str());
916+
addRequestOptionsDirect(Req, Opts);
915917
break;
916918

917919
case SourceKitRequest::PrintAnnotations:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,8 @@ class SKEditorConsumer : public EditorConsumer {
24962496

24972497
void recordFormattedText(StringRef Text) override;
24982498

2499+
bool diagnosticsEnabled() override { return Opts.EnableDiagnostics; }
2500+
24992501
void setDiagnosticStage(UIdent DiagStage) override;
25002502
void handleDiagnostic(const DiagnosticEntryInfo &Info,
25012503
UIdent DiagStage) override;

unittests/SourceKit/SwiftLang/CursorInfoTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class NullEditorConsumer : public EditorConsumer {
7272

7373
void recordAffectedLineRange(unsigned Line, unsigned Length) override {}
7474

75+
bool diagnosticsEnabled() override { return false; }
76+
7577
void setDiagnosticStage(UIdent DiagStage) override {}
7678
void handleDiagnostic(const DiagnosticEntryInfo &Info,
7779
UIdent DiagStage) override {}

unittests/SourceKit/SwiftLang/EditingTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class TestConsumer : public EditorConsumer {
9292

9393
void recordFormattedText(StringRef Text) override {}
9494

95+
bool diagnosticsEnabled() override { return true; }
96+
9597
void setDiagnosticStage(UIdent diagStage) override { DiagStage = diagStage; }
9698
void handleDiagnostic(const DiagnosticEntryInfo &Info,
9799
UIdent DiagStage) override {

0 commit comments

Comments
 (0)