From 92b045dd1f6f2d292819a75310c301266b1675f9 Mon Sep 17 00:00:00 2001 From: windwp Date: Fri, 23 Jul 2021 14:40:01 +0700 Subject: [PATCH] fix: make column value work on another condition add endwise is_end_line function --- lua/nvim-autopairs.lua | 8 ++-- lua/nvim-autopairs/conds.lua | 56 +++++++++++++----------- lua/nvim-autopairs/rules/endwise-lua.lua | 2 +- lua/nvim-autopairs/ts-rule.lua | 8 +++- lua/nvim-autopairs/utils.lua | 2 +- tests/endwise_spec.lua | 23 +++++----- tests/minimal.vim | 2 + tests/nvim-autopairs_spec.lua | 28 +++++++++--- tests/treesitter_spec.lua | 2 +- 9 files changed, 79 insertions(+), 52 deletions(-) diff --git a/lua/nvim-autopairs.lua b/lua/nvim-autopairs.lua index 84e237c..0fbee75 100644 --- a/lua/nvim-autopairs.lua +++ b/lua/nvim-autopairs.lua @@ -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, diff --git a/lua/nvim-autopairs/conds.lua b/lua/nvim-autopairs/conds.lua index f9bca7e..6889d50 100644 --- a/lua/nvim-autopairs/conds.lua +++ b/lua/nvim-autopairs/conds.lua @@ -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 diff --git a/lua/nvim-autopairs/rules/endwise-lua.lua b/lua/nvim-autopairs/rules/endwise-lua.lua index e9080cd..21fbdf9 100644 --- a/lua/nvim-autopairs/rules/endwise-lua.lua +++ b/lua/nvim-autopairs/rules/endwise-lua.lua @@ -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'}), } diff --git a/lua/nvim-autopairs/ts-rule.lua b/lua/nvim-autopairs/ts-rule.lua index 12fdec8..b01824a 100644 --- a/lua/nvim-autopairs/ts-rule.lua +++ b/lua/nvim-autopairs/ts-rule.lua @@ -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 } diff --git a/lua/nvim-autopairs/utils.lua b/lua/nvim-autopairs/utils.lua index 893a4de..c2e9492 100644 --- a/lua/nvim-autopairs/utils.lua +++ b/lua/nvim-autopairs/utils.lua @@ -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 diff --git a/tests/endwise_spec.lua b/tests/endwise_spec.lua index e943a08..824693d 100644 --- a/tests/endwise_spec.lua +++ b/tests/endwise_spec.lua @@ -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' , '','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 = [[]], + 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 = [[]], - before = [[if data== 'fdsafdsa' then|end]], - after = [[end]] - }, { name = "don't add endwise on match rule" , filepath = './tests/endwise/init.lua', diff --git a/tests/minimal.vim b/tests/minimal.vim index 878532f..c5a29d2 100644 --- a/tests/minimal.vim +++ b/tests/minimal.vim @@ -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 diff --git a/tests/nvim-autopairs_spec.lua b/tests/nvim-autopairs_spec.lua index 1c4c89e..3d1e1ef 100644 --- a/tests/nvim-autopairs_spec.lua +++ b/tests/nvim-autopairs_spec.lua @@ -26,12 +26,6 @@ npairs.add_rules({ opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char) .."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' , '','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="*", diff --git a/tests/treesitter_spec.lua b/tests/treesitter_spec.lua index 536b989..7fb0f01 100644 --- a/tests/treesitter_spec.lua +++ b/tests/treesitter_spec.lua @@ -20,7 +20,7 @@ npairs.add_rules({ vim.api.nvim_set_keymap('i' , '','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} }