1 //===--- SemanticHighlighting.cpp - ------------------------- ---*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "SemanticHighlighting.h" 10 #include "FindTarget.h" 11 #include "ParsedAST.h" 12 #include "Protocol.h" 13 #include "SourceCode.h" 14 #include "support/Logger.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/DeclarationName.h" 19 #include "clang/AST/ExprCXX.h" 20 #include "clang/AST/RecursiveASTVisitor.h" 21 #include "clang/AST/Type.h" 22 #include "clang/AST/TypeLoc.h" 23 #include "clang/Basic/LangOptions.h" 24 #include "clang/Basic/SourceLocation.h" 25 #include "clang/Basic/SourceManager.h" 26 #include "clang/Tooling/Syntax/Tokens.h" 27 #include "llvm/ADT/None.h" 28 #include "llvm/ADT/Optional.h" 29 #include "llvm/ADT/STLExtras.h" 30 #include "llvm/Support/Base64.h" 31 #include "llvm/Support/Casting.h" 32 #include <algorithm> 33 34 namespace clang { 35 namespace clangd { 36 namespace { 37 38 /// Some names are not written in the source code and cannot be highlighted, 39 /// e.g. anonymous classes. This function detects those cases. 40 bool canHighlightName(DeclarationName Name) { 41 if (Name.getNameKind() == DeclarationName::CXXConstructorName || 42 Name.getNameKind() == DeclarationName::CXXUsingDirective) 43 return true; 44 auto *II = Name.getAsIdentifierInfo(); 45 return II && !II->getName().empty(); 46 } 47 48 llvm::Optional<HighlightingKind> kindForType(const Type *TP); 49 llvm::Optional<HighlightingKind> kindForDecl(const NamedDecl *D) { 50 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) { 51 if (auto *Target = USD->getTargetDecl()) 52 D = Target; 53 } 54 if (auto *TD = dyn_cast<TemplateDecl>(D)) { 55 if (auto *Templated = TD->getTemplatedDecl()) 56 D = Templated; 57 } 58 if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { 59 // We try to highlight typedefs as their underlying type. 60 if (auto K = kindForType(TD->getUnderlyingType().getTypePtrOrNull())) 61 return K; 62 // And fallback to a generic kind if this fails. 63 return HighlightingKind::Typedef; 64 } 65 // We highlight class decls, constructor decls and destructor decls as 66 // `Class` type. The destructor decls are handled in `VisitTagTypeLoc` (we 67 // will visit a TypeLoc where the underlying Type is a CXXRecordDecl). 68 if (auto *RD = llvm::dyn_cast<RecordDecl>(D)) { 69 // We don't want to highlight lambdas like classes. 70 if (RD->isLambda()) 71 return llvm::None; 72 return HighlightingKind::Class; 73 } 74 if (isa<ClassTemplateDecl>(D) || isa<RecordDecl>(D) || 75 isa<CXXConstructorDecl>(D)) 76 return HighlightingKind::Class; 77 if (auto *MD = dyn_cast<CXXMethodDecl>(D)) 78 return MD->isStatic() ? HighlightingKind::StaticMethod 79 : HighlightingKind::Method; 80 if (isa<FieldDecl>(D)) 81 return HighlightingKind::Field; 82 if (isa<EnumDecl>(D)) 83 return HighlightingKind::Enum; 84 if (isa<EnumConstantDecl>(D)) 85 return HighlightingKind::EnumConstant; 86 if (isa<ParmVarDecl>(D)) 87 return HighlightingKind::Parameter; 88 if (auto *VD = dyn_cast<VarDecl>(D)) 89 return VD->isStaticDataMember() 90 ? HighlightingKind::StaticField 91 : VD->isLocalVarDecl() ? HighlightingKind::LocalVariable 92 : HighlightingKind::Variable; 93 if (isa<BindingDecl>(D)) 94 return HighlightingKind::Variable; 95 if (isa<FunctionDecl>(D)) 96 return HighlightingKind::Function; 97 if (isa<NamespaceDecl>(D) || isa<NamespaceAliasDecl>(D) || 98 isa<UsingDirectiveDecl>(D)) 99 return HighlightingKind::Namespace; 100 if (isa<TemplateTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || 101 isa<NonTypeTemplateParmDecl>(D)) 102 return HighlightingKind::TemplateParameter; 103 if (isa<ConceptDecl>(D)) 104 return HighlightingKind::Concept; 105 return llvm::None; 106 } 107 llvm::Optional<HighlightingKind> kindForType(const Type *TP) { 108 if (!TP) 109 return llvm::None; 110 if (TP->isBuiltinType()) // Builtins are special, they do not have decls. 111 return HighlightingKind::Primitive; 112 if (auto *TD = dyn_cast<TemplateTypeParmType>(TP)) 113 return kindForDecl(TD->getDecl()); 114 if (auto *TD = TP->getAsTagDecl()) 115 return kindForDecl(TD); 116 return llvm::None; 117 } 118 119 llvm::Optional<HighlightingKind> kindForReference(const ReferenceLoc &R) { 120 llvm::Optional<HighlightingKind> Result; 121 for (const NamedDecl *Decl : R.Targets) { 122 if (!canHighlightName(Decl->getDeclName())) 123 return llvm::None; 124 auto Kind = kindForDecl(Decl); 125 if (!Kind || (Result && Kind != Result)) 126 return llvm::None; 127 Result = Kind; 128 } 129 return Result; 130 } 131 132 // For a macro usage `DUMP(foo)`, we want: 133 // - DUMP --> "macro" 134 // - foo --> "variable". 135 SourceLocation getHighlightableSpellingToken(SourceLocation L, 136 const SourceManager &SM) { 137 if (L.isFileID()) 138 return SM.isWrittenInMainFile(L) ? L : SourceLocation{}; 139 // Tokens expanded from the macro body contribute no highlightings. 140 if (!SM.isMacroArgExpansion(L)) 141 return {}; 142 // Tokens expanded from macro args are potentially highlightable. 143 return getHighlightableSpellingToken(SM.getImmediateSpellingLoc(L), SM); 144 } 145 146 unsigned evaluateHighlightPriority(HighlightingKind Kind) { 147 enum HighlightPriority { Dependent = 0, Resolved = 1 }; 148 return Kind == HighlightingKind::DependentType || 149 Kind == HighlightingKind::DependentName 150 ? Dependent 151 : Resolved; 152 } 153 154 // Sometimes we get conflicts between findExplicitReferences() returning 155 // a heuristic result for a dependent name (e.g. Method) and 156 // CollectExtraHighlighting returning a fallback dependent highlighting (e.g. 157 // DependentName). In such cases, resolve the conflict in favour of the 158 // resolved (non-dependent) highlighting. 159 // With macros we can get other conflicts (if a spelled token has multiple 160 // expansions with different token types) which we can't usefully resolve. 161 llvm::Optional<HighlightingToken> 162 resolveConflict(ArrayRef<HighlightingToken> Tokens) { 163 if (Tokens.size() == 1) 164 return Tokens[0]; 165 166 if (Tokens.size() != 2) 167 return llvm::None; 168 169 unsigned Priority1 = evaluateHighlightPriority(Tokens[0].Kind); 170 unsigned Priority2 = evaluateHighlightPriority(Tokens[1].Kind); 171 if (Priority1 == Priority2) 172 return llvm::None; 173 return Priority1 > Priority2 ? Tokens[0] : Tokens[1]; 174 } 175 176 /// Consumes source locations and maps them to text ranges for highlightings. 177 class HighlightingsBuilder { 178 public: 179 HighlightingsBuilder(const ParsedAST &AST) 180 : TB(AST.getTokens()), SourceMgr(AST.getSourceManager()), 181 LangOpts(AST.getLangOpts()) {} 182 183 void addToken(HighlightingToken T) { Tokens.push_back(T); } 184 185 void addToken(SourceLocation Loc, HighlightingKind Kind) { 186 Loc = getHighlightableSpellingToken(Loc, SourceMgr); 187 if (Loc.isInvalid()) 188 return; 189 const auto *Tok = TB.spelledTokenAt(Loc); 190 assert(Tok); 191 192 auto Range = halfOpenToRange(SourceMgr, 193 Tok->range(SourceMgr).toCharRange(SourceMgr)); 194 Tokens.push_back(HighlightingToken{Kind, std::move(Range)}); 195 } 196 197 std::vector<HighlightingToken> collect(ParsedAST &AST) && { 198 // Initializer lists can give duplicates of tokens, therefore all tokens 199 // must be deduplicated. 200 llvm::sort(Tokens); 201 auto Last = std::unique(Tokens.begin(), Tokens.end()); 202 Tokens.erase(Last, Tokens.end()); 203 204 // Macros can give tokens that have the same source range but conflicting 205 // kinds. In this case all tokens sharing this source range should be 206 // removed. 207 std::vector<HighlightingToken> NonConflicting; 208 NonConflicting.reserve(Tokens.size()); 209 for (ArrayRef<HighlightingToken> TokRef = Tokens; !TokRef.empty();) { 210 ArrayRef<HighlightingToken> Conflicting = 211 TokRef.take_while([&](const HighlightingToken &T) { 212 // TokRef is guaranteed at least one element here because otherwise 213 // this predicate would never fire. 214 return T.R == TokRef.front().R; 215 }); 216 if (auto Resolved = resolveConflict(Conflicting)) 217 NonConflicting.push_back(*Resolved); 218 // TokRef[Conflicting.size()] is the next token with a different range (or 219 // the end of the Tokens). 220 TokRef = TokRef.drop_front(Conflicting.size()); 221 } 222 // Add tokens indicating lines skipped by the preprocessor. 223 for (const Range &R : AST.getMacros().SkippedRanges) { 224 // Create one token for each line in the skipped range, so it works 225 // with line-based diffing. 226 assert(R.start.line <= R.end.line); 227 for (int Line = R.start.line; Line < R.end.line; ++Line) { 228 // Don't bother computing the offset for the end of the line, just use 229 // zero. The client will treat this highlighting kind specially, and 230 // highlight the entire line visually (i.e. not just to where the text 231 // on the line ends, but to the end of the screen). 232 NonConflicting.push_back({HighlightingKind::InactiveCode, 233 {Position{Line, 0}, Position{Line, 0}}}); 234 } 235 } 236 // Re-sort the tokens because that's what the diffing expects. 237 llvm::sort(NonConflicting); 238 return NonConflicting; 239 } 240 241 private: 242 const syntax::TokenBuffer &TB; 243 const SourceManager &SourceMgr; 244 const LangOptions &LangOpts; 245 std::vector<HighlightingToken> Tokens; 246 }; 247 248 /// Produces highlightings, which are not captured by findExplicitReferences, 249 /// e.g. highlights dependent names and 'auto' as the underlying type. 250 class CollectExtraHighlightings 251 : public RecursiveASTVisitor<CollectExtraHighlightings> { 252 public: 253 CollectExtraHighlightings(HighlightingsBuilder &H) : H(H) {} 254 255 bool VisitDecltypeTypeLoc(DecltypeTypeLoc L) { 256 if (auto K = kindForType(L.getTypePtr())) 257 H.addToken(L.getBeginLoc(), *K); 258 return true; 259 } 260 261 bool VisitDeclaratorDecl(DeclaratorDecl *D) { 262 auto *AT = D->getType()->getContainedAutoType(); 263 if (!AT) 264 return true; 265 if (auto K = kindForType(AT->getDeducedType().getTypePtrOrNull())) 266 H.addToken(D->getTypeSpecStartLoc(), *K); 267 return true; 268 } 269 270 bool VisitOverloadExpr(OverloadExpr *E) { 271 if (!E->decls().empty()) 272 return true; // handled by findExplicitReferences. 273 H.addToken(E->getNameLoc(), HighlightingKind::DependentName); 274 return true; 275 } 276 277 bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { 278 H.addToken(E->getMemberNameInfo().getLoc(), 279 HighlightingKind::DependentName); 280 return true; 281 } 282 283 bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { 284 H.addToken(E->getNameInfo().getLoc(), HighlightingKind::DependentName); 285 return true; 286 } 287 288 bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) { 289 H.addToken(L.getNameLoc(), HighlightingKind::DependentType); 290 return true; 291 } 292 293 bool VisitDependentTemplateSpecializationTypeLoc( 294 DependentTemplateSpecializationTypeLoc L) { 295 H.addToken(L.getTemplateNameLoc(), HighlightingKind::DependentType); 296 return true; 297 } 298 299 // findExplicitReferences will walk nested-name-specifiers and 300 // find anything that can be resolved to a Decl. However, non-leaf 301 // components of nested-name-specifiers which are dependent names 302 // (kind "Identifier") cannot be resolved to a decl, so we visit 303 // them here. 304 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Q) { 305 if (NestedNameSpecifier *NNS = Q.getNestedNameSpecifier()) { 306 if (NNS->getKind() == NestedNameSpecifier::Identifier) 307 H.addToken(Q.getLocalBeginLoc(), HighlightingKind::DependentType); 308 } 309 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(Q); 310 } 311 312 private: 313 HighlightingsBuilder &H; 314 }; 315 316 void write32be(uint32_t I, llvm::raw_ostream &OS) { 317 std::array<char, 4> Buf; 318 llvm::support::endian::write32be(Buf.data(), I); 319 OS.write(Buf.data(), Buf.size()); 320 } 321 322 void write16be(uint16_t I, llvm::raw_ostream &OS) { 323 std::array<char, 2> Buf; 324 llvm::support::endian::write16be(Buf.data(), I); 325 OS.write(Buf.data(), Buf.size()); 326 } 327 328 // Get the highlightings on \c Line where the first entry of line is at \c 329 // StartLineIt. If it is not at \c StartLineIt an empty vector is returned. 330 ArrayRef<HighlightingToken> 331 takeLine(ArrayRef<HighlightingToken> AllTokens, 332 ArrayRef<HighlightingToken>::iterator StartLineIt, int Line) { 333 return ArrayRef<HighlightingToken>(StartLineIt, AllTokens.end()) 334 .take_while([Line](const HighlightingToken &Token) { 335 return Token.R.start.line == Line; 336 }); 337 } 338 } // namespace 339 340 std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST) { 341 auto &C = AST.getASTContext(); 342 // Add highlightings for AST nodes. 343 HighlightingsBuilder Builder(AST); 344 // Highlight 'decltype' and 'auto' as their underlying types. 345 CollectExtraHighlightings(Builder).TraverseAST(C); 346 // Highlight all decls and references coming from the AST. 347 findExplicitReferences(C, [&](ReferenceLoc R) { 348 if (auto Kind = kindForReference(R)) 349 Builder.addToken(R.NameLoc, *Kind); 350 }); 351 // Add highlightings for macro references. 352 for (const auto &SIDToRefs : AST.getMacros().MacroRefs) { 353 for (const auto &M : SIDToRefs.second) 354 Builder.addToken({HighlightingKind::Macro, M}); 355 } 356 for (const auto &M : AST.getMacros().UnknownMacros) 357 Builder.addToken({HighlightingKind::Macro, M}); 358 359 return std::move(Builder).collect(AST); 360 } 361 362 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K) { 363 switch (K) { 364 case HighlightingKind::Variable: 365 return OS << "Variable"; 366 case HighlightingKind::LocalVariable: 367 return OS << "LocalVariable"; 368 case HighlightingKind::Parameter: 369 return OS << "Parameter"; 370 case HighlightingKind::Function: 371 return OS << "Function"; 372 case HighlightingKind::Method: 373 return OS << "Method"; 374 case HighlightingKind::StaticMethod: 375 return OS << "StaticMethod"; 376 case HighlightingKind::Field: 377 return OS << "Field"; 378 case HighlightingKind::StaticField: 379 return OS << "StaticField"; 380 case HighlightingKind::Class: 381 return OS << "Class"; 382 case HighlightingKind::Enum: 383 return OS << "Enum"; 384 case HighlightingKind::EnumConstant: 385 return OS << "EnumConstant"; 386 case HighlightingKind::Typedef: 387 return OS << "Typedef"; 388 case HighlightingKind::DependentType: 389 return OS << "DependentType"; 390 case HighlightingKind::DependentName: 391 return OS << "DependentName"; 392 case HighlightingKind::Namespace: 393 return OS << "Namespace"; 394 case HighlightingKind::TemplateParameter: 395 return OS << "TemplateParameter"; 396 case HighlightingKind::Concept: 397 return OS << "Concept"; 398 case HighlightingKind::Primitive: 399 return OS << "Primitive"; 400 case HighlightingKind::Macro: 401 return OS << "Macro"; 402 case HighlightingKind::InactiveCode: 403 return OS << "InactiveCode"; 404 } 405 llvm_unreachable("invalid HighlightingKind"); 406 } 407 408 std::vector<LineHighlightings> 409 diffHighlightings(ArrayRef<HighlightingToken> New, 410 ArrayRef<HighlightingToken> Old) { 411 assert(std::is_sorted(New.begin(), New.end()) && 412 "New must be a sorted vector"); 413 assert(std::is_sorted(Old.begin(), Old.end()) && 414 "Old must be a sorted vector"); 415 416 // FIXME: There's an edge case when tokens span multiple lines. If the first 417 // token on the line started on a line above the current one and the rest of 418 // the line is the equal to the previous one than we will remove all 419 // highlights but the ones for the token spanning multiple lines. This means 420 // that when we get into the LSP layer the only highlights that will be 421 // visible are the ones for the token spanning multiple lines. 422 // Example: 423 // EndOfMultilineToken Token Token Token 424 // If "Token Token Token" don't differ from previously the line is 425 // incorrectly removed. Suggestion to fix is to separate any multiline tokens 426 // into one token for every line it covers. This requires reading from the 427 // file buffer to figure out the length of each line though. 428 std::vector<LineHighlightings> DiffedLines; 429 // ArrayRefs to the current line in the highlightings. 430 ArrayRef<HighlightingToken> NewLine(New.begin(), 431 /*length*/ static_cast<size_t>(0)); 432 ArrayRef<HighlightingToken> OldLine(Old.begin(), 433 /*length*/ static_cast<size_t>(0)); 434 auto NewEnd = New.end(); 435 auto OldEnd = Old.end(); 436 auto NextLineNumber = [&]() { 437 int NextNew = NewLine.end() != NewEnd ? NewLine.end()->R.start.line 438 : std::numeric_limits<int>::max(); 439 int NextOld = OldLine.end() != OldEnd ? OldLine.end()->R.start.line 440 : std::numeric_limits<int>::max(); 441 return std::min(NextNew, NextOld); 442 }; 443 444 for (int LineNumber = 0; NewLine.end() < NewEnd || OldLine.end() < OldEnd; 445 LineNumber = NextLineNumber()) { 446 NewLine = takeLine(New, NewLine.end(), LineNumber); 447 OldLine = takeLine(Old, OldLine.end(), LineNumber); 448 if (NewLine != OldLine) { 449 DiffedLines.push_back({LineNumber, NewLine, /*IsInactive=*/false}); 450 451 // Turn a HighlightingKind::InactiveCode token into the IsInactive flag. 452 auto &AddedLine = DiffedLines.back(); 453 llvm::erase_if(AddedLine.Tokens, [&](const HighlightingToken &T) { 454 if (T.Kind == HighlightingKind::InactiveCode) { 455 AddedLine.IsInactive = true; 456 return true; 457 } 458 return false; 459 }); 460 } 461 } 462 463 return DiffedLines; 464 } 465 466 bool operator==(const HighlightingToken &L, const HighlightingToken &R) { 467 return std::tie(L.R, L.Kind) == std::tie(R.R, R.Kind); 468 } 469 bool operator<(const HighlightingToken &L, const HighlightingToken &R) { 470 return std::tie(L.R, L.Kind) < std::tie(R.R, R.Kind); 471 } 472 bool operator==(const LineHighlightings &L, const LineHighlightings &R) { 473 return std::tie(L.Line, L.Tokens) == std::tie(R.Line, R.Tokens); 474 } 475 476 std::vector<SemanticToken> 477 toSemanticTokens(llvm::ArrayRef<HighlightingToken> Tokens) { 478 assert(std::is_sorted(Tokens.begin(), Tokens.end())); 479 std::vector<SemanticToken> Result; 480 const HighlightingToken *Last = nullptr; 481 for (const HighlightingToken &Tok : Tokens) { 482 // FIXME: support inactive code - we need to provide the actual bounds. 483 if (Tok.Kind == HighlightingKind::InactiveCode) 484 continue; 485 Result.emplace_back(); 486 SemanticToken &Out = Result.back(); 487 // deltaStart/deltaLine are relative if possible. 488 if (Last) { 489 assert(Tok.R.start.line >= Last->R.start.line); 490 Out.deltaLine = Tok.R.start.line - Last->R.start.line; 491 if (Out.deltaLine == 0) { 492 assert(Tok.R.start.character >= Last->R.start.character); 493 Out.deltaStart = Tok.R.start.character - Last->R.start.character; 494 } else { 495 Out.deltaStart = Tok.R.start.character; 496 } 497 } else { 498 Out.deltaLine = Tok.R.start.line; 499 Out.deltaStart = Tok.R.start.character; 500 } 501 assert(Tok.R.end.line == Tok.R.start.line); 502 Out.length = Tok.R.end.character - Tok.R.start.character; 503 Out.tokenType = static_cast<unsigned>(Tok.Kind); 504 505 Last = &Tok; 506 } 507 return Result; 508 } 509 llvm::StringRef toSemanticTokenType(HighlightingKind Kind) { 510 switch (Kind) { 511 case HighlightingKind::Variable: 512 case HighlightingKind::LocalVariable: 513 case HighlightingKind::StaticField: 514 return "variable"; 515 case HighlightingKind::Parameter: 516 return "parameter"; 517 case HighlightingKind::Function: 518 return "function"; 519 case HighlightingKind::Method: 520 return "member"; 521 case HighlightingKind::StaticMethod: 522 // FIXME: better function/member with static modifier? 523 return "function"; 524 case HighlightingKind::Field: 525 return "member"; 526 case HighlightingKind::Class: 527 return "class"; 528 case HighlightingKind::Enum: 529 return "enum"; 530 case HighlightingKind::EnumConstant: 531 return "enumConstant"; // nonstandard 532 case HighlightingKind::Typedef: 533 return "type"; 534 case HighlightingKind::DependentType: 535 return "dependent"; // nonstandard 536 case HighlightingKind::DependentName: 537 return "dependent"; // nonstandard 538 case HighlightingKind::Namespace: 539 return "namespace"; 540 case HighlightingKind::TemplateParameter: 541 return "typeParameter"; 542 case HighlightingKind::Concept: 543 return "concept"; // nonstandard 544 case HighlightingKind::Primitive: 545 return "type"; 546 case HighlightingKind::Macro: 547 return "macro"; 548 case HighlightingKind::InactiveCode: 549 return "comment"; 550 } 551 llvm_unreachable("unhandled HighlightingKind"); 552 } 553 554 std::vector<TheiaSemanticHighlightingInformation> 555 toTheiaSemanticHighlightingInformation( 556 llvm::ArrayRef<LineHighlightings> Tokens) { 557 if (Tokens.size() == 0) 558 return {}; 559 560 // FIXME: Tokens might be multiple lines long (block comments) in this case 561 // this needs to add multiple lines for those tokens. 562 std::vector<TheiaSemanticHighlightingInformation> Lines; 563 Lines.reserve(Tokens.size()); 564 for (const auto &Line : Tokens) { 565 llvm::SmallVector<char, 128> LineByteTokens; 566 llvm::raw_svector_ostream OS(LineByteTokens); 567 for (const auto &Token : Line.Tokens) { 568 // Writes the token to LineByteTokens in the byte format specified by the 569 // LSP proposal. Described below. 570 // |<---- 4 bytes ---->|<-- 2 bytes -->|<--- 2 bytes -->| 571 // | character | length | index | 572 573 write32be(Token.R.start.character, OS); 574 write16be(Token.R.end.character - Token.R.start.character, OS); 575 write16be(static_cast<int>(Token.Kind), OS); 576 } 577 578 Lines.push_back({Line.Line, encodeBase64(LineByteTokens), Line.IsInactive}); 579 } 580 581 return Lines; 582 } 583 584 llvm::StringRef toTextMateScope(HighlightingKind Kind) { 585 // FIXME: Add scopes for C and Objective C. 586 switch (Kind) { 587 case HighlightingKind::Function: 588 return "entity.name.function.cpp"; 589 case HighlightingKind::Method: 590 return "entity.name.function.method.cpp"; 591 case HighlightingKind::StaticMethod: 592 return "entity.name.function.method.static.cpp"; 593 case HighlightingKind::Variable: 594 return "variable.other.cpp"; 595 case HighlightingKind::LocalVariable: 596 return "variable.other.local.cpp"; 597 case HighlightingKind::Parameter: 598 return "variable.parameter.cpp"; 599 case HighlightingKind::Field: 600 return "variable.other.field.cpp"; 601 case HighlightingKind::StaticField: 602 return "variable.other.field.static.cpp"; 603 case HighlightingKind::Class: 604 return "entity.name.type.class.cpp"; 605 case HighlightingKind::Enum: 606 return "entity.name.type.enum.cpp"; 607 case HighlightingKind::EnumConstant: 608 return "variable.other.enummember.cpp"; 609 case HighlightingKind::Typedef: 610 return "entity.name.type.typedef.cpp"; 611 case HighlightingKind::DependentType: 612 return "entity.name.type.dependent.cpp"; 613 case HighlightingKind::DependentName: 614 return "entity.name.other.dependent.cpp"; 615 case HighlightingKind::Namespace: 616 return "entity.name.namespace.cpp"; 617 case HighlightingKind::TemplateParameter: 618 return "entity.name.type.template.cpp"; 619 case HighlightingKind::Concept: 620 return "entity.name.type.concept.cpp"; 621 case HighlightingKind::Primitive: 622 return "storage.type.primitive.cpp"; 623 case HighlightingKind::Macro: 624 return "entity.name.function.preprocessor.cpp"; 625 case HighlightingKind::InactiveCode: 626 return "meta.disabled"; 627 } 628 llvm_unreachable("unhandled HighlightingKind"); 629 } 630 631 std::vector<SemanticTokensEdit> 632 diffTokens(llvm::ArrayRef<SemanticToken> Old, 633 llvm::ArrayRef<SemanticToken> New) { 634 // For now, just replace everything from the first-last modification. 635 // FIXME: use a real diff instead, this is bad with include-insertion. 636 637 unsigned Offset = 0; 638 while (!Old.empty() && !New.empty() && Old.front() == New.front()) { 639 ++Offset; 640 Old = Old.drop_front(); 641 New = New.drop_front(); 642 } 643 while (!Old.empty() && !New.empty() && Old.back() == New.back()) { 644 Old = Old.drop_back(); 645 New = New.drop_back(); 646 } 647 648 if (Old.empty() && New.empty()) 649 return {}; 650 SemanticTokensEdit Edit; 651 Edit.startToken = Offset; 652 Edit.deleteTokens = Old.size(); 653 Edit.tokens = New; 654 return {std::move(Edit)}; 655 } 656 657 } // namespace clangd 658 } // namespace clang 659