Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/Highlighter.cpp
Line
Count
Source
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
9
#include "lldb/Core/Highlighter.h"
10
11
#include "lldb/Target/Language.h"
12
#include "lldb/Utility/AnsiTerminal.h"
13
#include "lldb/Utility/StreamString.h"
14
#include <optional>
15
16
using namespace lldb_private;
17
using namespace lldb_private::ansi;
18
19
64.6k
void HighlightStyle::ColorStyle::Apply(Stream &s, llvm::StringRef value) const {
20
64.6k
  s << m_prefix << value << m_suffix;
21
64.6k
}
22
23
void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
24
135
                                     llvm::StringRef suffix) {
25
135
  m_prefix = FormatAnsiTerminalCodes(prefix);
26
135
  m_suffix = FormatAnsiTerminalCodes(suffix);
27
135
}
28
29
void DefaultHighlighter::Highlight(const HighlightStyle &options,
30
                                   llvm::StringRef line,
31
                                   std::optional<size_t> cursor_pos,
32
                                   llvm::StringRef previous_lines,
33
11
                                   Stream &s) const {
34
  // If we don't have a valid cursor, then we just print the line as-is.
35
11
  if (!cursor_pos || 
*cursor_pos >= line.size()5
) {
36
7
    s << line;
37
7
    return;
38
7
  }
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
4
  size_t column = *cursor_pos;
45
  // Print the characters before the cursor.
46
4
  s << line.substr(0, column);
47
  // Print the selected character with the defined color codes.
48
4
  options.selected.Apply(s, line.substr(column, 1));
49
  // Print the rest of the line.
50
4
  s << line.substr(column + 1U);
51
4
}
52
53
78
static HighlightStyle::ColorStyle GetColor(const char *c) {
54
78
  return HighlightStyle::ColorStyle(c, "${ansi.normal}");
55
78
}
56
57
26
HighlightStyle HighlightStyle::MakeVimStyle() {
58
26
  HighlightStyle result;
59
26
  result.comment = GetColor("${ansi.fg.purple}");
60
26
  result.scalar_literal = GetColor("${ansi.fg.red}");
61
26
  result.keyword = GetColor("${ansi.fg.green}");
62
26
  return result;
63
26
}
64
65
const Highlighter &
66
HighlighterManager::getHighlighterFor(lldb::LanguageType language_type,
67
10.0k
                                      llvm::StringRef path) const {
68
10.0k
  Language *language = lldb_private::Language::FindPlugin(language_type, path);
69
10.0k
  if (language && 
language->GetHighlighter()10.0k
)
70
10.0k
    return *language->GetHighlighter();
71
13
  return m_default;
72
10.0k
}
73
74
std::string Highlighter::Highlight(const HighlightStyle &options,
75
                                   llvm::StringRef line,
76
                                   std::optional<size_t> cursor_pos,
77
51
                                   llvm::StringRef previous_lines) const {
78
51
  StreamString s;
79
51
  Highlight(options, line, cursor_pos, previous_lines, s);
80
51
  s.Flush();
81
51
  return s.GetString().str();
82
51
}