blob: 2ef99c800b134bad4e7531217d8b7b2237c6fd4b [file] [log] [blame]
Raphael Isemann566afa02018-08-02 00:30:15 +00001//===-- Highlighter.cpp -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/Highlighter.h"
11
12#include "lldb/Target/Language.h"
13#include "lldb/Utility/AnsiTerminal.h"
14#include "lldb/Utility/StreamString.h"
15
16using namespace lldb_private;
17
18std::size_t HighlightStyle::ColorStyle::Apply(Stream &s,
19 llvm::StringRef value) const {
20 s << m_prefix << value << m_suffix;
21 // Calculate how many bytes we have written.
22 return m_prefix.size() + value.size() + m_suffix.size();
23}
24
25void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
26 llvm::StringRef suffix) {
27 m_prefix = lldb_utility::ansi::FormatAnsiTerminalCodes(prefix);
28 m_suffix = lldb_utility::ansi::FormatAnsiTerminalCodes(suffix);
29}
30
31std::size_t NoHighlighter::Highlight(const HighlightStyle &options,
32 llvm::StringRef line,
33 llvm::StringRef previous_lines,
34 Stream &s) const {
35 // We just forward the input to the output and do no highlighting.
36 s << line;
37 return line.size();
38}
39
40static HighlightStyle::ColorStyle GetColor(const char *c) {
41 return HighlightStyle::ColorStyle(c, "${ansi.normal}");
42}
43
44HighlightStyle HighlightStyle::MakeVimStyle() {
45 HighlightStyle result;
46 result.comment = GetColor("${ansi.fg.purple}");
47 result.scalar_literal = GetColor("${ansi.fg.red}");
48 result.keyword = GetColor("${ansi.fg.green}");
49 return result;
50}
51
52const Highlighter &
53HighlighterManager::getHighlighterFor(lldb::LanguageType language_type,
54 llvm::StringRef path) const {
55 Language *language = lldb_private::Language::FindPlugin(language_type, path);
56 if (language && language->GetHighlighter())
57 return *language->GetHighlighter();
58 return m_no_highlighter;
59}
60
61std::string Highlighter::Highlight(const HighlightStyle &options,
62 llvm::StringRef line,
63 llvm::StringRef previous_lines) const {
64 StreamString s;
65 Highlight(options, line, previous_lines, s);
66 s.Flush();
67 return s.GetString().str();
68}