clang-tools 17.0.0git
SemanticHighlighting.h
Go to the documentation of this file.
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/ADT/StringRef.h"
25#include "llvm/Support/raw_ostream.h"
26
27namespace clang {
28namespace clangd {
29class ParsedAST;
30
31enum class HighlightingKind {
32 Variable = 0,
36 Method,
38 Field,
40 Class,
42 Enum,
44 Typedef,
45 Type,
46 Unknown,
49 Concept,
51 Macro,
54 Bracket,
55
56 // This one is different from the other kinds as it's a line style
57 // rather than a token style.
59
61};
62
63llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K);
64std::optional<HighlightingKind>
65highlightingKindFromString(llvm::StringRef Name);
66
71 Deduced,
73 Static,
75 Virtual,
82
87
89};
90static_assert(static_cast<unsigned>(HighlightingModifier::LastModifier) < 32,
91 "Increase width of modifiers bitfield!");
92llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K);
93std::optional<HighlightingModifier>
95
96// Contains all information needed for the highlighting a token.
99 uint32_t Modifiers = 0;
101
103 Modifiers |= 1 << static_cast<unsigned>(M);
104 return *this;
105 }
106};
107
108bool operator==(const HighlightingToken &L, const HighlightingToken &R);
109bool operator<(const HighlightingToken &L, const HighlightingToken &R);
110
111// Returns all HighlightingTokens from an AST. Only generates highlights for the
112// main AST.
113std::vector<HighlightingToken>
114getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens);
115
116std::vector<SemanticToken> toSemanticTokens(llvm::ArrayRef<HighlightingToken>,
117 llvm::StringRef Code);
120std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Before,
121 llvm::ArrayRef<SemanticToken> After);
122
123} // namespace clangd
124} // namespace clang
125
126#endif
BindArgumentKind Kind
std::string Code
Token Name
Kind K
Definition: Rename.cpp:454
const google::protobuf::Message & M
Definition: Server.cpp:309
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
std::optional< HighlightingModifier > highlightingModifierFromString(llvm::StringRef Name)
llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier)
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens)
std::optional< HighlightingKind > highlightingKindFromString(llvm::StringRef Name)
llvm::StringRef toSemanticTokenType(HighlightingKind Kind)
std::vector< SemanticToken > toSemanticTokens(llvm::ArrayRef< HighlightingToken > Tokens, llvm::StringRef Code)
std::vector< SemanticTokensEdit > diffTokens(llvm::ArrayRef< SemanticToken > Old, llvm::ArrayRef< SemanticToken > New)
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:316
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:95
@ Parameter
An inlay hint that is for a parameter.
@ Type
An inlay hint that for a type annotation.
@ Deprecated
Deprecated or obsolete code.
Definition: Protocol.h:901
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
HighlightingToken & addModifier(HighlightingModifier M)