line-number: Add option to use a different separator on the current line

This commit is contained in:
hss 2021-03-19 23:23:01 -04:00
parent 545db22ae4
commit f16783b0a8

View File

@ -1082,11 +1082,12 @@ private:
struct LineNumbersHighlighter : Highlighter struct LineNumbersHighlighter : Highlighter
{ {
LineNumbersHighlighter(bool relative, bool hl_cursor_line, String separator, int min_digits) LineNumbersHighlighter(bool relative, bool hl_cursor_line, String separator, Optional<String> separator_cursor, int min_digits)
: Highlighter{HighlightPass::Move}, : Highlighter{HighlightPass::Move},
m_relative{relative}, m_relative{relative},
m_hl_cursor_line{hl_cursor_line}, m_hl_cursor_line{hl_cursor_line},
m_separator{std::move(separator)}, m_separator{std::move(separator)},
m_separator_cursor{std::move(separator_cursor)},
m_min_digits{min_digits} {} m_min_digits{min_digits} {}
static std::unique_ptr<Highlighter> create(HighlighterParameters params, Highlighter*) static std::unique_ptr<Highlighter> create(HighlighterParameters params, Highlighter*)
@ -1094,6 +1095,7 @@ struct LineNumbersHighlighter : Highlighter
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "relative", { false, "" } }, { { "relative", { false, "" } },
{ "separator", { true, "" } }, { "separator", { true, "" } },
{ "separator-cursor", { true, "" } },
{ "min-digits", { true, "" } }, { "min-digits", { true, "" } },
{ "hlcursor", { false, "" } } }, { "hlcursor", { false, "" } } },
ParameterDesc::Flags::None, 0, 0 ParameterDesc::Flags::None, 0, 0
@ -1104,13 +1106,23 @@ struct LineNumbersHighlighter : Highlighter
if (separator.length() > 10) if (separator.length() > 10)
throw runtime_error("separator length is limited to 10 bytes"); throw runtime_error("separator length is limited to 10 bytes");
Optional<StringView> separator_cursor = parser.get_switch("separator-cursor");
if (separator_cursor.value_or(separator).length() != separator.length()) {
// Throw runtime error instead?
write_to_debug_buffer("line-numbers: Separator for active line should have the same length as seperator");
separator_cursor.reset();
}
// Effectively separator_cursor?.str(), except idk how to code in C++
Optional<String> separator_cursor_str = separator_cursor ? separator_cursor->str() : Optional<String>{};
int min_digits = parser.get_switch("min-digits").map(str_to_int).value_or(2); int min_digits = parser.get_switch("min-digits").map(str_to_int).value_or(2);
if (min_digits < 0) if (min_digits < 0)
throw runtime_error("min digits must be positive"); throw runtime_error("min digits must be positive");
if (min_digits > 10) if (min_digits > 10)
throw runtime_error("min digits is limited to 10"); throw runtime_error("min digits is limited to 10");
return std::make_unique<LineNumbersHighlighter>((bool)parser.get_switch("relative"), (bool)parser.get_switch("hlcursor"), separator.str(), min_digits); return std::make_unique<LineNumbersHighlighter>((bool)parser.get_switch("relative"), (bool)parser.get_switch("hlcursor"), separator.str(), separator_cursor_str, min_digits);
} }
private: private:
@ -1142,7 +1154,10 @@ private:
const auto atom_face = last_line == current_line ? face_wrapped : const auto atom_face = last_line == current_line ? face_wrapped :
((m_hl_cursor_line and is_cursor_line) ? face_absolute : face); ((m_hl_cursor_line and is_cursor_line) ? face_absolute : face);
line.insert(line.begin(), {buffer, atom_face}); line.insert(line.begin(), {buffer, atom_face});
line.insert(line.begin() + 1, {m_separator, face});
const auto separator = is_cursor_line && m_separator_cursor ? *m_separator_cursor
: m_separator;
line.insert(line.begin() + 1, {separator, face});
last_line = current_line; last_line = current_line;
} }
@ -1174,6 +1189,7 @@ private:
const bool m_relative; const bool m_relative;
const bool m_hl_cursor_line; const bool m_hl_cursor_line;
const String m_separator; const String m_separator;
const Optional<String> m_separator_cursor;
const int m_min_digits; const int m_min_digits;
}; };
@ -2272,7 +2288,7 @@ void register_highlighters()
"number-lines", "number-lines",
{ LineNumbersHighlighter::create, { LineNumbersHighlighter::create,
"Display line numbers \n" "Display line numbers \n"
"Parameters: -relative, -hlcursor, -separator <separator text>, -min-digits <cols>\n" } }); "Parameters: -relative, -hlcursor, -separator <separator text>, -separator-cursor <separator text>, -min-digits <cols>\n" } });
registry.insert({ registry.insert({
"show-matching", "show-matching",
{ create_matching_char_highlighter, { create_matching_char_highlighter,