mirror of
https://github.com/ilyakooo0/nvim-autopairs.git
synced 2024-09-11 07:15:44 +03:00
fix: make column value work on another condition
add endwise is_end_line function
This commit is contained in:
parent
b0bbe8d908
commit
92b045dd1f
@ -273,7 +273,7 @@ M.autopairs_map = function(bufnr, char)
|
|||||||
text = new_text,
|
text = new_text,
|
||||||
rule = rule,
|
rule = rule,
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
col = col,
|
col = col + 1,
|
||||||
char = char,
|
char = char,
|
||||||
line = line,
|
line = line,
|
||||||
prev_char = prev_char,
|
prev_char = prev_char,
|
||||||
@ -296,7 +296,7 @@ M.autopairs_map = function(bufnr, char)
|
|||||||
then
|
then
|
||||||
local end_pair = rule:get_end_pair(cond_opt)
|
local end_pair = rule:get_end_pair(cond_opt)
|
||||||
local move_text = utils.repeat_key(utils.key.join_left,#end_pair)
|
local move_text = utils.repeat_key(utils.key.join_left,#end_pair)
|
||||||
if add_char==0 then
|
if add_char == 0 then
|
||||||
move_text =""
|
move_text =""
|
||||||
char = ""
|
char = ""
|
||||||
end
|
end
|
||||||
@ -325,7 +325,7 @@ M.autopairs_insert = function(bufnr, char)
|
|||||||
text = new_text,
|
text = new_text,
|
||||||
rule = rule,
|
rule = rule,
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
col = col,
|
col = col + 1,
|
||||||
char = char,
|
char = char,
|
||||||
line = line,
|
line = line,
|
||||||
prev_char = prev_char,
|
prev_char = prev_char,
|
||||||
@ -387,6 +387,7 @@ M.autopairs_cr = function(bufnr)
|
|||||||
check_endwise_ts = true,
|
check_endwise_ts = true,
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
rule = rule,
|
rule = rule,
|
||||||
|
col = col,
|
||||||
prev_char = prev_char,
|
prev_char = prev_char,
|
||||||
next_char = next_char,
|
next_char = next_char,
|
||||||
line = line
|
line = line
|
||||||
@ -406,6 +407,7 @@ M.autopairs_cr = function(bufnr)
|
|||||||
check_endwise_ts = false,
|
check_endwise_ts = false,
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
rule = rule,
|
rule = rule,
|
||||||
|
col = col,
|
||||||
prev_char = prev_char,
|
prev_char = prev_char,
|
||||||
next_char = next_char,
|
next_char = next_char,
|
||||||
line = line,
|
line = line,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
local utils = require('nvim-autopairs.utils')
|
local utils = require('nvim-autopairs.utils')
|
||||||
local log = require('nvim-autopairs._log')
|
local log = require('nvim-autopairs._log')
|
||||||
|
|
||||||
local cond={}
|
local cond = {}
|
||||||
|
|
||||||
-- cond
|
-- cond
|
||||||
-- @return false when it is not correct
|
-- @return false when it is not correct
|
||||||
@ -13,12 +13,10 @@ cond.none = function()
|
|||||||
return function() return false end
|
return function() return false end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
cond.done = function()
|
cond.done = function()
|
||||||
return function() return true end
|
return function() return true end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
cond.invert = function(func)
|
cond.invert = function(func)
|
||||||
return function(...)
|
return function(...)
|
||||||
local result = func(...)
|
local result = func(...)
|
||||||
@ -31,7 +29,7 @@ cond.before_regex_check = function(regex, length)
|
|||||||
length = length or 1
|
length = length or 1
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('before_regex_check')
|
log.debug('before_regex_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col, - length)
|
local str = utils.text_sub_char(opts.line, opts.col - 1, -length)
|
||||||
if str:match(regex) then
|
if str:match(regex) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -42,7 +40,7 @@ cond.before_text_check = function(text)
|
|||||||
local length = #text
|
local length = #text
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('before_text_check')
|
log.debug('before_text_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col, - length)
|
local str = utils.text_sub_char(opts.line, opts.col - 1, -length)
|
||||||
if str == text then
|
if str == text then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -53,7 +51,7 @@ cond.after_text_check = function(text)
|
|||||||
local length = #text
|
local length = #text
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('after_text_check')
|
log.debug('after_text_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col + 1, length)
|
local str = utils.text_sub_char(opts.line, opts.col, length)
|
||||||
if str == text then
|
if str == text then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -66,7 +64,7 @@ cond.after_regex_check = function(regex, length)
|
|||||||
return function(opts)
|
return function(opts)
|
||||||
if not regex then return end
|
if not regex then return end
|
||||||
log.debug('after_regex_check')
|
log.debug('after_regex_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col + 1, length)
|
local str = utils.text_sub_char(opts.line, opts.col, length)
|
||||||
if str:match(regex) then
|
if str:match(regex) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -78,7 +76,7 @@ cond.not_before_text_check = function(text)
|
|||||||
local length = #text
|
local length = #text
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('not_before_text_check')
|
log.debug('not_before_text_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col, - length)
|
local str = utils.text_sub_char(opts.line, opts.col - 1, -length)
|
||||||
if str == text then
|
if str == text then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -89,7 +87,7 @@ cond.not_after_text_check = function(text)
|
|||||||
local length = #text
|
local length = #text
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('not_after_text_check')
|
log.debug('not_after_text_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col + 1, length)
|
local str = utils.text_sub_char(opts.line, opts.col, length)
|
||||||
if str == text then
|
if str == text then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -100,7 +98,7 @@ cond.not_before_regex_check = function(regex, length)
|
|||||||
length = length or 1
|
length = length or 1
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('not_before_regex_check')
|
log.debug('not_before_regex_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col, - length)
|
local str = utils.text_sub_char(opts.line, opts.col - 1, -length)
|
||||||
if str:match(regex) then
|
if str:match(regex) then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -110,22 +108,21 @@ end
|
|||||||
cond.not_after_regex_check = function(regex, length)
|
cond.not_after_regex_check = function(regex, length)
|
||||||
length = length or 1
|
length = length or 1
|
||||||
return function(opts)
|
return function(opts)
|
||||||
if not regex then return end
|
if not regex then
|
||||||
|
return
|
||||||
|
end
|
||||||
log.debug('not_after_regex_check')
|
log.debug('not_after_regex_check')
|
||||||
local str = utils.text_sub_char(opts.line, opts.col + 1, length)
|
local str = utils.text_sub_char(opts.line, opts.col, length)
|
||||||
if str:match(regex) then
|
if str:match(regex) then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
cond.check_is_bracket_line=function ()
|
cond.check_is_bracket_line = function()
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('check_is_bracket_line')
|
log.debug('check_is_bracket_line')
|
||||||
if
|
if utils.is_bracket(opts.char) and opts.next_char == opts.rule.end_pair then
|
||||||
utils.is_bracket(opts.char)
|
|
||||||
and opts.next_char == opts.rule.end_pair
|
|
||||||
then
|
|
||||||
-- (( many char |)) => add
|
-- (( many char |)) => add
|
||||||
-- ( many char |)) => not add
|
-- ( many char |)) => not add
|
||||||
local count_prev_char = 0
|
local count_prev_char = 0
|
||||||
@ -149,9 +146,7 @@ end
|
|||||||
cond.not_inside_quote = function()
|
cond.not_inside_quote = function()
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('not_add_quote_inside_quote')
|
log.debug('not_add_quote_inside_quote')
|
||||||
if
|
if utils.is_in_quote(opts.text, opts.col - 1, opts.char) then
|
||||||
utils.is_in_quote(opts.text, opts.col, opts.char)
|
|
||||||
then
|
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -162,14 +157,14 @@ cond.not_add_quote_inside_quote = function()
|
|||||||
log.debug('not_add_quote_inside_quote')
|
log.debug('not_add_quote_inside_quote')
|
||||||
if
|
if
|
||||||
utils.is_quote(opts.char)
|
utils.is_quote(opts.char)
|
||||||
and utils.is_in_quote(opts.text, opts.col, opts.char)
|
and utils.is_in_quote(opts.text, opts.col - 1, opts.char)
|
||||||
then
|
then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
cond.move_right = function ()
|
cond.move_right = function()
|
||||||
return function(opts)
|
return function(opts)
|
||||||
log.debug('move_right')
|
log.debug('move_right')
|
||||||
if opts.next_char == opts.char then
|
if opts.next_char == opts.char then
|
||||||
@ -179,17 +174,28 @@ cond.move_right = function ()
|
|||||||
-- move right when have quote on end line or in quote
|
-- move right when have quote on end line or in quote
|
||||||
-- situtaion |" => "|
|
-- situtaion |" => "|
|
||||||
if utils.is_quote(opts.char) then
|
if utils.is_quote(opts.char) then
|
||||||
if opts.col + 1 == string.len(opts.line) then
|
if opts.col == string.len(opts.line) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
-- ("|") => (""|)
|
-- ("|") => (""|)
|
||||||
-- "" |" " => "" "| "
|
-- "" |" " => "" "| "
|
||||||
if utils.is_in_quote(opts.line, opts.col, opts.char) then
|
if utils.is_in_quote(opts.line, opts.col -1, opts.char) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
cond.is_end_line = function()
|
||||||
|
return function(opts)
|
||||||
|
log.debug('is_end_line')
|
||||||
|
-- if the next char is blank
|
||||||
|
if opts.next_char ~= "" and opts.next_char:match("%s+") == nil then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return cond
|
return cond
|
||||||
|
@ -2,7 +2,7 @@ local endwise = require('nvim-autopairs.ts-rule').endwise
|
|||||||
|
|
||||||
local rules = {
|
local rules = {
|
||||||
endwise('then$', 'end', 'lua', 'if_statement'),
|
endwise('then$', 'end', 'lua', 'if_statement'),
|
||||||
endwise('function.*%(.*%)$', 'end', 'lua', 'function_definition'),
|
endwise('function.*%(.*%)$', 'end', 'lua', {'function_definition', 'local_function', 'function'}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,9 +5,13 @@ local ts_conds = require('nvim-autopairs.ts-conds')
|
|||||||
return {
|
return {
|
||||||
endwise = function (...)
|
endwise = function (...)
|
||||||
local params = {...}
|
local params = {...}
|
||||||
return Rule(...)
|
local rule = Rule(...)
|
||||||
:use_regex(true)
|
:use_regex(true)
|
||||||
:end_wise(ts_conds.is_endwise_node(params[4]))
|
:end_wise(cond.is_end_line())
|
||||||
|
if params[4] then
|
||||||
|
rule:with_cr(ts_conds.is_endwise_node(params[4]))
|
||||||
|
end
|
||||||
|
return rule
|
||||||
end
|
end
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -164,7 +164,7 @@ end
|
|||||||
|
|
||||||
--- get prev_char with out key_map
|
--- get prev_char with out key_map
|
||||||
M.get_prev_char = function(opt)
|
M.get_prev_char = function(opt)
|
||||||
return opt.line:sub(opt.col, opt.col + #opt.rule.start_pair -1)
|
return opt.line:sub(opt.col -1, opt.col + #opt.rule.start_pair -2)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -3,7 +3,7 @@ local ts = require 'nvim-treesitter.configs'
|
|||||||
local log = require('nvim-autopairs._log')
|
local log = require('nvim-autopairs._log')
|
||||||
|
|
||||||
ts.setup {
|
ts.setup {
|
||||||
ensure_installed = 'maintained',
|
ensure_installed = {'lua'},
|
||||||
highlight = {enable = true},
|
highlight = {enable = true},
|
||||||
}
|
}
|
||||||
_G.npairs = npairs;
|
_G.npairs = npairs;
|
||||||
@ -12,7 +12,16 @@ vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {ex
|
|||||||
|
|
||||||
local data = {
|
local data = {
|
||||||
{
|
{
|
||||||
name = "lua if add endwise" ,
|
name = "lua function add endwise" ,
|
||||||
|
filepath = './tests/endwise/init.lua',
|
||||||
|
filetype = "lua",
|
||||||
|
linenr = 5,
|
||||||
|
key = [[<cr>]],
|
||||||
|
before = [[function a()| ]],
|
||||||
|
after = [[ end ]]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name = "add if endwise" ,
|
||||||
filepath = './tests/endwise/init.lua',
|
filepath = './tests/endwise/init.lua',
|
||||||
filetype = "lua",
|
filetype = "lua",
|
||||||
linenr = 5,
|
linenr = 5,
|
||||||
@ -20,16 +29,6 @@ local data = {
|
|||||||
before = [[if data== 'fdsafdsa' then| ]],
|
before = [[if data== 'fdsafdsa' then| ]],
|
||||||
after = [[end ]]
|
after = [[end ]]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
-- only = true;
|
|
||||||
name = "add newline have endwise" ,
|
|
||||||
filepath = './tests/endwise/init.lua',
|
|
||||||
filetype = "lua",
|
|
||||||
linenr = 5,
|
|
||||||
key = [[<cr>]],
|
|
||||||
before = [[if data== 'fdsafdsa' then|end]],
|
|
||||||
after = [[end]]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name = "don't add endwise on match rule" ,
|
name = "don't add endwise on match rule" ,
|
||||||
filepath = './tests/endwise/init.lua',
|
filepath = './tests/endwise/init.lua',
|
||||||
|
@ -4,6 +4,7 @@ set rtp +=../nvim-treesitter
|
|||||||
set rtp +=../playground/
|
set rtp +=../playground/
|
||||||
|
|
||||||
lua _G.__is_log = true
|
lua _G.__is_log = true
|
||||||
|
lua vim.fn.setenv("DEBUG_PLENARY", true)
|
||||||
runtime! plugin/plenary.vim
|
runtime! plugin/plenary.vim
|
||||||
runtime! plugin/nvim-treesitter.vim
|
runtime! plugin/nvim-treesitter.vim
|
||||||
runtime! plugin/playground.vim
|
runtime! plugin/playground.vim
|
||||||
@ -13,6 +14,7 @@ set noswapfile
|
|||||||
set nobackup
|
set nobackup
|
||||||
|
|
||||||
filetype indent off
|
filetype indent off
|
||||||
|
set expandtab!
|
||||||
set nowritebackup
|
set nowritebackup
|
||||||
set noautoindent
|
set noautoindent
|
||||||
set nocindent
|
set nocindent
|
||||||
|
@ -26,12 +26,6 @@ npairs.add_rules({
|
|||||||
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
|
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
|
||||||
.."<esc>viwUi"
|
.."<esc>viwUi"
|
||||||
end),
|
end),
|
||||||
Rule("-","+","vim")
|
|
||||||
:with_move(function(opt)
|
|
||||||
return utils.get_prev_char(opt) == "x"end)
|
|
||||||
:with_move(cond.done()),
|
|
||||||
Rule("/**", "**/", "javascript")
|
|
||||||
:with_move(cond.none()),
|
|
||||||
|
|
||||||
})
|
})
|
||||||
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
|
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
|
||||||
@ -148,6 +142,12 @@ local data = {
|
|||||||
before = [[( many char |))]],
|
before = [[( many char |))]],
|
||||||
after = [[( many char (|))]]
|
after = [[( many char (|))]]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name = "move right on quote line " ,
|
||||||
|
key = [["]],
|
||||||
|
before = [["|"]],
|
||||||
|
after = [[""|]]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name = "move right end line " ,
|
name = "move right end line " ,
|
||||||
key = [["]],
|
key = [["]],
|
||||||
@ -292,13 +292,27 @@ local data = {
|
|||||||
after = [[B|1234S1234S ]]
|
after = [[B|1234S1234S ]]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name="test move right custom char plus",
|
setup_func = function()
|
||||||
|
npairs.add_rules({
|
||||||
|
Rule("-","+","vim")
|
||||||
|
:with_move(function(opt)
|
||||||
|
return utils.get_prev_char(opt) == "x" end)
|
||||||
|
:with_move(cond.done())
|
||||||
|
})
|
||||||
|
end,
|
||||||
|
name = "test move right custom char plus",
|
||||||
filetype="vim",
|
filetype="vim",
|
||||||
key="+",
|
key="+",
|
||||||
before = [[x|+ ]],
|
before = [[x|+ ]],
|
||||||
after = [[x+| ]]
|
after = [[x+| ]]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
setup_func = function()
|
||||||
|
npairs.add_rules({
|
||||||
|
Rule("/**", "**/", "javascript")
|
||||||
|
:with_move(cond.none())
|
||||||
|
})
|
||||||
|
end,
|
||||||
name="test javascript comment",
|
name="test javascript comment",
|
||||||
filetype = "javascript",
|
filetype = "javascript",
|
||||||
key="*",
|
key="*",
|
||||||
|
@ -20,7 +20,7 @@ npairs.add_rules({
|
|||||||
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
|
vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {expr = true , noremap = true})
|
||||||
|
|
||||||
ts.setup {
|
ts.setup {
|
||||||
ensure_installed = 'maintained',
|
ensure_installed = {'lua', 'javascript'},
|
||||||
highlight = {enable = true},
|
highlight = {enable = true},
|
||||||
autopairs = {enable = true}
|
autopairs = {enable = true}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user