Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure keymaps when new language API is added #28

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading