mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
feat: Add TSEditQueryUserAfter
This commit is contained in:
parent
4598f90f34
commit
4df2667303
2 changed files with 34 additions and 7 deletions
|
|
@ -290,6 +290,13 @@ If no such file exists, a buffer for a new file in the user's config directory
|
|||
is created. If {lang} is not specified, the language of the current buffer
|
||||
is used.
|
||||
|
||||
*:TSEditQueryUserAfter*
|
||||
:TSEditQueryUserAfter {query-group} [{lang}]~
|
||||
|
||||
Same as |:TSEditQuery| but edits a file in the `after` directory of the
|
||||
user's config directory. Useful to add custom extensions for the queries
|
||||
provided by a plugin.
|
||||
|
||||
==============================================================================
|
||||
UTILS *nvim-treesitter-utils*
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
local api = vim.api
|
||||
|
||||
local queries = require'nvim-treesitter.query'
|
||||
local nvim_query = require'vim.treesitter.query'
|
||||
local ts_query = require'vim.treesitter.query'
|
||||
local parsers = require'nvim-treesitter.parsers'
|
||||
local utils = require'nvim-treesitter.utils'
|
||||
local caching = require'nvim-treesitter.caching'
|
||||
|
|
@ -199,23 +199,36 @@ end
|
|||
|
||||
function M.edit_query_file(query_group, lang)
|
||||
lang = lang or parsers.get_buf_lang()
|
||||
local files = nvim_query.get_query_files(lang, query_group, true)
|
||||
local files = ts_query.get_query_files(lang, query_group, true)
|
||||
if #files == 0 then
|
||||
local folder = utils.join_path(vim.fn.stdpath('config'), 'after', 'queries', lang)
|
||||
local file = utils.join_path(folder, query_group..'.scm')
|
||||
pcall(vim.fn.mkdir, folder, "p", "0755")
|
||||
vim.cmd(':edit '..file)
|
||||
vim.notify('No query file found! Creating a new one!')
|
||||
M.edit_query_file_user_after(query_group, lang)
|
||||
elseif #files == 1 then
|
||||
vim.cmd(':edit '..files[1])
|
||||
else
|
||||
local counter = 0
|
||||
local choice = vim.fn.inputlist(vim.tbl_map(function(f) counter = counter + 1;return counter..'. '..f end, files))
|
||||
if choice > 0 then
|
||||
vim.cmd(':edit '..files[choice])
|
||||
vim.cmd(':edit '..files[choice + 1])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M.edit_query_file_user_after(query_group, lang)
|
||||
lang = lang or parsers.get_buf_lang()
|
||||
local folder = utils.join_path(vim.fn.stdpath('config'), 'after', 'queries', lang)
|
||||
local file = utils.join_path(folder, query_group..'.scm')
|
||||
if vim.fn.isdirectory(folder) ~= 1 then
|
||||
local choice = vim.fn.inputlist({'"'..folder.." does not exist. Create it?", "1. Yes", "2. No"})
|
||||
if choice == 1 then
|
||||
vim.fn.mkdir(folder, "p", "0755")
|
||||
else
|
||||
return
|
||||
end
|
||||
end
|
||||
vim.cmd(':edit '..file)
|
||||
end
|
||||
|
||||
M.commands = {
|
||||
TSBufEnable = {
|
||||
run = enable_module,
|
||||
|
|
@ -272,6 +285,13 @@ M.commands = {
|
|||
"-complete=custom,nvim_treesitter#available_query_groups",
|
||||
},
|
||||
},
|
||||
TSEditQueryUserAfter = {
|
||||
run = M.edit_query_file_user_after,
|
||||
args = {
|
||||
"-nargs=+",
|
||||
"-complete=custom,nvim_treesitter#available_query_groups",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
-- @param mod: module (string)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue