Skip to content

Commit f770ace

Browse files
authored
Merge pull request #77 from francescarpi/develop
release: v2025-03-13
2 parents ba5e50e + 38264c8 commit f770ace

File tree

9 files changed

+72
-28
lines changed

9 files changed

+72
-28
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Take a look at the default shortcuts for navigating between buffers, changing th
128128
move_buffer_top = "<s-t>",
129129
move_buffer_bottom = "<s-b>",
130130
toggle_buffon_window = "<buffonleader>n",
131+
--- Toggle window position allows moving the main window position
132+
--- between top-right and bottom-right positions
133+
toggle_buffon_window_position = "<buffonleader>nn",
131134
switch_previous_used_buffer = "<buffonleader><buffonleader>",
132135
close_buffer = "<buffonleader>d",
133136
close_buffers_above = "<buffonleader>v",

lua/buffon/config.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ local default = {
2525
move_buffer_top = "<s-t>",
2626
move_buffer_bottom = "<s-b>",
2727
toggle_buffon_window = "<buffonleader>n",
28+
--- Toggle window position allows moving the main window position
29+
--- between top-right and bottom-right positions
30+
toggle_buffon_window_position = "<buffonleader>nn",
2831
switch_previous_used_buffer = "<buffonleader><buffonleader>",
2932
close_buffer = "<buffonleader>d",
3033
close_buffers_above = "<buffonleader>v",
@@ -52,6 +55,7 @@ local default = {
5255
---@field move_buffer_top string|false
5356
---@field move_buffer_bottom string|false
5457
---@field toggle_buffon_window string
58+
---@field toggle_buffon_window_position string
5559
---@field switch_previous_used_buffer string|false
5660
---@field close_buffer string|false
5761
---@field close_buffers_above string|false

lua/buffon/maincontroller.lua

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ function MainController:get_shortcuts()
9393
help = "Show/hide buffer list",
9494
method = self.action_show_hide_buffon_window,
9595
},
96+
{
97+
shortcut = "toggle_buffon_window_position",
98+
help = "Toggle window position",
99+
method = self.action_toggle_window_position,
100+
},
96101
{
97102
shortcut = "goto_next_buffer",
98103
help = "Next buffer",
@@ -309,6 +314,10 @@ function MainController:action_show_hide_buffon_window()
309314
self.main_window:toggle()
310315
end
311316

317+
function MainController:action_toggle_window_position()
318+
self.main_window.window:toggle_position_between_top_right_bottom_right()
319+
end
320+
312321
function MainController:event_add_buffer(buf)
313322
local existent_buf, num_page = self.page_controller:get_buffer_and_page(buf.match)
314323
log.debug("add", vim.fn.fnamemodify(buf.match, ":t"), "in page", num_page)
@@ -523,12 +532,19 @@ end
523532
--------------------------------------------------------------------------------------------
524533

525534
function MainController:event_buffer_will_rename(buf)
535+
if buf.match == "" then
536+
return
537+
end
538+
526539
log.debug("buffer will be renamed", vim.fn.fnamemodify(buf.match, ":t"))
527540
self.buffer_will_be_renamed = buf.match
528541
end
529542

530543
function MainController:event_rename_buffer(buf)
531-
assert(self.buffer_will_be_renamed, "new buffer name is required")
544+
if not self.buffer_will_be_renamed or self.buffer_will_be_renamed == buf.match then
545+
return
546+
end
547+
532548
log.debug("set new name", vim.fn.fnamemodify(buf.match, ":t"))
533549
for _, page in ipairs(self.page_controller.pages) do
534550
page.bufferslist:rename(self.buffer_will_be_renamed, buf.match)

lua/buffon/page.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,29 @@ function Page:render(active_buffer)
117117
},
118118
}
119119
local filenames = self:_get_filenames(self.bufferslist.buffers)
120+
local max_length = utils.calc_max_length(filenames)
120121

121122
for idx, buffer in ipairs(self.bufferslist.buffers) do
122123
-- content
123124
local shortcut = self:_get_shortcut(idx)
124125
local filename = filenames[idx]
125126
local modified = self:_get_modified(buffer)
126127
local icon = self:_get_icon(buffer.filename)
127-
local line = string.format("%s %s %s%s", shortcut, filename, icon, modified)
128+
129+
local diff_file_length = max_length - vim.fn.strdisplaywidth(filename)
130+
local spaces = string.rep(" ", diff_file_length + 1)
131+
if diff_file_length == 0 then
132+
spaces = " "
133+
end
134+
135+
local line = string.format("%s %s%s%s%s", shortcut, filename, spaces, icon, modified)
128136
table.insert(response.content, line)
129137

130138
-- highlights
131139
local shortcut_start = 0
132140
local shortcut_end = shortcut_start + #self.config.leader_key + CHAR
133141
local filename_start = shortcut_end + WHITESPACE
134-
local filename_end = filename_start + #filename + WHITESPACE + ICON
142+
local filename_end = filename_start + #filename + #spaces + WHITESPACE + ICON
135143
local modified_start = filename_end + WHITESPACE
136144
local modified_end = modified_start + MODIFIED
137145

lua/buffon/ui/window.lua

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local log = require("buffon.log")
2+
local utils = require("buffon.utils")
23

34
local M = {}
45

@@ -114,19 +115,6 @@ function Window:set_highlight(highlights)
114115
end
115116
end
116117

117-
---@return number
118-
function Window:get_max_width()
119-
local lines = vim.api.nvim_buf_get_lines(self.buf_id, 0, -1, false)
120-
local max_width = 0
121-
for _, line in ipairs(lines) do
122-
local line_length = vim.fn.strdisplaywidth(line)
123-
if line_length > max_width then
124-
max_width = line_length
125-
end
126-
end
127-
return max_width
128-
end
129-
130118
function Window:refresh_dimensions()
131119
if not self.win_id then
132120
return
@@ -136,7 +124,7 @@ function Window:refresh_dimensions()
136124
local editor_lines = vim.api.nvim_get_option("lines")
137125
local lines = vim.api.nvim_buf_get_lines(self.buf_id, 0, -1, false)
138126
local height = #lines
139-
local max_width = self:get_max_width()
127+
local max_width = utils.calc_max_length(vim.api.nvim_buf_get_lines(self.buf_id, 0, -1, false))
140128
local row = 1
141129
local col = 0
142130

@@ -163,6 +151,14 @@ function Window:refresh_dimensions()
163151
vim.api.nvim_win_set_config(self.win_id, cfg)
164152
end
165153

154+
function Window:toggle_position_between_top_right_bottom_right()
155+
if self.position == WIN_POSITION.TOP_RIGHT then
156+
self.position = WIN_POSITION.BOTTOM_RIGHT
157+
else
158+
self.position = WIN_POSITION.TOP_RIGHT
159+
end
160+
end
161+
166162
M.Window = Window
167163
M.WIN_POSITIONS = WIN_POSITION
168164

lua/buffon/utils.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ M.replace_leader = function(cfg, shortcut)
1919
return replaced_string
2020
end
2121

22+
---@param lines table<string>
23+
---@return number
24+
M.calc_max_length = function(lines)
25+
local max_length = 0
26+
for _, line in ipairs(lines) do
27+
local line_length = vim.fn.strdisplaywidth(line)
28+
if line_length > max_length then
29+
max_length = line_length
30+
end
31+
end
32+
return max_length
33+
end
34+
2235
---@class BuffonRecentlyClosed
2336
---@field filenames? table<string>
2437
---@field limit? number

tests/config_spec.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe("config", function()
2525
move_buffer_top = "<s-t>",
2626
move_buffer_bottom = "<s-b>",
2727
toggle_buffon_window = "<buffonleader>n",
28+
toggle_buffon_window_position = "<buffonleader>nn",
2829
switch_previous_used_buffer = "<buffonleader><buffonleader>",
2930
close_buffer = "<buffonleader>d",
3031
close_buffers_above = "<buffonleader>v",

tests/maincontroller_spec.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ end
1515

1616
local default_shortcuts = {
1717
"toggle_buffon_window",
18+
"toggle_buffon_window_position",
1819
"goto_next_buffer",
1920
"goto_previous_buffer",
2021
"next_page",
@@ -60,6 +61,7 @@ describe("maincontrolelr", function()
6061
local shortcuts = ctrl:get_shortcuts()
6162
compare_shortcuts(shortcuts, {
6263
"toggle_buffon_window",
64+
"toggle_buffon_window_position",
6365
"goto_next_buffer",
6466
"goto_previous_buffer",
6567
"next_page",
@@ -96,6 +98,7 @@ describe("maincontrolelr", function()
9698
local shortcuts = ctrl:get_shortcuts()
9799
compare_shortcuts(shortcuts, {
98100
"toggle_buffon_window",
101+
"toggle_buffon_window_position",
99102
"goto_next_buffer",
100103
"goto_previous_buffer",
101104
"move_buffer_up",

tests/page_spec.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ describe("page", function()
2323
---@type BuffonPageRender
2424
local render = pag:render("/foo/buffer2.lua")
2525
eq(render.content, {
26-
";q buffer1.lua ",
27-
";w buffer2.lua ",
28-
";e buffer3.lua ",
29-
";r buffer4.lua ",
30-
";y buffer5.lua ",
31-
";u buffer6.lua ",
32-
";i buffer7.lua ",
33-
";o buffer8.lua ",
34-
";p buffer9.lua ",
26+
";q buffer1.lua ",
27+
";w buffer2.lua ",
28+
";e buffer3.lua ",
29+
";r buffer4.lua ",
30+
";y buffer5.lua ",
31+
";u buffer6.lua ",
32+
";i buffer7.lua ",
33+
";o buffer8.lua ",
34+
";p buffer9.lua ",
3535
" buffer10.lua ",
3636
})
3737

@@ -87,15 +87,15 @@ describe("page", function()
8787

8888
eq(render.highlights.Label, {
8989
{
90-
col_end = 16,
90+
col_end = 18,
9191
col_start = 3,
9292
line = 1,
9393
},
9494
})
9595

9696
eq(render.highlights.LineNr, {
9797
{
98-
col_end = 16,
98+
col_end = 18,
9999
col_start = 0,
100100
line = 0,
101101
},

0 commit comments

Comments
 (0)