llvm-project/lldb/source/Core/Highlighter.cpp
Raphael Isemann 207863261c Move the column marking functionality to the Highlighter framework
Summary:
The syntax highlighting feature so far is mutually exclusive with the lldb feature
that marks the current column in the line by underlining it via an ANSI color code.
Meaning that if you enable one, the other is automatically disabled by LLDB.

This was caused by the fact that both features inserted color codes into the the
source code and were likely to interfere with each other (which would result
in a broken source code printout to the user).

This patch moves the cursor code into the highlighting framework, which provides
the same feature to the user in normal non-C source code. For any source code
that is highlighted by Clang, we now also have cursor marking for the whole token
that is under the current source location. E.g., before we underlined only the '!' in the
expression '1 != 2', but now the whole token '!=' is underlined. The same for function
calls and so on. Below you can see two examples where we before only underlined
the first character of the token, but now underline the whole token.

{F7075400}
{F7075414}

It also simplifies the DisplaySourceLines method in the SourceManager as most of
the code in there was essentially just for getting this column marker to work as
a FormatEntity.

Reviewers: aprantl

Reviewed By: aprantl

Subscribers: lldb-commits

Differential Revision: https://reviews.llvm.org/D51466

llvm-svn: 341003
2018-08-30 00:09:21 +00:00

82 lines
2.9 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;
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();
}