diff --git a/README.md b/README.md index a384802..b09cdbd 100644 --- a/README.md +++ b/README.md @@ -206,12 +206,13 @@ npairs.add_rules({ :with_move(cond.none()) -- don't delete if the next character is xx :with_del(cond.not_after_regex_check("xx")) - -- disable add newline when press + -- disable add newline when press :with_cr(cond.none()) }, + --it is not working on .vim but it working on another filetype + Rule("a","a","-vim") ) - npairs.add_rules({ Rule("$$","$$","tex") :with_pair(function(opts) diff --git a/lua/nvim-autopairs.lua b/lua/nvim-autopairs.lua index eb4481d..d5178f9 100644 --- a/lua/nvim-autopairs.lua +++ b/lua/nvim-autopairs.lua @@ -156,7 +156,7 @@ M.set_buf_rule = function(rules, bufnr) end M.on_attach = function(bufnr) - log.debug('on_attach' .. vim.bo.filetype) + -- log.debug('on_attach' .. vim.bo.filetype) if is_disable() then return end @@ -164,7 +164,10 @@ M.on_attach = function(bufnr) local rules = {} for _, rule in pairs(M.config.rules) do - if utils.check_filetype(rule.filetypes, vim.bo.filetype) then + if + utils.check_filetype(rule.filetypes, vim.bo.filetype) + and utils.check_not_filetype(rule.not_filetypes, vim.bo.filetype) + then table.insert(rules, rule) end end diff --git a/lua/nvim-autopairs/rule.lua b/lua/nvim-autopairs/rule.lua index d832109..d582721 100644 --- a/lua/nvim-autopairs/rule.lua +++ b/lua/nvim-autopairs/rule.lua @@ -1,4 +1,5 @@ local Cond = require('nvim-autopairs.conds') +local log = require('nvim-autopairs._log') --- @class Rule --- @field start_pair string @@ -6,7 +7,8 @@ local Cond = require('nvim-autopairs.conds') --- @field end_pair_func function dynamic change end_pair --- @field end_pair_length number change end_pair length for key map like --- @field key_map string|nil equal nil mean it will skip on autopairs map ---- @field filetypes table|string +--- @field filetypes table +--- @field not_filetypes table --- @field is_regex boolean use regex to compare --- @field is_multibyte boolean --- @field is_endwise boolean only use on end_wise @@ -34,6 +36,7 @@ function Rule.new(...) end_pair = nil, end_pair_func = false, filetypes = nil, + not_filetypes = nil, move_cond = nil, del_cond = {}, cr_cond = {}, @@ -44,15 +47,29 @@ function Rule.new(...) end_pair_length = nil, },opt) - local function verify(rule) + local function constructor(rule) -- check multibyte if #rule.start_pair ~= vim.api.nvim_strwidth(rule.start_pair) then rule:use_multibyte() end + -- check filetypes and not_filetypes + -- if have something like "-vim" it will add to not_filetypes + if rule.filetypes then + local ft, not_ft = {},{} + for _, value in pairs(rule.filetypes) do + if value:sub(1, 1) == "-" then + table.insert(not_ft, value:sub(2,#value)) + else + table.insert(ft, value) + end + end + rule.filetypes = #ft>0 and ft or nil + rule.not_filetypes = #not_ft>0 and not_ft or nil + end return rule end local r = setmetatable(opt, {__index = Rule}) - return verify(r) + return constructor(r) end function Rule:use_regex(value,key_map) diff --git a/lua/nvim-autopairs/rules/basic.lua b/lua/nvim-autopairs/rules/basic.lua index 92c113c..fc076fb 100644 --- a/lua/nvim-autopairs/rules/basic.lua +++ b/lua/nvim-autopairs/rules/basic.lua @@ -24,13 +24,11 @@ local function setup(opt) Rule("```", "```", { 'markdown', 'vimwiki', 'rmarkdown', 'rmd', 'pandoc' }), Rule("```.*$", "```", { 'markdown', 'vimwiki', 'rmarkdown', 'rmd', 'pandoc' }) :only_cr() - :use_regex(true) - , + :use_regex(true), Rule('"""', '"""', { 'python', 'elixir' }), - basic("'", "'") - :with_pair(cond.not_before_regex_check("%w")) - :with_pair(cond.not_filetypes({"rust"})), - basic("'", "'", "rust") + basic("'", "'", '-rust') + :with_pair(cond.not_before_regex_check("%w")), + basic("'", "'", 'rust') :with_pair(cond.not_before_regex_check("[%w<&]")) :with_pair(cond.not_after_text_check(">")), basic("`", "`"), diff --git a/lua/nvim-autopairs/utils.lua b/lua/nvim-autopairs/utils.lua index 5293fbd..69b264e 100644 --- a/lua/nvim-autopairs/utils.lua +++ b/lua/nvim-autopairs/utils.lua @@ -90,7 +90,7 @@ M.check_filetype = function(tbl, filetype) return M.is_in_table(tbl, filetype) end -M.check_disable_ft = function(tbl, filetype) +M.check_not_filetype = function(tbl, filetype) if tbl == nil then return true end return not M.is_in_table(tbl, filetype) end diff --git a/tests/nvim-autopairs_spec.lua b/tests/nvim-autopairs_spec.lua index b24c6d6..169f7a1 100644 --- a/tests/nvim-autopairs_spec.lua +++ b/tests/nvim-autopairs_spec.lua @@ -497,6 +497,17 @@ local data = { before = [[aa'|' ]], after = [[aa| ]], }, + { + setup_func = function() + npairs.clear_rules() + npairs.add_rule(Rule("x", "x",{'-vim','-rust'})) + end, + filetype = 'vim', + name = "disable filetype vim", + key = [[x]], + before = [[a | ]], + after = [[a x| ]] + }, } local run_data = {}