feat!: update to Neovim 0.7 APIs (#75)

master
Christian Clason 2 years ago committed by GitHub
parent 13e2d2d63c
commit dd250b05d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,13 +29,6 @@ function M.init()
end, end,
}, },
} }
vim.cmd [[
command! TSHighlightCapturesUnderCursor :lua require'nvim-treesitter-playground.hl-info'.show_hl_captures()<cr>
]]
vim.cmd [[
command! TSNodeUnderCursor :lua require'nvim-treesitter-playground.hl-info'.show_ts_node()<cr>
]]
end end
return M return M

@ -158,21 +158,18 @@ function M.show_ts_node(opts)
lines[#lines + 1] = "* Node not found" lines[#lines + 1] = "* Node not found"
end end
local ns = vim.api.nvim_create_namespace "nvim-treesitter-current-node"
if opts.highlight_node and node_under_cursor then if opts.highlight_node and node_under_cursor then
local ns = vim.api.nvim_create_namespace "nvim-treesitter-current-node"
ts_utils.highlight_node(node_under_cursor, bufnr, ns, opts.hl_group) ts_utils.highlight_node(node_under_cursor, bufnr, ns, opts.hl_group)
vim.cmd(string.format( vim.api.nvim_create_autocmd("CursorMoved", {
[[ group = vim.api.nvim_create_augroup("TSNodeUnderCursor", {}),
augroup TreesitterNodeUnderCursor buffer = bufnr,
au! callback = function()
autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_highlights(%d, %d) require("nvim-treesitter-playground.internal").clear_highlights(bufnr, ns)
augroup END end,
]], desc = "TSPlayground: clear highlights",
bufnr, })
bufnr,
ns
))
end end
return vim.lsp.util.open_floating_preview(lines, "markdown", { border = "single", pad_left = 4, pad_right = 4 }) return vim.lsp.util.open_floating_preview(lines, "markdown", { border = "single", pad_left = 4, pad_right = 4 })

@ -42,6 +42,8 @@ local query_buf_var_name = "TSPlaygroundForBuf"
local playground_ns = api.nvim_create_namespace "nvim-treesitter-playground" local playground_ns = api.nvim_create_namespace "nvim-treesitter-playground"
local query_hl_ns = api.nvim_create_namespace "nvim-treesitter-playground-query" local query_hl_ns = api.nvim_create_namespace "nvim-treesitter-playground-query"
local augroup = vim.api.nvim_create_augroup("TSPlayground", {})
local function get_node_at_cursor(options) local function get_node_at_cursor(options)
options = options or {} options = options or {}
@ -179,30 +181,31 @@ local function setup_buf(for_buf)
api.nvim_buf_set_option(buf, "filetype", "tsplayground") api.nvim_buf_set_option(buf, "filetype", "tsplayground")
api.nvim_buf_set_var(buf, query_buf_var_name, for_buf) api.nvim_buf_set_var(buf, query_buf_var_name, for_buf)
vim.cmd(string.format("augroup TreesitterPlayground_%d", buf)) vim.api.nvim_clear_autocmds { group = augroup, buffer = buf }
vim.cmd "au!" vim.api.nvim_create_autocmd("CursorMoved", {
vim.cmd( group = augroup,
string.format( buffer = buf,
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.highlight_node(%d)]], callback = function()
buf, require("nvim-treesitter-playground.internal").highlight_node(for_buf)
for_buf end,
) desc = "TSPlayground: highlight node",
) })
vim.cmd( vim.api.nvim_create_autocmd("BufLeave", {
string.format( group = augroup,
[[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_highlights(%d)]], buffer = buf,
buf, callback = function()
for_buf require("nvim-treesitter-playground.internal").clear_highlights(for_buf)
) end,
) desc = "TSPlayground: clear highlights",
vim.cmd( })
string.format( vim.api.nvim_create_autocmd("BufWinEnter", {
[[autocmd BufWinEnter <buffer=%d> lua require'nvim-treesitter-playground.internal'.update(%d)]], group = augroup,
buf, buffer = buf,
for_buf callback = function()
) require("nvim-treesitter-playground.internal").update(for_buf)
) end,
vim.cmd "augroup END" desc = "TSPlayground: update",
})
local config = configs.get_module "playground" local config = configs.get_module "playground"
@ -256,21 +259,24 @@ local function setup_query_editor(bufnr)
api.nvim_buf_set_option(buf, "filetype", "query") api.nvim_buf_set_option(buf, "filetype", "query")
api.nvim_buf_set_var(buf, query_buf_var_name, bufnr) api.nvim_buf_set_var(buf, query_buf_var_name, bufnr)
vim.cmd( vim.api.nvim_create_autocmd("CursorMoved", {
string.format( group = augroup,
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.on_query_cursor_move(%d)]], buffer = buf,
buf, callback = function()
bufnr require("nvim-treesitter-playground.internal").on_query_cursor_move(bufnr)
) end,
) desc = "TSPlayground: on query cursor move",
})
api.nvim_buf_set_keymap(
buf, api.nvim_buf_set_keymap(buf, "n", "R", {
"n", silent = true,
"R", noremap = true,
string.format(':lua require "nvim-treesitter-playground.internal".update_query(%d, %d)<CR>', bufnr, buf), callback = function()
{ silent = true } require("nvim-treesitter-playground.internal").update_query(bufnr, buf)
) end,
desc = "TSPlayground: update query",
})
api.nvim_buf_attach(buf, false, { api.nvim_buf_attach(buf, false, {
on_lines = utils.debounce(function() on_lines = utils.debounce(function()
M.update_query(bufnr, buf) M.update_query(bufnr, buf)
@ -743,28 +749,28 @@ function M.attach(bufnr)
end, get_update_time)), end, get_update_time)),
}) })
vim.cmd(string.format("augroup TreesitterPlayground_%d", bufnr)) vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr }
vim.cmd "au!" vim.api.nvim_create_autocmd("CursorMoved", {
vim.cmd(string.format( group = augroup,
-- luacheck: no max line length buffer = bufnr,
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'._highlight_playground_node_debounced(%d)]], callback = function()
bufnr, require("nvim-treesitter-playground.internal")._highlight_playground_node_debounced(bufnr)
bufnr end,
)) desc = "TSPlayground: highlight playground node debounce",
vim.cmd( })
string.format( vim.api.nvim_create_autocmd("BufLeave", {
[[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_playground_highlights(%d)]], group = augroup,
bufnr, buffer = bufnr,
bufnr callback = function()
) require("nvim-treesitter-playground.internal").clear_playground_highlights(bufnr)
) end,
vim.cmd "augroup END" desc = "TSPlayground: clear playground highlights",
})
end end
function M.detach(bufnr) function M.detach(bufnr)
clear_entry(bufnr) clear_entry(bufnr)
vim.cmd(string.format("autocmd! TreesitterPlayground_%d CursorMoved", bufnr)) vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr, event = { "CursorMoved", "BufLeave" } }
vim.cmd(string.format("autocmd! TreesitterPlayground_%d BufLeave", bufnr))
end end
return M return M

@ -139,27 +139,20 @@ function M.attach(buf, _)
M.use_virtual_text = config.use_virtual_text M.use_virtual_text = config.use_virtual_text
M.lint_events = config.lint_events M.lint_events = config.lint_events
vim.cmd(string.format("augroup TreesitterPlaygroundLint_%d", buf)) vim.api.nvim_create_autocmd(M.lint_events, {
vim.cmd "au!" group = vim.api.nvim_create_augroup("TSPlaygroundLint", {}),
for _, e in pairs(M.lint_events) do buffer = buf,
vim.cmd( callback = function()
string.format( require("nvim-treesitter-playground.query_linter").lint(buf)
[[autocmd! %s <buffer=%d> lua require'nvim-treesitter-playground.query_linter'.lint(%d)]], end,
e, desc = "TSPlayground: lint query",
buf, })
buf
)
)
end
vim.cmd "augroup END"
end end
function M.detach(buf) function M.detach(buf)
M.lints[buf] = nil M.lints[buf] = nil
M.clear_virtual_text(buf) M.clear_virtual_text(buf)
for _, e in pairs(M.lint_events) do vim.api.nvim_clear_autocmds { group = "TSPlaygroundLint", buffer = buf, event = M.lint_events }
vim.cmd(string.format("autocmd! TreesitterPlaygroundLint_%d %s", buf, e))
end
end end
return M return M

@ -0,0 +1,28 @@
-- setup playground module
require("nvim-treesitter-playground").init()
local api = vim.api
-- define highlights
local highlights = {
TSPlaygroundFocus = { link = "Visual", default = true },
TSQueryLinterError = { link = "Error", default = true },
TSPlaygroundLang = { link = "String", default = true },
}
for k, v in pairs(highlights) do
api.nvim_set_hl(0, k, v)
end
-- define commands
api.nvim_create_user_command("TSPlaygroundToggle", function()
require("nvim-treesitter-playground.internal").toggle()
end, {})
api.nvim_create_user_command("TSNodeUnderCursor", function()
require("nvim-treesitter-playground.hl-info").show_ts_node()
end, {})
api.nvim_create_user_command("TSCaptureUnderCursor", function()
require("nvim-treesitter-playground.hl-info").show_hl_captures()
end, {})
---@deprecated
api.nvim_create_user_command("TSHighlightCapturesUnderCursor", function()
require("nvim-treesitter-playground.hl-info").show_hl_captures()
end, {})

@ -1,9 +0,0 @@
lua << EOF
require "nvim-treesitter-playground".init()
EOF
highlight default link TSPlaygroundFocus Visual
highlight default link TSQueryLinterError Error
highlight default link TSPlaygroundLang String
command! TSPlaygroundToggle lua require "nvim-treesitter-playground.internal".toggle()
Loading…
Cancel
Save