LLDB mainline
Highlighter.h
Go to the documentation of this file.
1//===-- Highlighter.h -------------------------------------------*- 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#ifndef LLDB_CORE_HIGHLIGHTER_H
10#define LLDB_CORE_HIGHLIGHTER_H
11
12#include <optional>
13#include <utility>
14#include <vector>
15
17#include "lldb/Utility/Stream.h"
19#include "llvm/ADT/StringRef.h"
20
21namespace lldb_private {
22
23/// Represents style that the highlighter should apply to the given source code.
24/// Stores information about how every kind of token should be annotated.
26
27 /// A pair of strings that should be placed around a certain token. Usually
28 /// stores color codes in these strings (the suffix string is often used for
29 /// resetting the terminal attributes back to normal).
30 class ColorStyle {
31 std::string m_prefix;
32 std::string m_suffix;
33
34 public:
35 ColorStyle() = default;
36 ColorStyle(llvm::StringRef prefix, llvm::StringRef suffix) {
37 Set(prefix, suffix);
38 }
39
40 /// Applies this style to the given value.
41 /// \param s
42 /// The stream to which the result should be appended.
43 /// \param value
44 /// The value that we should place our strings around.
45 void Apply(Stream &s, llvm::StringRef value) const;
46
47 /// Sets the prefix and suffix strings.
48 void Set(llvm::StringRef prefix, llvm::StringRef suffix);
49
50 explicit operator bool() const {
51 return !m_prefix.empty() && !m_suffix.empty();
52 }
53 };
54
55 /// The style for the token which is below the cursor of the user. Note that
56 /// this style is overwritten by the SourceManager with the values of
57 /// stop-show-column-ansi-prefix/stop-show-column-ansi-suffix.
59
60 /// Matches identifiers to variable or functions.
62 /// Matches any string or character literals in the language: "foo" or 'f'
64 /// Matches scalar value literals like '42' or '0.1'.
66 /// Matches all reserved keywords in the language.
68 /// Matches any comments in the language.
70 /// Matches commas: ','
72 /// Matches one colon: ':'
74 /// Matches any semicolon: ';'
76 /// Matches operators like '+', '-', '%', '&', '='
78
79 /// Matches '{' or '}'
81 /// Matches '[' or ']'
83 /// Matches '(' or ')'
85
86 // C language specific options
87
88 /// Matches directives to a preprocessor (if the language has any).
90
91 /// Returns a HighlightStyle that is based on vim's default highlight style.
93};
94
95/// Annotates source code with color attributes.
97public:
98 Highlighter() = default;
99 virtual ~Highlighter() = default;
100 Highlighter(const Highlighter &) = delete;
101 const Highlighter &operator=(const Highlighter &) = delete;
102
103 /// Returns a human readable name for the selected highlighter.
104 virtual llvm::StringRef GetName() const = 0;
105
106 /// Highlights the given line
107 /// \param options
108 /// The highlight options.
109 /// \param line
110 /// The user supplied line that needs to be highlighted.
111 /// \param cursor_pos
112 /// The cursor position of the user in this line, starting at 0 (which
113 /// means the cursor is on the first character in 'line').
114 /// \param previous_lines
115 /// Any previous lines the user has written which we should only use
116 /// for getting the context of the Highlighting right.
117 /// \param s
118 /// The stream to which the highlighted version of the user string should
119 /// be written.
120 virtual void Highlight(const HighlightStyle &options, llvm::StringRef line,
121 std::optional<size_t> cursor_pos,
122 llvm::StringRef previous_lines, Stream &s) const = 0;
123
124 /// Utility method for calling Highlight without a stream.
125 std::string Highlight(const HighlightStyle &options, llvm::StringRef line,
126 std::optional<size_t> cursor_pos,
127 llvm::StringRef previous_lines = "") const;
128};
129
130/// Manages the available highlighters.
132public:
133 /// Queries all known highlighter for one that can highlight some source code.
134 ///
135 /// \param language_type
136 /// The language type that the caller thinks the source code was given in.
137 /// \param path
138 /// The path to the file the source code is from. Used as a fallback when
139 /// the user can't provide a language.
140 /// \return
141 /// The highlighter that wants to highlight the source code. Could be an
142 /// empty highlighter that does nothing.
144 llvm::StringRef path) const;
145
146private:
147 mutable std::mutex m_mutex;
148 mutable llvm::DenseMap<lldb::LanguageType, std::unique_ptr<Highlighter>>
150};
151
152} // namespace lldb_private
153
154namespace llvm {
155
156/// DenseMapInfo implementation.
157/// \{
158template <> struct DenseMapInfo<lldb::LanguageType> {
161 }
164 }
165 static unsigned getHashValue(lldb::LanguageType language_type) {
166 return static_cast<unsigned>(language_type);
167 }
169 return LHS == RHS;
170 }
171};
172/// \}
173
174} // namespace llvm
175
176#endif // LLDB_CORE_HIGHLIGHTER_H
A pair of strings that should be placed around a certain token.
Definition Highlighter.h:30
ColorStyle(llvm::StringRef prefix, llvm::StringRef suffix)
Definition Highlighter.h:36
void Set(llvm::StringRef prefix, llvm::StringRef suffix)
Sets the prefix and suffix strings.
void Apply(Stream &s, llvm::StringRef value) const
Applies this style to the given value.
Manages the available highlighters.
const Highlighter & getHighlighterFor(lldb::LanguageType language_type, llvm::StringRef path) const
Queries all known highlighter for one that can highlight some source code.
llvm::DenseMap< lldb::LanguageType, std::unique_ptr< Highlighter > > m_highlighters
Annotates source code with color attributes.
Definition Highlighter.h:96
Highlighter(const Highlighter &)=delete
virtual void Highlight(const HighlightStyle &options, llvm::StringRef line, std::optional< size_t > cursor_pos, llvm::StringRef previous_lines, Stream &s) const =0
Highlights the given line.
const Highlighter & operator=(const Highlighter &)=delete
virtual llvm::StringRef GetName() const =0
Returns a human readable name for the selected highlighter.
virtual ~Highlighter()=default
A stream class that can stream formatted output to a file.
Definition Stream.h:28
A class that represents a running process on the host machine.
LanguageType
Programming language type.
Represents style that the highlighter should apply to the given source code.
Definition Highlighter.h:25
ColorStyle square_brackets
Matches '[' or ']'.
Definition Highlighter.h:82
ColorStyle comment
Matches any comments in the language.
Definition Highlighter.h:69
ColorStyle semicolons
Matches any semicolon: ';'.
Definition Highlighter.h:75
static HighlightStyle MakeVimStyle()
Returns a HighlightStyle that is based on vim's default highlight style.
ColorStyle braces
Matches '{' or '}'.
Definition Highlighter.h:80
ColorStyle scalar_literal
Matches scalar value literals like '42' or '0.1'.
Definition Highlighter.h:65
ColorStyle comma
Matches commas: ','.
Definition Highlighter.h:71
ColorStyle pp_directive
Matches directives to a preprocessor (if the language has any).
Definition Highlighter.h:89
ColorStyle operators
Matches operators like '+', '-', '', '&', '='.
Definition Highlighter.h:77
ColorStyle string_literal
Matches any string or character literals in the language: "foo" or 'f'.
Definition Highlighter.h:63
ColorStyle keyword
Matches all reserved keywords in the language.
Definition Highlighter.h:67
ColorStyle parentheses
Matches '(' or ')'.
Definition Highlighter.h:84
ColorStyle selected
The style for the token which is below the cursor of the user.
Definition Highlighter.h:58
ColorStyle identifier
Matches identifiers to variable or functions.
Definition Highlighter.h:61
ColorStyle colon
Matches one colon: ':'.
Definition Highlighter.h:73
static lldb::LanguageType getEmptyKey()
static lldb::LanguageType getTombstoneKey()
static unsigned getHashValue(lldb::LanguageType language_type)
static bool isEqual(lldb::LanguageType LHS, lldb::LanguageType RHS)