add markdown rule and add docs for endwise

This commit is contained in:
windwp 2021-04-19 10:36:48 +07:00
parent 57080c4c96
commit 8a5dbfcbcc
9 changed files with 77 additions and 11 deletions

View File

@ -194,4 +194,14 @@ Before Input After
|+foobar ( (|)+foobar
```
### Plugin Intergation
if you use another plugin you can temporary to disable autopair
``` lua
require('nvim-autopairs').disable()
-- do your task
require('nvim-autopairs').enable()
```
### Endwise
[endwise](./doc/endwise.md)

41
doc/endwise.md Normal file
View File

@ -0,0 +1,41 @@
#Endwise (experiment)
I don't have time to add rule for all language.
so you can create your rule and make a PR. Thank
read docs in readme.md about rules first.
and install TSPlayground
``` lua
local npairs=require('nvim-autopairs')
npairs.setup()
-- clear all rule if you don't want to use autopairs
npairs.clear_rules()
```
``` lua
local endwise = require('nvim-autopairs.ts-rule').endwise
npairs.add_rules({
-- then$ is a lua regex
-- end is a match pair
-- lua is a filetype
-- if_statement is a treesitter name you can use nil to skip it
endwise('then$', 'end', 'lua', 'if_statement')
})
```
-- run TSPlaygroudnToggle and get treesitter name
![treesitter](./images/endwise.png)
if that builtin endwise rule is not correct you can make your custom rule
conditon
take a look of that file and write your function
`lua/nvim-autopairs/ts-rule.lua`
`lua/nvim-autopairs/ts-conds.lua`

BIN
doc/images/endwise.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 516 KiB

View File

@ -178,6 +178,7 @@ M.autopairs_cr = function(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
-- log.debug("on_cr")
for _, rule in pairs(state.rules) do
if rule.start_pair then
local prev_char, next_char = utils.text_cusor_line(
@ -186,6 +187,9 @@ M.autopairs_cr = function(bufnr)
#rule.start_pair,
#rule.end_pair, rule.is_regex
)
-- log.debug('prev_char' .. rule.start_pair)
-- log.debug('prev_char' .. prev_char)
-- log.debug('next_char' .. next_char)
if
rule.is_endwise
and utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
@ -200,13 +204,13 @@ M.autopairs_cr = function(bufnr)
then
log.debug('do endwise')
return utils.esc(
rule.end_pair
.. utils.repeat_key(utils.key.left, 3)
.."<cr><esc><<O"
)
rule.end_pair
.. utils.repeat_key(utils.key.left, 3)
.. "<cr><esc><<O"
)
end
if
rule.start_pair == prev_char
utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
and rule.end_pair == next_char
and rule:can_cr({
check_ts = false,
@ -217,7 +221,7 @@ M.autopairs_cr = function(bufnr)
line = line
})
then
log.debug('do _cr')
log.debug('do_cr')
return utils.esc("<cr><c-o>O")
end
end

View File

@ -11,6 +11,12 @@ local function setup(opt)
local rules = {
Rule("<!--", "-->", 'html'):with_cr(cond.none()),
Rule("```", "```", 'markdown'),
Rule("```.*$", "```", 'markdown')
:with_move(cond.none())
:with_del(cond.none())
:with_pair(cond.none())
:use_regex(true)
,
Rule('"""', '"""', 'python'),
basic("'", "'")
:with_pair(cond.not_before_regex_check("%w")) ,

View File

@ -6,6 +6,8 @@ local parsers = require'nvim-treesitter.parsers'
conds.is_ts_node = function(nodename)
return function (opts)
if not opts.check_ts then return true end
if nodename == nil then return true end
parsers.get_parser():parse()
local target = ts_utils.get_node_at_cursor()
if target ~= nil and target:type() == nodename then

View File

@ -3,7 +3,7 @@ local cond = require('nvim-autopairs.conds')
local ts_conds = require('nvim-autopairs.ts-conds')
return {
endwise=function (...)
endwise = function (...)
local params = {...}
assert(type(params[4]) == 'string', 'type ')

View File

@ -29,9 +29,6 @@ M.is_close_bracket = function (char)
end
M.is_equal = function (value,text, is_regex)
log.debug(vim.inspect( is_regex))
log.debug("text" .. value)
log.debug("text" .. text)
if is_regex and string.match(text, value) then
return true
elseif text == value then

View File

@ -11,7 +11,6 @@ npairs.add_rules({
Rule("x%d%d%d%d*$", "number", "lua")
:use_regex(true)
:replace_endpair(function(opts)
-- print(vim.inspect(opts))
return opts.prev_char:sub(#opts.prev_char - 3,#opts.prev_char)
end)
@ -204,6 +203,13 @@ local data = {
before = [[a[|] ]],
after = "] "
},
{
name = "breakline on markdown " ,
filetype="markdown",
key = [[<cr>]],
before = [[``` lua|```]],
after = [[```]]
},
{
name = "breakline on < html" ,
filetype = "html",