Coverage Report

Created: 2022-01-18 06:27

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