docs(api): document nvim_buf_add_highlight vs nvim_buf_set_extmark (#23310)

This commit is contained in:
JP 2023-04-30 06:02:38 -03:00 committed by GitHub
parent 668f16bac7
commit 4f3f81ed86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -400,6 +400,23 @@ external highlighter plugin wants to add many highlights in a batch,
performance can be improved by calling |nvim_buf_add_highlight()| as an performance can be improved by calling |nvim_buf_add_highlight()| as an
asynchronous notification, after first (synchronously) requesting a source id. asynchronous notification, after first (synchronously) requesting a source id.
|nvim_buf_add_highlight()| adds highlights as |extmarks|. If highlights need to
be tracked or manipulated after adding them, it is better to use
|nvim_buf_set_extmark()| directly, as this function returns the placed |extmark|
id. Thus, instead of >lua
vim.api.nvim_buf_add_highlight(buf, ns_id, hl_group, line, col_start, col_end)
<
use >lua
-- create the highlight through an extmark
extid = vim.api.nvim_buf_set_extmark(buf, ns_id, hl_group, line, col_start, {end_col = col_end, hl_group = hl_group})
-- example: modify the extmark's highlight group
vim.api.nvim_buf_set_extmark(buf, ns_id, NEW_HL_GROUP, line, col_start, {end_col = col_end, hl_group = hl_group, id = extid})
-- example: change the highlight's position
vim.api.nvim_buf_set_extmark(buf, ns_id, hl_group, NEW_LINE, col_start, {end_col = col_end, hl_group = hl_group, id = extid})
<
Example using the Python API client (|pynvim|): Example using the Python API client (|pynvim|):
>python >python
src = vim.new_highlight_source() src = vim.new_highlight_source()