@@ -533,8 +533,8 @@ bool SwiftREPL::PrintOneVariable(Debugger &debugger, StreamFileSP &output_sp,
533
533
return handled;
534
534
}
535
535
536
- int SwiftREPL::CompleteCode (const std::string ¤t_code,
537
- lldb_private::StringList &matches ) {
536
+ void SwiftREPL::CompleteCode (const std::string ¤t_code,
537
+ CompletionRequest &request ) {
538
538
// ----------------------------------------------------------------------g
539
539
// If we use the target's SwiftASTContext for completion, it reaaallly
540
540
// slows down subsequent expressions. The compiler team doesn't have time
@@ -546,7 +546,7 @@ int SwiftREPL::CompleteCode(const std::string ¤t_code,
546
546
auto type_system_or_err = m_target.GetScratchTypeSystemForLanguage (eLanguageTypeSwift);
547
547
if (!type_system_or_err) {
548
548
llvm::consumeError (type_system_or_err.takeError ());
549
- return 0 ;
549
+ return ;
550
550
}
551
551
552
552
auto *target_swift_ast =
@@ -579,54 +579,56 @@ int SwiftREPL::CompleteCode(const std::string ¤t_code,
579
579
swift::SourceFile &repl_source_file =
580
580
repl_module->getMainSourceFile (swift::SourceFileKind::REPL);
581
581
582
+ // Swift likes to give us strings to append to the current token but
583
+ // the CompletionRequest requires a replacement for the full current
584
+ // token. Fix this by getting the current token here and we attach
585
+ // the suffix we get from Swift.
586
+ std::string prefix = request.GetCursorArgumentPrefix ();
582
587
llvm::StringRef current_code_ref (current_code);
583
588
completions.populate (repl_source_file, current_code_ref);
589
+
590
+ // The root is the unique completion we need to use, so let's add it
591
+ // to the completion list. As the completion is unique we can stop here.
584
592
llvm::StringRef root = completions.getRoot ();
585
593
if (!root.empty ()) {
586
- matches. AppendString ( root.data (), root. size () );
587
- return 1 ;
594
+ request. AddCompletion (prefix + root.str (), " " , CompletionMode::Partial );
595
+ return ;
588
596
}
597
+
589
598
// Otherwise, advance through the completion state machine.
590
599
const swift::CompletionState completion_state = completions.getState ();
591
600
switch (completion_state) {
592
601
case swift::CompletionState::CompletedRoot: {
593
- // We completed the root. Next step is to display the completion list.
594
- matches.AppendString (" " ); // Empty string to indicate no completion,
595
- // just display other strings that come after it
602
+ // Display the completion list.
596
603
llvm::ArrayRef<llvm::StringRef> llvm_matches =
597
604
completions.getCompletionList ();
598
605
for (const auto &llvm_match : llvm_matches) {
606
+ // The completions here aren't really useful for actually completing
607
+ // the token but are more descriptive hints for the user
608
+ // (e.g. "isMultiple(of: Int) -> Bool"). They aren't useful for
609
+ // actually completing anything so let's use the current token as
610
+ // a placeholder that is always valid.
599
611
if (!llvm_match.empty ())
600
- matches. AppendString (llvm_match. data () , llvm_match. size () );
612
+ request. AddCompletion (prefix , llvm_match);
601
613
}
602
- // Don't include the empty string we appended above or we will display
603
- // one
604
- // too many we need to return the magical value of one less than our
605
- // actual matches.
606
- // TODO: modify all IOHandlerDelegate::IOHandlerComplete() to use a
607
- // CompletionMatches
608
- // class that wraps up the "StringList matches;" along with other smarts
609
- // so we don't
610
- // have to return magic values and incorrect sizes.
611
- return matches.GetSize () - 1 ;
612
614
} break ;
613
615
614
616
case swift::CompletionState::DisplayedCompletionList: {
615
617
// Complete the next completion stem in the cycle.
616
- llvm::StringRef stem = completions.getPreviousStem ().InsertableString ;
617
- matches.AppendString (stem.data (), stem.size ());
618
+ request.AddCompletion (prefix + completions.getPreviousStem ().InsertableString .str ());
618
619
} break ;
619
620
620
621
case swift::CompletionState::Empty:
621
- case swift::CompletionState::Unique:
622
- // We already provided a definitive completion--nothing else to do.
623
- break ;
622
+ case swift::CompletionState::Unique: {
623
+ llvm::StringRef root = completions.getRoot ();
624
+
625
+ if (!root.empty ())
626
+ request.AddCompletion (prefix + root.str ());
627
+ } break ;
624
628
625
629
case swift::CompletionState::Invalid:
626
630
llvm_unreachable (" got an invalid completion set?!" );
627
631
}
628
632
}
629
633
}
630
-
631
- return matches.GetSize ();
632
634
}
0 commit comments