clang-tools 22.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
63
64llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K);
65std::optional<HighlightingKind>
66highlightingKindFromString(llvm::StringRef Name);
67
91static_assert(static_cast<unsigned>(HighlightingModifier::LastModifier) < 32,
92 "Increase width of modifiers bitfield!");
93llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K);
94std::optional<HighlightingModifier>
95highlightingModifierFromString(llvm::StringRef Name);
96
97// Contains all information needed for the highlighting a token.
100 uint32_t Modifiers = 0;
102
104 Modifiers |= 1 << static_cast<unsigned>(M);
105 return *this;
106 }
107};
108
109bool operator==(const HighlightingToken &L, const HighlightingToken &R);
110bool operator<(const HighlightingToken &L, const HighlightingToken &R);
111
112// Returns all HighlightingTokens from an AST. Only generates highlights for the
113// main AST.
114std::vector<HighlightingToken>
115getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens);
116
117std::vector<SemanticToken> toSemanticTokens(llvm::ArrayRef<HighlightingToken>,
118 llvm::StringRef Code);
119llvm::StringRef toSemanticTokenType(HighlightingKind Kind);
121std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Before,
122 llvm::ArrayRef<SemanticToken> After);
123
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
126// are not included.
127std::vector<Range> getInactiveRegions(ParsedAST &AST);
128
129} // namespace clangd
130} // namespace clang
131
132#endif
Stores and provides access to parsed AST.
Definition ParsedAST.h:46
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
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:356
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
bool operator<(const Ref &L, const Ref &R)
Definition Ref.h:98
@ Parameter
An inlay hint that is for a parameter.
Definition Protocol.h:1679
@ Type
An inlay hint that for a type annotation.
Definition Protocol.h:1672
@ Deprecated
Deprecated or obsolete code.
Definition Protocol.h:919
std::vector< Range > getInactiveRegions(ParsedAST &AST)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
HighlightingToken & addModifier(HighlightingModifier M)