Skip to content

Commit 34cdb01

Browse files
committed
misc: update docs and handle errors
1 parent 9e72344 commit 34cdb01

File tree

4 files changed

+127
-56
lines changed

4 files changed

+127
-56
lines changed

HANDLERS.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Handlers
2+
3+
Satellite provides an API to implement handlers for the scrollbar.
4+
5+
The API for the handler is as follows:
6+
7+
```lua
8+
--- @class Handler
9+
---
10+
--- @field name string
11+
--- Name of the handler
12+
---
13+
--- @field setup fun(config: HandlerConfig, update: fun())
14+
--- Setup the handler and autocmds that are required to trigger the handler.
15+
---
16+
--- @field update fun(bufnr: integer, winid: integer): SatelliteMark[]
17+
--- This function is called when the handler needs to update. It must return
18+
--- a list of SatelliteMark's
19+
---
20+
--- @field enabled fun(): boolean
21+
--- Whether the handler is enabled or not.
22+
```
23+
24+
Handlers can accept any configuration but must also support the following
25+
base class:
26+
27+
```lua
28+
--- @class HandlerConfig
29+
---
30+
--- @field enable boolean
31+
--- Whether the handler is enabled
32+
---
33+
--- @field overlap boolean
34+
--- If `true` decorations are rendered on top of the scrollbar. If `false` the
35+
--- decorations are rendered in a separate column to the right of the scrollbar.
36+
---
37+
--- @field priority integer
38+
--- Priority of the decorations from the handler.
39+
```
40+
41+
The handlers `update()` method returns a list of `SatelliteMark`'s which is defined as:
42+
43+
```lua
44+
--- @class SatelliteMark
45+
---
46+
--- @field pos integer
47+
--- Row of the mark, use `require('satellite.util').row_to_barpos(winid, lnum)`
48+
--- to translate an `lnum` from window `winid` to its respective scrollbar row.
49+
---
50+
--- @field highlight string
51+
--- Highlight group of the mark.
52+
---
53+
--- @field symbol string
54+
--- Symbol of the mark. Must be a single character.
55+
---
56+
--- @field unique boolean
57+
--- By default, for each position in the scrollbar, Satellite will only use the
58+
--- last mark with that position. This field indicates the mark is special and
59+
--- must be rendered even if there is another mark at the same position from the
60+
--- handler.
61+
```
62+
63+
To register a handler call:
64+
```lua
65+
require('satellite.handlers').register(handler)
66+
```
67+
68+
Please see the [cursor handler](lua/satellite/handlers/cursor.lua) as an example.

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# 🚧 WIP and EXPERIMENTAL 🚧
2-
31
# satellite.nvim
42

53
`satellite.nvim` is a Neovim plugin that displays decorated scrollbars.
@@ -8,7 +6,6 @@
86

97
**NOTE**: Many API's required to implement a decorated scrollbar in Neovim do not yet exist,
108
and because of this, this plugin implements fairly unideal and unoptimised workarounds to get desired behaviours.
11-
Therefore, this plugin is highly experimental and currently serves as a platform to experiment, investigate and design the required API's that are needed to be implemented in Neovim core.
129

1310
## Features
1411

@@ -84,7 +81,7 @@ for details.
8481

8582
## Handlers
8683

87-
TODO
84+
Satellite provides an API to implement handlers for the scrollbar, see [HANDLERS](HANDLERS.md) for more details.
8885

8986
## Documentation
9087

lua/satellite/config.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
---@field marks MarksConfig
2323

2424
---@class SatelliteConfig
25-
---@field handlers HandlerConfigs
25+
---@field handlers HandlerConfigs ssss
2626
---@field current_only boolean
2727
---@field winblend integer
2828
---@field zindex integer

lua/satellite/view.lua

+57-51
Original file line numberDiff line numberDiff line change
@@ -83,73 +83,79 @@ end
8383
---@param bbufnr integer
8484
---@param handler Handler
8585
local function render_handler(bufnr, winid, bbufnr, handler)
86-
local name = handler.name
87-
8886
if not handler:enabled() then
8987
return
9088
end
9189

90+
if not api.nvim_buf_is_loaded(bbufnr) then
91+
return
92+
end
93+
94+
local max_pos = api.nvim_buf_line_count(bbufnr) - 1
95+
local name = handler.name
9296
local handler_config = user_config.handlers[name] or {}
9397

9498
api.nvim_buf_clear_namespace(bbufnr, handler.ns, 0, -1)
9599
for _, m in ipairs(handler.update(bufnr, winid)) do
96100
local pos, symbol = m.pos, m.symbol
97101

98-
local opts = {
99-
id = not m.unique and pos + 1 or nil,
100-
priority = handler_config.priority,
101-
}
102-
103-
if handler_config.overlap ~= false then
104-
opts.virt_text = { { symbol, m.highlight } }
105-
opts.virt_text_pos = 'overlay'
106-
opts.hl_mode = 'combine'
107-
else
108-
-- Signs are 2 chars so fill the first char with whitespace
109-
opts.sign_text = ' ' .. symbol
110-
opts.sign_hl_group = m.highlight
111-
end
102+
if pos <= max_pos then
103+
local opts = {
104+
id = not m.unique and pos + 1 or nil,
105+
priority = handler_config.priority,
106+
}
107+
108+
if handler_config.overlap ~= false then
109+
opts.virt_text = { { symbol, m.highlight } }
110+
opts.virt_text_pos = 'overlay'
111+
opts.hl_mode = 'combine'
112+
else
113+
-- Signs are 2 chars so fill the first char with whitespace
114+
opts.sign_text = ' ' .. symbol
115+
opts.sign_hl_group = m.highlight
116+
end
112117

113-
local ok, err = pcall(api.nvim_buf_set_extmark, bbufnr, handler.ns, pos, 0, opts)
114-
if not ok then
115-
print(
116-
string.format(
117-
'error(satellite.nvim): handler=%s buf=%d row=%d opts=%s, err="%s"',
118-
handler.name,
119-
bbufnr,
120-
pos,
121-
vim.inspect(opts, { newline = ' ', indent = '' }),
122-
err
118+
local ok, err = pcall(api.nvim_buf_set_extmark, bbufnr, handler.ns, pos, 0, opts)
119+
if not ok then
120+
print(
121+
string.format(
122+
'error(satellite.nvim): handler=%s buf=%d row=%d opts=%s, err="%s"',
123+
handler.name,
124+
bbufnr,
125+
pos,
126+
vim.inspect(opts, { newline = ' ', indent = '' }),
127+
err
128+
)
123129
)
124-
)
130+
end
125131
end
126132
end
127133
end
128134

129-
---@param winid integer
130-
---@param bar_winid integer
131-
---@param toprow integer
132-
local function reposition_bar(winid, bar_winid, toprow)
133-
local winwidth = api.nvim_win_get_width(winid)
134-
local wininfo = vim.fn.getwininfo(bar_winid)[1]
135-
136-
--- @type integer
137-
local signwidth = wininfo.textoff
138-
139-
local cfg = {
140-
relative = 'win',
141-
win = winid,
142-
row = 0,
143-
col = winwidth - signwidth - 1,
144-
width = 1 + signwidth,
145-
}
146-
147-
api.nvim_win_set_config(bar_winid, cfg)
148-
149-
vim.w[bar_winid].col = cfg.col
150-
vim.w[bar_winid].width = cfg.width
151-
vim.w[bar_winid].row = toprow
152-
end
135+
-----@param winid integer
136+
-----@param bar_winid integer
137+
-----@param toprow integer
138+
--local function reposition_bar(winid, bar_winid, toprow)
139+
-- local winwidth = api.nvim_win_get_width(winid)
140+
-- local wininfo = vim.fn.getwininfo(bar_winid)[1]
141+
142+
-- --- @type integer
143+
-- local signwidth = wininfo.textoff
144+
145+
-- local cfg = {
146+
-- relative = 'win',
147+
-- win = winid,
148+
-- row = 0,
149+
-- col = winwidth - signwidth - 1,
150+
-- width = 1 + signwidth,
151+
-- }
152+
153+
-- api.nvim_win_set_config(bar_winid, cfg)
154+
155+
-- vim.w[bar_winid].col = cfg.col
156+
-- vim.w[bar_winid].width = cfg.width
157+
-- vim.w[bar_winid].row = toprow
158+
--end
153159

154160
---@param bbufnr integer
155161
---@param winid integer

0 commit comments

Comments
 (0)