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
diff --git a/lldb/unittests/Language/Highlighting/HighlighterTest.cpp b/lldb/unittests/Language/Highlighting/HighlighterTest.cpp
index 8e9a2ea..ea1f637 100644
--- a/lldb/unittests/Language/Highlighting/HighlighterTest.cpp
+++ b/lldb/unittests/Language/Highlighting/HighlighterTest.cpp
@@ -99,31 +99,49 @@
style.semicolons.Set("<", ">");
const char *code = "program Hello;";
- std::string output = h.Highlight(style, code);
+ std::string output = h.Highlight(style, code, llvm::Optional<size_t>());
EXPECT_STREQ(output.c_str(), code);
}
+static std::string
+highlightDefault(llvm::StringRef code, HighlightStyle style,
+ llvm::Optional<size_t> cursor = llvm::Optional<size_t>()) {
+ HighlighterManager mgr;
+ return mgr.getDefaultHighlighter().Highlight(style, code, cursor);
+}
+
TEST_F(HighlighterTest, DefaultHighlighter) {
- HighlighterManager mgr;
- const Highlighter &h = mgr.getHighlighterFor(lldb::eLanguageTypeC, "main.c");
+ const char *code = "int my_main() { return 22; } \n";
HighlightStyle style;
-
- const char *code = "int my_main() { return 22; } \n";
- std::string output = h.Highlight(style, code);
-
- EXPECT_STREQ(output.c_str(), code);
+ EXPECT_EQ(code, highlightDefault(code, style));
}
+TEST_F(HighlighterTest, DefaultHighlighterWithCursor) {
+ HighlightStyle style;
+ style.selected.Set("<c>", "</c>");
+ EXPECT_EQ("<c>a</c> bc", highlightDefault("a bc", style, 0));
+ EXPECT_EQ("a<c> </c>bc", highlightDefault("a bc", style, 1));
+ EXPECT_EQ("a <c>b</c>c", highlightDefault("a bc", style, 2));
+ EXPECT_EQ("a b<c>c</c>", highlightDefault("a bc", style, 3));
+}
+
+TEST_F(HighlighterTest, DefaultHighlighterWithCursorOutOfBounds) {
+ HighlightStyle style;
+ style.selected.Set("<c>", "</c>");
+ EXPECT_EQ("a bc", highlightDefault("a bc", style, 4));
+}
//------------------------------------------------------------------------------
// Tests highlighting with the Clang highlighter.
//------------------------------------------------------------------------------
-static std::string highlightC(llvm::StringRef code, HighlightStyle style) {
+static std::string
+highlightC(llvm::StringRef code, HighlightStyle style,
+ llvm::Optional<size_t> cursor = llvm::Optional<size_t>()) {
HighlighterManager mgr;
const Highlighter &h = mgr.getHighlighterFor(lldb::eLanguageTypeC, "main.c");
- return h.Highlight(style, code);
+ return h.Highlight(style, code, cursor);
}
TEST_F(HighlighterTest, ClangEmptyInput) {
@@ -219,3 +237,67 @@
EXPECT_EQ(" <id>foo</id> <id>c</id> = <id>bar</id>(); return 1;",
highlightC(" foo c = bar(); return 1;", s));
}
+
+TEST_F(HighlighterTest, ClangCursorPos) {
+ HighlightStyle s;
+ s.selected.Set("<c>", "</c>");
+
+ EXPECT_EQ("<c> </c>foo c = bar(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 0));
+ EXPECT_EQ(" <c>foo</c> c = bar(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 1));
+ EXPECT_EQ(" <c>foo</c> c = bar(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 2));
+ EXPECT_EQ(" <c>foo</c> c = bar(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 3));
+ EXPECT_EQ(" foo<c> </c>c = bar(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 4));
+ EXPECT_EQ(" foo <c>c</c> = bar(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 5));
+}
+
+TEST_F(HighlighterTest, ClangCursorPosEndOfLine) {
+ HighlightStyle s;
+ s.selected.Set("<c>", "</c>");
+
+ EXPECT_EQ("f", highlightC("f", s, 1));
+}
+
+TEST_F(HighlighterTest, ClangCursorOutOfBounds) {
+ HighlightStyle s;
+ s.selected.Set("<c>", "</c>");
+ EXPECT_EQ("f", highlightC("f", s, 2));
+ EXPECT_EQ("f", highlightC("f", s, 3));
+ EXPECT_EQ("f", highlightC("f", s, 4));
+}
+
+TEST_F(HighlighterTest, ClangCursorPosBeforeOtherToken) {
+ HighlightStyle s;
+ s.selected.Set("<c>", "</c>");
+ s.identifier.Set("<id>", "</id>");
+
+ EXPECT_EQ("<c> </c><id>foo</id> <id>c</id> = <id>bar</id>(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 0));
+}
+
+TEST_F(HighlighterTest, ClangCursorPosAfterOtherToken) {
+ HighlightStyle s;
+ s.selected.Set("<c>", "</c>");
+ s.identifier.Set("<id>", "</id>");
+
+ EXPECT_EQ(" <id>foo</id><c> </c><id>c</id> = <id>bar</id>(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 4));
+}
+
+TEST_F(HighlighterTest, ClangCursorPosInOtherToken) {
+ HighlightStyle s;
+ s.selected.Set("<c>", "</c>");
+ s.identifier.Set("<id>", "</id>");
+
+ EXPECT_EQ(" <id><c>foo</c></id> <id>c</id> = <id>bar</id>(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 1));
+ EXPECT_EQ(" <id><c>foo</c></id> <id>c</id> = <id>bar</id>(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 2));
+ EXPECT_EQ(" <id><c>foo</c></id> <id>c</id> = <id>bar</id>(); return 1;",
+ highlightC(" foo c = bar(); return 1;", s, 3));
+}