From f5da1671fdb19c8930acc6af276ba3e8cab8cb7b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 19 Mar 2015 00:31:11 +0000 Subject: [PATCH] Refactor number_line higlighter, use a switch to higlhight the cursor line --- README.asciidoc | 5 ++-- src/face_registry.cc | 2 +- src/highlighters.cc | 64 ++++++++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 35d7540f..f952c049 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -637,8 +637,9 @@ general highlighters are: . * +show_matching+: highlight matching char of the character under the selections cursor using +MatchingChar+ face. - * +number_lines <-relative>+: show line numbers. The -relative switch will enable relative - line numbers. + * +number_lines <-relative> <-hlcursor>+: show line numbers. The -relative switch + will show line numbers to main cursor line, the -hlcursor switch will + highlight the cursor line with a separate face.. * +fill +: fill using given face, mostly useful with the +regions+ highlighter (see below) diff --git a/src/face_registry.cc b/src/face_registry.cc index ffdf5ab6..1c373be1 100644 --- a/src/face_registry.cc +++ b/src/face_registry.cc @@ -96,7 +96,7 @@ FaceRegistry::FaceRegistry() { "PrimaryCursor", Face{ Colors::Black, Colors::White } }, { "SecondaryCursor", Face{ Colors::Black, Colors::White } }, { "LineNumbers", Face{ Colors::Default, Colors::Default } }, - { "LineNumberAbsolute", Face{ Colors::Black, Colors::Blue } }, + { "LineNumberCursor", Face{ Colors::Default, Colors::Default, Attribute::Reverse } }, { "MenuForeground", Face{ Colors::White, Colors::Blue } }, { "MenuBackground", Face{ Colors::Blue, Colors::White } }, { "Information", Face{ Colors::Black, Colors::Yellow } }, diff --git a/src/highlighters.cc b/src/highlighters.cc index 9e0461e7..286d9db2 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -594,11 +594,12 @@ void show_whitespaces(const Context& context, HighlightFlags flags, DisplayBuffe } } -template -void show_line_numbers(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange) +template +void show_line_numbers(const Context& context, HighlightFlags flags, + DisplayBuffer& display_buffer, BufferRange) { const Face face = get_face("LineNumbers"); - const Face face_absolute = get_face("LineNumberAbsolute"); + const Face face_absolute = get_face("LineNumberCursor"); LineCount last_line = context.buffer().line_count(); int digit_count = 0; for (LineCount c = last_line; c > 0; c /= 10) @@ -609,41 +610,40 @@ void show_line_numbers(const Context& context, HighlightFlags flags, DisplayBuff int main_selection = (int)context.selections().main().cursor().line + 1; for (auto& line : display_buffer.lines()) { - int current_line = (int)line.range().first.line + 1; + const int current_line = (int)line.range().first.line + 1; + const bool is_cursor_line = main_selection == current_line; + const int line_to_format = (relative and not is_cursor_line) ? + current_line - main_selection : current_line; char buffer[16]; - if (relative) - { - if (main_selection == current_line) - { - snprintf(buffer, 16, format, current_line); - DisplayAtom atom{buffer}; - atom.face = face_absolute; - line.insert(line.begin(), std::move(atom)); - } - else { - snprintf(buffer, 16, format, current_line - main_selection); - DisplayAtom atom{buffer}; - atom.face = face; - line.insert(line.begin(), std::move(atom)); - } - } - else { - snprintf(buffer, 16, format, current_line); - DisplayAtom atom{buffer}; - (main_selection == current_line) ? atom.face = face_absolute - : atom.face = face; - line.insert(line.begin(), std::move(atom)); - } + snprintf(buffer, 16, format, line_to_format); + DisplayAtom atom{buffer}; + atom.face = (hl_cursor_line and is_cursor_line) ? face_absolute : face; + line.insert(line.begin(), std::move(atom)); } } HighlighterAndId number_lines_factory(HighlighterParameters params) { - if (params.size() > 1) - throw runtime_error("wrong parameter count"); + static const ParameterDesc param_desc{ + { { "relative", { false, "" } }, + { "hlcursor", { false, "" } } }, + ParameterDesc::Flags::None, 0, 0 + }; + ParametersParser parser(params, param_desc); - return (params.size() == 1 && params[0] == "-relative") ? HighlighterAndId("number_lines_relative", make_simple_highlighter(show_line_numbers)) - : HighlighterAndId("number_lines", make_simple_highlighter(show_line_numbers)); + constexpr struct { + StringView name; + void (*func)(const Context&, HighlightFlags, DisplayBuffer&, BufferRange); + } funcs[] = { + { "number_lines", show_line_numbers }, + { "number_lines", show_line_numbers }, + { "number_lines_relative", show_line_numbers }, + { "number_lines_relative", show_line_numbers }, + }; + const int index = (parser.get_switch("relative") ? 1 : 0) * 2 + + (parser.get_switch("hlcursor") ? 1 : 0); + + return {funcs[index].name.str(), make_simple_highlighter(funcs[index].func)}; } void show_matching_char(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange) @@ -1239,7 +1239,7 @@ void register_highlighters() "number_lines", { number_lines_factory, "Display line numbers \n" - "Parameters: -relative" } }); + "Parameters: -relative, -hlcursor\n" } }); registry.append({ "show_matching", { simple_factory("show_matching", show_matching_char),