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