Skip to content

Commit 31e7873

Browse files
authored
Merge pull request #30767 from rintaro/sourcekit-completion-annotateddesc-rdar60801189
[SourceKit/CodeCompletion] Add an option to emit annotated description
2 parents 2af7d8a + 339ebbf commit 31e7873

19 files changed

+855
-184
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ class ASTPrinter {
206206
void printKeyword(StringRef name, PrintOptions Opts, StringRef Suffix = "") {
207207
if (Opts.SkipUnderscoredKeywords && name.startswith("_"))
208208
return;
209+
assert(!name.empty() && "Tried to print empty keyword");
209210
callPrintNamePre(PrintNameContext::Keyword);
210211
*this << name;
211212
printNamePost(PrintNameContext::Keyword);

include/swift/IDE/CodeCompletion.h

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,18 @@ class CodeCompletionStringChunk {
8484
/// The keyword part of a declaration before the name, like "func".
8585
DeclIntroducer,
8686

87+
/// Other generic keyword.
88+
Keyword,
89+
90+
/// Other generic attributes.
91+
Attribute,
92+
8793
/// Normal text chunk.
8894
Text,
8995

96+
/// Base name of the result.
97+
BaseName,
98+
9099
/// The first chunk of an optional substring that continues until
91100
/// \c NestingLevel decreases.
92101
OptionalBegin,
@@ -133,6 +142,16 @@ class CodeCompletionStringChunk {
133142

134143
/// Required parameter type.
135144
CallParameterType,
145+
146+
/// Parameter type tag for annotated results.
147+
CallParameterTypeBegin,
148+
149+
/// System type name.
150+
TypeIdSystem,
151+
152+
/// Non-system type name.
153+
TypeIdUser,
154+
136155
/// Desugared closure parameter type. This can be used to get the
137156
/// closure type if CallParameterType is a TypeAliasType.
138157
CallParameterClosureType,
@@ -169,7 +188,8 @@ class CodeCompletionStringChunk {
169188
static bool chunkStartsNestedGroup(ChunkKind Kind) {
170189
return Kind == ChunkKind::CallParameterBegin ||
171190
Kind == ChunkKind::GenericParameterBegin ||
172-
Kind == ChunkKind::OptionalBegin;
191+
Kind == ChunkKind::OptionalBegin ||
192+
Kind == ChunkKind::CallParameterTypeBegin;
173193
}
174194

175195
static bool chunkHasText(ChunkKind Kind) {
@@ -179,6 +199,9 @@ class CodeCompletionStringChunk {
179199
Kind == ChunkKind::RethrowsKeyword ||
180200
Kind == ChunkKind::DeclAttrKeyword ||
181201
Kind == ChunkKind::DeclIntroducer ||
202+
Kind == ChunkKind::Keyword ||
203+
Kind == ChunkKind::Attribute ||
204+
Kind == ChunkKind::BaseName ||
182205
Kind == ChunkKind::Text ||
183206
Kind == ChunkKind::LeftParen ||
184207
Kind == ChunkKind::RightParen ||
@@ -205,7 +228,9 @@ class CodeCompletionStringChunk {
205228
Kind == ChunkKind::DynamicLookupMethodCallTail ||
206229
Kind == ChunkKind::OptionalMethodCallTail ||
207230
Kind == ChunkKind::TypeAnnotation ||
208-
Kind == ChunkKind::BraceStmtWithCursor;
231+
Kind == ChunkKind::BraceStmtWithCursor ||
232+
Kind == ChunkKind::TypeIdSystem ||
233+
Kind == ChunkKind::TypeIdUser;
209234
}
210235

211236
private:
@@ -768,7 +793,7 @@ class CodeCompletionResult {
768793
}
769794

770795
/// Print a debug representation of the code completion result to \p OS.
771-
void print(raw_ostream &OS) const;
796+
void printPrefix(raw_ostream &OS) const;
772797
SWIFT_DEBUG_DUMP;
773798

774799
static CodeCompletionDeclKind getCodeCompletionDeclKind(const Decl *D);
@@ -788,6 +813,9 @@ struct CodeCompletionResultSink {
788813
/// other sinks.
789814
std::vector<AllocatorPtr> ForeignAllocators;
790815

816+
/// Whether to annotate the results with XML.
817+
bool annotateResult = false;
818+
791819
std::vector<CodeCompletionResult *> Results;
792820

793821
/// A single-element cache for module names stored in Allocator, keyed by a
@@ -831,6 +859,9 @@ class CodeCompletionContext {
831859
CodeCompletionContext(CodeCompletionCache &Cache)
832860
: Cache(Cache) {}
833861

862+
void setAnnotateResult(bool flag) { CurrentResults.annotateResult = flag; }
863+
bool getAnnnoateResult() { return CurrentResults.annotateResult; }
864+
834865
/// Allocate a string owned by the code completion context.
835866
StringRef copyString(StringRef Str);
836867

@@ -880,14 +911,17 @@ class PrintingCodeCompletionConsumer
880911
llvm::raw_ostream &OS;
881912
bool IncludeKeywords;
882913
bool IncludeComments;
914+
bool PrintAnnotatedDescription;
883915

884916
public:
885917
PrintingCodeCompletionConsumer(llvm::raw_ostream &OS,
886918
bool IncludeKeywords = true,
887-
bool IncludeComments = true)
919+
bool IncludeComments = true,
920+
bool PrintAnnotatedDescription = false)
888921
: OS(OS),
889922
IncludeKeywords(IncludeKeywords),
890-
IncludeComments(IncludeComments) {}
923+
IncludeComments(IncludeComments),
924+
PrintAnnotatedDescription(PrintAnnotatedDescription) {}
891925

892926
void handleResults(MutableArrayRef<CodeCompletionResult *> Results) override;
893927
};

include/swift/IDE/CodeCompletionCache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CodeCompletionCache {
4343
bool ForTestableLookup;
4444
bool ForPrivateImportLookup;
4545
bool CodeCompleteInitsInPostfixExpr;
46+
bool Annotated;
4647

4748
friend bool operator==(const Key &LHS, const Key &RHS) {
4849
return LHS.ModuleFilename == RHS.ModuleFilename &&
@@ -108,10 +109,10 @@ template<>
108109
struct DenseMapInfo<swift::ide::CodeCompletionCache::Key> {
109110
using KeyTy = swift::ide::CodeCompletionCache::Key;
110111
static inline KeyTy getEmptyKey() {
111-
return KeyTy{"", "", {}, false, false, false, false};
112+
return KeyTy{"", "", {}, false, false, false, false, false};
112113
}
113114
static inline KeyTy getTombstoneKey() {
114-
return KeyTy{"", "", {}, true, false, false, false};
115+
return KeyTy{"", "", {}, true, false, false, false, false};
115116
}
116117
static unsigned getHashValue(const KeyTy &Val) {
117118
size_t H = 0;
@@ -122,6 +123,7 @@ struct DenseMapInfo<swift::ide::CodeCompletionCache::Key> {
122123
H ^= std::hash<bool>()(Val.ResultsHaveLeadingDot);
123124
H ^= std::hash<bool>()(Val.ForTestableLookup);
124125
H ^= std::hash<bool>()(Val.ForPrivateImportLookup);
126+
H ^= std::hash<bool>()(Val.Annotated);
125127
return static_cast<unsigned>(H);
126128
}
127129
static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- CodeCompletionResultPrinter.h --------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_IDE_CODECOMPLETIONRESULTPRINTER_H
14+
#define SWIFT_IDE_CODECOMPLETIONRESULTPRINTER_H
15+
16+
#include "llvm/Support/raw_ostream.h"
17+
18+
namespace swift {
19+
namespace ide {
20+
21+
class CodeCompletionResult;
22+
23+
void printCodeCompletionResultDescription(const CodeCompletionResult &Result,
24+
llvm::raw_ostream &OS,
25+
bool leadingPunctuation);
26+
27+
void printCodeCompletionResultDescriptionAnnotated(
28+
const CodeCompletionResult &Result, llvm::raw_ostream &OS,
29+
bool leadingPunctuation);
30+
} // namespace ide
31+
} // namespace swift
32+
33+
#endif

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,9 +2566,9 @@ static bool isEscaping(Type type) {
25662566
static void printParameterFlags(ASTPrinter &printer, PrintOptions options,
25672567
ParameterTypeFlags flags, bool escaping) {
25682568
if (!options.excludeAttrKind(TAK_autoclosure) && flags.isAutoClosure())
2569-
printer << "@autoclosure ";
2569+
printer.printAttrName("@autoclosure ");
25702570
if (!options.excludeAttrKind(TAK_noDerivative) && flags.isNoDerivative())
2571-
printer << "@noDerivative ";
2571+
printer.printAttrName("@noDerivative ");
25722572

25732573
switch (flags.getValueOwnership()) {
25742574
case ValueOwnership::Default:

lib/IDE/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_swift_host_library(swiftIDE STATIC
22
CodeCompletion.cpp
33
CodeCompletionCache.cpp
4+
CodeCompletionResultPrinter.cpp
45
CommentConversion.cpp
56
CompletionInstance.cpp
67
ConformingMethodList.cpp

0 commit comments

Comments
 (0)