fix: allow remove filetype when create rule

cond.not_filetypes() is not remove rule, that rule still exist.
We need to remove rule from constructor by using syntax -filetype
sample:
Rule("a","a","-vim")
This commit is contained in:
zztrieuzz 2021-10-01 20:37:15 +07:00
parent 09569be17f
commit 94e97c7cca
6 changed files with 44 additions and 14 deletions

View File

@ -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 <cr>
-- disable add newline when press <cr>
: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)

View File

@ -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

View File

@ -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 <left>
--- @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)

View File

@ -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("`", "`"),

View File

@ -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

View File

@ -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 = {}