Skip to content

Commit

Permalink
fix: make column value work on another condition
Browse files Browse the repository at this point in the history
add endwise is_end_line function
  • Loading branch information
windwp committed Jul 23, 2021
1 parent b0bbe8d commit 92b045d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 48 deletions.
8 changes: 5 additions & 3 deletions lua/nvim-autopairs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
56 changes: 31 additions & 25 deletions lua/nvim-autopairs/conds.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(...)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 changes: 1 addition & 1 deletion lua/nvim-autopairs/rules/endwise-lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'}),
}


Expand Down
8 changes: 6 additions & 2 deletions lua/nvim-autopairs/ts-rule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
2 changes: 1 addition & 1 deletion lua/nvim-autopairs/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 7 additions & 8 deletions tests/endwise_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,23 +12,22 @@ 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 = [[if data== 'fdsafdsa' then| ]],
after = [[end ]]
before = [[function a()| ]],
after = [[ end ]]
},
{
-- only = true;
name = "add newline have endwise" ,
name = "add if endwise" ,
filepath = './tests/endwise/init.lua',
filetype = "lua",
linenr = 5,
key = [[<cr>]],
before = [[if data== 'fdsafdsa' then|end]],
after = [[end]]
before = [[if data== 'fdsafdsa' then| ]],
after = [[end ]]
},
{
name = "don't add endwise on match rule" ,
Expand Down
2 changes: 2 additions & 0 deletions tests/minimal.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +14,7 @@ set noswapfile
set nobackup

filetype indent off
set expandtab!
set nowritebackup
set noautoindent
set nocindent
Expand Down
28 changes: 21 additions & 7 deletions tests/nvim-autopairs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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 = [["]],
Expand Down Expand Up @@ -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="*",
Expand Down
2 changes: 1 addition & 1 deletion tests/treesitter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down

0 comments on commit 92b045d

Please sign in to comment.