diff --git a/README.md b/README.md index dc1bf8a..926c5a4 100644 --- a/README.md +++ b/README.md @@ -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", { ... }). ``` --- diff --git a/lua/nvim-paredit/init.lua b/lua/nvim-paredit/init.lua index 81d6d2a..6b691b8 100644 --- a/lua/nvim-paredit/init.lua +++ b/lua/nvim-paredit/init.lua @@ -4,24 +4,41 @@ 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 {} @@ -29,15 +46,6 @@ function M.setup(opts) 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 @@ -46,7 +54,6 @@ function M.setup(opts) config.update_config(defaults.defaults) config.update_config(common.merge(opts, { - filetypes = filetypes, keys = keys, }))