Skip to content

Commit b70fcc7

Browse files
committed
[IDE] Fetch declaration full documentation on-demand
1 parent fb9b043 commit b70fcc7

16 files changed

+91
-77
lines changed

include/swift/IDE/CodeCompletionResult.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/Basic/StringExtras.h"
1717
#include "swift/IDE/CodeCompletionResultType.h"
1818
#include "swift/IDE/CodeCompletionString.h"
19+
#include "swift/IDE/CommentConversion.h"
1920

2021
namespace swift {
2122
namespace ide {
@@ -378,7 +379,6 @@ class ContextFreeCodeCompletionResult {
378379
CodeCompletionString *CompletionString;
379380
NullTerminatedStringRef ModuleName;
380381
NullTerminatedStringRef BriefDocComment;
381-
NullTerminatedStringRef FullDocComment;
382382
ArrayRef<NullTerminatedStringRef> AssociatedUSRs;
383383
CodeCompletionResultType ResultType;
384384

@@ -411,7 +411,6 @@ class ContextFreeCodeCompletionResult {
411411
bool HasAsyncAlternative, CodeCompletionString *CompletionString,
412412
NullTerminatedStringRef ModuleName,
413413
NullTerminatedStringRef BriefDocComment,
414-
NullTerminatedStringRef FullDocComment,
415414
ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
416415
CodeCompletionResultType ResultType,
417416
ContextFreeNotRecommendedReason NotRecommended,
@@ -423,9 +422,9 @@ class ContextFreeCodeCompletionResult {
423422
MacroRoles(MacroRoles), IsSystem(IsSystem),
424423
HasAsyncAlternative(HasAsyncAlternative),
425424
CompletionString(CompletionString), ModuleName(ModuleName),
426-
BriefDocComment(BriefDocComment), FullDocComment(FullDocComment),
427-
AssociatedUSRs(AssociatedUSRs), ResultType(ResultType),
428-
NotRecommended(NotRecommended), DiagnosticSeverity(DiagnosticSeverity),
425+
BriefDocComment(BriefDocComment), AssociatedUSRs(AssociatedUSRs),
426+
ResultType(ResultType), NotRecommended(NotRecommended),
427+
DiagnosticSeverity(DiagnosticSeverity),
429428
DiagnosticMessage(DiagnosticMessage), FilterName(FilterName),
430429
NameForDiagnostics(NameForDiagnostics) {
431430
this->AssociatedKind.Opaque = AssociatedKind;
@@ -455,23 +454,21 @@ class ContextFreeCodeCompletionResult {
455454
CodeCompletionString *CompletionString,
456455
CodeCompletionOperatorKind KnownOperatorKin,
457456
NullTerminatedStringRef BriefDocComment,
458-
NullTerminatedStringRef FullDocComment,
459457
CodeCompletionResultType ResultType,
460458
ContextFreeNotRecommendedReason NotRecommended,
461459
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
462460
NullTerminatedStringRef DiagnosticMessage);
463461

464462
/// Constructs a \c Keyword result.
465463
///
466-
/// \note The caller must ensure that the \p CompletionString, \p BriefDocComment,
467-
/// and \p FullDocComment outlive this result, typically by storing them in the same
468-
/// \c CodeCompletionResultSink as the result itself.
464+
/// \note The caller must ensure that the \p CompletionString and
465+
/// \p BriefDocComment outlive this result, typically by storing them in
466+
/// the same \c CodeCompletionResultSink as the result itself.
469467
static ContextFreeCodeCompletionResult *
470468
createKeywordResult(CodeCompletionResultSink &Sink,
471469
CodeCompletionKeywordKind Kind,
472470
CodeCompletionString *CompletionString,
473471
NullTerminatedStringRef BriefDocComment,
474-
NullTerminatedStringRef FullDocComment,
475472
CodeCompletionResultType ResultType);
476473

477474
/// Constructs a \c Literal result.
@@ -496,7 +493,6 @@ class ContextFreeCodeCompletionResult {
496493
const Decl *AssociatedDecl,
497494
bool HasAsyncAlternative, NullTerminatedStringRef ModuleName,
498495
NullTerminatedStringRef BriefDocComment,
499-
NullTerminatedStringRef FullDocComment,
500496
ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
501497
CodeCompletionResultType ResultType,
502498
ContextFreeNotRecommendedReason NotRecommended,
@@ -541,8 +537,6 @@ class ContextFreeCodeCompletionResult {
541537

542538
NullTerminatedStringRef getBriefDocComment() const { return BriefDocComment; }
543539

544-
NullTerminatedStringRef getFullDocComment() const { return FullDocComment; }
545-
546540
ArrayRef<NullTerminatedStringRef> getAssociatedUSRs() const {
547541
return AssociatedUSRs;
548542
}
@@ -617,6 +611,8 @@ class CodeCompletionResult {
617611
/// should be erased first if this completion string is inserted in the
618612
/// editor buffer.
619613
unsigned NumBytesToErase : 7;
614+
615+
// std::optional<NullTerminatedStringRef> FullDocComment;
620616

621617
public:
622618
static const unsigned MaxNumBytesToErase = 127;
@@ -738,14 +734,14 @@ class CodeCompletionResult {
738734
}
739735
}
740736

741-
bool getHasValidAssociatedDecl() const { return HasValidAssociatedDecl; }
737+
bool hasValidAssociatedDecl() const { return HasValidAssociatedDecl; }
742738

743739
const Decl *getAssociatedDecl() const {
744740
assert(HasValidAssociatedDecl && "AssociatedDecl hasn't been loaded yet");
745741
return AssociatedDecl;
746742
}
747743

748-
const Decl *findAssociatedDecl(DeclContext *DC);
744+
const Decl *findAssociatedDecl(const DeclContext *DC);
749745

750746
SemanticContextKind getSemanticContext() const { return SemanticContext; }
751747

@@ -772,8 +768,15 @@ class CodeCompletionResult {
772768
return getContextFreeResult().getBriefDocComment();
773769
}
774770

775-
NullTerminatedStringRef getFullDocComment() const {
776-
return getContextFreeResult().getFullDocComment();
771+
/// Prints the full documentation comment as XML to the provided \c OS stream.
772+
///
773+
/// \returns true if the result has a full documentation comment, false otherwise.
774+
bool printFullDocComment(raw_ostream &OS) const {
775+
assert(HasValidAssociatedDecl && "Associated declaration hasn't been fetched");
776+
if (AssociatedDecl)
777+
return ide::getDocumentationCommentAsXML(AssociatedDecl, OS);
778+
779+
return false;
777780
}
778781

779782
ArrayRef<NullTerminatedStringRef> getAssociatedUSRs() const {

lib/IDE/CodeCompletionCache.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
241241
auto chunkIndex = read32le(cursor);
242242
auto moduleIndex = read32le(cursor);
243243
auto briefDocIndex = read32le(cursor);
244-
auto fullDocIndex = read32le(cursor);
245244
auto diagMessageIndex = read32le(cursor);
246245
auto filterNameIndex = read32le(cursor);
247246
auto nameForDiagnosticsIndex = read32le(cursor);
@@ -262,7 +261,6 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
262261
CodeCompletionString *string = getCompletionString(chunkIndex);
263262
auto moduleName = getString(moduleIndex);
264263
auto briefDocComment = getString(briefDocIndex);
265-
auto fullDocComment = getString(fullDocIndex);
266264
auto diagMessage = getString(diagMessageIndex);
267265
auto filterName = getString(filterNameIndex);
268266
auto nameForDiagnostics = getString(nameForDiagnosticsIndex);
@@ -271,7 +269,7 @@ static bool readCachedModule(llvm::MemoryBuffer *in,
271269
new (*V.Allocator) ContextFreeCodeCompletionResult(
272270
kind, associatedKind, opKind, roles, isSystem,
273271
hasAsyncAlternative, string, moduleName, briefDocComment,
274-
fullDocComment, llvm::ArrayRef(assocUSRs).copy(*V.Allocator),
272+
llvm::ArrayRef(assocUSRs).copy(*V.Allocator),
275273
CodeCompletionResultType(resultTypes), notRecommended, diagSeverity,
276274
diagMessage, filterName, nameForDiagnostics);
277275

@@ -434,7 +432,6 @@ static void writeCachedModule(llvm::raw_ostream &out,
434432
static_cast<uint32_t>(addCompletionString(R->getCompletionString())));
435433
LE.write(addString(R->getModuleName())); // index into strings
436434
LE.write(addString(R->getBriefDocComment())); // index into strings
437-
LE.write(addString(R->getFullDocComment())); // index into strings
438435
LE.write(addString(R->getDiagnosticMessage())); // index into strings
439436
LE.write(addString(R->getFilterName())); // index into strings
440437
LE.write(addString(R->getNameForDiagnostics())); // index into strings

lib/IDE/CodeCompletionContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ static MutableArrayRef<CodeCompletionResult *> copyCodeCompletionResults(
114114
*contextFreeResult, SemanticContextKind::OtherModule,
115115
CodeCompletionFlair(),
116116
/*numBytesToErase=*/0, typeRelation, notRecommendedReason);
117+
118+
contextualResult->findAssociatedDecl(DC);
119+
117120
targetSink.Results.push_back(contextualResult);
118121
}
119122

lib/IDE/CodeCompletionResult.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
138138
CodeCompletionString *CompletionString,
139139
CodeCompletionOperatorKind KnownOperatorKind,
140140
NullTerminatedStringRef BriefDocComment,
141-
NullTerminatedStringRef FullDocComment,
142141
CodeCompletionResultType ResultType,
143142
ContextFreeNotRecommendedReason NotRecommended,
144143
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
@@ -156,7 +155,7 @@ ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
156155
Kind, /*AssociatedKind=*/0, KnownOperatorKind, /*MacroRoles=*/{},
157156
/*IsSystem=*/false, /*HasAsyncAlternative=*/false,
158157
CompletionString,
159-
/*ModuleName=*/"", BriefDocComment, FullDocComment,
158+
/*ModuleName=*/"", BriefDocComment,
160159
/*AssociatedUSRs=*/{}, ResultType, NotRecommended, DiagnosticSeverity,
161160
DiagnosticMessage,
162161
getCodeCompletionResultFilterName(CompletionString, Sink.getAllocator()),
@@ -168,7 +167,6 @@ ContextFreeCodeCompletionResult::createKeywordResult(
168167
CodeCompletionResultSink &Sink, CodeCompletionKeywordKind Kind,
169168
CodeCompletionString *CompletionString,
170169
NullTerminatedStringRef BriefDocComment,
171-
NullTerminatedStringRef FullDocComment,
172170
CodeCompletionResultType ResultType) {
173171
if (Sink.shouldProduceContextFreeResults()) {
174172
ResultType = ResultType.usrBasedType(Sink.getUSRTypeArena());
@@ -177,7 +175,7 @@ ContextFreeCodeCompletionResult::createKeywordResult(
177175
CodeCompletionResultKind::Keyword, static_cast<uint8_t>(Kind),
178176
CodeCompletionOperatorKind::None, /*MacroRoles=*/{},
179177
/*IsSystem=*/false, /*HasAsyncAlternative=*/false, CompletionString,
180-
/*ModuleName=*/"", BriefDocComment, FullDocComment,
178+
/*ModuleName=*/"", BriefDocComment,
181179
/*AssociatedUSRs=*/{}, ResultType, ContextFreeNotRecommendedReason::None,
182180
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"",
183181
getCodeCompletionResultFilterName(CompletionString, Sink.getAllocator()),
@@ -199,7 +197,6 @@ ContextFreeCodeCompletionResult::createLiteralResult(
199197
CompletionString,
200198
/*ModuleName=*/"",
201199
/*BriefDocComment=*/"",
202-
/*FullDocComment=*/"",
203200
/*AssociatedUSRs=*/{}, ResultType, ContextFreeNotRecommendedReason::None,
204201
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"",
205202
getCodeCompletionResultFilterName(CompletionString, Sink.getAllocator()),
@@ -226,7 +223,6 @@ ContextFreeCodeCompletionResult::createDeclResult(
226223
CodeCompletionResultSink &Sink, CodeCompletionString *CompletionString,
227224
const Decl *AssociatedDecl, bool HasAsyncAlternative,
228225
NullTerminatedStringRef ModuleName, NullTerminatedStringRef BriefDocComment,
229-
NullTerminatedStringRef FullDocComment,
230226
ArrayRef<NullTerminatedStringRef> AssociatedUSRs,
231227
CodeCompletionResultType ResultType,
232228
ContextFreeNotRecommendedReason NotRecommended,
@@ -240,8 +236,8 @@ ContextFreeCodeCompletionResult::createDeclResult(
240236
CodeCompletionResultKind::Declaration,
241237
static_cast<uint8_t>(getCodeCompletionDeclKind(AssociatedDecl)),
242238
CodeCompletionOperatorKind::None, getCompletionMacroRoles(AssociatedDecl),
243-
getDeclIsSystem(AssociatedDecl), HasAsyncAlternative, CompletionString,
244-
ModuleName, BriefDocComment, FullDocComment, AssociatedUSRs, ResultType,
239+
getDeclIsSystem(AssociatedDecl), HasAsyncAlternative,
240+
CompletionString, ModuleName, BriefDocComment, AssociatedUSRs, ResultType,
245241
NotRecommended, DiagnosticSeverity, DiagnosticMessage,
246242
getCodeCompletionResultFilterName(CompletionString, Sink.getAllocator()),
247243
/*NameForDiagnostics=*/getDeclNameForDiagnostics(AssociatedDecl, Sink));
@@ -442,7 +438,7 @@ ContextFreeCodeCompletionResult::calculateContextualTypeRelation(
442438

443439
// MARK: - CodeCompletionResult
444440

445-
const Decl *CodeCompletionResult::findAssociatedDecl(DeclContext *DC) {
441+
const Decl *CodeCompletionResult::findAssociatedDecl(const DeclContext *DC) {
446442
if (HasValidAssociatedDecl)
447443
return AssociatedDecl;
448444

lib/IDE/CodeCompletionResultBuilder.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
137137
ContextFreeResult = ContextFreeCodeCompletionResult::createDeclResult(
138138
Sink, CCS, AssociatedDecl, HasAsyncAlternative, ModuleName,
139139
NullTerminatedStringRef(BriefDocComment, Allocator),
140-
NullTerminatedStringRef(FullDocComment, Allocator),
141140
copyAssociatedUSRs(Allocator, AssociatedDecl), ResultType,
142141
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
143142
ContextFreeDiagnosticMessage);
@@ -147,16 +146,14 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
147146
case CodeCompletionResultKind::Keyword:
148147
ContextFreeResult = ContextFreeCodeCompletionResult::createKeywordResult(
149148
Sink, KeywordKind, CCS,
150-
NullTerminatedStringRef(BriefDocComment, Allocator),
151-
NullTerminatedStringRef(FullDocComment, Allocator), ResultType);
149+
NullTerminatedStringRef(BriefDocComment, Allocator), ResultType);
152150
break;
153151
case CodeCompletionResultKind::BuiltinOperator:
154152
case CodeCompletionResultKind::Pattern:
155153
ContextFreeResult =
156154
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
157155
Sink, Kind, CCS, CodeCompletionOperatorKind::None,
158-
NullTerminatedStringRef(BriefDocComment, Allocator),
159-
NullTerminatedStringRef(FullDocComment, Allocator), ResultType,
156+
NullTerminatedStringRef(BriefDocComment, Allocator), ResultType,
160157
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
161158
ContextFreeDiagnosticMessage);
162159
break;
@@ -241,12 +238,6 @@ void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D) {
241238
} else {
242239
setBriefDocComment(AssociatedDecl->getSemanticBriefComment());
243240
}
244-
245-
SmallString<256> Buffer;
246-
llvm::raw_svector_ostream OS(Buffer);
247-
ide::getDocumentationCommentAsXML(D, OS);
248-
249-
setFullDocComment(StringRef(Buffer).copy(Sink.getAllocator()));
250241
}
251242

252243
void CodeCompletionResultBuilder::addCallArgument(

lib/IDE/CodeCompletionResultBuilder.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class CodeCompletionResultBuilder {
5757
ContextualNotRecommendedReason ContextualNotRecReason =
5858
ContextualNotRecommendedReason::None;
5959
StringRef BriefDocComment;
60-
StringRef FullDocComment;
6160

6261
/// The result type that this completion item produces.
6362
CodeCompletionResultType ResultType = CodeCompletionResultType::unknown();
@@ -492,10 +491,6 @@ class CodeCompletionResultBuilder {
492491
void setBriefDocComment(StringRef comment) {
493492
BriefDocComment = comment;
494493
}
495-
496-
void setFullDocComment(StringRef comment) {
497-
FullDocComment = comment;
498-
}
499494
};
500495

501496
} // namespace ide

lib/IDE/CompletionOverrideLookup.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ void CompletionOverrideLookup::addResultBuilderBuildCompletion(
464464

465465
StringRef DocComment = getResultBuilderDocComment(function);
466466
Builder.setBriefDocComment(DocComment);
467-
Builder.setFullDocComment(DocComment);
468467
}
469468

470469
void CompletionOverrideLookup::addResultBuilderBuildCompletions(

tools/SourceKit/lib/SwiftLang/CodeCompletion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ class Completion {
166166
return getSwiftResult().getBriefDocComment();
167167
}
168168

169-
StringRef getFullDocComment() const {
170-
return getSwiftResult().getFullDocComment();
169+
bool printFullDocComment(llvm::raw_ostream &OS) const {
170+
return getSwiftResult().printFullDocComment(OS);
171171
}
172172

173173
ArrayRef<NullTerminatedStringRef> getAssociatedUSRs() const {

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
135135
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
136136
sink.swiftSink, CodeCompletionResultKind::Pattern, completionString,
137137
CodeCompletionOperatorKind::None,
138-
/*BriefDocComment=*/"", /*FullDocComment=*/"",
139-
CodeCompletionResultType::unknown(),
138+
/*BriefDocComment=*/"", CodeCompletionResultType::unknown(),
140139
ContextFreeNotRecommendedReason::None,
141140
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"");
142141
auto *swiftResult = new (sink.allocator) CodeCompletion::SwiftResult(
@@ -1162,7 +1161,6 @@ Completion *CompletionBuilder::finish() {
11621161
contextFreeBase.hasAsyncAlternative(),
11631162
newCompletionString, contextFreeBase.getModuleName(),
11641163
contextFreeBase.getBriefDocComment(),
1165-
contextFreeBase.getFullDocComment(),
11661164
contextFreeBase.getAssociatedUSRs(),
11671165
contextFreeBase.getResultType(),
11681166
contextFreeBase.getNotRecommendedReason(),

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct Options {
4646
bool annotatedDescription = false;
4747
bool includeObjectLiterals = true;
4848
bool addCallWithNoDefaultArgs = true;
49+
bool includeFullDocumentation = false;
4950
unsigned minFuzzyLength = 2;
5051
unsigned showTopNonLiteralResults = 3;
5152

0 commit comments

Comments
 (0)