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