clang-tools 10.0.0

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 // An implementation of semantic highlighting based on this proposal:
10 // https://github.com/microsoft/vscode-languageserver-node/pull/367 in clangd.
11 // Semantic highlightings are calculated for an AST by visiting every AST node
12 // and classifying nodes that are interesting to highlight (variables/function
13 // calls etc.).
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
18 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
19
20 #include "Protocol.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 namespace clang {
24 namespace clangd {
25 class ParsedAST;
26
27 enum class HighlightingKind {
28 Variable = 0,
29 LocalVariable,
30 Parameter,
31 Function,
32 Method,
33 StaticMethod,
34 Field,
35 StaticField,
36 Class,
37 Enum,
38 EnumConstant,
39 Typedef,
40 DependentType,
41 DependentName,
42 Namespace,
43 TemplateParameter,
44 Primitive,
45 Macro,
46
47 // This one is different from the other kinds as it's a line style
48 // rather than a token style.
49 InactiveCode,
50
51 LastKind = InactiveCode
52 };
53 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K);
54
55 // Contains all information needed for the highlighting a token.
56 struct HighlightingToken {
57 HighlightingKind Kind;
58 Range R;
59 };
60
61 bool operator==(const HighlightingToken &L, const HighlightingToken &R);
62 bool operator<(const HighlightingToken &L, const HighlightingToken &R);
63
64 /// Contains all information about highlightings on a single line.
65 struct LineHighlightings {
66 int Line;
67 std::vector<HighlightingToken> Tokens;
68 bool IsInactive;
69 };
70
71 bool operator==(const LineHighlightings &L, const LineHighlightings &R);
72
73 // Returns all HighlightingTokens from an AST. Only generates highlights for the
74 // main AST.
75 std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST);
76
77 /// Converts a HighlightingKind to a corresponding TextMate scope
78 /// (https://manual.macromates.com/en/language_grammars).
79 llvm::StringRef toTextMateScope(HighlightingKind Kind);
80
81 /// Convert to LSP's semantic highlighting information.
82 std::vector<SemanticHighlightingInformation>
83 toSemanticHighlightingInformation(llvm::ArrayRef<LineHighlightings> Tokens);
84
85 /// Return a line-by-line diff between two highlightings.
86 /// - if the tokens on a line are the same in both highlightings, this line is
87 /// omitted.
88 /// - if a line exists in New but not in Old, the tokens on this line are
89 /// emitted.
90 /// - if a line does not exist in New but exists in Old, an empty line is
91 /// emitted (to tell client to clear the previous highlightings on this line).
92 ///
93 /// REQUIRED: Old and New are sorted.
94 std::vector<LineHighlightings>
95 diffHighlightings(ArrayRef<HighlightingToken> New,
96 ArrayRef<HighlightingToken> Old);
97
98 } // namespace clangd
99 } // namespace clang
100
101 #endif
clang::clangd::HighlightingKind::StaticMethod
clang::clangd::HighlightingToken::R
Range R
Definition: SemanticHighlighting.h:58
clang::clangd::HighlightingToken
Definition: SemanticHighlighting.h:56
clang::clangd::HighlightingKind::DependentName
clang::clangd::LineHighlightings::Tokens
std::vector< HighlightingToken > Tokens
Definition: SemanticHighlighting.h:67
clang::clangd::HighlightingKind::Macro
clang::clangd::LineHighlightings::Line
int Line
Definition: SemanticHighlighting.h:66
clang::clangd::operator==
bool operator==(const HoverInfo::Param &LHS, const HoverInfo::Param &RHS)
Definition: Hover.h:78
clang::clangd::HighlightingKind::LastKind
clang::clangd::HighlightingKind::Method
clang::clangd::toTextMateScope
llvm::StringRef toTextMateScope(HighlightingKind Kind)
Converts a HighlightingKind to a corresponding TextMate scope (https://manual.macromates.com/en/language_grammars).
Definition: SemanticHighlighting.cpp:502
clang::clangd::HighlightingKind::Function
Protocol.h
clang::clangd::HighlightingKind::Namespace
clang::clangd::HighlightingKind::Typedef
clang::clangd::LineHighlightings::IsInactive
bool IsInactive
Definition: SemanticHighlighting.h:68
clang::clangd::HighlightingKind::LocalVariable
clang::clangd::HighlightingKind::TemplateParameter
clang::clangd::HighlightingKind::Primitive
clang::clangd::HighlightingKind::StaticField
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:57
clang::clangd::HighlightingKind::Parameter
clang::clangd::Range
Definition: Protocol.h:156
clang::clangd::SymbolOrigin::AST
clang::clangd::HighlightingKind
HighlightingKind
Definition: SemanticHighlighting.h:27
clang::clangd::toSemanticHighlightingInformation
std::vector< SemanticHighlightingInformation > toSemanticHighlightingInformation(llvm::ArrayRef< LineHighlightings > Tokens)
Convert to LSP&#39;s semantic highlighting information.
Definition: SemanticHighlighting.cpp:474
clang::clangd::operator<
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:58
clang::clangd::HighlightingKind::Class
clang::clangd::CompletionItemKind::Field
clang::clangd::CompletionItemKind::Enum
clang::clangd::HighlightingKind::Variable
clang::clangd::diffHighlightings
std::vector< LineHighlightings > diffHighlightings(ArrayRef< HighlightingToken > New, ArrayRef< HighlightingToken > Old)
Return a line-by-line diff between two highlightings.
Definition: SemanticHighlighting.cpp:406
clang::clangd::HighlightingKind::DependentType
clang::clangd::HighlightingToken::Kind
HighlightingKind Kind
Definition: SemanticHighlighting.h:57
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::LineHighlightings
Contains all information about highlightings on a single line.
Definition: SemanticHighlighting.h:65
clang::clangd::HighlightingKind::InactiveCode
clang::clangd::getSemanticHighlightings
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST)
Definition: SemanticHighlighting.cpp:339
clang::clangd::HighlightingKind::EnumConstant
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1850