Skip to content

Commit

Permalink
feat(match): allow hl group to be defined after :match command
Browse files Browse the repository at this point in the history
  • Loading branch information
bfredl committed Aug 19, 2021
1 parent a9f563a commit fca52f5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions runtime/doc/vim_diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Commands:
|:drop| is always available
|:Man| is available by default, with many improvements such as completion
|:sign-define| accepts a `numhl` argument, to highlight the line number
|:match| can be invoked before highlight group is defined

Events:
|Signal|
Expand All @@ -176,6 +177,7 @@ Functions:
|msgpackdump()|, |msgpackparse()| provide msgpack de/serialization
|stdpath()|
|system()|, |systemlist()| can run {cmd} directly (without 'shell')
|matchadd()| can be called before highlight group is defined

Highlight groups:
|highlight-blend| controls blend level for a highlight group
Expand Down
5 changes: 4 additions & 1 deletion src/nvim/testdir/test_match.vim
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ func Test_match_error()
endfunc

func Test_matchadd_error()
call assert_fails("call matchadd('GroupDoesNotExist', 'X')", 'E28:')
call clearmatches()
" Nvim: not an error anymore:
call matchadd('GroupDoesNotExist', 'X')
call assert_equal([{'group': 'GroupDoesNotExist', 'pattern': 'X', 'priority': 10, 'id': 13}], getmatches())
call assert_fails("call matchadd('Search', '\\(')", 'E475:')
call assert_fails("call matchadd('Search', 'XXX', 1, 123, 1)", 'E715:')
call assert_fails("call matchadd('Error', 'XXX', 1, 3)", 'E798:')
Expand Down
3 changes: 1 addition & 2 deletions src/nvim/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -6534,8 +6534,7 @@ int match_add(win_T *wp, const char *const grp, const char *const pat,
cur = cur->next;
}
}
if ((hlg_id = syn_name2id((const char_u *)grp)) == 0) {
EMSG2(_(e_nogroup), grp);
if ((hlg_id = syn_check_group((const char_u *)grp, strlen(grp))) == 0) {
return -1;
}
if (pat != NULL && (regprog = vim_regcomp((char_u *)pat, RE_MAGIC)) == NULL) {
Expand Down
15 changes: 7 additions & 8 deletions test/functional/eval/match_functions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local clear = helpers.clear
local funcs = helpers.funcs
local command = helpers.command
local exc_exec = helpers.exc_exec
local pcall_err = helpers.pcall_err

before_each(clear)

Expand Down Expand Up @@ -40,13 +39,13 @@ describe('setmatches()', function()
}}, funcs.getmatches())
end)

it('fails with -1 if highlight group is not defined', function()
eq('Vim:E28: No such highlight group name: 1',
pcall_err(funcs.setmatches, {{group=1, pattern=2, id=3, priority=4}}))
eq({}, funcs.getmatches())
eq('Vim:E28: No such highlight group name: 1',
pcall_err(funcs.setmatches, {{group=1, pos1={2}, pos2={6}, id=3, priority=4, conceal=5}}))
eq({}, funcs.getmatches())
it('does not fail if highlight group is not defined', function()
eq(0, funcs.setmatches{{group=1, pattern=2, id=3, priority=4}})
eq({{group='1', pattern='2', id=3, priority=4}},
funcs.getmatches())
eq(0, funcs.setmatches{{group=1, pos1={2}, pos2={6}, id=3, priority=4, conceal=5}})
eq({{group='1', pos1={2}, pos2={6}, id=3, priority=4, conceal='5'}},
funcs.getmatches())
end)
end)

Expand Down

0 comments on commit fca52f5

Please sign in to comment.