Skip to content

Commit

Permalink
fix: type error in case config.bindings = false. (#57)
Browse files Browse the repository at this point in the history
* fix(keybinds): type error in case `config.bindings = false`.

* fix(keybinds): incomplete check for lhs=nil

* docs(keybinds): update docs on modifying bindings

* fix(tests): incomplete type annotations

* feat(tests): add tests for disabling all key bindings

---------

Co-authored-by: Nico Madysa <nico.madysa@cern.ch>
Co-authored-by: troiganto <troiganto@proton.me>
  • Loading branch information
3 people authored Aug 3, 2024
1 parent 4515356 commit bef24c1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
19 changes: 17 additions & 2 deletions DOCS.org
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@
** Modifying bindings

Bindings can be changed during configuration by overwriting them within the
=bindings= table:
=bindings= table:

#+begin_src lua
require("org-roam").setup({
Expand Down Expand Up @@ -940,7 +940,8 @@
})
#+end_src

To disable all bindings, set the =bindings= field to =false=:
To disable all bindings (including those of extensions), set the =bindings=
field to =false=:

#+begin_src lua
require("org-roam").setup({
Expand All @@ -949,6 +950,20 @@
})
#+end_src

To disable only the bindings of an extension, set its respective =bindings=
field to =false=:

#+begin_src lua
require("org-roam").setup({
-- ...
extensions = {
dailies = {
bindings = false,
}
},
})
#+end_src

** Coming from Emacs

Want to have bindings similar to Emacs's [[https://www.orgroam.com/][Org Roam]]?
Expand Down
16 changes: 12 additions & 4 deletions lua/org-roam/setup/keybindings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ local function assign(lhs, desc, cb, prefix)
if type(lhs) == "table" then
modes = lhs.modes
lhs = lhs.lhs
if not lhs then
return
end
end

if vim.trim(lhs) == "" or #modes == 0 then
Expand Down Expand Up @@ -85,9 +88,9 @@ end

---@param roam OrgRoam
local function assign_core_keybindings(roam)
-- User can remove all bindings by setting this to nil
-- User can remove all bindings by setting this to false
local bindings = roam.config.bindings or {}
local prefix = roam.config.bindings.prefix
local prefix = bindings.prefix

assign(bindings.add_alias, "Adds an alias to the roam node under cursor", function()
roam.api.add_alias()
Expand Down Expand Up @@ -222,9 +225,14 @@ end

---@param roam OrgRoam
local function assign_dailies_keybindings(roam)
-- User can remove all bindings by setting this to nil
-- User can remove all bindings by setting this to false
local bindings = roam.config.extensions.dailies.bindings or {}
local prefix = roam.config.bindings.prefix
-- If core bindings are disabled, extension bindings are disabled as well.
local core_bindings = roam.config.bindings
if not core_bindings then
return
end
local prefix = core_bindings.prefix

assign(bindings.capture_date, "Capture a specific date's note", function()
roam.ext.dailies.capture_date()
Expand Down
80 changes: 80 additions & 0 deletions spec/setup_keybindings_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -889,4 +889,84 @@ describe("org-roam.setup.keybindings", function()
assert(id, "could not find id of newly-inserted node")
assert.are.same({ "some [[id:" .. id .. "][series of text on multiple]] lines" }, lines)
end)

it("bindings can be disabled by setting them to false", function()
-- Remove binding created by other tests.
local binding = "<Leader>nc"
if utils.global_mapping_exists("n", binding) then
vim.api.nvim_del_keymap("n", binding)
end

-- Configure plugin to set up _no_ key bindings.
utils.init_plugin({
setup = { bindings = false },
})

local exists = utils.global_mapping_exists("n", binding)
assert(not exists, "found global mapping " .. binding .. " despite disabling it")
end)

it("default binding for capture is <Leader>nc", function()
-- Remove binding created by other tests.
local binding = "<Leader>nc"
if utils.global_mapping_exists("n", binding) then
vim.api.nvim_del_keymap("n", binding)
end

-- Configure plugin to set up key bindings.
utils.init_plugin({ setup = true })

-- This test exists to confirm that the previous
-- test's success wasn't a silent failure.
local exists = utils.global_mapping_exists("n", binding)
assert(exists, "could not find global mapping " .. binding)
end)

it("dailies bindings can be disabled by setting them to false", function()
-- Remove binding created by other tests.
local binding = "<Leader>ndN"
if utils.global_mapping_exists("n", binding) then
vim.api.nvim_del_keymap("n", binding)
end

-- Configure plugin to set up _no_ key bindings.
utils.init_plugin({
setup = { extensions = { dailies = { bindings = false } } },
})

local exists = utils.global_mapping_exists("n", binding)
assert(not exists, "found global mapping " .. binding .. " despite disabling it")
end)

it("dailies bindings can be disabled by setting core bindings to false", function()
-- Remove binding created by other tests.
local binding = "<Leader>ndN"
if utils.global_mapping_exists("n", binding) then
vim.api.nvim_del_keymap("n", binding)
end

-- Configure plugin to set up _no_ key bindings.
utils.init_plugin({
setup = { bindings = false },
})

local exists = utils.global_mapping_exists("n", binding)
assert(not exists, "found global mapping " .. binding .. " despite disabling it")
end)

it("default binding for capture today is <Leader>ndN", function()
-- Remove binding created by other tests.
local binding = "<Leader>ndN"
if utils.global_mapping_exists("n", binding) then
vim.api.nvim_del_keymap("n", binding)
end

-- Configure plugin to set up key bindings.
utils.init_plugin({ setup = true })

-- This test exists to confirm that the previous
-- test's success wasn't a silent failure.
local exists = utils.global_mapping_exists("n", binding)
assert(exists, "could not find global mapping " .. binding)
end)
end)
2 changes: 1 addition & 1 deletion spec/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ end
---If `wait` provided, schedules mapping and waits N milliseconds.
---@param mode org-roam.config.NvimMode
---@param lhs string
---@param opts? {buf?:integer, wait?:integer}
---@param opts? {buf?:integer, wait?:integer, prefix?:string}
function M.trigger_mapping(mode, lhs, opts)
opts = opts or {}
if opts.prefix then
Expand Down

0 comments on commit bef24c1

Please sign in to comment.