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) {
27}
28
29static HighlightStyle::ColorStyle GetColor(const char *c) {
30 return HighlightStyle::ColorStyle(c, "${ansi.normal}");
31}
32
34 HighlightStyle result;
35 result.comment = GetColor("${ansi.fg.purple}");
36 result.scalar_literal = GetColor("${ansi.fg.red}");
37 result.keyword = GetColor("${ansi.fg.green}");
38 return result;
39}
40
41const Highlighter &
43 llvm::StringRef path) const {
44 // The language may be able to provide a language type based on the path.
45 if (Language *language =
46 lldb_private::Language::FindPlugin(language_type, path))
47 language_type = language->GetLanguageType();
48
49 std::lock_guard<std::mutex> guard(m_mutex);
50 auto it = m_highlighters.find(language_type);
51 if (it != m_highlighters.end())
52 return *it->second;
53
54 for (auto create_instance : PluginManager::GetHighlighterCreateCallbacks()) {
55 if (Highlighter *highlighter = create_instance(language_type))
56 m_highlighters.try_emplace(language_type,
57 std::unique_ptr<Highlighter>(highlighter));
58 }
59
60 assert(m_highlighters.contains(language_type) &&
61 "we should always find the default highlighter");
62 return *m_highlighters[language_type];
63}
64
65std::string Highlighter::Highlight(const HighlightStyle &options,
66 llvm::StringRef line,
67 std::optional<size_t> cursor_pos,
68 llvm::StringRef previous_lines) const {
70 Highlight(options, line, cursor_pos, previous_lines, s);
71 s.Flush();
72 return s.GetString().str();
73}
static HighlightStyle::ColorStyle GetColor(const char *c)
A pair of strings that should be placed around a certain token.
Definition Highlighter.h:30
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.
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
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
static llvm::SmallVector< HighlighterCreateInstance > GetHighlighterCreateCallbacks()
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)
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 comment
Matches any comments in the language.
Definition Highlighter.h:69
static HighlightStyle MakeVimStyle()
Returns a HighlightStyle that is based on vim's default highlight style.
ColorStyle scalar_literal
Matches scalar value literals like '42' or '0.1'.
Definition Highlighter.h:65
ColorStyle keyword
Matches all reserved keywords in the language.
Definition Highlighter.h:67