LLDB mainline
Highlighter.cpp
Go to the documentation of this file.
1//===-- Highlighter.cpp ---------------------------------------------------===//
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
10
14#include <optional>
15
16using namespace lldb_private;
17using namespace lldb_private::ansi;
18
19void HighlightStyle::ColorStyle::Apply(Stream &s, llvm::StringRef value) const {
20 s << m_prefix << value << m_suffix;
21}
22
23void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
24 llvm::StringRef suffix) {
25 m_prefix = FormatAnsiTerminalCodes(prefix);
26 m_suffix = FormatAnsiTerminalCodes(suffix);
27}
28
30 llvm::StringRef line,
31 std::optional<size_t> cursor_pos,
32 llvm::StringRef previous_lines,
33 Stream &s) const {
34 // If we don't have a valid cursor, then we just print the line as-is.
35 if (!cursor_pos || *cursor_pos >= line.size()) {
36 s << line;
37 return;
38 }
39
40 // If we have a valid cursor, we have to apply the 'selected' style around
41 // the character below the cursor.
42
43 // Split the line around the character which is below the cursor.
44 size_t column = *cursor_pos;
45 // Print the characters before the cursor.
46 s << line.substr(0, column);
47 // Print the selected character with the defined color codes.
48 options.selected.Apply(s, line.substr(column, 1));
49 // Print the rest of the line.
50 s << line.substr(column + 1U);
51}
52
53static HighlightStyle::ColorStyle GetColor(const char *c) {
54 return HighlightStyle::ColorStyle(c, "${ansi.normal}");
55}
56
58 HighlightStyle result;
59 result.comment = GetColor("${ansi.fg.purple}");
60 result.scalar_literal = GetColor("${ansi.fg.red}");
61 result.keyword = GetColor("${ansi.fg.green}");
62 return result;
63}
64
65const Highlighter &
67 llvm::StringRef path) const {
68 Language *language = lldb_private::Language::FindPlugin(language_type, path);
69 if (language && language->GetHighlighter())
70 return *language->GetHighlighter();
71 return m_default;
72}
73
74std::string Highlighter::Highlight(const HighlightStyle &options,
75 llvm::StringRef line,
76 std::optional<size_t> cursor_pos,
77 llvm::StringRef previous_lines) const {
79 Highlight(options, line, cursor_pos, previous_lines, s);
80 s.Flush();
81 return s.GetString().str();
82}
static HighlightStyle::ColorStyle GetColor(const char *c)
Definition: Highlighter.cpp:53
void Highlight(const HighlightStyle &options, llvm::StringRef line, std::optional< size_t > cursor_pos, llvm::StringRef previous_lines, Stream &s) const override
Highlights the given line.
Definition: Highlighter.cpp:29
A pair of strings that should be placed around a certain token.
Definition: Highlighter.h:29
void Set(llvm::StringRef prefix, llvm::StringRef suffix)
Sets the prefix and suffix strings.
Definition: Highlighter.cpp:23
void Apply(Stream &s, llvm::StringRef value) const
Applies this style to the given value.
Definition: Highlighter.cpp:19
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:66
Annotates source code with color attributes.
Definition: Highlighter.h:91
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.
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:84
virtual const Highlighter * GetHighlighter() const
Definition: Language.h:180
void Flush() override
Flush the stream.
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
std::string FormatAnsiTerminalCodes(llvm::StringRef format, bool do_color=true)
Definition: AnsiTerminal.h:83
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:24
ColorStyle comment
Matches any comments in the language.
Definition: Highlighter.h:64
static HighlightStyle MakeVimStyle()
Returns a HighlightStyle that is based on vim's default highlight style.
Definition: Highlighter.cpp:57
ColorStyle scalar_literal
Matches scalar value literals like '42' or '0.1'.
Definition: Highlighter.h:60
ColorStyle keyword
Matches all reserved keywords in the language.
Definition: Highlighter.h:62
ColorStyle selected
The style for the token which is below the cursor of the user.
Definition: Highlighter.h:53