Back to Spacevim

NuiText

bundle/nui.nvim/lua/nui/text/README.md

2.4.04.5 KB
Original Source

NuiText

NuiText is an abstraction layer on top of the following native functions:

  • vim.api.nvim_buf_set_text (check :h nvim_buf_set_text())
  • vim.api.nvim_buf_set_extmark (check :h nvim_buf_set_extmark())

It helps you set text and add highlight for it on the buffer.

Signature: NuiText(content, extmark?)

Examples

lua
local NuiText = require("nui.text")

local text = NuiText("Something Went Wrong!", "Error")

local bufnr, ns_id, linenr_start, byte_start = 0, -1, 1, 0

text:render(bufnr, ns_id, linenr_start, byte_start)

Parameters

content

Type: string or table

Text content or NuiText object.

If NuiText object is passed, a copy of it is created.

extmark

Type: string or table

Highlight group name or extmark options.

If a string is passed, it is used as the highlight group name.

If a table is passed it is used as extmark data. It can have the following keys:

KeyDescription
"hl_group"highlight group name

For more, check :help nvim_buf_set_extmark().

Methods

text:set

Signature: text:set(content, extmark?)

Sets the text content and highlight information.

Parameters

NameTypeDescription
contentstringtext content
extmarkstring or tablehighlight group name or extmark options

This extmark parameter is exactly the same as NuiText's extmark parameter.

text:content

Signature: text:content()

Returns the text content.

text:length

Signature: text:length()

Returns the byte length of the text.

text:width

Signature: text:width()

Returns the character length of the text.

text:highlight

Signature: text:highlight(bufnr, ns_id, linenr, byte_start)

Applies highlight for the text.

Parameters

NameTypeDescription
bufnrnumberbuffer number
ns_idnumbernamespace id (use -1 for fallback namespace)
linenrnumberline number (1-indexed)
byte_startnumberstart position of the text on the line (0-indexed)

text:render

Signature: text:render(bufnr, ns_id, linenr_start, byte_start, linenr_end?, byte_end?)

Sets the text on buffer and applies highlight.

Parameters

NameTypeDescription
bufnrnumberbuffer number
ns_idnumbernamespace id (use -1 for fallback namespace)
linenr_startnumberstart line number (1-indexed)
byte_startnumberstart position of the text on the line (0-indexed)
linenr_endnumberend line number (1-indexed)
byte_endnumberend position of the text on the line (0-indexed)

text:render_char

Signature: text:render_char(bufnr, ns_id, linenr_start, char_start, linenr_end?, char_end?)

Sets the text on buffer and applies highlight.

This does the thing as text:render method, but you can use character count instead of byte count. It will convert multibyte character count to appropriate byte count for you.

Parameters

NameTypeDescription
bufnrnumberbuffer number
ns_idnumbernamespace id (use -1 for fallback namespace)
linenr_startnumberstart line number (1-indexed)
char_startnumberstart position of the text on the line (0-indexed)
linenr_endnumberend line number (1-indexed)
char_endnumberend position of the text on the line (0-indexed)

Wiki Page

You can find additional documentation/examples/guides/tips-n-tricks in nui.text wiki page.