This commit is contained in:
windwp 2021-04-18 08:40:38 +07:00 committed by windwp
parent fd20894296
commit ce60a10e6d
7 changed files with 51 additions and 286 deletions

1
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1 @@
github:windwp

32
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,32 @@
name: Tests
on: [push, pull_request]
jobs:
x64-ubuntu:
name: X64-ubuntu
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- run: date +%F > todays-date
- name: Restore cache for today's nightly.
uses: actions/cache@v2
with:
path: |
_neovim
key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }}
- name: Prepare
run: |
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/nvim-lua/popup.nvim ~/.local/share/nvim/site/pack/vendor/start/popup.nvim
git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter ~/.local/share/nvim/site/pack/vendor/start/nvim-treesitter
git clone --depth 1 https://github.com/nvim-treesitter/playground ~/.local/share/nvim/site/pack/vendor/start/playground
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
- name: Run tests
run: |
curl -OL https://raw.githubusercontent.com/norcalli/bot-ci/master/scripts/github-actions-setup.sh
source github-actions-setup.sh nightly-x64
nvim --headless -u tests/minimal.vim -c "TSInstallSync all" -c "q"
make test

View File

@ -40,7 +40,7 @@ npairs.add_rule({
})
-- you can use some builtin condition
local cond = require('nvim-autopairs.cond')
local cond = require('nvim-autopairs.conds')
print(vim.inspect(cond))
npairs.add_rules({

View File

@ -93,7 +93,6 @@ M.autopairs_bs = function(bufnr)
line = line
})
then
log.debug('delete')
local input = ""
for _ = 1, (#rule.start_pair), 1 do
input = input .. utils.key.bs
@ -101,7 +100,6 @@ M.autopairs_bs = function(bufnr)
for _ = 1, #rule.end_pair, 1 do
input = input .. utils.key.right .. utils.key.bs
end
log.debug(input)
return utils.esc("<c-g>U" .. input)
end
end

View File

@ -1,259 +0,0 @@
local MPairs = {}
local pairs_map = {
["'"] = "'",
['"'] = '"',
['('] = ')',
['['] = ']',
['{'] = '}',
['`'] = '`',
}
local disable_filetype = { "TelescopePrompt" }
local check_line_pair = true
local close_triple_quotes = false
local break_line_rule ={
{
pairs_map = {
['('] = ')',
['['] = ']',
['{'] = '}',
},
disable_filetype = nil,
filetype = nil
},
{
pairs_map = {
['>'] = '<',
},
disable_filetype = nil,
filetype = { 'html' , 'vue' , 'typescriptreact' , 'svelte' , 'javascriptreact' }
}
}
local ignored_next_char = "%w"
local function is_in_table(tbl, val)
for _, value in pairs(tbl) do
if string.match(val, value) then return true end
end
return false
end
local function check_filetype(tbl, filetype)
if tbl == nil then return true end
return is_in_table(tbl, filetype)
end
local function check_disable_ft(tbl, filetype)
if tbl == nil then return true end
return not is_in_table(tbl, filetype)
end
MPairs.setup = function(opts)
opts = opts or {}
pairs_map = opts.pairs_map or pairs_map
disable_filetype = opts.disable_filetype or disable_filetype
if opts.check_line_pair ~= nil then check_line_pair = opts.check_line_pair end
break_line_rule[1].filetypes = opts.break_line_filetype or break_line_rule[1].filetype
break_line_rule[1].disable_filetype = opts.break_line_disable_filetype or disable_filetype
break_line_rule[2].filetype = opts.html_break_line_filetype or break_line_rule[2].filetype
break_line_rule[2].disable_filetype = opts.html_break_line_disable_filetype or disable_filetype
ignored_next_char = opts.ignored_next_char or ignored_next_char
close_triple_quotes = opts.close_triple_quotes or close_triple_quotes
for char, char_end in pairs(pairs_map) do
if string.len(char) == 1 then
local mapCommand = string.format([[v:lua.MPairs.autopairs("%s","%s")]], char, char_end)
if char == '"' then
mapCommand = string.format([[v:lua.MPairs.autopairs('%s','%s')]], char, char_end)
end
vim.api.nvim_set_keymap('i', char, mapCommand, {expr = true, noremap = true})
-- map char to move right when close pairs
if char== "(" or char == '[' or char == "{" then
mapCommand = string.format([[v:lua.MPairs.check_jump('%s')]], char_end)
vim.api.nvim_set_keymap('i', char_end, mapCommand, {expr = true, noremap = true})
end
else
print(string.format("Skip [%s], autopairs plugin is not support that "), char)
end
end
-- delete pairs when press <bs>
vim.api.nvim_set_keymap('i', "<bs>", "v:lua.MPairs.autopair_bs()", {expr = true, noremap = true})
end
local function esc(cmd)
return vim.api.nvim_replace_termcodes(cmd, true, false, true)
end
MPairs.autopairs = function(char, char_end)
local result= MPairs.check_add(char)
if result == 1 then
return esc(char..char_end.."<c-g>U<left>")
elseif result == 2 then
return esc("<c-g>U<right>")
elseif result == 3 then
return esc(char..char..char..char.."<c-g>U<left><left><left>")
else
return esc(char)
end
end
MPairs.check_jump = function(char)
local next_col = vim.fn.col('.')
local line = vim.fn.getline('.')
local next_char = line:sub(next_col, next_col)
if char == next_char then
return esc("<c-g>U<right>")
end
return esc(char)
end
local is_quote = function (char)
return char == "'" or char == '"' or char == '`'
end
local function is_in_quote(line, pos, quote)
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
result = false
elseif result == false and char == quote then
result = true
end
end
return result
end
MPairs.check_add = function(char)
if not check_disable_ft(disable_filetype, vim.bo.filetype) then return 0 end
local next_col = vim.fn.col('.')
local line = vim.fn.getline('.')
local next_char = line:sub(next_col, next_col)
local prev_char = line:sub(next_col -1, next_col -1)
local prevprev_char = line:sub(next_col -2, next_col -2)
local nextnext_char = line:sub(next_col +1, next_col +1)
if close_triple_quotes then
-- Keep adding one quote if surrounded by multiple quotes
if (prev_char == "'" or prev_char == '"' or prev_char == '`') and prevprev_char == prev_char and prev_char == char and char == next_char and next_char == nextnext_char then
return 1
end
-- Support three quotes
if (prev_char == "'" or prev_char == '"' or prev_char == '`') and prevprev_char == prev_char and prev_char == char then
return 3
end
end
-- move right when have quote on end line or in quote
-- situtaion |" => "|
if is_quote(next_char) and next_char == char then
if next_col == string.len(line) then
return 2
end
-- ("|") => (""|)
-- "" |" " => "" "| "
if is_in_quote(line, next_col - 1, char) then
return 2
end
end
-- don' t add single quote if prev char is a word
-- a| => not add
if char == "'" and prev_char:match("%w")then
return 0
end
-- situtaion |( => not add
if next_char == char then
return 0
end
-- don't add pairs based on `ignored_next_char`
-- situation: ignored_next_char = "%w" -> |abcde => not add
-- ignored_next_char = "[%w%.]" -> |.abcde => not add
if next_char:match(ignored_next_char) then
return 0
end
if check_line_pair then
local char_end = pairs_map[char]
-- (( many char |)) => add
-- ( many char |)) => not add
if next_char == char_end then
local count_prev_char = 0
local count_next_char = 0
for i = 1, #line, 1 do
local c = line:sub(i, i)
if c == char then
count_prev_char = count_prev_char + 1
elseif c == char_end then
count_next_char = count_next_char + 1
end
end
if count_prev_char ~= count_next_char then
return 0
end
end
-- " abc | xyz => not add
if is_quote(char) and is_in_quote(line, next_col -1, char) then
return 0
end
end
return 1
end
-- break line on <CR> and html
-- use it for add new line after enter
MPairs.check_break_line_char = function()
local result = 0
local prev_col = vim.fn.col('.') - 1
local next_col = vim.fn.col('.')
local prev_char = vim.fn.getline('.'):sub(prev_col, prev_col)
local next_char = vim.fn.getline('.'):sub(next_col, next_col)
for _, rule in pairs(break_line_rule) do
if
result == 0 and rule.pairs_map[prev_char] == next_char and
check_filetype(rule.filetype, vim.bo.filetype) and
check_disable_ft(rule.disable_filetype, vim.bo.filetype)
then
result = 1
break
end
end
if result == 1 then
return esc("<cr><c-o>O")
end
return esc("<cr>")
end
-- delete pair on <bs>
MPairs.autopair_bs = function()
local next_col = vim.fn.col('.')
local line = vim.fn.getline('.')
local next_char = line:sub(next_col, next_col)
local prev_char = line:sub(next_col - 1 , next_col - 1)
local char_end = pairs_map[prev_char]
if char_end ~= nil and next_char == char_end then
return esc("<c-g>U<bs><right><bs>")
end
return esc("<bs>")
end
MPairs.esc = esc
_G.MPairs = MPairs
return MPairs
-- vim: ts=2 sw=2

View File

@ -52,7 +52,6 @@ end
function Rule:with_pair(cond)
if self.pair_cond == nil then self.pair_cond = {}end
table.insert(self.pair_cond, cond)
if self=="dafs" the
return self
end

View File

@ -1,5 +1,7 @@
local helpers = {}
local npairs = require('nvim-autopairs')
local Rule = require('nvim-autopairs.rule')
local cond = require('nvim-autopairs.conds')
local log = require('nvim-autopairs._log')
_G.npairs = npairs;
local eq=_G.eq
@ -17,7 +19,6 @@ end
local data = {
{
name = "add normal bracket" ,
key = [[{]],
before = [[x| ]],
@ -165,14 +166,6 @@ local data = {
before = [[aaa(|) ]],
after = [[aaa| ]]
},
-- {
-- only = true,
-- name = "delete bracket",
-- filetype="python",
-- key = [[<bs>]],
-- before = [[a"""|"""" ]],
-- after = [[a| ]]
-- },
{
name = "breakline on {" ,
filetype="javascript",
@ -253,19 +246,20 @@ end
describe('autopairs ', function()
Test(run_data)
if isOnly then return end
-- run_data = {
-- {
-- name = "nil breakline_file_type " ,
-- filetype="javascript",
-- key = [[<cr>]],
-- before = [[a[|] ]],
-- after = "] "
-- },
-- }
npairs.add_rules({
Rule("$$", "$$",{"tex", "latex"})
-- don't add a pair if the next character is %
:with_pair(cond.not_after_regex_check("%%"))
})
run_data = {
{
name = "test add_rules" ,
filetype = "latex",
key = [[$]],
before = [[asdas$| ]],
after = [[asdas$$|$$ ]],
},
}
Test(run_data)
-- npairs.setup({
-- break_line_filetype = nil
-- })
-- Test(run_data)
end)