1 //===-- Highlighter.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Highlighter_h_
11 #define liblldb_Highlighter_h_
16 #include "lldb/Utility/Stream.h"
17 #include "lldb/lldb-enumerations.h"
18 #include "llvm/ADT/StringRef.h"
20 namespace 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.
25 //----------------------------------------------------------------------
26 struct HighlightStyle {
28 //----------------------------------------------------------------------
29 /// A pair of strings that should be placed around a certain token. Usually
30 /// stores color codes in these strings (the suffix string is often used for
31 /// resetting the terminal attributes back to normal).
32 //----------------------------------------------------------------------
38 ColorStyle() = default;
39 ColorStyle(llvm::StringRef prefix, llvm::StringRef suffix) {
43 /// Applies this style to the given value.
45 /// The stream to which the result should be appended.
47 /// The value that we should place our strings around.
48 void Apply(Stream &s, llvm::StringRef value) const;
50 /// Sets the prefix and suffix strings.
53 void Set(llvm::StringRef prefix, llvm::StringRef suffix);
56 /// The style for the token which is below the cursor of the user. Note that
57 /// this style is overwritten by the SourceManager with the values of
58 /// stop-show-column-ansi-prefix/stop-show-column-ansi-suffix.
61 /// Matches identifiers to variable or functions.
62 ColorStyle identifier;
63 /// Matches any string or character literals in the language: "foo" or 'f'
64 ColorStyle string_literal;
65 /// Matches scalar value literals like '42' or '0.1'.
66 ColorStyle scalar_literal;
67 /// Matches all reserved keywords in the language.
69 /// Matches any comments in the language.
71 /// Matches commas: ','
73 /// Matches one colon: ':'
75 /// Matches any semicolon: ';'
76 ColorStyle semicolons;
77 /// Matches operators like '+', '-', '%', '&', '='
80 /// Matches '{' or '}'
82 /// Matches '[' or ']'
83 ColorStyle square_brackets;
84 /// Matches '(' or ')'
85 ColorStyle parentheses;
87 //-----------------------------------------------------------------------
88 // C language specific options
89 //-----------------------------------------------------------------------
91 /// Matches directives to a preprocessor (if the language has any).
92 ColorStyle pp_directive;
94 /// Returns a HighlightStyle that is based on vim's default highlight style.
95 static HighlightStyle MakeVimStyle();
98 //----------------------------------------------------------------------
99 /// Annotates source code with color attributes.
100 //----------------------------------------------------------------------
103 Highlighter() = default;
104 virtual ~Highlighter() = default;
105 DISALLOW_COPY_AND_ASSIGN(Highlighter);
107 /// Returns a human readable name for the selected highlighter.
108 virtual llvm::StringRef GetName() const = 0;
110 /// Highlights the given line
113 /// The user supplied line that needs to be highlighted.
114 /// \param cursor_pos
115 /// The cursor position of the user in this line, starting at 0 (which
116 /// means the cursor is on the first character in 'line').
117 /// \param previous_lines
118 /// Any previous lines the user has written which we should only use
119 /// for getting the context of the Highlighting right.
121 /// The stream to which the highlighted version of the user string should
123 virtual void Highlight(const HighlightStyle &options, llvm::StringRef line,
124 llvm::Optional<size_t> cursor_pos,
125 llvm::StringRef previous_lines, Stream &s) const = 0;
127 /// Utility method for calling Highlight without a stream.
128 std::string Highlight(const HighlightStyle &options, llvm::StringRef line,
129 llvm::Optional<size_t> cursor_pos,
130 llvm::StringRef previous_lines = "") const;
133 /// A default highlighter that only highlights the user cursor, but doesn't
134 /// do any other highlighting.
135 class DefaultHighlighter : public Highlighter {
137 llvm::StringRef GetName() const override { return "none"; }
139 void Highlight(const HighlightStyle &options, llvm::StringRef line,
140 llvm::Optional<size_t> cursor_pos,
141 llvm::StringRef previous_lines, Stream &s) const override;
144 /// Manages the available highlighters.
145 class HighlighterManager {
146 DefaultHighlighter m_default;
149 /// Queries all known highlighter for one that can highlight some source code.
150 /// \param language_type
151 /// The language type that the caller thinks the source code was given in.
153 /// The path to the file the source code is from. Used as a fallback when
154 /// the user can't provide a language.
156 /// The highlighter that wants to highlight the source code. Could be an
157 /// empty highlighter that does nothing.
158 const Highlighter &getHighlighterFor(lldb::LanguageType language_type,
159 llvm::StringRef path) const;
160 const Highlighter &getDefaultHighlighter() const { return m_default; }
163 } // namespace lldb_private
165 #endif // liblldb_Highlighter_h_