Skip to content

Commit

Permalink
Configure keymaps when new language API is added
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Sep 10, 2023
1 parent e3aa905 commit a09438a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ require("nvim-paredit").setup({
Or by calling the `add_language_extension` API directly before the setup. This would be the recommended approach for extension plugin authors.

```lua
require("nvim-paredit.lang").add_language_extension("commonlisp", { ... }).
require("nvim-paredit").extension.add_language_extension("commonlisp", { ... }).
```

---
Expand Down
51 changes: 29 additions & 22 deletions lua/nvim-paredit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,48 @@ local defaults = require("nvim-paredit.defaults")
local config = require("nvim-paredit.config")
local lang = require("nvim-paredit.lang")

local M = {
api = require("nvim-paredit.api"),
wrap = require("nvim-paredit.api.wrap"),
cursor = require("nvim-paredit.api.cursor"),
}

local function setup_keybingings(filetype, buf)
local filetypes = config.config.filetypes
local whitelist = config.config.filetypes
local keys = config.config.keys

if common.included_in_table(filetypes, filetype) then
keybindings.setup_keybindings({
keys = keys,
buf = buf,
})
if whitelist and not common.included_in_table(whitelist, filetype) then
return
end

if not common.included_in_table(lang.filetypes(), filetype) then
return
end

keybindings.setup_keybindings({
keys = keys,
buf = buf,
})
end

local function add_language_extension(ft, api)
lang.add_language_extension(ft, api)

if vim.bo.filetype == ft then
setup_keybingings(ft, vim.api.nvim_get_current_buf())
end
end

local M = {
api = require("nvim-paredit.api"),
wrap = require("nvim-paredit.api.wrap"),
cursor = require("nvim-paredit.api.cursor"),
extension = {
add_language_extension = add_language_extension,
},
}

function M.setup(opts)
opts = opts or {}

for filetype, api in pairs(opts.extensions or {}) do
lang.add_language_extension(filetype, api)
end

local filetypes
if type(opts.filetypes) == "table" then
-- substract langs form opts.filetypes to avoid
-- binding keymaps to unsupported buffers
filetypes = common.intersection(opts.filetypes, lang.filetypes())
else
filetypes = lang.filetypes()
end

local keys = opts.keys or {}

if type(opts.use_default_keys) ~= "boolean" or opts.use_default_keys then
Expand All @@ -46,7 +54,6 @@ function M.setup(opts)

config.update_config(defaults.defaults)
config.update_config(vim.tbl_deep_extend("force", opts, {
filetypes = filetypes,
keys = keys,
}))

Expand Down

0 comments on commit a09438a

Please sign in to comment.