blob: 53aa16df36b725aed2c7b414e77805df06dd6682 [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
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) {
24 m_prefix = lldb_utility::ansi::FormatAnsiTerminalCodes(prefix);
25 m_suffix = lldb_utility::ansi::FormatAnsiTerminalCodes(suffix);
26}
27
Raphael Isemann2d437f62018-08-14 17:12:54 +000028void NoHighlighter::Highlight(const HighlightStyle &options,
29 llvm::StringRef line,
30 llvm::StringRef previous_lines, Stream &s) const {
Raphael Isemann566afa02018-08-02 00:30:15 +000031 // We just forward the input to the output and do no highlighting.
32 s << line;
Raphael Isemann566afa02018-08-02 00:30:15 +000033}
34
35static HighlightStyle::ColorStyle GetColor(const char *c) {
36 return HighlightStyle::ColorStyle(c, "${ansi.normal}");
37}
38
39HighlightStyle HighlightStyle::MakeVimStyle() {
40 HighlightStyle result;
41 result.comment = GetColor("${ansi.fg.purple}");
42 result.scalar_literal = GetColor("${ansi.fg.red}");
43 result.keyword = GetColor("${ansi.fg.green}");
44 return result;
45}
46
47const Highlighter &
48HighlighterManager::getHighlighterFor(lldb::LanguageType language_type,
49 llvm::StringRef path) const {
50 Language *language = lldb_private::Language::FindPlugin(language_type, path);
51 if (language && language->GetHighlighter())
52 return *language->GetHighlighter();
53 return m_no_highlighter;
54}
55
56std::string Highlighter::Highlight(const HighlightStyle &options,
57 llvm::StringRef line,
58 llvm::StringRef previous_lines) const {
59 StreamString s;
60 Highlight(options, line, previous_lines, s);
61 s.Flush();
62 return s.GetString().str();
63}