@@ -533,8 +533,8 @@ bool SwiftREPL::PrintOneVariable(Debugger &debugger, StreamFileSP &output_sp,
533533  return  handled;
534534}
535535
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 ) {
538538  // ----------------------------------------------------------------------g
539539  //  If we use the target's SwiftASTContext for completion, it reaaallly
540540  //  slows down subsequent expressions. The compiler team doesn't have time
@@ -546,7 +546,7 @@ int SwiftREPL::CompleteCode(const std::string ¤t_code,
546546    auto  type_system_or_err = m_target.GetScratchTypeSystemForLanguage (eLanguageTypeSwift);
547547    if  (!type_system_or_err) {
548548      llvm::consumeError (type_system_or_err.takeError ());
549-       return   0 ;
549+       return ;
550550    }
551551
552552    auto  *target_swift_ast =
@@ -579,54 +579,56 @@ int SwiftREPL::CompleteCode(const std::string ¤t_code,
579579      swift::SourceFile &repl_source_file =
580580          repl_module->getMainSourceFile (swift::SourceFileKind::REPL);
581581
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 ();
582587      llvm::StringRef current_code_ref (current_code);
583588      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.
584592      llvm::StringRef root = completions.getRoot ();
585593      if  (!root.empty ()) {
586-         matches. AppendString ( root.data (), root. size () );
587-         return   1 ;
594+         request. AddCompletion (prefix +  root.str (), " " , CompletionMode::Partial );
595+         return ;
588596      }
597+ 
589598      //  Otherwise, advance through the completion state machine.
590599      const  swift::CompletionState completion_state = completions.getState ();
591600      switch  (completion_state) {
592601      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.
596603        llvm::ArrayRef<llvm::StringRef> llvm_matches =
597604            completions.getCompletionList ();
598605        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.
599611          if  (!llvm_match.empty ())
600-             matches. AppendString (llvm_match. data () , llvm_match. size () );
612+             request. AddCompletion (prefix , llvm_match);
601613        }
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 ;
612614      } break ;
613615
614616      case  swift::CompletionState::DisplayedCompletionList: {
615617        //  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 ());
618619      } break ;
619620
620621      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 ;
624628
625629      case  swift::CompletionState::Invalid:
626630        llvm_unreachable (" got an invalid completion set?!"  );
627631      }
628632    }
629633  }
630- 
631-   return  matches.GetSize ();
632634}
0 commit comments