@@ -643,36 +643,6 @@ struct EditorConsumerSyntaxMapEntry {
643643 :Offset(Offset), Length(Length), Kind(Kind) { }
644644};
645645
646- struct SwiftSemanticToken {
647- unsigned ByteOffset;
648- unsigned Length : 24 ;
649- // The code-completion kinds are a good match for the semantic kinds we want.
650- // FIXME: Maybe rename CodeCompletionDeclKind to a more general concept ?
651- CodeCompletionDeclKind Kind : 6 ;
652- unsigned IsRef : 1 ;
653- unsigned IsSystem : 1 ;
654-
655- SwiftSemanticToken (CodeCompletionDeclKind Kind,
656- unsigned ByteOffset, unsigned Length,
657- bool IsRef, bool IsSystem)
658- : ByteOffset(ByteOffset), Length(Length), Kind(Kind),
659- IsRef (IsRef), IsSystem(IsSystem) { }
660-
661- bool getIsRef () const { return static_cast <bool >(IsRef); }
662-
663- bool getIsSystem () const { return static_cast <bool >(IsSystem); }
664-
665- UIdent getUIdentForKind () const {
666- return SwiftLangSupport::getUIDForCodeCompletionDeclKind (Kind, getIsRef ());
667- }
668- };
669- #if !defined(_MSC_VER)
670- static_assert (sizeof (SwiftSemanticToken) == 8 , " Too big" );
671- // FIXME: MSVC doesn't pack bitfields with different underlying types.
672- // Giving up to check this in MSVC for now, because static_assert is only for
673- // keeping low memory usage.
674- #endif
675-
676646class SwiftDocumentSemanticInfo :
677647 public ThreadSafeRefCountedBase<SwiftDocumentSemanticInfo> {
678648
@@ -2535,3 +2505,68 @@ void SwiftLangSupport::editorExpandPlaceholder(StringRef Name, unsigned Offset,
25352505 }
25362506 EditorDoc->expandPlaceholder (Offset, Length, Consumer);
25372507}
2508+
2509+ // ===----------------------------------------------------------------------===//
2510+ // Semantic Tokens
2511+ // ===----------------------------------------------------------------------===//
2512+
2513+ void SwiftLangSupport::getSemanticTokens (
2514+ StringRef PrimaryFilePath, StringRef InputBufferName,
2515+ ArrayRef<const char *> Args, llvm::Optional<VFSOptions> VfsOptions,
2516+ SourceKitCancellationToken CancellationToken,
2517+ std::function<void (const RequestResult<SemanticTokensResult> &)> Receiver) {
2518+ std::string FileSystemError;
2519+ auto FileSystem = getFileSystem (VfsOptions, PrimaryFilePath, FileSystemError);
2520+ if (!FileSystem) {
2521+ Receiver (RequestResult<SemanticTokensResult>::fromError (FileSystemError));
2522+ return ;
2523+ }
2524+
2525+ std::string InvocationError;
2526+ SwiftInvocationRef Invok = ASTMgr->getTypecheckInvocation (
2527+ Args, PrimaryFilePath, FileSystem, InvocationError);
2528+ if (!InvocationError.empty ()) {
2529+ LOG_WARN_FUNC (" error creating ASTInvocation: " << InvocationError);
2530+ }
2531+ if (!Invok) {
2532+ Receiver (RequestResult<SemanticTokensResult>::fromError (InvocationError));
2533+ return ;
2534+ }
2535+
2536+ class SemanticTokensConsumer : public SwiftASTConsumer {
2537+ StringRef InputBufferName;
2538+ std::function<void (const RequestResult<SemanticTokensResult> &)> Receiver;
2539+
2540+ public:
2541+ SemanticTokensConsumer (
2542+ StringRef InputBufferName,
2543+ std::function<void (const RequestResult<SemanticTokensResult> &)>
2544+ Receiver)
2545+ : InputBufferName(InputBufferName), Receiver(Receiver) {}
2546+
2547+ void handlePrimaryAST (ASTUnitRef AstUnit) override {
2548+ auto &CompIns = AstUnit->getCompilerInstance ();
2549+ SourceFile *SF = retrieveInputFile (InputBufferName, CompIns);
2550+ if (!SF) {
2551+ Receiver (RequestResult<SemanticTokensResult>::fromError (
2552+ " Unable to find input file" ));
2553+ return ;
2554+ }
2555+ SemanticAnnotator Annotator (CompIns.getSourceMgr (), *SF->getBufferID ());
2556+ Annotator.walk (SF);
2557+ Receiver (
2558+ RequestResult<SemanticTokensResult>::fromResult (Annotator.SemaToks ));
2559+ }
2560+
2561+ void cancelled () override {
2562+ Receiver (RequestResult<SemanticTokensResult>::cancelled ());
2563+ }
2564+ };
2565+
2566+ auto Consumer = std::make_shared<SemanticTokensConsumer>(InputBufferName,
2567+ std::move (Receiver));
2568+
2569+ getASTManager ()->processASTAsync (Invok, std::move (Consumer),
2570+ /* OncePerASTToken=*/ nullptr ,
2571+ CancellationToken, FileSystem);
2572+ }
0 commit comments