Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 1 | //===-- 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 | |
| 16 | using namespace lldb_private; |
| 17 | |
Raphael Isemann | 2d437f6 | 2018-08-14 17:12:54 +0000 | [diff] [blame] | 18 | void HighlightStyle::ColorStyle::Apply(Stream &s, llvm::StringRef value) const { |
Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 19 | s << m_prefix << value << m_suffix; |
Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void 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 Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 28 | void 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 Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static HighlightStyle::ColorStyle GetColor(const char *c) { |
| 53 | return HighlightStyle::ColorStyle(c, "${ansi.normal}"); |
| 54 | } |
| 55 | |
| 56 | HighlightStyle 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 | |
| 64 | const Highlighter & |
| 65 | HighlighterManager::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 Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 70 | return m_default; |
Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | std::string Highlighter::Highlight(const HighlightStyle &options, |
| 74 | llvm::StringRef line, |
Raphael Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 75 | llvm::Optional<size_t> cursor_pos, |
Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 76 | llvm::StringRef previous_lines) const { |
| 77 | StreamString s; |
Raphael Isemann | 2078632 | 2018-08-30 00:09:21 +0000 | [diff] [blame] | 78 | Highlight(options, line, cursor_pos, previous_lines, s); |
Raphael Isemann | 566afa0 | 2018-08-02 00:30:15 +0000 | [diff] [blame] | 79 | s.Flush(); |
| 80 | return s.GetString().str(); |
| 81 | } |