Skip to content

Commit dbdbd78

Browse files
committed
add-plugin: nvim-keysitter, my plugin :)
1 parent d73982f commit dbdbd78

1 file changed

Lines changed: 62 additions & 176 deletions

File tree

Lines changed: 62 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,68 @@
1-
---@class TreesitterTextobjectGroup
2-
---@field group_leader string
3-
local TSTO_GROUP = {}
4-
5-
---@param query_group string
6-
---@param opts table<string,string>
7-
---@return any
8-
function TSTO_GROUP:new(query_group, opts)
9-
self.query_group = query_group
10-
self.__index = self
11-
return setmetatable(opts or {
12-
group_leader = '',
13-
}, self)
14-
end
15-
16-
---@param key string
17-
---@param object string
18-
---@return TreesitterTextobjectGroup
19-
function TSTO_GROUP:set(key, object)
20-
self.key = key
21-
self.object = object
22-
return self
23-
end
24-
25-
---@param query_name string
26-
---@return string|nil
27-
function TSTO_GROUP:textobject_check(query_name)
28-
local lang = vim.treesitter.language.get_lang(vim.bo.filetype) or ''
29-
local query = vim.treesitter.query.get(lang, self.query_group) or {}
30-
31-
return vim.iter(query.captures or {}):any(function(val)
32-
return val:match(query_name)
33-
end) and query_name or nil
34-
end
35-
36-
---@param attr string|nil
37-
---@return TreesitterTextobjectGroup
38-
function TSTO_GROUP:sel_outer(attr)
39-
attr = attr or 'outer'
40-
41-
local obj = TSTO_GROUP:textobject_check(self.object .. '.' .. attr)
42-
if not obj then
43-
return self
44-
end
45-
obj = '@' .. obj
46-
47-
vim.keymap.set({ 'x', 'o' }, 'a' .. self.group_leader .. self.key, function()
48-
require('nvim-treesitter-textobjects.select').select_textobject(obj, self.query_group)
49-
end, { buffer = true, desc = 'arround ' .. self.object })
50-
51-
return self
52-
end
53-
54-
---@param attr string|nil
55-
---@return TreesitterTextobjectGroup
56-
function TSTO_GROUP:sel_inner(attr)
57-
attr = attr or 'inner'
58-
59-
local obj = TSTO_GROUP:textobject_check(self.object .. '.' .. attr)
60-
if not obj then
61-
return self
62-
end
63-
obj = '@' .. obj
64-
65-
vim.keymap.set({ 'x', 'o' }, 'i' .. self.group_leader .. self.key, function()
66-
require('nvim-treesitter-textobjects.select').select_textobject(obj, self.query_group)
67-
end, { buffer = true, desc = 'inner ' .. self.object })
68-
69-
return self
70-
end
71-
72-
---@param opts table<string,string>
73-
---@return TreesitterTextobjectGroup
74-
function TSTO_GROUP:goto_start(opts)
75-
---@class opts
76-
---@field attribute? string
77-
---@field key? string
78-
---@field desc? string
79-
opts = vim.tbl_extend('keep', opts or {}, {
80-
attribute = 'outer',
81-
key = self.key,
82-
desc = 'start',
83-
})
84-
85-
local obj = TSTO_GROUP:textobject_check(self.object .. '.' .. opts.attribute)
86-
if not obj then
87-
return self
88-
end
89-
obj = '@' .. obj
90-
91-
vim.keymap.set({ 'n', 'x', 'o' }, ']' .. self.group_leader .. opts.key, function()
92-
require('nvim-treesitter-textobjects.move').goto_next_start(obj, self.query_group)
93-
end, { buffer = true, desc = 'next ' .. self.object .. ' ' .. opts.desc })
94-
vim.keymap.set({ 'n', 'x', 'o' }, '[' .. self.group_leader .. opts.key, function()
95-
require('nvim-treesitter-textobjects.move').goto_previous_start(obj, self.query_group)
96-
end, { buffer = true, desc = 'previous ' .. self.object .. ' ' .. opts.desc })
97-
98-
return self
99-
end
100-
101-
---@param attr string|nil
102-
---@return TreesitterTextobjectGroup
103-
function TSTO_GROUP:goto_end(opts)
104-
attr = attr or 'outer'
105-
opts = vim.tbl_extend('keep', opts or {}, {
106-
attribute = 'outer',
107-
key = self.key:upper(),
108-
desc = 'end',
109-
})
110-
111-
local obj = TSTO_GROUP:textobject_check(self.object .. '.' .. opts.attribute)
112-
if not obj then
113-
return self
114-
end
115-
obj = '@' .. obj
116-
117-
vim.keymap.set({ 'n', 'x', 'o' }, ']' .. self.group_leader .. opts.key, function()
118-
require('nvim-treesitter-textobjects.move').goto_next_end(obj, self.query_group)
119-
end, { buffer = true, desc = 'next ' .. self.object .. ' ' .. opts.desc })
120-
vim.keymap.set({ 'n', 'x', 'o' }, '[' .. self.group_leader .. opts.key, function()
121-
require('nvim-treesitter-textobjects.move').goto_previous_end(obj, self.query_group)
122-
end, { buffer = true, desc = 'previous ' .. self.object .. ' ' .. opts.desc })
123-
124-
return self
125-
end
126-
1271
return {
1282
'nvim-treesitter/nvim-treesitter-textobjects',
1293
branch = 'main',
130-
-- init = function()
131-
-- -- Disable entire built-in ftplugin mappings to avoid conflicts.
132-
-- -- See https://github.com/neovim/neovim/tree/master/runtime/ftplugin for built-in ftplugins.
133-
-- vim.g.no_plugin_maps = true
134-
--
135-
-- -- Or, disable per filetype (add as you like)
136-
-- -- vim.g.no_python_maps = true
137-
-- -- vim.g.no_ruby_maps = true
138-
-- -- vim.g.no_rust_maps = true
139-
-- -- vim.g.no_go_maps = true
140-
-- end,
4+
dependencies = {
5+
{
6+
'ABDsheikho/nvim-keysitter',
7+
-- dir = '~/Repos/nvim-keysitter/',
8+
-- name = 'keysitter',
9+
config = function()
10+
local keysitter = require 'keysitter'
11+
local tsto = keysitter.new('treesitter-textobjects', { group_prefix = 'o' })
12+
13+
-- a setup function
14+
tsto.setup({ 'FileType', 'BufEnter' }, 'keysitter', function()
15+
-- tsto:set('f', 'function'):around():inner():next():prev()
16+
17+
for k, v in pairs {
18+
-- ['b'] = 'block', -- or b for brace-less languages like python
19+
['f'] = 'function',
20+
['i'] = 'conditional',
21+
['l'] = 'loop',
22+
['r'] = 'return',
23+
['t'] = 'attribute',
24+
['x'] = 'regex',
25+
} do
26+
tsto:set(k, v):around():inner():next():prev()
27+
end
28+
29+
tsto:set('/', 'comment'):around():inner():goto_start()
30+
tsto:set('{', 'block'):around():inner():goto_start():goto_end { key = '}' }
31+
tsto:set('(', 'call'):around():inner():goto_start():goto_end { key = ')' }
32+
tsto:set(',', 'parameter'):around():inner():goto_start():goto_end { key = '.' }
33+
tsto:set(';', 'statement'):around():inner({ attribute = 'outer' }):goto_start():goto_end { key = ':' }
34+
35+
tsto
36+
:set('=', 'assignment')
37+
:around()
38+
:inner({ attribute = 'rhs' })
39+
:goto_start({ attribute = 'lhs' })
40+
:next_start({ attribute = 'rhs', key = '-' }, { desc = 'next assignment rhs' })
41+
:previous_start({ attribute = 'rhs', key = '-' }, { desc = 'previous assignment rhs' })
42+
43+
tsto
44+
:set('c', 'class')
45+
:around()
46+
:inner()
47+
:next_start({ motion = ']', group_prefix = '', key = ']' })
48+
:next_end({ motion = ']', group_prefix = '', key = '[' })
49+
:previous_start({ motion = '[', group_prefix = '', key = '[' })
50+
:previous_end { motion = '[', group_prefix = '', key = ']' }
51+
end, { desc = 'Set keysitter keymaps for nvim-treesitter-textobjects' })
52+
end,
53+
},
54+
},
55+
init = function()
56+
-- Disable entire built-in ftplugin mappings to avoid conflicts.
57+
-- See https://github.com/neovim/neovim/tree/master/runtime/ftplugin for built-in ftplugins.
58+
vim.g.no_plugin_maps = true
59+
60+
-- Or, disable per filetype (add as you like)
61+
-- vim.g.no_python_maps = true
62+
-- vim.g.no_ruby_maps = true
63+
-- vim.g.no_rust_maps = true
64+
-- vim.g.no_go_maps = true
65+
end,
14166
config = function()
14267
-- put your config here
14368
-- configuration
@@ -174,44 +99,5 @@ return {
17499
set_jumps = true,
175100
},
176101
}
177-
178-
vim.api.nvim_create_autocmd({ 'FileType', 'BufEnter' }, {
179-
desc = 'Set treesitter keymaps when treesitter is avaliable for the file',
180-
group = vim.api.nvim_create_augroup('TSparser', { clear = true }),
181-
callback = function()
182-
local buf = vim.api.nvim_get_current_buf()
183-
local has_parser, parser = pcall(vim.treesitter.get_parser, buf, nil, { error = false })
184-
185-
if not has_parser or parser == nil then
186-
return
187-
end
188-
189-
local tsto = TSTO_GROUP:new('textobjects', { group_leader = 'o' })
190-
for k, v in pairs {
191-
-- ['b'] = 'block', -- for brace-less languages like python
192-
['c'] = 'class',
193-
['f'] = 'function',
194-
['i'] = 'conditional',
195-
['l'] = 'loop',
196-
['p'] = 'parameter',
197-
['r'] = 'return',
198-
['t'] = 'attribute',
199-
['x'] = 'regex',
200-
} do
201-
tsto:set(k, v):sel_outer():sel_inner():goto_start():goto_end()
202-
end
203-
204-
tsto:set('{', 'block'):sel_outer():sel_inner():goto_start():goto_end { key = '}' }
205-
tsto:set('(', 'call'):sel_outer():sel_inner():goto_start():goto_end { key = ')' }
206-
tsto:set('/', 'comment'):sel_outer():sel_inner():goto_start()
207-
tsto:set(';', 'statement'):sel_outer():goto_start():goto_end { key = ':' }
208-
tsto
209-
:set('=', 'assignment')
210-
:sel_outer()
211-
:sel_inner('rhs')
212-
:goto_start({ attribute = 'lhs' })
213-
:goto_start { attribute = 'rhs', key = '-', desc = 'rhs' }
214-
end,
215-
})
216102
end,
217103
}

0 commit comments

Comments
 (0)