1 //==-- SemanticHighlighting.h - Generating highlights from the AST-- 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 // This file supports semantic highlighting: categorizing tokens in the file so 10 // that the editor can color/style them differently. 11 // This is particularly valuable for C++: its complex and context-dependent 12 // grammar is a challenge for simple syntax-highlighting techniques. 13 // 14 // Semantic highlightings are calculated for an AST by visiting every AST node 15 // and classifying nodes that are interesting to highlight (variables/function 16 // calls etc.). 17 // 18 //===----------------------------------------------------------------------===// 19 20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H 21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H 22 23 #include "Protocol.h" 24 #include "llvm/Support/raw_ostream.h" 25 26 namespace clang { 27 namespace clangd { 28 class ParsedAST; 29 30 enum class HighlightingKind { 31 Variable = 0, 32 LocalVariable, 33 Parameter, 34 Function, 35 Method, 36 StaticMethod, 37 Field, 38 StaticField, 39 Class, 40 Interface, 41 Enum, 42 EnumConstant, 43 Typedef, 44 Type, 45 Unknown, 46 Namespace, 47 TemplateParameter, 48 Concept, 49 Primitive, 50 Macro, 51 52 // This one is different from the other kinds as it's a line style 53 // rather than a token style. 54 InactiveCode, 55 56 LastKind = InactiveCode 57 }; 58 59 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K); 60 61 enum class HighlightingModifier { 62 Declaration, 63 // FIXME: Definition (needs findExplicitReferences support) 64 Deprecated, 65 Deduced, 66 Readonly, 67 Static, 68 Abstract, 69 DependentName, 70 DefaultLibrary, 71 72 FunctionScope, 73 ClassScope, 74 FileScope, 75 GlobalScope, 76 77 LastModifier = GlobalScope 78 }; 79 static_assert(static_cast<unsigned>(HighlightingModifier::LastModifier) < 32, 80 "Increase width of modifiers bitfield!"); 81 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K); 82 83 // Contains all information needed for the highlighting a token. 84 struct HighlightingToken { 85 HighlightingKind Kind; 86 uint32_t Modifiers = 0; 87 Range R; 88 addModifierHighlightingToken89 HighlightingToken &addModifier(HighlightingModifier M) { 90 Modifiers |= 1 << static_cast<unsigned>(M); 91 return *this; 92 } 93 }; 94 95 bool operator==(const HighlightingToken &L, const HighlightingToken &R); 96 bool operator<(const HighlightingToken &L, const HighlightingToken &R); 97 98 // Returns all HighlightingTokens from an AST. Only generates highlights for the 99 // main AST. 100 std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST); 101 102 std::vector<SemanticToken> toSemanticTokens(llvm::ArrayRef<HighlightingToken>); 103 llvm::StringRef toSemanticTokenType(HighlightingKind Kind); 104 llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier); 105 std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Before, 106 llvm::ArrayRef<SemanticToken> After); 107 108 } // namespace clangd 109 } // namespace clang 110 111 #endif 112