Skip to content

Commit

Permalink
Merge pull request #28 from julienvincent/jv/extension-api
Browse files Browse the repository at this point in the history
Configure keymaps when new language API is added
  • Loading branch information
julienvincent authored Sep 11, 2023
2 parents f9bd28b + 19a1a4c commit 4b1b6ff
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 @@ -160,7 +160,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(common.merge(opts, {
filetypes = filetypes,
keys = keys,
}))

Expand Down

0 comments on commit 4b1b6ff

Please sign in to comment.