Summary: This patch adds syntax highlighting support to LLDB. When enabled (and lldb is allowed to use colors), printed source code is annotated with the ANSI color escape sequences. So far we have only one highlighter which is based on Clang and is responsible for all languages that are supported by Clang. It essentially just runs the raw lexer over the input and then surrounds the specific tokens with the configured escape sequences. Reviewers: zturner, davide Reviewed By: davide Subscribers: labath, teemperor, llvm-commits, mgorny, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D49334 llvm-svn: 338662
69 lines
2.4 KiB
C++
69 lines
2.4 KiB
C++
//===-- Highlighter.cpp -----------------------------------------*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "lldb/Core/Highlighter.h"
|
|
|
|
#include "lldb/Target/Language.h"
|
|
#include "lldb/Utility/AnsiTerminal.h"
|
|
#include "lldb/Utility/StreamString.h"
|
|
|
|
using namespace lldb_private;
|
|
|
|
std::size_t HighlightStyle::ColorStyle::Apply(Stream &s,
|
|
llvm::StringRef value) const {
|
|
s << m_prefix << value << m_suffix;
|
|
// Calculate how many bytes we have written.
|
|
return m_prefix.size() + value.size() + m_suffix.size();
|
|
}
|
|
|
|
void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
|
|
llvm::StringRef suffix) {
|
|
m_prefix = lldb_utility::ansi::FormatAnsiTerminalCodes(prefix);
|
|
m_suffix = lldb_utility::ansi::FormatAnsiTerminalCodes(suffix);
|
|
}
|
|
|
|
std::size_t NoHighlighter::Highlight(const HighlightStyle &options,
|
|
llvm::StringRef line,
|
|
llvm::StringRef previous_lines,
|
|
Stream &s) const {
|
|
// We just forward the input to the output and do no highlighting.
|
|
s << line;
|
|
return line.size();
|
|
}
|
|
|
|
static HighlightStyle::ColorStyle GetColor(const char *c) {
|
|
return HighlightStyle::ColorStyle(c, "${ansi.normal}");
|
|
}
|
|
|
|
HighlightStyle HighlightStyle::MakeVimStyle() {
|
|
HighlightStyle result;
|
|
result.comment = GetColor("${ansi.fg.purple}");
|
|
result.scalar_literal = GetColor("${ansi.fg.red}");
|
|
result.keyword = GetColor("${ansi.fg.green}");
|
|
return result;
|
|
}
|
|
|
|
const Highlighter &
|
|
HighlighterManager::getHighlighterFor(lldb::LanguageType language_type,
|
|
llvm::StringRef path) const {
|
|
Language *language = lldb_private::Language::FindPlugin(language_type, path);
|
|
if (language && language->GetHighlighter())
|
|
return *language->GetHighlighter();
|
|
return m_no_highlighter;
|
|
}
|
|
|
|
std::string Highlighter::Highlight(const HighlightStyle &options,
|
|
llvm::StringRef line,
|
|
llvm::StringRef previous_lines) const {
|
|
StreamString s;
|
|
Highlight(options, line, previous_lines, s);
|
|
s.Flush();
|
|
return s.GetString().str();
|
|
}
|