clang-tools  10.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 // 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,
30  Parameter,
31  Function,
32  Method,
34  Field,
36  Class,
37  Enum,
39  Typedef,
42  Namespace,
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.
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.
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.
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
std::vector< HighlightingToken > Tokens
bool operator==(const HoverInfo::Param &LHS, const HoverInfo::Param &RHS)
Definition: Hover.h:78
llvm::StringRef toTextMateScope(HighlightingKind Kind)
Converts a HighlightingKind to a corresponding TextMate scope (https://manual.macromates.com/en/language_grammars).
BindArgumentKind Kind
std::vector< SemanticHighlightingInformation > toSemanticHighlightingInformation(llvm::ArrayRef< LineHighlightings > Tokens)
Convert to LSP&#39;s semantic highlighting information.
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:58
std::vector< LineHighlightings > diffHighlightings(ArrayRef< HighlightingToken > New, ArrayRef< HighlightingToken > Old)
Return a line-by-line diff between two highlightings.
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Contains all information about highlightings on a single line.
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST)
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)