Skip to content

Commit d54e70c

Browse files
committed
[CodeCompletion] Option to swift-ide-test to test annotated results
1 parent ad8415a commit d54e70c

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ class CodeCompletionResult {
793793
}
794794

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

799799
static CodeCompletionDeclKind getCodeCompletionDeclKind(const Decl *D);
@@ -911,14 +911,17 @@ class PrintingCodeCompletionConsumer
911911
llvm::raw_ostream &OS;
912912
bool IncludeKeywords;
913913
bool IncludeComments;
914+
bool PrintAnnotatedDescription;
914915

915916
public:
916917
PrintingCodeCompletionConsumer(llvm::raw_ostream &OS,
917918
bool IncludeKeywords = true,
918-
bool IncludeComments = true)
919+
bool IncludeComments = true,
920+
bool PrintAnnotatedDescription = false)
919921
: OS(OS),
920922
IncludeKeywords(IncludeKeywords),
921-
IncludeComments(IncludeComments) {}
923+
IncludeComments(IncludeComments),
924+
PrintAnnotatedDescription(PrintAnnotatedDescription) {}
922925

923926
void handleResults(MutableArrayRef<CodeCompletionResult *> Results) override;
924927
};

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/ClangImporter/ClangImporter.h"
3232
#include "swift/ClangImporter/ClangModule.h"
3333
#include "swift/IDE/CodeCompletionCache.h"
34+
#include "swift/IDE/CodeCompletionResultPrinter.h"
3435
#include "swift/IDE/Utils.h"
3536
#include "swift/Parse/CodeCompletionCallbacks.h"
3637
#include "swift/Sema/IDETypeChecking.h"
@@ -535,7 +536,7 @@ CodeCompletionResult::getCodeCompletionDeclKind(const Decl *D) {
535536
llvm_unreachable("invalid DeclKind");
536537
}
537538

538-
void CodeCompletionResult::print(raw_ostream &OS) const {
539+
void CodeCompletionResult::printPrefix(raw_ostream &OS) const {
539540
llvm::SmallString<64> Prefix;
540541
switch (getKind()) {
541542
case ResultKind::Declaration:
@@ -738,11 +739,12 @@ void CodeCompletionResult::print(raw_ostream &OS) const {
738739
Prefix.append(" ");
739740
}
740741
OS << Prefix;
741-
CompletionString->print(OS);
742742
}
743743

744744
void CodeCompletionResult::dump() const {
745-
print(llvm::errs());
745+
printPrefix(llvm::errs());
746+
CompletionString->print(llvm::errs());
747+
llvm::errs() << "\n";
746748
}
747749

748750
static StringRef copyString(llvm::BumpPtrAllocator &Allocator,
@@ -5969,7 +5971,11 @@ void PrintingCodeCompletionConsumer::handleResults(
59695971
for (auto Result : Results) {
59705972
if (!IncludeKeywords && Result->getKind() == CodeCompletionResult::Keyword)
59715973
continue;
5972-
Result->print(OS);
5974+
Result->printPrefix(OS);
5975+
if (PrintAnnotatedDescription)
5976+
printCodeCompletionResultDescriptionAnnotated(*Result, OS, /*leadingPunctuation=*/false);
5977+
else
5978+
Result->getCompletionString()->print(OS);
59735979

59745980
llvm::SmallString<64> Name;
59755981
llvm::raw_svector_ostream NameOs(Name);

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ bool SwiftToSourceKitCompletionAdapter::handleResult(
449449

450450
LogMessageOs << "Code completion result with empty description "
451451
"was ignored: \n";
452-
Result->print(LogMessageOs);
452+
Result->printPrefix(LogMessageOs);
453+
Result->getCompletionString()->print(LogMessageOs);
453454

454455
*Log << LogMessage;
455456
}

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,12 @@ CodeCompletionComments("code-completion-comments",
429429
llvm::cl::cat(Category),
430430
llvm::cl::init(false));
431431

432+
static llvm::cl::opt<bool>
433+
CodeCOmpletionAnnotateResults("code-completion-annotate-results",
434+
llvm::cl::desc("annotate completion results with XML"),
435+
llvm::cl::cat(Category),
436+
llvm::cl::init(false));
437+
432438
static llvm::cl::opt<std::string>
433439
DebugClientDiscriminator("debug-client-discriminator",
434440
llvm::cl::desc("A discriminator to prefer in lookups"),
@@ -842,19 +848,22 @@ static int doCodeCompletion(const CompilerInvocation &InitInvok,
842848
StringRef CodeCompletionToken,
843849
bool CodeCompletionDiagnostics,
844850
bool CodeCompletionKeywords,
845-
bool CodeCompletionComments) {
851+
bool CodeCompletionComments,
852+
bool CodeCompletionAnnotateResults) {
846853
std::unique_ptr<ide::OnDiskCodeCompletionCache> OnDiskCache;
847854
if (!options::CompletionCachePath.empty()) {
848855
OnDiskCache = std::make_unique<ide::OnDiskCodeCompletionCache>(
849856
options::CompletionCachePath);
850857
}
851858
ide::CodeCompletionCache CompletionCache(OnDiskCache.get());
852859
ide::CodeCompletionContext CompletionContext(CompletionCache);
860+
CompletionContext.setAnnotateResult(CodeCompletionAnnotateResults);
853861

854862
// Create a CodeCompletionConsumer.
855863
std::unique_ptr<ide::CodeCompletionConsumer> Consumer(
856864
new ide::PrintingCodeCompletionConsumer(
857-
llvm::outs(), CodeCompletionKeywords, CodeCompletionComments));
865+
llvm::outs(), CodeCompletionKeywords, CodeCompletionComments,
866+
CodeCompletionAnnotateResults));
858867

859868
// Create a factory for code completion callbacks that will feed the
860869
// Consumer.
@@ -3318,7 +3327,8 @@ int main(int argc, char *argv[]) {
33183327

33193328
ide::PrintingCodeCompletionConsumer Consumer(
33203329
llvm::outs(), options::CodeCompletionKeywords,
3321-
options::CodeCompletionComments);
3330+
options::CodeCompletionComments,
3331+
options::CodeCOmpletionAnnotateResults);
33223332
for (StringRef filename : options::InputFilenames) {
33233333
auto resultsOpt = ide::OnDiskCodeCompletionCache::getFromFile(filename);
33243334
if (!resultsOpt) {
@@ -3527,7 +3537,8 @@ int main(int argc, char *argv[]) {
35273537
options::CodeCompletionToken,
35283538
options::CodeCompletionDiagnostics,
35293539
options::CodeCompletionKeywords,
3530-
options::CodeCompletionComments);
3540+
options::CodeCompletionComments,
3541+
options::CodeCOmpletionAnnotateResults);
35313542
break;
35323543

35333544
case ActionType::REPLCodeCompletion:

0 commit comments

Comments
 (0)