1 //==-- SemanticHighlighting.h - Generating highlights from the AST-- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
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.
14 // Semantic highlightings are calculated for an AST by visiting every AST node
15 // and classifying nodes that are interesting to highlight (variables/function
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/raw_ostream.h"
31 enum class HighlightingKind
{
57 // This one is different from the other kinds as it's a line style
58 // rather than a token style.
61 LastKind
= InactiveCode
64 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, HighlightingKind K
);
65 std::optional
<HighlightingKind
>
66 highlightingKindFromString(llvm::StringRef Name
);
68 enum class HighlightingModifier
{
79 UsedAsMutableReference
,
81 ConstructorOrDestructor
,
89 LastModifier
= GlobalScope
91 static_assert(static_cast<unsigned>(HighlightingModifier::LastModifier
) < 32,
92 "Increase width of modifiers bitfield!");
93 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, HighlightingModifier K
);
94 std::optional
<HighlightingModifier
>
95 highlightingModifierFromString(llvm::StringRef Name
);
97 // Contains all information needed for the highlighting a token.
98 struct HighlightingToken
{
99 HighlightingKind Kind
;
100 uint32_t Modifiers
= 0;
103 HighlightingToken
&addModifier(HighlightingModifier M
) {
104 Modifiers
|= 1 << static_cast<unsigned>(M
);
109 bool operator==(const HighlightingToken
&L
, const HighlightingToken
&R
);
110 bool operator<(const HighlightingToken
&L
, const HighlightingToken
&R
);
112 // Returns all HighlightingTokens from an AST. Only generates highlights for the
114 std::vector
<HighlightingToken
>
115 getSemanticHighlightings(ParsedAST
&AST
, bool IncludeInactiveRegionTokens
);
117 std::vector
<SemanticToken
> toSemanticTokens(llvm::ArrayRef
<HighlightingToken
>,
118 llvm::StringRef Code
);
119 llvm::StringRef
toSemanticTokenType(HighlightingKind Kind
);
120 llvm::StringRef
toSemanticTokenModifier(HighlightingModifier Modifier
);
121 std::vector
<SemanticTokensEdit
> diffTokens(llvm::ArrayRef
<SemanticToken
> Before
,
122 llvm::ArrayRef
<SemanticToken
> After
);
124 // Returns ranges of the file that are inside an inactive preprocessor branch.
125 // The preprocessor directives at the beginning and end of a branch themselves
127 std::vector
<Range
> getInactiveRegions(ParsedAST
&AST
);
129 } // namespace clangd