blob: c3c614aac21082c36561468e2762ea3cd49615e3 [file] [log] [blame]
Raphael Isemann566afa02018-08-02 00:30:15 +00001//===-- Highlighter.cpp -----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Raphael Isemann566afa02018-08-02 00:30:15 +00006//
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
15using namespace lldb_private;
Jonas Devlieghere4b3c0fd2019-08-21 00:50:46 +000016using namespace lldb_private::ansi;
Raphael Isemann566afa02018-08-02 00:30:15 +000017
Raphael Isemann2d437f62018-08-14 17:12:54 +000018void HighlightStyle::ColorStyle::Apply(Stream &s, llvm::StringRef value) const {
Raphael Isemann566afa02018-08-02 00:30:15 +000019 s << m_prefix << value << m_suffix;
Raphael Isemann566afa02018-08-02 00:30:15 +000020}
21
22void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
23 llvm::StringRef suffix) {
Jonas Devlieghere4b3c0fd2019-08-21 00:50:46 +000024 m_prefix = FormatAnsiTerminalCodes(prefix);
25 m_suffix = FormatAnsiTerminalCodes(suffix);
Raphael Isemann566afa02018-08-02 00:30:15 +000026}
27
Raphael Isemann20786322018-08-30 00:09:21 +000028void DefaultHighlighter::Highlight(const HighlightStyle &options,
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);
Raphael Isemann566afa02018-08-02 00:30:15 +000050}
51
52static HighlightStyle::ColorStyle GetColor(const char *c) {
53 return HighlightStyle::ColorStyle(c, "${ansi.normal}");
54}
55
56HighlightStyle HighlightStyle::MakeVimStyle() {
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
64const Highlighter &
65HighlighterManager::getHighlighterFor(lldb::LanguageType language_type,
66 llvm::StringRef path) const {
67 Language *language = lldb_private::Language::FindPlugin(language_type, path);
68 if (language && language->GetHighlighter())
69 return *language->GetHighlighter();
Raphael Isemann20786322018-08-30 00:09:21 +000070 return m_default;
Raphael Isemann566afa02018-08-02 00:30:15 +000071}
72
73std::string Highlighter::Highlight(const HighlightStyle &options,
74 llvm::StringRef line,
Raphael Isemann20786322018-08-30 00:09:21 +000075 llvm::Optional<size_t> cursor_pos,
Raphael Isemann566afa02018-08-02 00:30:15 +000076 llvm::StringRef previous_lines) const {
77 StreamString s;
Raphael Isemann20786322018-08-30 00:09:21 +000078 Highlight(options, line, cursor_pos, previous_lines, s);
Raphael Isemann566afa02018-08-02 00:30:15 +000079 s.Flush();
80 return s.GetString().str();
81}