Skip to content

Commit c76d23b

Browse files
committed
feat: sidebars on rhs of editor
1 parent e5a2eab commit c76d23b

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

lua/cokeline/hover.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ function M.get_current(col)
3939
local cx = rendering.prepare(bufs)
4040

4141
local current_width = 0
42-
for _, component in ipairs(cx.sidebar) do
42+
for _, component in ipairs(cx.sidebar_left) do
4343
current_width = current_width + component.width
4444
if current_width >= col then
45-
return component, cx.sidebar
45+
return component, cx.sidebar_left
4646
end
4747
end
4848
if
@@ -81,6 +81,12 @@ function M.get_current(col)
8181
end
8282
end
8383
end
84+
for _, component in ipairs(cx.sidebar_right) do
85+
current_width = current_width + component.width
86+
if current_width >= col then
87+
return component, cx.sidebar_right
88+
end
89+
end
8490
end
8591

8692
local function mouse_leave(hovered)
@@ -89,6 +95,10 @@ local function mouse_leave(hovered)
8995
cx = buffers.get_buffer(hovered.bufnr)
9096
elseif hovered.kind == "tab" then
9197
cx = tabs.get_tabpage(hovered.bufnr)
98+
elseif hovered.kind == "sidebar" then
99+
cx = { number = hovered.bufnr, side = hovered.sidebar }
100+
else
101+
cx = {}
92102
end
93103
if cx then
94104
cx.is_hovered = false
@@ -107,6 +117,10 @@ local function mouse_enter(component, current)
107117
cx = buffers.get_buffer(component.bufnr)
108118
elseif component.kind == "tab" then
109119
cx = tabs.get_tabpage(component.bufnr)
120+
elseif component.kind == "sidebar" then
121+
cx = { number = component.bufnr, side = component.sidebar }
122+
else
123+
cx = {}
110124
end
111125
if cx then
112126
cx.is_hovered = true

lua/cokeline/rendering.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ end
9797
---@param visible_buffers Buffer[]
9898
---@return table|string
9999
local prepare = function(visible_buffers)
100-
local sidebar_components = sidebar.get_components()
100+
local sidebar_components_l = sidebar.get_components("left")
101+
local sidebar_components_r = sidebar.get_components("right")
101102
local rhs_components = rhs.get_components()
102-
local available_width = o.columns - components.width(sidebar_components)
103+
local available_width = o.columns - components.width(sidebar_components_l)
103104
if available_width == 0 then
104-
return components.render(sidebar_components)
105+
return components.render(sidebar_components_l)
105106
end
106107

107108
local tab_placement
@@ -113,7 +114,7 @@ local prepare = function(visible_buffers)
113114
tabs_width = components.width(tab_components)
114115
available_width = available_width - tabs_width
115116
if available_width == 0 then
116-
return components.render(sidebar_components)
117+
return components.render(sidebar_components_l)
117118
.. components.render(tab_components)
118119
end
119120
end
@@ -135,7 +136,8 @@ local prepare = function(visible_buffers)
135136
end
136137

137138
return {
138-
sidebar = sidebar_components,
139+
sidebar_left = sidebar_components_l,
140+
sidebar_right = sidebar_components_r,
139141
buffers = current_components,
140142
rhs = rhs_components,
141143
tabs = tab_components,
@@ -159,6 +161,7 @@ local prepare = function(visible_buffers)
159161
}, _G.cokeline.components)
160162

161163
local rhs_width = components.width(rhs_components)
164+
+ components.width(sidebar_components_r)
162165
local available_width_left, available_width_right =
163166
_G.cokeline.config.rendering.slider(
164167
available_width
@@ -194,7 +197,8 @@ local prepare = function(visible_buffers)
194197

195198
local bufs_width = components.width(buffer_components)
196199
return {
197-
sidebar = sidebar_components,
200+
sidebar_left = sidebar_components_l,
201+
sidebar_right = sidebar_components_r,
198202
buffers = buffer_components,
199203
rhs = rhs_components,
200204
tabs = tab_components,
@@ -210,7 +214,7 @@ end
210214
---@return string
211215
local render = function(visible_buffers, fill_hl)
212216
local cx = prepare(visible_buffers)
213-
local rendered = components.render(cx.sidebar) .. "%#" .. fill_hl .. "#"
217+
local rendered = components.render(cx.sidebar_left) .. "%#" .. fill_hl .. "#"
214218
if
215219
_G.cokeline.config.tabs and _G.cokeline.config.tabs.placement == "left"
216220
then
@@ -228,6 +232,7 @@ local render = function(visible_buffers, fill_hl)
228232
then
229233
rendered = rendered .. "%#" .. fill_hl .. "#" .. components.render(cx.tabs)
230234
end
235+
rendered = rendered .. components.render(cx.sidebar_right)
231236
return rendered
232237
end
233238

lua/cokeline/sidebar.lua

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ local bo = vim.bo
1212
local fn = vim.fn
1313
local o = vim.o
1414

15-
---@return Component<SidebarContext>[]
16-
local get_components = function()
17-
if not _G.cokeline.config.sidebar then
18-
return {}
19-
end
20-
21-
local hover = require("cokeline/hover").hovered()
15+
local get_win = function(side)
2216
local layout = fn.winlayout()
2317

2418
-- If the first split level is not given by vertically split windows we
2519
-- return early.
2620
if layout[1] ~= "row" then
27-
return {}
21+
return nil
2822
end
2923

3024
-- The second element of the `layout` table is a nested list representing the
@@ -48,16 +42,45 @@ local get_components = function()
4842
--
4943
-- However, with edgy.nvim, a sidebar can contain multiple splits nested one level in.
5044
-- So if the first window found is a leaf, we check the first window of the container.
51-
local first_split = window_tree[1]
52-
local winid = first_split[2]
53-
if first_split[1] ~= "leaf" then
54-
local win = first_split[2][1]
55-
if win and win[1] == "leaf" then
56-
winid = win[2]
57-
else
58-
return {}
45+
local first_split
46+
local winid
47+
if side == nil or side == "left" then
48+
first_split = window_tree[1]
49+
winid = first_split[2]
50+
if first_split[1] ~= "leaf" then
51+
local win = first_split[2][1]
52+
if win and win[1] == "leaf" then
53+
winid = win[2]
54+
else
55+
return nil
56+
end
57+
end
58+
else
59+
first_split = window_tree[#window_tree]
60+
winid = first_split[2]
61+
if first_split[1] ~= "leaf" then
62+
local win = first_split[2][#first_split[2]]
63+
if win and win[1] == "leaf" then
64+
winid = win[2]
65+
else
66+
return nil
67+
end
5968
end
6069
end
70+
return winid
71+
end
72+
73+
---@param side "left" | "right"
74+
---@return Component<SidebarContext>[]
75+
local get_components = function(side)
76+
if not _G.cokeline.config.sidebar then
77+
return {}
78+
end
79+
80+
local winid = get_win(side)
81+
if not winid then
82+
return {}
83+
end
6184

6285
local bufnr = api.nvim_win_get_buf(winid)
6386

@@ -85,8 +108,12 @@ local get_components = function()
85108
local sidebar_components = {}
86109
local width = 0
87110
local id = #_G.cokeline.components + #_G.cokeline.rhs + 1
111+
local hover = require("cokeline/hover").hovered()
88112
for _, c in ipairs(_G.cokeline.sidebar) do
89-
buffer.is_hovered = hover and hover.index == id
113+
c.sidebar = side
114+
buffer.is_hovered = hover ~= nil
115+
and hover.index == id
116+
and hover.bufnr == buffer.number
90117
local component = c:render(RenderContext:buffer(buffer))
91118
buffer.is_hovered = false
92119
-- We need at least one component, otherwise we can't add padding to the
@@ -117,5 +144,6 @@ local get_components = function()
117144
end
118145

119146
return {
147+
get_win = get_win,
120148
get_components = get_components,
121149
}

0 commit comments

Comments
 (0)