Skip to content

Commit

Permalink
Merge pull request #68 from francescarpi/develop
Browse files Browse the repository at this point in the history
release: v2025-02-26
  • Loading branch information
francescarpi authored Feb 26, 2025
2 parents a47ed21 + b03846e commit 3b93890
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 65 deletions.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,28 @@ Take a look at the default shortcuts for navigating between buffers, changing th
"gitrebase",
},
},
leader_key = ";",
mapping_chars = "qweryuiop",
keybindings = {
goto_next_buffer = "<s-j>",
goto_previous_buffer = "<s-k>",
move_buffer_up = "<s-l>",
move_buffer_down = "<s-h>",
move_buffer_top = "<s-t>",
move_buffer_bottom = "<s-b>",
toggle_buffon_window = ";n",
switch_previous_used_buffer = ";;",
close_buffer = ";d",
close_buffers_above = ";v",
close_buffers_below = ";b",
close_all_buffers = ";cc",
close_others = ";cd",
reopen_recent_closed_buffer = ";t",
buffer_mapping = {
mapping_chars = "qweryuiop",
leader_key = ";",
},
show_help = ";h",
toggle_buffon_window = "<buffonleader>n",
switch_previous_used_buffer = "<buffonleader><buffonleader>",
close_buffer = "<buffonleader>d",
close_buffers_above = "<buffonleader>v",
close_buffers_below = "<buffonleader>b",
close_all_buffers = "<buffonleader>cc",
close_others = "<buffonleader>cd",
reopen_recent_closed_buffer = "<buffonleader>t",
show_help = "<buffonleader>h",
previous_page = "<s-tab>",
next_page = "<tab>",
move_to_previous_page = ";a",
move_to_next_page = ";s",
move_to_previous_page = "<buffonleader>a",
move_to_next_page = "<buffonleader>s",
},
}
```
Expand Down
35 changes: 15 additions & 20 deletions lua/buffon/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,31 @@ local default = {
"gitrebase",
},
},
leader_key = ";",
mapping_chars = "qweryuiop",
keybindings = {
goto_next_buffer = "<s-j>",
goto_previous_buffer = "<s-k>",
move_buffer_up = "<s-l>",
move_buffer_down = "<s-h>",
move_buffer_top = "<s-t>",
move_buffer_bottom = "<s-b>",
toggle_buffon_window = ";n",
switch_previous_used_buffer = ";;",
close_buffer = ";d",
close_buffers_above = ";v",
close_buffers_below = ";b",
close_all_buffers = ";cc",
close_others = ";cd",
reopen_recent_closed_buffer = ";t",
buffer_mapping = {
mapping_chars = "qweryuiop",
leader_key = ";",
},
show_help = ";h",
toggle_buffon_window = "<buffonleader>n",
switch_previous_used_buffer = "<buffonleader><buffonleader>",
close_buffer = "<buffonleader>d",
close_buffers_above = "<buffonleader>v",
close_buffers_below = "<buffonleader>b",
close_all_buffers = "<buffonleader>cc",
close_others = "<buffonleader>cd",
reopen_recent_closed_buffer = "<buffonleader>t",
show_help = "<buffonleader>h",
previous_page = "<s-tab>",
next_page = "<tab>",
move_to_previous_page = ";a",
move_to_next_page = ";s",
move_to_previous_page = "<buffonleader>a",
move_to_next_page = "<buffonleader>s",
},
}

---@class BuffonConfigKeyBindingBufferMapping
---@field mapping_chars string -- Each character maps to a buffer ("qwer" maps 'q' to buffer 1, 'w' to buffer 2, etc.)
---@field leader_key string -- Leader key used as a prefix for buffer mappings (';' creates mappings ';q', ';w', etc.)

---@class BuffonConfigOpen
---@field by_default boolean
---@field ignore_ft table<string>
Expand All @@ -64,7 +58,6 @@ local default = {
---@field close_all_buffers string
---@field close_others string
---@field restore_last_closed_buffer string
---@field buffer_mapping BuffonConfigKeyBindingBufferMapping
---@field show_help string
---@field next_page string
---@field previous_page string
Expand All @@ -77,6 +70,8 @@ local default = {
---@field new_buffer_position "start" | "end" | "after"
---@field open BuffonConfigOpen
---@field keybindings BuffonConfigKeyBinding
---@field leader_key string -- Leader key used as a prefix for buffer mappings (';' creates mappings ';q', ';w', etc.)
---@field mapping_chars string -- Each character maps to a buffer ("qwer" maps 'q' to buffer 1, 'w' to buffer 2, etc.)
local Config = {}

---@param opts any
Expand Down
12 changes: 6 additions & 6 deletions lua/buffon/maincontroller.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function MainController:new(cfg, page_controller, stg)
local o = {
config = cfg,
main_window = mainwindow.MainWindow:new(cfg, page_controller),
help_window = helpwindow.HelpWindow:new(),
help_window = helpwindow.HelpWindow:new(cfg),
group = vim.api.nvim_create_augroup("Buffon", { clear = true }),
page_controller = page_controller,
storage = stg,
Expand Down Expand Up @@ -208,19 +208,19 @@ end

function MainController:register_shortcuts()
for _, action in ipairs(self:get_shortcuts()) do
set_keymap(action.shortcut, function()
set_keymap(utils.replace_leader(self.config, action.shortcut), function()
self:dispatch(action)
end, action.help)
end

for idx = 1, #self.config.keybindings.buffer_mapping.mapping_chars do
local char = self.config.keybindings.buffer_mapping.mapping_chars:sub(idx, idx)
set_keymap(";" .. char, function()
for idx = 1, #self.config.mapping_chars do
local char = self.config.mapping_chars:sub(idx, idx)
set_keymap(self.config.leader_key .. char, function()
self:action_open_buffer_by_index(idx)
end, "Goto to buffer " .. idx)
end

set_keymap(self.config.keybindings.show_help, function()
set_keymap(utils.replace_leader(self.config, self.config.keybindings.show_help), function()
self:action_show_help()
end, "Show the help window")

Expand Down
8 changes: 4 additions & 4 deletions lua/buffon/page.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ end
---@param index number
---@return string
function Page:_get_shortcut(index)
local shortcut = self.config.keybindings.buffer_mapping.mapping_chars:sub(index, index)
local shortcut = self.config.mapping_chars:sub(index, index)
if shortcut ~= "" then
shortcut = self.config.keybindings.buffer_mapping.leader_key .. shortcut
shortcut = self.config.leader_key .. shortcut
else
shortcut = string.rep(" ", #self.config.keybindings.buffer_mapping.leader_key + 1)
shortcut = string.rep(" ", #self.config.leader_key + 1)
end
return shortcut
end
Expand Down Expand Up @@ -129,7 +129,7 @@ function Page:render(active_buffer)

-- highlights
local shortcut_start = 0
local shortcut_end = shortcut_start + #self.config.keybindings.buffer_mapping.leader_key + CHAR
local shortcut_end = shortcut_start + #self.config.leader_key + CHAR
local filename_start = shortcut_end + WHITESPACE
local filename_end = filename_start + #filename + WHITESPACE + ICON
local modified_start = filename_end + WHITESPACE
Expand Down
14 changes: 10 additions & 4 deletions lua/buffon/ui/help.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
local window = require("buffon.ui.window")
local utils = require("buffon.utils")

local M = {}

---@class BuffonHelpWindow
---@field config BuffonConfig
---@field window BuffonWindow
---@field content_set boolean
local HelpWindow = {}

function HelpWindow:new()
---@param cfg BuffonConfig
function HelpWindow:new(cfg)
local o = {
config = cfg,
window = window.Window:new(" Buffon Help ", window.WIN_POSITIONS.BOTTOM_RIGHT),
content_set = false,
}
Expand All @@ -28,14 +32,16 @@ function HelpWindow:toggle(actions)
local highlight = { Constant = {} }

for _, action in ipairs(actions) do
if #action.shortcut > max_length then
max_length = #action.shortcut
local action_len = #utils.replace_leader(self.config, action.shortcut)
if action_len > max_length then
max_length = action_len
end
end

local content = {}
for idx, action in ipairs(actions) do
local shortcut_padded = action.shortcut .. string.rep(" ", max_length - #action.shortcut)
local shortcut = utils.replace_leader(self.config, action.shortcut)
local shortcut_padded = shortcut .. string.rep(" ", max_length - #shortcut)
local line = string.format("%s %s", shortcut_padded, action.help)
table.insert(content, line)
table.insert(highlight.Constant, { line = idx - 1, col_start = 0, col_end = max_length })
Expand Down
3 changes: 2 additions & 1 deletion lua/buffon/ui/mainwindow.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local window = require("buffon.ui.window")
local utils = require("buffon.utils")

local M = {}

Expand All @@ -11,7 +12,7 @@ local MainWindow = {}
---@param config BuffonConfig
---@param page_controller BuffonPageController
function MainWindow:new(config, page_controller)
local title = " Buffon (" .. config.keybindings.show_help .. ") "
local title = " Buffon (" .. utils.replace_leader(config, config.keybindings.show_help) .. ") "
local o = {
window = window.Window:new(title, window.WIN_POSITIONS.TOP_RIGHT),
page_controller = page_controller,
Expand Down
8 changes: 8 additions & 0 deletions lua/buffon/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ M.table_add = function(tbl1, tbl2)
end
end

---@param cfg BuffonConfig
---@param shortcut string
---@return string
M.replace_leader = function(cfg, shortcut)
local replaced_string = shortcut:gsub("<buffonleader>", cfg.leader_key)
return replaced_string
end

---@class BuffonRecentlyClosed
---@field filenames? table<string>
---@field limit? number
Expand Down
28 changes: 13 additions & 15 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@ describe("config", function()
"gitrebase",
},
},
leader_key = ";",
mapping_chars = "qweryuiop",
keybindings = {
goto_next_buffer = "<s-j>",
goto_previous_buffer = "<s-k>",
move_buffer_up = "<s-l>",
move_buffer_down = "<s-h>",
move_buffer_top = "<s-t>",
move_buffer_bottom = "<s-b>",
toggle_buffon_window = ";n",
switch_previous_used_buffer = ";;",
close_buffer = ";d",
close_buffers_above = ";v",
close_buffers_below = ";b",
close_all_buffers = ";cc",
close_others = ";cd",
reopen_recent_closed_buffer = ";t",
buffer_mapping = {
mapping_chars = "qweryuiop",
leader_key = ";",
},
show_help = ";h",
toggle_buffon_window = "<buffonleader>n",
switch_previous_used_buffer = "<buffonleader><buffonleader>",
close_buffer = "<buffonleader>d",
close_buffers_above = "<buffonleader>v",
close_buffers_below = "<buffonleader>b",
close_all_buffers = "<buffonleader>cc",
close_others = "<buffonleader>cd",
reopen_recent_closed_buffer = "<buffonleader>t",
show_help = "<buffonleader>h",
previous_page = "<s-tab>",
next_page = "<tab>",
move_to_previous_page = ";a",
move_to_next_page = ";s",
move_to_previous_page = "<buffonleader>a",
move_to_next_page = "<buffonleader>s",
},
})
end)
Expand Down

0 comments on commit 3b93890

Please # to comment.