Skip to content

Commit

Permalink
feat(menu): add scrollbar
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Jun 19, 2024
1 parent c7c57e3 commit 16ed834
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 4 deletions.
10 changes: 8 additions & 2 deletions docs/config.norg
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ version: 1.1.1
---@class neocomplete.config.ui.menu
@end

It includes some basic window properties like the border and the maximum height of the window.
It includes some basic window properties like the border and the maximum height of the window. It
also has a field to define the character used for the scrollbar.
#tangle
@code lua
--- Maximum height of the menu
---@field max_height integer
--- The border of the completion menu
---@field border string|string[]|string[][]
--- Character used for the scrollbar
---@field scrollbar string
@end

** Position
Expand Down Expand Up @@ -171,13 +174,16 @@ version: 1.1.1
--- Configuration of the completion menu of neocomplete.nvim
---@class neocomplete.config.ui.docs
@end
It consists of window properties like the border and the maximum height of the window.
It consists of some basic window properties like the border and the maximum height of the window.
It also has a field to define the character used for the scrollbar.
#tangle
@code lua
--- Maximum height of the documentation view
---@field max_height integer
--- The border of the documentation view
---@field border string|string[]|string[][]
--- Character used for the scrollbar
---@field scrollbar string
@end

** Type Icons
Expand Down
14 changes: 13 additions & 1 deletion docs/menu.norg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories: [
types
]
created: 2024-05-29T11:30:25+0100
updated: 2024-06-18T19:31:41+0100
updated: 2024-06-19T21:37:01+0100
tangle: {
languages: {
lua: ../lua/neocomplete/types/menu.lua
Expand Down Expand Up @@ -196,3 +196,15 @@ version: 1.1.1
--- Index of selected item
---@field index integer
@end

** Scrollbar
This field is used to store some data for drawing the scrollbar.
#tangle
@code lua
--- Data for the scrollbar
---@field scrollbar neocomplete.scrollbar_data

---@class neocomplete.scrollbar_data
---@field buf integer
---@field win integer
@end
2 changes: 2 additions & 0 deletions lua/neocomplete/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ config.defaults = {
{ { type_icons[entry_kind] or "", ("@neocomplete.type.%s"):format(entry_kind) } },
}
end,
scrollbar = "",
alignment = {},
},
docs_view = {
max_height = 7,
border = "rounded",
scrollbar = "",
},
type_icons = {
Class = "",
Expand Down
1 change: 1 addition & 0 deletions lua/neocomplete/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ hl("@neocomplete.type", { link = "Normal", default = true })
hl("@neocomplete.selected", { link = "Visual", default = true })
hl("@neocomplete.match", { link = "Special", default = true })
hl("@neocomplete.menu", { link = "NormalFloat", default = true })
hl("@neocomplete.scrollbar", { link = "PmenuSbar", default = true })
hl("@neocomplete.entry", { italic = true })
15 changes: 15 additions & 0 deletions lua/neocomplete/menu/draw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ return function(self)
local aligned_table = format_utils.get_align_tables(self.entries)
local column = 0
vim.api.nvim_buf_clear_namespace(self.buf, self.ns, 0, -1)
vim.api.nvim_buf_clear_namespace(self.scrollbar.buf, self.ns, 0, -1)
local spaces = {}
for _ = 1, #self.entries do
table.insert(spaces, (" "):rep(width))
Expand Down Expand Up @@ -93,4 +94,18 @@ return function(self)
vim.api.nvim_buf_add_highlight(self.buf, self.ns, "@neocomplete.match", line - 1, idx - 1, idx)
end
end

local top_visible = vim.fn.line("w0", self.winnr)
local bottom_visible = vim.fn.line("w$", self.winnr)
local visible_entries = bottom_visible - top_visible + 1
local scrollbar_height =
math.max(math.min(math.floor(visible_entries * (visible_entries / #self.entries) + 0.5), visible_entries), 1)
vim.api.nvim_buf_set_lines(self.scrollbar.buf, 0, -1, false, vim.split(string.rep(" ", visible_entries + 1), ""))
local scrollbar_offset = math.max(math.floor(visible_entries * (top_visible / #self.entries)), 1)
for i = scrollbar_offset, scrollbar_offset + scrollbar_height do
vim.api.nvim_buf_set_extmark(self.scrollbar.buf, self.ns, i - 1, 0, {
virt_text = { { self.config.ui.menu.scrollbar, "PmenuSbar" } },
virt_text_pos = "overlay",
})
end
end
16 changes: 16 additions & 0 deletions lua/neocomplete/menu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function Menu.new()
self.buf = vim.api.nvim_create_buf(false, true)
self.winnr = nil
self.index = 0
self.scrollbar = {}
self.scrollbar.win = nil
self.scrollbar.buf = vim.api.nvim_create_buf(false, true)
return self
end

Expand Down Expand Up @@ -54,13 +57,26 @@ function Menu:open_win(offset)
border = self.config.ui.menu.border,
row = position == "below" and 1 or -(height + 2),
col = -offset,
zindex = 1000,
})
vim.wo[self.winnr][self.buf].scrolloff = 0

self.scrollbar.win = vim.api.nvim_open_win(self.scrollbar.buf, false, {
height = height,
relative = "cursor",
col = -offset + width,
row = position == "below" and 2 or -(height + 2) + 1,
width = 1,
style = "minimal",
border = "none",
zindex = 2000,
})
end

function Menu:close()
-- TODO: reset more things?
pcall(vim.api.nvim_win_close, self.winnr, true)
pcall(vim.api.nvim_win_close, self.scrollbar.win, true)
Menu.winnr = nil
end

Expand Down
4 changes: 4 additions & 0 deletions lua/neocomplete/types/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
---@field max_height integer
--- The border of the completion menu
---@field border string|string[]|string[][]
--- Character used for the scrollbar
---@field scrollbar string
--- Position of the menu
---@field position "auto"|"bottom"|"top"
--- How an entry should be formatted
Expand All @@ -34,5 +36,7 @@
---@field max_height integer
--- The border of the documentation view
---@field border string|string[]|string[][]
--- Character used for the scrollbar
---@field scrollbar string
--- The icons used for the different completion item types
---@alias neocomplete.config.ui.type_icons table<string, string>
8 changes: 7 additions & 1 deletion lua/neocomplete/types/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@
--- Window of menu
---@field winnr integer?
--- Index of selected item
---@field index integer
---@field index integer
--- Data for the scrollbar
---@field scrollbar neocomplete.scrollbar_data

---@class neocomplete.scrollbar_data
---@field buf integer
---@field win integer

0 comments on commit 16ed834

Please sign in to comment.