aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglepnir <glephunter@gmail.com>2023-09-07 18:42:38 +0800
committerglepnir <glephunter@gmail.com>2023-09-09 17:15:58 +0800
commit8afb3a49c0762eb60368aee0314e6de261daa6ef (patch)
treebc954a00873a049f5a3a93cd532f3185d2f8cc38
parent82150ca51bf396e5795b49f6e1cb2faaaa07b28b (diff)
downloadrneovim-8afb3a49c0762eb60368aee0314e6de261daa6ef.tar.gz
rneovim-8afb3a49c0762eb60368aee0314e6de261daa6ef.tar.bz2
rneovim-8afb3a49c0762eb60368aee0314e6de261daa6ef.zip
fix(highlight): add create param in nvim_get_hl
-rw-r--r--runtime/doc/api.txt2
-rw-r--r--runtime/lua/vim/_meta/api.lua2
-rw-r--r--runtime/lua/vim/_meta/api_keysets.lua1
-rw-r--r--src/nvim/api/keysets.h1
-rw-r--r--src/nvim/api/vim.c1
-rw-r--r--src/nvim/highlight_group.c8
-rw-r--r--test/functional/api/highlight_spec.lua9
7 files changed, 23 insertions, 1 deletions
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt
index e68aafe051..7dd760b6a5 100644
--- a/runtime/doc/api.txt
+++ b/runtime/doc/api.txt
@@ -966,6 +966,8 @@ nvim_get_hl({ns_id}, {*opts}) *nvim_get_hl()*
• id: (integer) Get a highlight definition by id.
• link: (boolean, default true) Show linked group name
instead of effective definition |:hi-link|.
+ • create: (boolean, default true) When highlight group
+ doesn't exist create it.
Return: ~
Highlight groups as a map from group name to a highlight definition
diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua
index dd67fdb38b..c46b604b90 100644
--- a/runtime/lua/vim/_meta/api.lua
+++ b/runtime/lua/vim/_meta/api.lua
@@ -1193,6 +1193,8 @@ function vim.api.nvim_get_current_win() end
--- • id: (integer) Get a highlight definition by id.
--- • link: (boolean, default true) Show linked group name
--- instead of effective definition `:hi-link`.
+--- • create: (boolean, default true) When highlight group
+--- doesn't exist create it.
--- @return table<string,any>
function vim.api.nvim_get_hl(ns_id, opts) end
diff --git a/runtime/lua/vim/_meta/api_keysets.lua b/runtime/lua/vim/_meta/api_keysets.lua
index 08c29ebe7a..8f36edba77 100644
--- a/runtime/lua/vim/_meta/api_keysets.lua
+++ b/runtime/lua/vim/_meta/api_keysets.lua
@@ -126,6 +126,7 @@ error('Cannot require a meta file')
--- @field id? integer
--- @field name? string
--- @field link? boolean
+--- @field create? boolean
--- @class vim.api.keyset.highlight
--- @field bold? boolean
diff --git a/src/nvim/api/keysets.h b/src/nvim/api/keysets.h
index 1f5c7069a9..0a07e8c16f 100644
--- a/src/nvim/api/keysets.h
+++ b/src/nvim/api/keysets.h
@@ -181,6 +181,7 @@ typedef struct {
Integer id;
String name;
Boolean link;
+ Boolean create;
} Dict(get_highlight);
typedef struct {
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 411d63b921..da10ab5bd4 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -96,6 +96,7 @@ Integer nvim_get_hl_id_by_name(String name)
/// - name: (string) Get a highlight definition by name.
/// - id: (integer) Get a highlight definition by id.
/// - link: (boolean, default true) Show linked group name instead of effective definition |:hi-link|.
+/// - create: (boolean, default true) When highlight group doesn't exist create it.
///
/// @param[out] err Error details, if any.
/// @return Highlight groups as a map from group name to a highlight definition map as in |nvim_set_hl()|,
diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c
index c4d140d1e1..b970e752bb 100644
--- a/src/nvim/highlight_group.c
+++ b/src/nvim/highlight_group.c
@@ -1569,7 +1569,13 @@ Dictionary ns_get_hl_defs(NS ns_id, Dict(get_highlight) *opts, Arena *arena, Err
Boolean link = GET_BOOL_OR_TRUE(opts, get_highlight, link);
int id = -1;
if (HAS_KEY(opts, get_highlight, name)) {
- id = syn_check_group(opts->name.data, opts->name.size);
+ Boolean create = GET_BOOL_OR_TRUE(opts, get_highlight, create);
+ id = create ? syn_check_group(opts->name.data, opts->name.size)
+ : syn_name2id_len(opts->name.data, opts->name.size);
+ if (id == 0 && !create) {
+ Dictionary attrs = ARRAY_DICT_INIT;
+ return attrs;
+ }
} else if (HAS_KEY(opts, get_highlight, id)) {
id = (int)opts->id;
}
diff --git a/test/functional/api/highlight_spec.lua b/test/functional/api/highlight_spec.lua
index 5fa2235018..492fd73223 100644
--- a/test/functional/api/highlight_spec.lua
+++ b/test/functional/api/highlight_spec.lua
@@ -439,6 +439,15 @@ describe('API: get highlight', function()
eq('Highlight id out of bounds', pcall_err(meths.get_hl, 0, { name = 'Test set hl' }))
end)
+ it('nvim_get_hl with create flag', function()
+ eq({}, nvim("get_hl", 0, {name = 'Foo', create = false}))
+ eq(0, funcs.hlexists('Foo'))
+ meths.get_hl(0, {name = 'Bar', create = true})
+ eq(1, funcs.hlexists('Bar'))
+ meths.get_hl(0, {name = 'FooBar'})
+ eq(1, funcs.hlexists('FooBar'))
+ end)
+
it('can get all highlights in current namespace', function()
local ns = get_ns()
meths.set_hl(ns, 'Test_hl', { bg = '#B4BEFE' })