mirror of
https://github.com/neovim/neovim
synced 2025-07-28 09:21:58 +00:00
feat(treesitter): add lang parameter to the query editor (#25181)
This commit is contained in:
committed by
GitHub
parent
7e5ce42977
commit
28f54a7878
@ -853,7 +853,7 @@ add_predicate({name}, {handler}, {force})
|
|||||||
meanings
|
meanings
|
||||||
• {force} (boolean|nil)
|
• {force} (boolean|nil)
|
||||||
|
|
||||||
edit() *vim.treesitter.query.edit()*
|
edit({lang}) *vim.treesitter.query.edit()*
|
||||||
Open a window for live editing of a treesitter query.
|
Open a window for live editing of a treesitter query.
|
||||||
|
|
||||||
Can also be shown with `:EditQuery`. *:EditQuery*
|
Can also be shown with `:EditQuery`. *:EditQuery*
|
||||||
@ -861,6 +861,10 @@ edit() *vim.treesitter.query.edit()*
|
|||||||
Note that the editor opens a scratch buffer, and so queries aren't
|
Note that the editor opens a scratch buffer, and so queries aren't
|
||||||
persisted on disk.
|
persisted on disk.
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
• {lang} (string|nil) language to open the query editor for. If
|
||||||
|
omitted, inferred from the current buffer's filetype.
|
||||||
|
|
||||||
get({lang}, {query_name}) *vim.treesitter.query.get()*
|
get({lang}, {query_name}) *vim.treesitter.query.get()*
|
||||||
Returns the runtime query {query_name} for {lang}.
|
Returns the runtime query {query_name} for {lang}.
|
||||||
|
|
||||||
|
@ -468,11 +468,11 @@ local edit_ns = api.nvim_create_namespace('treesitter/dev-edit')
|
|||||||
|
|
||||||
---@param query_win integer
|
---@param query_win integer
|
||||||
---@param base_win integer
|
---@param base_win integer
|
||||||
local function update_editor_highlights(query_win, base_win)
|
---@param lang string
|
||||||
|
local function update_editor_highlights(query_win, base_win, lang)
|
||||||
local base_buf = api.nvim_win_get_buf(base_win)
|
local base_buf = api.nvim_win_get_buf(base_win)
|
||||||
local query_buf = api.nvim_win_get_buf(query_win)
|
local query_buf = api.nvim_win_get_buf(query_win)
|
||||||
local parser = vim.treesitter.get_parser(base_buf)
|
local parser = vim.treesitter.get_parser(base_buf, lang)
|
||||||
local lang = parser:lang()
|
|
||||||
api.nvim_buf_clear_namespace(base_buf, edit_ns, 0, -1)
|
api.nvim_buf_clear_namespace(base_buf, edit_ns, 0, -1)
|
||||||
local query_content = table.concat(api.nvim_buf_get_lines(query_buf, 0, -1, false), '\n')
|
local query_content = table.concat(api.nvim_buf_get_lines(query_buf, 0, -1, false), '\n')
|
||||||
|
|
||||||
@ -506,7 +506,8 @@ local function update_editor_highlights(query_win, base_win)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- @private
|
--- @private
|
||||||
function M.edit_query()
|
--- @param lang? string language to open the query editor for.
|
||||||
|
function M.edit_query(lang)
|
||||||
local buf = api.nvim_get_current_buf()
|
local buf = api.nvim_get_current_buf()
|
||||||
local win = api.nvim_get_current_win()
|
local win = api.nvim_get_current_win()
|
||||||
|
|
||||||
@ -528,11 +529,11 @@ function M.edit_query()
|
|||||||
end
|
end
|
||||||
vim.cmd(cmd)
|
vim.cmd(cmd)
|
||||||
|
|
||||||
local ok, parser = pcall(vim.treesitter.get_parser, buf)
|
local ok, parser = pcall(vim.treesitter.get_parser, buf, lang)
|
||||||
if not ok then
|
if not ok then
|
||||||
return nil, 'No parser available for the given buffer'
|
return nil, 'No parser available for the given buffer'
|
||||||
end
|
end
|
||||||
local lang = parser:lang()
|
lang = parser:lang()
|
||||||
|
|
||||||
local query_win = api.nvim_get_current_win()
|
local query_win = api.nvim_get_current_win()
|
||||||
local query_buf = api.nvim_win_get_buf(query_win)
|
local query_buf = api.nvim_win_get_buf(query_win)
|
||||||
@ -561,7 +562,7 @@ function M.edit_query()
|
|||||||
desc = 'Update query editor highlights when the cursor moves',
|
desc = 'Update query editor highlights when the cursor moves',
|
||||||
callback = function()
|
callback = function()
|
||||||
if api.nvim_win_is_valid(win) then
|
if api.nvim_win_is_valid(win) then
|
||||||
update_editor_highlights(query_win, win)
|
update_editor_highlights(query_win, win, lang)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
@ -848,8 +848,10 @@ end
|
|||||||
--- Can also be shown with `:EditQuery`. *:EditQuery*
|
--- Can also be shown with `:EditQuery`. *:EditQuery*
|
||||||
---
|
---
|
||||||
--- Note that the editor opens a scratch buffer, and so queries aren't persisted on disk.
|
--- Note that the editor opens a scratch buffer, and so queries aren't persisted on disk.
|
||||||
function M.edit()
|
---
|
||||||
require('vim.treesitter.dev').edit_query()
|
--- @param lang? string language to open the query editor for. If omitted, inferred from the current buffer's filetype.
|
||||||
|
function M.edit(lang)
|
||||||
|
require('vim.treesitter.dev').edit_query(lang)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -19,6 +19,6 @@ vim.api.nvim_create_user_command('InspectTree', function(cmd)
|
|||||||
end
|
end
|
||||||
end, { desc = 'Inspect treesitter language tree for buffer', count = true })
|
end, { desc = 'Inspect treesitter language tree for buffer', count = true })
|
||||||
|
|
||||||
vim.api.nvim_create_user_command('EditQuery', function()
|
vim.api.nvim_create_user_command('EditQuery', function(cmd)
|
||||||
vim.treesitter.query.edit()
|
vim.treesitter.query.edit(cmd.fargs[1])
|
||||||
end, { desc = 'Edit treesitter query' })
|
end, { desc = 'Edit treesitter query', nargs = '?' })
|
||||||
|
Reference in New Issue
Block a user