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,
|
||||
rule = rule,
|
||||
bufnr = bufnr,
|
||||
col = col,
|
||||
col = col + 1,
|
||||
char = char,
|
||||
line = line,
|
||||
prev_char = prev_char,
|
||||
@ -296,7 +296,7 @@ M.autopairs_map = function(bufnr, char)
|
||||
then
|
||||
local end_pair = rule:get_end_pair(cond_opt)
|
||||
local move_text = utils.repeat_key(utils.key.join_left,#end_pair)
|
||||
if add_char==0 then
|
||||
if add_char == 0 then
|
||||
move_text =""
|
||||
char = ""
|
||||
end
|
||||
@ -325,7 +325,7 @@ M.autopairs_insert = function(bufnr, char)
|
||||
text = new_text,
|
||||
rule = rule,
|
||||
bufnr = bufnr,
|
||||
col = col,
|
||||
col = col + 1,
|
||||
char = char,
|
||||
line = line,
|
||||
prev_char = prev_char,
|
||||
@ -387,6 +387,7 @@ M.autopairs_cr = function(bufnr)
|
||||
check_endwise_ts = true,
|
||||
bufnr = bufnr,
|
||||
rule = rule,
|
||||
col = col,
|
||||
prev_char = prev_char,
|
||||
next_char = next_char,
|
||||
line = line
|
||||
@ -406,6 +407,7 @@ M.autopairs_cr = function(bufnr)
|
||||
check_endwise_ts = false,
|
||||
bufnr = bufnr,
|
||||
rule = rule,
|
||||
col = col,
|
||||
prev_char = prev_char,
|
||||
next_char = next_char,
|
||||
line = line,
|
||||
|
@ -1,7 +1,7 @@
|
||||
local utils = require('nvim-autopairs.utils')
|
||||
local log = require('nvim-autopairs._log')
|
||||
|
||||
local cond={}
|
||||
local cond = {}
|
||||
|
||||
-- cond
|
||||
-- @return false when it is not correct
|
||||
@ -13,12 +13,10 @@ cond.none = function()
|
||||
return function() return false end
|
||||
end
|
||||
|
||||
|
||||
cond.done = function()
|
||||
return function() return true end
|
||||
end
|
||||
|
||||
|
||||
cond.invert = function(func)
|
||||
return function(...)
|
||||
local result = func(...)
|
||||
@ -31,7 +29,7 @@ cond.before_regex_check = function(regex, length)
|
||||
length = length or 1
|
||||
return function(opts)
|
||||
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
|
||||
return true
|
||||
end
|
||||
@ -42,7 +40,7 @@ cond.before_text_check = function(text)
|
||||
local length = #text
|
||||
return function(opts)
|
||||
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
|
||||
return true
|
||||
end
|
||||
@ -53,7 +51,7 @@ cond.after_text_check = function(text)
|
||||
local length = #text
|
||||
return function(opts)
|
||||
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
|
||||
return true
|
||||
end
|
||||
@ -66,7 +64,7 @@ cond.after_regex_check = function(regex, length)
|
||||
return function(opts)
|
||||
if not regex then return end
|
||||
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
|
||||
return true
|
||||
end
|
||||
@ -78,7 +76,7 @@ cond.not_before_text_check = function(text)
|
||||
local length = #text
|
||||
return function(opts)
|
||||
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
|
||||
return false
|
||||
end
|
||||
@ -89,7 +87,7 @@ cond.not_after_text_check = function(text)
|
||||
local length = #text
|
||||
return function(opts)
|
||||
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
|
||||
return false
|
||||
end
|
||||
@ -100,7 +98,7 @@ cond.not_before_regex_check = function(regex, length)
|
||||
length = length or 1
|
||||
return function(opts)
|
||||
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
|
||||
return false
|
||||
end
|
||||
@ -110,22 +108,21 @@ end
|
||||
cond.not_after_regex_check = function(regex, length)
|
||||
length = length or 1
|
||||
return function(opts)
|
||||
if not regex then return end
|
||||
if not regex then
|
||||
return
|
||||
end
|
||||
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
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cond.check_is_bracket_line=function ()
|
||||
cond.check_is_bracket_line = function()
|
||||
return function(opts)
|
||||
log.debug('check_is_bracket_line')
|
||||
if
|
||||
utils.is_bracket(opts.char)
|
||||
and opts.next_char == opts.rule.end_pair
|
||||
then
|
||||
if utils.is_bracket(opts.char) and opts.next_char == opts.rule.end_pair then
|
||||
-- (( many char |)) => add
|
||||
-- ( many char |)) => not add
|
||||
local count_prev_char = 0
|
||||
@ -149,9 +146,7 @@ end
|
||||
cond.not_inside_quote = function()
|
||||
return function(opts)
|
||||
log.debug('not_add_quote_inside_quote')
|
||||
if
|
||||
utils.is_in_quote(opts.text, opts.col, opts.char)
|
||||
then
|
||||
if utils.is_in_quote(opts.text, opts.col - 1, opts.char) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
@ -162,14 +157,14 @@ cond.not_add_quote_inside_quote = function()
|
||||
log.debug('not_add_quote_inside_quote')
|
||||
if
|
||||
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
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
cond.move_right = function ()
|
||||
cond.move_right = function()
|
||||
return function(opts)
|
||||
log.debug('move_right')
|
||||
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
|
||||
-- situtaion |" => "|
|
||||
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
|
||||
end
|
||||
-- ("|") => (""|)
|
||||
-- "" |" " => "" "| "
|
||||
if utils.is_in_quote(opts.line, opts.col, opts.char) then
|
||||
return true
|
||||
if utils.is_in_quote(opts.line, opts.col -1, opts.char) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
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
|
||||
|
@ -2,7 +2,7 @@ local endwise = require('nvim-autopairs.ts-rule').endwise
|
||||
|
||||
local rules = {
|
||||
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 {
|
||||
endwise = function (...)
|
||||
local params = {...}
|
||||
return Rule(...)
|
||||
local rule = Rule(...)
|
||||
: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
|
||||
|
||||
}
|
||||
|
@ -164,7 +164,7 @@ end
|
||||
|
||||
--- get prev_char with out key_map
|
||||
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
|
||||
|
||||
return M
|
||||
|
@ -3,7 +3,7 @@ local ts = require 'nvim-treesitter.configs'
|
||||
local log = require('nvim-autopairs._log')
|
||||
|
||||
ts.setup {
|
||||
ensure_installed = 'maintained',
|
||||
ensure_installed = {'lua'},
|
||||
highlight = {enable = true},
|
||||
}
|
||||
_G.npairs = npairs;
|
||||
@ -12,7 +12,16 @@ vim.api.nvim_set_keymap('i' , '<CR>','v:lua.npairs.check_break_line_char()', {ex
|
||||
|
||||
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',
|
||||
filetype = "lua",
|
||||
linenr = 5,
|
||||
@ -20,16 +29,6 @@ local data = {
|
||||
before = [[if data== 'fdsafdsa' then| ]],
|
||||
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" ,
|
||||
filepath = './tests/endwise/init.lua',
|
||||
|
@ -4,6 +4,7 @@ set rtp +=../nvim-treesitter
|
||||
set rtp +=../playground/
|
||||
|
||||
lua _G.__is_log = true
|
||||
lua vim.fn.setenv("DEBUG_PLENARY", true)
|
||||
runtime! plugin/plenary.vim
|
||||
runtime! plugin/nvim-treesitter.vim
|
||||
runtime! plugin/playground.vim
|
||||
@ -13,6 +14,7 @@ set noswapfile
|
||||
set nobackup
|
||||
|
||||
filetype indent off
|
||||
set expandtab!
|
||||
set nowritebackup
|
||||
set noautoindent
|
||||
set nocindent
|
||||
|
@ -26,12 +26,6 @@ npairs.add_rules({
|
||||
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
|
||||
.."<esc>viwUi"
|
||||
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})
|
||||
@ -148,6 +142,12 @@ local data = {
|
||||
before = [[( many char |))]],
|
||||
after = [[( many char (|))]]
|
||||
},
|
||||
{
|
||||
name = "move right on quote line " ,
|
||||
key = [["]],
|
||||
before = [["|"]],
|
||||
after = [[""|]]
|
||||
},
|
||||
{
|
||||
name = "move right end line " ,
|
||||
key = [["]],
|
||||
@ -292,13 +292,27 @@ local data = {
|
||||
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",
|
||||
key="+",
|
||||
before = [[x|+ ]],
|
||||
after = [[x+| ]]
|
||||
},
|
||||
{
|
||||
setup_func = function()
|
||||
npairs.add_rules({
|
||||
Rule("/**", "**/", "javascript")
|
||||
:with_move(cond.none())
|
||||
})
|
||||
end,
|
||||
name="test javascript comment",
|
||||
filetype = "javascript",
|
||||
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})
|
||||
|
||||
ts.setup {
|
||||
ensure_installed = 'maintained',
|
||||
ensure_installed = {'lua', 'javascript'},
|
||||
highlight = {enable = true},
|
||||
autopairs = {enable = true}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user