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