test(messages/cmdline_spec): convert highlight IDs to name and format (#34845)

Problem:  Hardcoded highlight IDs for ext_messages/cmdline output need
          to be adjusted everytime a builtin highlight group is added.
Solution: Store a global map of default highlights through nvim_get_hl()
          and fetch missing (custom) highlight groups through synIDattr().
          Use more compact formatting for screen:expect().
This commit is contained in:
luukvbaal
2025-07-09 11:33:19 +02:00
committed by GitHub
parent 3c9484b550
commit 3a3484be29
6 changed files with 1037 additions and 1631 deletions

View File

@@ -81,7 +81,8 @@ end
--- @field cmdline table<integer,table>
--- @field cmdline_hide_level integer?
--- @field cmdline_block table[]
--- @field hl_groups table<string,integer>
--- @field hl_groups table<string,integer> Highlight group to attr ID map
--- @field hl_names table<integer,string> Highlight ID to group map
--- @field messages table<integer,table>
--- @field private _cursor {grid:integer,row:integer,col:integer}
--- @field private _grids table<integer,test.functional.ui.screen.Grid>
@@ -154,6 +155,11 @@ local function _init_colors()
[29] = { foreground = Screen.colors.SlateBlue, bold = true },
[30] = { background = Screen.colors.Red },
}
Screen._global_hl_names = {}
for group in pairs(n.api.nvim_get_hl(0, {})) do
Screen._global_hl_names[n.api.nvim_get_hl_id_by_name(group)] = group
end
end
--- @class test.functional.ui.screen.Opts
@@ -210,6 +216,7 @@ function Screen.new(width, height, options, session)
showcmd = {},
ruler = {},
hl_groups = {},
hl_names = vim.deepcopy(Screen._global_hl_names),
_default_attr_ids = nil,
mouse_enabled = true,
_attrs = {},
@@ -1343,12 +1350,12 @@ function Screen:_handle_cmdline_show(content, pos, firstc, prompt, indent, level
firstc = firstc,
prompt = prompt,
indent = indent,
hl_id = prompt and hl_id,
hl = hl_id,
}
end
function Screen:_handle_cmdline_hide(level, abort)
self.cmdline[level] = { abort = abort }
self.cmdline[level] = abort and { abort = abort } or nil
self.cmdline_hide_level = level
end
@@ -1485,6 +1492,13 @@ function Screen:_row_repr(gridnr, rownr, attr_state, cursor)
return table.concat(rv, '') --:gsub('%s+$', '')
end
local function hl_id_to_name(self, id)
if id and id > 0 and not self.hl_names[id] then
self.hl_names[id] = n.fn.synIDattr(id, 'name')
end
return id and self.hl_names[id] or nil
end
function Screen:_extstate_repr(attr_state)
local cmdline = {}
for i, entry in pairs(self.cmdline) do
@@ -1492,6 +1506,7 @@ function Screen:_extstate_repr(attr_state)
if entry.content ~= nil then
entry.content = self:_chunks_repr(entry.content, attr_state)
end
entry.hl = hl_id_to_name(self, entry.hl)
cmdline[i] = entry
end
@@ -1552,7 +1567,8 @@ function Screen:_chunks_repr(chunks, attr_state)
attrs = hl
end
local attr_id = self:_get_attr_id(attr_state, attrs, hl)
repr_chunks[i] = { text, attr_id, attr_id and id or nil }
repr_chunks[i] = { text, attr_id }
repr_chunks[i][#repr_chunks[i] + 1] = hl_id_to_name(self, id)
end
return repr_chunks
end