Skip to content

Commit

Permalink
add treesitter node
Browse files Browse the repository at this point in the history
  • Loading branch information
windwp committed Apr 21, 2021
1 parent bc18313 commit 5eed64c
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 123 deletions.
48 changes: 41 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require('nvim-autopairs').setup()

local disable_filetype = { "TelescopePrompt" }
local ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", "")
local check_ts = false,

```

Expand Down Expand Up @@ -155,18 +156,51 @@ npairs.add_rules({
-- you can do anything with regex +special key
-- example press tab will upper text
-- press b1234s<tab> => B1234S1234S
Rule("b%d%d%d%d%w$", "", "vim")
:use_regex(true,"<tab>")
:replace_endpair(function(opts)
return
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
.."<esc>viwU"
end)

npairs.add_rules({
Rule("b%d%d%d%d%w$", "", "vim")
:use_regex(true,"<tab>")
:replace_endpair(function(opts)
return
opts.prev_char:sub(#opts.prev_char - 4,#opts.prev_char)
.."<esc>viwU"
end)
})
--- check ./lua/nvim-autopairs/rules/basic.lua

```

### Treesitter
You can use treesitter to check

```lua
local npairs = require("nvim-autopairs")

npairs.setup({
check_ts = true,
ts_config = {
lua = {'string', 'comment'}-- it will not add pair on that treesitter node
javascript = {'template_string', 'comment'}
java = false,-- don't check treesitter on java
}
})

require('nvim-treesitter.configs').setup {
autopairs = {enable = true}
}

local ts_conds = require('nvim-autopairs.ts-conds')


-- press % => %% is only inside comment or string
npairs.add_rules({
Rule("%", "%", "lua")
:with_pair(ts_conds.is_ts_node({'string','comment'})),
Rule("$", "$", "lua")
:with_pair(ts_conds.is_not_ts_node({'function'}))
})
```

### Don't add pairs if it already have a close pairs in same line

if **next character** is a close pairs and it doesn't have an open pairs in same line then it will not add a close pairs
Expand Down
2 changes: 1 addition & 1 deletion doc/endwise.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Endwise (experiment)

** warning** you endwise base on treesitter is not always correct.
** warning** use endwise base on treesitter is not always correct.
treesitter group all error node with parent node so we can't find
a perfect solution for this.

Expand Down
82 changes: 64 additions & 18 deletions lua/nvim-autopairs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,47 @@ local api = vim.api

local M={}

local state = {}
M.state = {
disabled=false,
rules = {},
buf_ts = {}
}

local default = {
disable_filetype = {"TelescopePrompt", "spectre_panel"},
ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", "")
ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]],"%s+", ""),
check_ts = false,
ts_config = {
lua = {'string', 'comment'}
}
}

M.init = function()
require "nvim-treesitter".define_modules {
autopairs = {
module_path = 'nvim-autopairs.internal',
is_supported = function()
return true
end
}
}

end

M.setup = function(opt)
M.config = vim.tbl_extend('force', default, opt or {})
M.config.rules = basic_rule.setup(M.config)

if M.config.check_ts then
local ok, ts_rule = pcall(require, 'nvim-autopairs.rules.ts_basic')
if ok then
M.config.rules = ts_rule.setup(M.config)
else
print("you need to install treesitter")
end

end

api.nvim_exec ([[
augroup autopairs_buf
autocmd!
Expand Down Expand Up @@ -48,15 +79,15 @@ M.clear_rules = function()
end

M.disable=function()
state.disabled = true
M.state.disabled = true
end

M.enable = function()
state.disabled = false
M.state.disabled = false
end

M.on_attach = function(bufnr)
if state.disabled then return end
if M.state.disabled then return end
bufnr = bufnr or api.nvim_get_current_buf()
if not utils.check_disable_ft(M.config.disable_filetype, vim.bo.filetype) then return end
local rules = {};
Expand All @@ -70,10 +101,20 @@ M.on_attach = function(bufnr)
return (#a.start_pair or 0) > (#b.start_pair or 0)
end)

state.rules = rules
M.state.rules = rules

if M.state.buf_ts[bufnr] == true then
M.state.ts_node = M.config.ts_config[vim.bo.filetype]
if M.state.ts_node == nil then
M.state.ts_node = {'string', 'comment'}
end
else
M.state.ts_node = nil
end

if utils.is_attached(bufnr) then return end
local enable_insert_auto = false
for _, rule in pairs(state.rules) do
for _, rule in pairs(M.state.rules) do
if rule.is_regex == false then
if rule.key_map == "" then
rule.key_map = rule.start_pair:sub((#rule.start_pair))
Expand Down Expand Up @@ -114,10 +155,10 @@ M.on_attach = function(bufnr)
end

M.autopairs_bs = function(bufnr)
if state.disabled then return end
if M.state.disabled then return end
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
for _, rule in pairs(state.rules) do
for _, rule in pairs(M.state.rules) do
if rule.start_pair then

local prev_char, next_char = utils.text_cusor_line(
Expand All @@ -131,6 +172,7 @@ M.autopairs_bs = function(bufnr)
utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
and rule.end_pair == next_char
and rule:can_del({
ts_node = M.state.ts_node,
bufnr = bufnr,
prev_char = prev_char,
next_char = next_char,
Expand All @@ -156,26 +198,27 @@ local skip_next = false


M.autopairs_map = function(bufnr, char)
if state.disabled then return end
if M.state.disabled then return end
if skip_next then skip_next = false return end
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
local new_text = line:sub(1, col) .. char .. line:sub(col + 1,#line)
local add_char = 1
for _, rule in pairs(state.rules) do
for _, rule in pairs(M.state.rules) do
if rule.start_pair then
if rule.is_regex and rule.key_map ~= "" then
new_text = line:sub(1, col) .. line:sub(col + 1,#line)
add_char = 0
end
log.debug("new_text:[" .. new_text .. "]")
-- log.debug("new_text:[" .. new_text .. "]")
local prev_char, next_char = utils.text_cusor_line(
new_text,
col+ add_char,
#rule.start_pair,
#rule.end_pair, rule.is_regex
)
local cond_opt = {
ts_node = M.state.ts_node,
text = new_text,
rule = rule,
bufnr = bufnr,
Expand Down Expand Up @@ -208,12 +251,12 @@ M.autopairs_map = function(bufnr, char)
return char
end
M.autopairs_insert = function(bufnr, char)
if state.disabled then return end
if M.state.disabled then return end
if skip_next then skip_next = false return end
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
local new_text = line:sub(1, col) .. char .. line:sub(col + 1,#line)
for _, rule in pairs(state.rules) do
for _, rule in pairs(M.state.rules) do
if rule.start_pair and rule.is_regex and rule.key_map == "" then
local prev_char, next_char = utils.text_cusor_line(
new_text,
Expand All @@ -222,6 +265,7 @@ M.autopairs_insert = function(bufnr, char)
#rule.end_pair, rule.is_regex
)
local cond_opt = {
ts_node = M.state.ts_node,
text = new_text,
rule = rule,
bufnr = bufnr,
Expand Down Expand Up @@ -262,12 +306,12 @@ M.autopairs_insert = function(bufnr, char)
end

M.autopairs_cr = function(bufnr)
if state.disabled then return end
if M.state.disabled then return end
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
for _, rule in pairs(M.state.rules) do
if rule.start_pair then
local prev_char, next_char = utils.text_cusor_line(
line,
Expand All @@ -282,7 +326,8 @@ M.autopairs_cr = function(bufnr)
rule.is_endwise
and utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
and rule:can_cr({
check_ts = true,
ts_node = M.state.ts_node,
check_endwise_ts = true,
bufnr = bufnr,
rule = rule,
prev_char = prev_char,
Expand All @@ -301,7 +346,8 @@ M.autopairs_cr = function(bufnr)
utils.is_equal(rule.start_pair, prev_char, rule.is_regex)
and rule.end_pair == next_char
and rule:can_cr({
check_ts = false,
ts_node = M.state.ts_node,
check_endwise_ts = false,
bufnr = bufnr,
rule = rule,
prev_char = prev_char,
Expand Down
15 changes: 15 additions & 0 deletions lua/nvim-autopairs/internal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local log = require('nvim-autopairs._log')

local M = {}

M.attach = function (bufnr)
log.debug('treesitter.attach')
MPairs.state.buf_ts[bufnr] = true
end

M.detach = function (bufnr )
MPairs.state.buf_ts[bufnr] =nil
end

-- _G.AUTO = M
return M
24 changes: 24 additions & 0 deletions lua/nvim-autopairs/rules/ts_basic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local basic = require('nvim-autopairs.rules.basic')
local utils = require('nvim-autopairs.utils')
local ts_conds = require('nvim-autopairs.ts-conds')
local log = require('nvim-autopairs._log')
local ts_extend = {
"'",
'"',
'(',
'[',
'{',
'`',
}
return {
setup = function (config)
local rules=basic.setup(config)
for _, rule in pairs(rules) do
if utils.is_in_table(ts_extend, rule.start_pair) then
log.debug(rule.start_pair)
rule:with_pair(ts_conds.is_not_ts_node_comment())
end
end
return rules
end
}
Loading

0 comments on commit 5eed64c

Please sign in to comment.