Add a -previous switch to show-matching highlighter

This switch makes show-matching fallback to the character preceeding
the cursor if the character under the cursor is not a matching
character, which should make show-matching more useful in insert mode.
This commit is contained in:
Maxime Coste 2023-06-14 19:49:06 +10:00
parent 4106791adc
commit 5b1ad0bd0c
3 changed files with 18 additions and 4 deletions

View File

@ -30,7 +30,11 @@ highlighter is replaced with the new one.
*show-matching*:: *show-matching*::
highlight matching char of the character under the selections' cursor highlight matching char of the character under the selections' cursor
using `MatchingChar` face using `MatchingChar` face, with the following *options*:
*-previous*:::
fall back to the character before the cursor when the cursor is not
over a matching char
*show-whitespaces* [options]:: *show-whitespaces* [options]::
display symbols on top of whitespaces to make them more explicit display symbols on top of whitespaces to make them more explicit

View File

@ -197,7 +197,7 @@ public:
const auto faces = m_faces | transform([&faces = context.context.faces()](auto&& spec) { const auto faces = m_faces | transform([&faces = context.context.faces()](auto&& spec) {
return faces[spec.second]; return faces[spec.second];
}) | gather<Vector<Face>>(); }) | gather<Vector>();
const auto& matches = get_matches(context.context.buffer(), display_buffer.range(), range); const auto& matches = get_matches(context.context.buffer(), display_buffer.range(), range);
kak_assert(matches.size() % m_faces.size() == 0); kak_assert(matches.size() % m_faces.size() == 0);
@ -1154,8 +1154,9 @@ constexpr StringView LineNumbersHighlighter::ms_id;
const HighlighterDesc show_matching_desc = { const HighlighterDesc show_matching_desc = {
"Apply the MatchingChar face to the char matching the one under the cursor", "Apply the MatchingChar face to the char matching the one under the cursor",
{} { { { "previous", {} } }, ParameterDesc::Flags::SwitchesOnlyAtStart, 0, 0 }
}; };
template<bool match_prev>
void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer, BufferRange) void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer, BufferRange)
{ {
const Face face = context.context.faces()["MatchingChar"]; const Face face = context.context.faces()["MatchingChar"];
@ -1170,10 +1171,17 @@ void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer,
Utf8Iterator it{buffer.iterator_at(pos), buffer}; Utf8Iterator it{buffer.iterator_at(pos), buffer};
auto match = find(matching_pairs, *it); auto match = find(matching_pairs, *it);
bool matching_prev = match == matching_pairs.end() and match_prev;
if (matching_prev)
match = find(matching_pairs, *--it);
if (match == matching_pairs.end()) if (match == matching_pairs.end())
continue; continue;
if (matching_prev)
highlight_range(display_buffer, it.base().coord(), (it+1).base().coord(),
false, apply_face(face));
int level = 0; int level = 0;
if (((match - matching_pairs.begin()) % 2) == 0) if (((match - matching_pairs.begin()) % 2) == 0)
{ {
@ -1216,7 +1224,8 @@ void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer,
std::unique_ptr<Highlighter> create_matching_char_highlighter(HighlighterParameters params, Highlighter*) std::unique_ptr<Highlighter> create_matching_char_highlighter(HighlighterParameters params, Highlighter*)
{ {
return make_highlighter(show_matching_char); ParametersParser parser{params, show_matching_desc.params};
return make_highlighter(parser.get_switch("previous") ? show_matching_char<true> : show_matching_char<false>);
} }
void highlight_selections(HighlightContext context, DisplayBuffer& display_buffer, BufferRange) void highlight_selections(HighlightContext context, DisplayBuffer& display_buffer, BufferRange)

View File

@ -49,6 +49,7 @@ struct {
"» {+b}<a-u>{} and {+b}<a-U>{} now undo selection history\n" "» {+b}<a-u>{} and {+b}<a-U>{} now undo selection history\n"
"» {+u}%exp\\{...}{} expansions provide flexible quoting for expanded " "» {+u}%exp\\{...}{} expansions provide flexible quoting for expanded "
"strings (as double quoted strings)\n" "strings (as double quoted strings)\n"
"» {+u}show-matching -previous{} switch\n"
}, { }, {
20221031, 20221031,
"» {+b}<esc>{} does not end macro recording anymore, use {+b}Q{}\n" "» {+b}<esc>{} does not end macro recording anymore, use {+b}Q{}\n"