Meh I'll figure out submodules later

This commit is contained in:
mustard 2025-09-16 01:01:02 +02:00
parent 4ca9d44a90
commit 8cb281f436
352 changed files with 66107 additions and 0 deletions

View file

@ -0,0 +1,100 @@
# NuiLine
NuiLine is an abstraction layer on top of the following native functions:
- `vim.api.nvim_buf_set_lines` (check `:h nvim_buf_set_lines()`)
- `vim.api.nvim_buf_set_text` (check `:h nvim_buf_set_text()`)
- `vim.api.nvim_buf_add_highlight` (check `:h nvim_buf_add_highlight()`)
It helps you create line on the buffer containing multiple [`NuiText`](../text)s.
_Signature:_ `NuiLine(texts?)`
**Example**
```lua
local NuiLine = require("nui.line")
local line = NuiLine()
line:append("Something Went Wrong!", "Error")
local bufnr, ns_id, linenr_start = 0, -1, 1
line:render(bufnr, ns_id, linenr_start)
```
## Parameters
### `texts`
**Type:** `table[]`
List of `NuiText` objects to set as initial texts.
**Example**
```lua
local text_one = NuiText("One")
local text_two = NuiText("Two")
local line = NuiLine({ text_one, text_two })
```
## Methods
### `line:append`
_Signature:_ `line:append(content, highlight?)`
Adds a chunk of content to the line.
**Parameters**
| Name | Type | Description |
| ----------- | -------------------------------- | --------------------- |
| `content` | `string` / `NuiText` / `NuiLine` | content |
| `highlight` | `string` or `table` | highlight information |
If `text` is `string`, these parameters are passed to `NuiText`
and a `NuiText` object is returned.
It `content` is a `NuiText`/`NuiLine` object, it is returned unchanged.
### `line:content`
_Signature:_ `line:content()`
Returns the line content.
### `line:highlight`
_Signature:_ `line:highlight(bufnr, ns_id, linenr)`
Applies highlight for the line.
**Parameters**
| Name | Type | Description |
| -------- | -------- | ---------------------------------------------- |
| `bufnr` | `number` | buffer number |
| `ns_id` | `number` | namespace id (use `-1` for fallback namespace) |
| `linenr` | `number` | line number (1-indexed) |
### `line:render`
_Signature:_ `line:render(bufnr, ns_id, linenr_start, linenr_end?)`
Sets the line on buffer and applies highlight.
**Parameters**
| Name | Type | Description |
| -------------- | -------- | ---------------------------------------------- |
| `bufnr` | `number` | buffer number |
| `ns_id` | `number` | namespace id (use `-1` for fallback namespace) |
| `linenr_start` | `number` | start line number (1-indexed) |
| `linenr_end` | `number` | end line number (1-indexed) |
## Wiki Page
You can find additional documentation/examples/guides/tips-n-tricks in [nui.line wiki page](https://github.com/MunifTanjim/nui.nvim/wiki/nui.line).

View file

@ -0,0 +1,80 @@
local Object = require("nui.object")
local NuiText = require("nui.text")
local defaults = require("nui.utils").defaults
---@class NuiLine
---@field _texts NuiText[]
local Line = Object("NuiLine")
---@param texts? NuiText[]
function Line:init(texts)
self._texts = defaults(texts, {})
end
---@param content string|NuiText|NuiLine
---@param highlight? string|nui_text_extmark data for highlight
---@return NuiText|NuiLine
function Line:append(content, highlight)
local block = content
if type(block) == "string" then
block = NuiText(block, highlight)
end
if block._texts then
---@cast block NuiLine
for _, text in ipairs(block._texts) do
table.insert(self._texts, text)
end
else
---@cast block NuiText
table.insert(self._texts, block)
end
return block
end
---@return string
function Line:content()
return table.concat(vim.tbl_map(function(text)
return text:content()
end, self._texts))
end
---@return number
function Line:width()
local width = 0
for _, text in ipairs(self._texts) do
width = width + text:width()
end
return width
end
---@param bufnr number buffer number
---@param ns_id number namespace id
---@param linenr number line number (1-indexed)
---@param ___byte_start___? integer start byte position (0-indexed)
---@return nil
function Line:highlight(bufnr, ns_id, linenr, ___byte_start___)
local current_byte_start = ___byte_start___ or 0
for _, text in ipairs(self._texts) do
text:highlight(bufnr, ns_id, linenr, current_byte_start)
current_byte_start = current_byte_start + text:length()
end
end
---@param bufnr number buffer number
---@param ns_id number namespace id
---@param linenr_start number start line number (1-indexed)
---@param linenr_end? number end line number (1-indexed)
---@return nil
function Line:render(bufnr, ns_id, linenr_start, linenr_end)
local row_start = linenr_start - 1
local row_end = linenr_end and linenr_end - 1 or row_start + 1
local content = self:content()
vim.api.nvim_buf_set_lines(bufnr, row_start, row_end, false, { content })
self:highlight(bufnr, ns_id, linenr_start)
end
---@alias NuiLine.constructor fun(texts?: NuiText[]): NuiLine
---@type NuiLine|NuiLine.constructor
local NuiLine = Line
return NuiLine