add fast_wrap

This commit is contained in:
windwp 2021-06-25 10:01:48 +07:00
parent 86050e29dc
commit 7ff2f721cd
5 changed files with 222 additions and 127 deletions

View File

@ -223,7 +223,7 @@ local npairs = require("nvim-autopairs")
npairs.setup({
check_ts = true,
ts_config = {
lua = {'string'},-- it will not add pair on that treesitter node
lua = {'string',-- it will not add pair on that treesitter node
javascript = {'template_string'},
java = false,-- don't check treesitter on java
}
@ -287,6 +287,33 @@ Before Input After
require('nvim-autopairs').get_rule('"') -- get rule " then modify it
```
### FastWrap
``` text
Before Input After
--------------------------------------------------
(|foobar <M-e> then press $ (|foobar)
(|)(foobar) <M-e> then press q (|(foobar))
```
```lua
-- put this to setup function and press <a-e> to use fast_wrap
npairs.setup({
fast_wrap = {},
})
-- change default fast_wrap
npairs.setup({
fast_wrap = {
map = '<M-e>',
chars = { '{', '[', '(', '"', "'" },
pattern = string.gsub([[ [%'%"%)%>%]%)%}%,] ]], '%s+', ''),
end_key = '$',
keys = 'qwertyuiopzxcvbnmasdfghjkl',
check_comma = true
},
})
```
### autotag html and tsx

View File

@ -18,8 +18,6 @@ local default = {
enable_moveright = true,
enable_afterquote = true,
enable_check_bracket_line = true,
enable_fastwrap_key = "<M-e>",
fastwrap_previous_char = string.gsub([[ [%'%"%)%>%]%)%}] ]],"%s+", ""),
ts_config = {
lua = {'string', 'source'},
javascript = {'string', 'template_string'}
@ -42,6 +40,9 @@ end
M.setup = function(opt)
M.config = vim.tbl_deep_extend('force', default, opt or {})
if M.config.fast_wrap then
require('nvim-autopairs.fastwrap').setup(M.config.fast_wrap)
end
M.config.rules = basic_rule.setup(M.config)
if M.config.check_ts then
@ -189,10 +190,10 @@ M.on_attach = function(bufnr)
bufnr, bufnr, bufnr), false)
end
if M.config.enable_fastwrap_key then
api.nvim_buf_set_keymap(bufnr,"i",
M.config.enable_fastwrap_key,
"<cmd>lua require('nvim-autopairs').autopairs_fastwrap()<cr>",
if M.config.fast_wrap and M.config.fast_wrap.map then
api.nvim_buf_set_keymap(bufnr, "i",
M.config.fast_wrap.map,
"<esc>l<cmd>lua require('nvim-autopairs.fastwrap').show()<cr>",
{noremap = true})
end
@ -460,60 +461,6 @@ M.autopairs_afterquote = function(line, key_char)
return utils.esc(key_char)
end
M.autopairs_fastwrap = function(line)
line = line or utils.text_get_current_line(0)
local _, col = utils.get_cursor()
local prev_char, next_char = utils.text_cusor_line(line, col, 1, 1, false)
local end_pair = ''
if
utils.is_bracket(prev_char)
and not utils.is_in_quote(line, col, next_char)
then
for _, rule in pairs(M.state.rules) do
if rule.start_pair == prev_char then
end_pair = rule.end_pair
end
end
if end_pair == '' then return end
local is_prev_slash = false
local end_pair_pos = 0
local target_pos = 0
for i = col + 3, #line, 1 do
local char = line:sub(i, i )
if not is_prev_slash and char == end_pair then
end_pair_pos = i
end
if string.match(char, M.config.fastwrap_previous_char) then
if target_pos == 0 then
target_pos = i + 1
elseif end_pair_pos > 0 and i > end_pair_pos then
target_pos = i + 1
else
target_pos = 0
end
end
-- if char == ' ' then
-- if target_pos == 0 then
-- target_pos = i -1
-- elseif end_pair_pos > 0 and i > end_pair_pos then
-- target_pos = i -1
-- break
-- end
-- end
is_prev_slash = char == '\\'
end
if target_pos > 0 then
if end_pair_pos > 0 then
line = line:sub(1, end_pair_pos -1) .. line:sub(end_pair_pos + 1,#line)
end
line = line:sub(1, target_pos -1) .. end_pair .. line:sub(target_pos,#line)
else
line = line .. end_pair
end
vim.fn.setline('.', line)
end
end
M.autopairs_closequote_expr = function ()
vim.fn.setline('.', M.state.expr_quote)
end

View File

@ -0,0 +1,121 @@
local utils = require('nvim-autopairs.utils')
local log = require('nvim-autopairs._log')
local npairs = require('nvim-autopairs')
local M = {}
local default_config = {
map = '<M-e>',
chars = { '{', '[', '(', '"', "'" },
pattern = string.gsub([[ [%'%"%)%>%]%)%}%,] ]], '%s+', ''),
end_key = '$',
keys = 'qwertyuiopzxcvbnmasdfghjkl',
check_comma = true,
}
local state = npairs.state
M.ns_fast_wrap = vim.api.nvim_create_namespace('autopairs_fastwrap')
local config = {}
M.setup = function(cfg)
if config.chars == nil then
config = vim.tbl_extend('force', default_config, cfg or {})
npairs.config.fast_wrap = config
npairs.config.ignored_next_char = string.gsub([[ [%w%%%'%(%{%[%"%.] ]], '%s+', '')
end
end
M.setup()
function M.getchar_handler()
local ok, key = pcall(vim.fn.getchar)
if not ok then
return nil
end
if type(key) == 'number' then
local key_str = vim.fn.nr2char(key)
return key_str
end
return nil
end
M.show = function(line)
line = line or utils.text_get_current_line(0)
log.debug(line)
local row, col = utils.get_cursor()
local prev_char = utils.text_cusor_line(line, col, 1, 1, false)
local end_pair = ''
if utils.is_in_table(config.chars, prev_char) then
for _, rule in pairs(state.rules) do
if rule.start_pair == prev_char then
end_pair = rule.end_pair
end
end
if end_pair == '' then
return
end
local list_pos = {}
local index = 1
local str_length = #line
local is_have_end = false
for i = col + 2, #line, 1 do
local char = line:sub(i, i)
if string.match(char, config.pattern) then
local key = config.keys:sub(index, index)
index = index + 1
if str_length == i then
key = config.end_key
is_have_end = true
end
table.insert(list_pos, { col = i, key = key, char = char })
end
end
if not is_have_end then
table.insert(list_pos, { col = str_length + 1, key = config.end_key })
end
M.highlight_wrap(list_pos, row)
vim.defer_fn(function()
local char = M.getchar_handler()
for _, pos in pairs(list_pos) do
if char == pos.key then
M.move_bracket(line, pos.col, end_pair, pos.char)
end
end
vim.api.nvim_buf_clear_namespace(0, M.ns_fast_wrap, row - 1, row + 1)
vim.cmd('startinsert')
end, 10)
end
end
M.move_bracket = function(line, target_pos, end_pair, char_map)
line = line or utils.text_get_current_line(0)
local _, col = utils.get_cursor()
local _, next_char = utils.text_cusor_line(line, col, 1, 1, false)
-- remove an autopairs if that exist
-- ((fsadfsa)) dsafdsa
if next_char == end_pair then
line = line:sub(1, col) .. line:sub(col + 2, #line)
target_pos = target_pos - 1
end
if config.check_comma and char_map == ',' then
target_pos = target_pos - 1
end
line = line:sub(1, target_pos) .. end_pair .. line:sub(target_pos + 1, #line)
vim.fn.setline('.', line)
end
M.highlight_wrap = function(tbl_pos, row)
local bufnr = vim.api.nvim_win_get_buf(0)
for _, pos in ipairs(tbl_pos) do
vim.api.nvim_buf_set_extmark(bufnr, M.ns_fast_wrap, row, pos.col - 1, {
virt_text = { { pos.key, 'Search' } },
virt_text_pos = 'overlay',
hl_mode = 'blend',
})
end
end
return M

View File

@ -40,25 +40,23 @@ M.is_equal = function (value,text, is_regex)
end
M.is_in_quote = function(line, pos, quote)
local cIndex = 0
local result = false
local cIndex = 0
local result = false
while cIndex < string.len(line) and cIndex < pos do
cIndex = cIndex + 1
local char = line:sub(cIndex, cIndex)
if
result == true and
char == quote and
line:sub(cIndex -1, cIndex -1) ~= "\\"
then
log.debug('match')
result = false
elseif result == false and char == quote then
log.debug('match')
result = true
while cIndex < string.len(line) and cIndex < pos do
cIndex = cIndex + 1
local char = line:sub(cIndex, cIndex)
if
result == true and
char == quote and
line:sub(cIndex -1, cIndex -1) ~= "\\"
then
result = false
elseif result == false and char == quote then
result = true
end
end
end
return result
return result
end
M.is_attached = function(bufnr)

View File

@ -1,54 +1,56 @@
local npairs = require('nvim-autopairs')
-- local npairs = require('nvim-autopairs')
_G.npairs = npairs
-- _G.npairs = npairs
npairs.setup({
enable_afterquote = true,
})
local data = {
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[const abc=(|"test",data ]],
after = [[const abc=(|"test"),data ]],
},
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[const abc=(|"test"),data ]],
after = [[const abc=(|"test",data) ]],
},
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[const abc=(|"test",data),dadas ]],
after = [[const abc=(|"test",data,dadas) ]],
},
{
name = 'move end wise after quote ',
filepath = './tests/endwise/init.lua',
filetype = 'lua',
linenr = 5,
key = [[<M-e>]],
before = [[Plug {(|'dsfdsa',) on = 'aaa'} ]],
after = [[Plug {('dsfdsa', on = 'aaa')} ]],
},
}
-- npairs.setup({
-- enable_afterquote = true,
-- fast_wrap = true,
-- })
-- local data = {
-- {
-- only = true,
-- name = 'move end wise after quote ',
-- filepath = './tests/endwise/init.lua',
-- filetype = 'lua',
-- linenr = 5,
-- key = [[<M-e>]],
-- before = [[const abc=(|"test",data ]],
-- after = [[const abc=(|"test"),data ]],
-- },
-- {
-- name = 'move end wise after quote ',
-- filepath = './tests/endwise/init.lua',
-- filetype = 'lua',
-- linenr = 5,
-- key = [[<M-e>]],
-- before = [[const abc=(|"test"),data ]],
-- after = [[const abc=(|"test",data) ]],
-- },
-- {
-- name = 'move end wise after quote ',
-- filepath = './tests/endwise/init.lua',
-- filetype = 'lua',
-- linenr = 5,
-- key = [[<M-e>]],
-- before = [[const abc=(|"test",data),dadas ]],
-- after = [[const abc=(|"test",data,dadas) ]],
-- },
-- {
-- name = 'move end wise after quote ',
-- filepath = './tests/endwise/init.lua',
-- filetype = 'lua',
-- linenr = 5,
-- key = [[<M-e>]],
-- before = [[Plug {(|'dsfdsa',) on = 'aaa'} ]],
-- after = [[Plug {('dsfdsa', on = 'aaa')} ]],
-- },
-- }
local run_data = _G.Test_filter(data)
-- local run_data = _G.Test_filter(data)
local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
_G.TU = ts_utils
-- local _, ts_utils = pcall(require, 'nvim-treesitter.ts_utils')
-- _G.TU = ts_utils
describe('[endwise tag]', function()
_G.Test_withfile(run_data, {})
end)
-- describe('[endwise tag]', function()
-- _G.Test_withfile(run_data, {})
-- end)