llvm-project/lldb/source/Core/Highlighter.cpp
Chandler Carruth 2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00

81 lines
2.9 KiB
C++

//===-- Highlighter.cpp -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/Highlighter.h"
#include "lldb/Target/Language.h"
#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/StreamString.h"
using namespace lldb_private;
void HighlightStyle::ColorStyle::Apply(Stream &s, llvm::StringRef value) const {
s << m_prefix << value << m_suffix;
}
void HighlightStyle::ColorStyle::Set(llvm::StringRef prefix,
llvm::StringRef suffix) {
m_prefix = lldb_utility::ansi::FormatAnsiTerminalCodes(prefix);
m_suffix = lldb_utility::ansi::FormatAnsiTerminalCodes(suffix);
}
void DefaultHighlighter::Highlight(const HighlightStyle &options,
llvm::StringRef line,
llvm::Optional<size_t> cursor_pos,
llvm::StringRef previous_lines,
Stream &s) const {
// If we don't have a valid cursor, then we just print the line as-is.
if (!cursor_pos || *cursor_pos >= line.size()) {
s << line;
return;
}
// If we have a valid cursor, we have to apply the 'selected' style around
// the character below the cursor.
// Split the line around the character which is below the cursor.
size_t column = *cursor_pos;
// Print the characters before the cursor.
s << line.substr(0, column);
// Print the selected character with the defined color codes.
options.selected.Apply(s, line.substr(column, 1));
// Print the rest of the line.
s << line.substr(column + 1U);
}
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_default;
}
std::string Highlighter::Highlight(const HighlightStyle &options,
llvm::StringRef line,
llvm::Optional<size_t> cursor_pos,
llvm::StringRef previous_lines) const {
StreamString s;
Highlight(options, line, cursor_pos, previous_lines, s);
s.Flush();
return s.GetString().str();
}