This commit is contained in:
windwp 2021-04-17 21:10:16 +07:00 committed by windwp
parent 1e808ecdf0
commit 0172f38c46
6 changed files with 168 additions and 129 deletions

View File

@ -6,8 +6,10 @@ local api = vim.api
local M={}
local state = {}
local default = {
disable_filetype = {"TelescopePrompt", "spectre_panel"},
ignored_next_char = "%w",
rules = basic_rule
}
M.setup = function(opt)
@ -38,31 +40,30 @@ M.on_attach = function(bufnr)
end
M.autopairs_bs = function()
end
local skip_next = false
M.autopairs_insert = function(bufnr, char)
if skip_next then skip_next = false return end
local line = utils.text_get_current_line(bufnr)
log.debug("-----------------")
log.debug("line:" .. line)
log.debug("char:" .. char)
-- log.debug("-----------------")
-- log.debug("line:" .. line)
-- log.debug("char:" .. char)
local _, col = utils.get_cursor()
log.debug(vim.inspect(col))
local filetype = vim.bo.filetype
local new_text = line:sub(1, col) .. char .. line:sub(col + 1,#line)
log.debug("new_text:[" .. new_text .. "]")
-- log.debug("new_text:[" .. new_text .. "]")
for _, rule in pairs(state.rules) do
if rule.start_pair and utils.check_filetype(rule.filetypes, filetype) then
local prev_char = utils.text_sub_char(new_text, col + 1,-#rule.start_pair)
local next_char = utils.text_sub_char(new_text, col + 2,#rule.start_pair)
if rule.start_pair == '(' then
log.debug("start_pair" .. rule.start_pair)
log.debug('prev_char' .. prev_char)
log.debug('next_char' .. next_char)
end
if prev_char == rule.start_pair then
-- log.debug("start_pair" .. rule.start_pair)
-- log.debug('prev_char' .. prev_char)
-- log.debug('next_char' .. next_char)
local cond_opt = {
text = new_text,
rule = rule,
@ -75,13 +76,15 @@ M.autopairs_insert = function(bufnr, char)
}
if rule:can_move(cond_opt) then
utils.reset_vchar()
vim.schedule(function()
utils.feed(utils.key.del, 1)
utils.feed(utils.key.right, -1)
end)
return
return false
end
if rule:can_pair(cond_opt) then
log.debug('match')
-- log.debug('match')
vim.schedule(function()
utils.insert_char(rule.end_pair)
utils.feed(utils.key.left, #rule.end_pair)

View File

@ -2,28 +2,50 @@ local utils = require('nvim-autopairs.utils')
local log = require('nvim-autopairs._log')
local cond={}
cond.not_regex_check = function(regex, length)
cond.not_before_regex_check = function(regex, length)
length = length or 1
return function(opts)
local str = utils.text_sub_char(opts.line, opts.col, - length)
log.debug(vim.inspect(str))
if str:match(regex) then
return false
end
return true
end
end
cond.not_after_regex_check = function(regex, length)
length = length or 1
return function(opts)
local str = utils.text_sub_char(opts.line, opts.col + 1, length)
if str:match(regex) then
return false
end
return true
end
end
cond.not_add_quote_inside_quote=function()
return function(opts)
if
utils.is_quote(opts.char)
and utils.is_in_quote(opts.text, opts.col, opts.char)
then
return false
end
return true
end
end
cond.move_right = function ()
return function(opts)
log.debug("move right test")
log.debug(opts)
if utils.is_quote(opts.char)
if
utils.is_quote(opts.char)
and opts.next_char == opts.char
then
local length = string.len(opts.line) -- situtaion |" => "|
log.debug("is quote" .. length) -- move right when have quote on end line or in quote
if opts.col + 1 == string.len(opts.line) then
return true
log.debug("move right correct")
return true
end
-- ("|") => (""|)
-- "" |" " => "" "| "

View File

@ -10,7 +10,7 @@ function Rule.new(...)
else
opt.start_pair = params[1]
opt.end_pair = params[2]
if type(params[3])=="string" then
if type(params[3]) == "string" then
opt.filetypes = {params[3]}
else
opt.filetypes = params[3]
@ -21,21 +21,36 @@ function Rule.new(...)
end_pair = nil,
filetypes = nil,
-- allow move when press close_pairs
move_cond = function ()
return false
end,
move_cond = nil,
-- allow delete when press bs
del_cond = function()
return false
end,
pair_cond = function(_)
del_cond = nil,
pair_cond = {function(_)
-- local prev_char, line, ts_node = unpack(opts)
return true
end,
end},
},opt)
return setmetatable(opt, {__index = Rule})
end
function Rule:with_move(cond)
if self.move_cond == nil then self.move_cond = {}end
table.insert(self.move_cond, cond)
return self
end
function Rule:with_del(cond)
if self.del_cond == nil then self.del_cond = {}end
table.insert(self.del_cond, cond)
return self
end
function Rule:with_pair(cond)
if self.pair_cond == nil then self.pair_cond = {}end
table.insert(self.pair_cond, cond)
return self
end
local function can_do(conds, opt)
if type(conds) == 'table' then
for _, cond in pairs(conds) do
@ -45,7 +60,7 @@ local function can_do(conds, opt)
end
return true
elseif type(conds) == 'function' then
return conds(opt)
return conds(opt) == true
end
return false
end
@ -63,4 +78,4 @@ function Rule:can_del(opt)
return can_do(self.del_cond, opt)
end
return Rule.new
return {new = Rule.new}

View File

@ -1,31 +1,25 @@
local Rule = require('nvim-autopairs.rule')
local Rule = require('nvim-autopairs.rule').new
local cond = require('nvim-autopairs.conds')
local basic = function(...)
return Rule(...)
:with_move(cond.move_right())
:with_pair(cond.not_after_regex_check('%w'))
:with_pair(cond.not_add_quote_inside_quote())
end
local basic = {
Rule("```", "```", 'markdown'),
Rule('"""', '"""', 'python'),
Rule({
start_pair = "'",
end_pair = "'",
pair_cond = {
cond.not_regex_check('%w')
}
}),
Rule("`", "`"),
Rule('"', '"'),
-- Rule({
-- start_pair = '"',
-- end_pair = '"',
-- move_cond = {
-- cond.move_right()
-- }
-- }),
Rule("(", ")"),
Rule("[", "]"),
Rule("{", "}"),
local rules = {
Rule("```", "```", 'markdown'),
Rule('"""', '"""', 'python'),
basic("'", "'")
:with_pair(cond.not_before_regex_check("%w"))
,
basic("`", "`"),
basic('"', '"'),
basic("(", ")"),
basic("[", "]"),
basic("{", "}"),
}
return basic
return rules

View File

@ -9,7 +9,9 @@ M.key = {
right = "<right>"
}
M.reset_vchar = function()
vim.cmd [[let v:char = ""]]
end
M.is_quote = function (char)
return char == "'" or char == '"' or char == '`'
@ -105,21 +107,16 @@ M.insert_char = function(text)
end
M.feed = function(text, num)
-- num = num or 1
-- local result = ''
-- for _ = 1, num, 1 do
-- result = result .. text
-- end
-- log.debug("result" .. result)
-- api.nvim_feedkeys (api.nvim_replace_termcodes(
-- result, true, false, true),
-- "n", true)
for i = 1, num, 1 do
api.nvim_feedkeys (api.nvim_replace_termcodes(
text, true, false, true),
"n", true)
num = num or 1
if num < 1 then num = 1 end
local result = ''
for _ = 1, num, 1 do
result = result .. text
end
log.debug("result" .. result)
api.nvim_feedkeys (api.nvim_replace_termcodes(
result, true, false, true),
"n", true)
end
M.esc = function(cmd)

View File

@ -33,8 +33,8 @@ local data = {
{
name = "add normal bracket" ,
key = [[(]],
before = [[aaaa|x ]],
after = [[aaaa(|)x ]]
before = [[aaaa| x ]],
after = [[aaaa(|) x ]]
},
{
name = "add normal quote" ,
@ -65,73 +65,81 @@ local data = {
after = [[aa'| aa ]]
},
{
-- only = true,
name = "don't add single quote with alphabet char" ,
key = [[']],
before = [[a|x ]],
after = [[a'|x ]]
},
-- {
-- name = "don't add single quote on end line",
-- key = [[<right>']],
-- before = [[c aa|]],
-- after = [[c aa'|]]
-- },
-- {
-- name = "don't add quote after alphabet char" ,
-- key = [["]],
-- before = [[aa |aa]],
-- after = [[aa "|aa]]
-- },
-- {
-- name = "don't add quote inside quote" ,
-- key = [["]],
-- before = [["aa | aa]],
-- after = [["aa "| aa]]
-- },
-- {
-- name = "add quote if not inside quote" ,
-- key = [["]],
-- before = [["aa " | aa]],
-- after = [["aa " "|" aa]]
-- },
-- {
-- name = "don't add pair after alphabet char" ,
-- key = [[(]],
-- before = [[aa |aa]],
-- after = [[aa (|aa]]
-- },
{
name = "don't add single quote on end line",
key = [[<right>']],
before = [[c aa|]],
after = [[c aa'|]]
},
{
name = "don't add quote after alphabet char" ,
key = [["]],
before = [[aa |aa]],
after = [[aa "|aa]]
},
{
name = "don't add quote inside quote" ,
key = [["]],
before = [["aa | aa]],
after = [["aa "| aa]]
},
{
name = "add quote if not inside quote" ,
key = [["]],
before = [["aa " | aa]],
after = [["aa " "|" aa]]
},
{
name = "don't add pair after alphabet char" ,
key = [[(]],
before = [[aa |aa]],
after = [[aa (|aa]]
},
{
only = true,
name = "move right on square bracket" ,
key = [[)]],
before = [[(|) ]],
after = [[()| ]]
},
{
name = "move right end line " ,
key = [["]],
before = [[aaaa|"]],
after = [[aaaa"|]]
},
-- {
-- name = "move right when inside quote" ,
-- key = [["]],
-- before = [[("abcd|")]],
-- after = [[("abcd"|)]]
-- },
{
name = "move right when inside quote" ,
key = [["]],
before = [[("abcd|")]],
after = [[("abcd"|)]]
},
-- {
-- name = "move right when inside grave with special slash" ,
-- key = [[`]],
-- before = [[(`abcd\"|`)]],
-- after = [[(`abcd\"`|)]]
-- },
-- {
-- name = "move right when inside quote with special slash" ,
-- key = [["]],
-- before = [[("abcd\"|")]],
-- after = [[("abcd\""|)]]
-- },
-- {
-- name = "move right when inside single quote with special slash",
-- filetype="javascript",
-- key = [[']],
-- before = [[nvim_set_var('test_thing|')]],
-- after = [[nvim_set_var('test_thing'|)]]
-- },
{
name = "move right when inside grave with special slash" ,
key = [[`]],
before = [[(`abcd\"|`)]],
after = [[(`abcd\"`|)]]
},
{
name = "move right when inside quote with special slash" ,
key = [["]],
before = [[("abcd\"|")]],
after = [[("abcd\""|)]]
},
{
name = "move right when inside single quote with special slash",
filetype="javascript",
key = [[']],
before = [[nvim_set_var('test_thing|')]],
after = [[nvim_set_var('test_thing'|)]]
},
-- {
-- name = "breakline on {" ,
-- filetype="javascript",