-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from grapp-dev/feature/buffer-component
feat: add Buffer component
- Loading branch information
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Callout } from 'nextra/components'; | ||
import { Property } from '../../../components/Property' | ||
|
||
## Buffer | ||
|
||
`Buffer` is a component that handles window layout only and renders the contents of a given buffer. | ||
|
||
![](/gifs/buffer-1.gif) | ||
|
||
### Usage Example | ||
|
||
<Callout type="info"> | ||
The code example is using the [`gen.nvim`](https://github.com/David-Kunz/gen.nvim) plugin. | ||
</Callout> | ||
|
||
```lua | ||
local renderer = n.create_renderer({ | ||
width = 60, | ||
height = 3, | ||
}) | ||
|
||
local signal = n.create_signal({ | ||
chat = "", | ||
is_preview_visible = false, | ||
}) | ||
|
||
local buf = vim.api.nvim_create_buf(false, true) | ||
|
||
local body = function() | ||
return n.rows( | ||
n.text_input({ | ||
border_label = "Chat", | ||
autofocus = true, | ||
wrap = true, | ||
on_change = function(value) | ||
signal.chat = value | ||
end, | ||
}), | ||
n.buffer({ | ||
id = "preview", | ||
flex = 1, | ||
buf = buf, | ||
autoscroll = true, | ||
border_label = "Preview", | ||
hidden = signal.is_preview_visible:negate(), | ||
}) | ||
) | ||
end | ||
|
||
renderer:add_mappings({ | ||
{ | ||
mode = { "n", "i" }, | ||
key = "<D-CR>", | ||
handler = function() | ||
local gen = require("gen") | ||
local state = signal:get_value() | ||
|
||
renderer:set_size({ height = 20 }) | ||
signal.is_preview_visible = true | ||
|
||
renderer:schedule(function() | ||
gen.float_win = renderer:get_component_by_id("preview").winid | ||
gen.result_buffer = buf | ||
gen.exec({ prompt = state.chat }) | ||
end) | ||
end, | ||
}, | ||
}) | ||
|
||
renderer:render(body) | ||
``` | ||
|
||
### Properties | ||
|
||
#### buf | ||
|
||
<Property | ||
required={true} | ||
types={['number']} | ||
/> | ||
|
||
#### autoscroll | ||
|
||
<Property | ||
defaultValue="false" | ||
types={['boolean']} | ||
/> | ||
|
||
#### filetype | ||
|
||
<Property | ||
types={['string']} | ||
/> | ||
|
||
#### linebreak | ||
|
||
<Property | ||
defaultValue="true" | ||
types={['boolean']} | ||
/> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
local Component = require("nui-components.component") | ||
local buf_storage = require("nui.utils.buf_storage") | ||
|
||
local fn = require("nui-components.utils.fn") | ||
|
||
local Buffer = Component:extend("Buffer") | ||
|
||
function Buffer:init(props, popup_options) | ||
Buffer.super.init( | ||
self, | ||
fn.merge({ | ||
buf = nil, | ||
autoscroll = false, | ||
}, props), | ||
fn.deep_merge({ | ||
buf_options = { | ||
filetype = props.filetype or "", | ||
}, | ||
win_options = { | ||
linebreak = fn.default_to(props.linebreak, true), | ||
}, | ||
}, popup_options) | ||
) | ||
end | ||
|
||
function Buffer:prop_types() | ||
return { | ||
buf = "number", | ||
autoscroll = "boolean", | ||
filetype = { "string", "nil" }, | ||
} | ||
end | ||
|
||
function Buffer:_buf_create() | ||
local props = self:get_props() | ||
|
||
self.bufnr = props.buf | ||
|
||
if props.autoscroll then | ||
self._private.autoscroll_id = vim.api.nvim_create_autocmd("TextChanged", { | ||
buffer = self.bufnr, | ||
callback = function() | ||
if not self.winid or not vim.api.nvim_win_is_valid(self.winid) then | ||
return | ||
end | ||
|
||
if not self.bufnr or not vim.api.nvim_buf_is_valid(self.bufnr) then | ||
return true | ||
end | ||
|
||
vim.api.nvim_win_set_cursor(self.winid, { | ||
vim.api.nvim_buf_line_count(self.bufnr), | ||
9999, | ||
}) | ||
end, | ||
}) | ||
end | ||
end | ||
|
||
function Buffer:_buf_destroy() | ||
buf_storage.cleanup(self.bufnr) | ||
|
||
if self._private.autoscroll_id then | ||
pcall(vim.api.nvim_del_autocmd, self._private.autoscroll_id) | ||
self._private.autoscroll_id = nil | ||
end | ||
|
||
self.bufnr = nil | ||
end | ||
|
||
function Buffer:on_update() | ||
local props = self:get_props() | ||
|
||
if props.buf and self.bufnr ~= props.buf and self._.layout_ready then | ||
local is_focused = vim.api.nvim_get_current_win() == self.winid | ||
|
||
self:unmount() | ||
self:mount() | ||
|
||
if is_focused then | ||
vim.api.nvim_set_current_win(self.winid) | ||
end | ||
end | ||
end | ||
|
||
return Buffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters