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,
},
}
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
return M

@ -158,21 +158,18 @@ function M.show_ts_node(opts)
lines[#lines + 1] = "* Node not found"
end
local ns = vim.api.nvim_create_namespace "nvim-treesitter-current-node"
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)
vim.cmd(string.format(
[[
augroup TreesitterNodeUnderCursor
au!
autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_highlights(%d, %d)
augroup END
]],
bufnr,
bufnr,
ns
))
vim.api.nvim_create_autocmd("CursorMoved", {
group = vim.api.nvim_create_augroup("TSNodeUnderCursor", {}),
buffer = bufnr,
callback = function()
require("nvim-treesitter-playground.internal").clear_highlights(bufnr, ns)
end,
desc = "TSPlayground: clear highlights",
})
end
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 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)
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_var(buf, query_buf_var_name, for_buf)
vim.cmd(string.format("augroup TreesitterPlayground_%d", buf))
vim.cmd "au!"
vim.cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.highlight_node(%d)]],
buf,
for_buf
)
)
vim.cmd(
string.format(
[[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_highlights(%d)]],
buf,
for_buf
)
)
vim.cmd(
string.format(
[[autocmd BufWinEnter <buffer=%d> lua require'nvim-treesitter-playground.internal'.update(%d)]],
buf,
for_buf
)
)
vim.cmd "augroup END"
vim.api.nvim_clear_autocmds { group = augroup, buffer = buf }
vim.api.nvim_create_autocmd("CursorMoved", {
group = augroup,
buffer = buf,
callback = function()
require("nvim-treesitter-playground.internal").highlight_node(for_buf)
end,
desc = "TSPlayground: highlight node",
})
vim.api.nvim_create_autocmd("BufLeave", {
group = augroup,
buffer = buf,
callback = function()
require("nvim-treesitter-playground.internal").clear_highlights(for_buf)
end,
desc = "TSPlayground: clear highlights",
})
vim.api.nvim_create_autocmd("BufWinEnter", {
group = augroup,
buffer = buf,
callback = function()
require("nvim-treesitter-playground.internal").update(for_buf)
end,
desc = "TSPlayground: update",
})
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_var(buf, query_buf_var_name, bufnr)
vim.cmd(
string.format(
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'.on_query_cursor_move(%d)]],
buf,
bufnr
)
)
api.nvim_buf_set_keymap(
buf,
"n",
"R",
string.format(':lua require "nvim-treesitter-playground.internal".update_query(%d, %d)<CR>', bufnr, buf),
{ silent = true }
)
vim.api.nvim_create_autocmd("CursorMoved", {
group = augroup,
buffer = buf,
callback = function()
require("nvim-treesitter-playground.internal").on_query_cursor_move(bufnr)
end,
desc = "TSPlayground: on query cursor move",
})
api.nvim_buf_set_keymap(buf, "n", "R", {
silent = true,
noremap = true,
callback = function()
require("nvim-treesitter-playground.internal").update_query(bufnr, buf)
end,
desc = "TSPlayground: update query",
})
api.nvim_buf_attach(buf, false, {
on_lines = utils.debounce(function()
M.update_query(bufnr, buf)
@ -743,28 +749,28 @@ function M.attach(bufnr)
end, get_update_time)),
})
vim.cmd(string.format("augroup TreesitterPlayground_%d", bufnr))
vim.cmd "au!"
vim.cmd(string.format(
-- luacheck: no max line length
[[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter-playground.internal'._highlight_playground_node_debounced(%d)]],
bufnr,
bufnr
))
vim.cmd(
string.format(
[[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter-playground.internal'.clear_playground_highlights(%d)]],
bufnr,
bufnr
)
)
vim.cmd "augroup END"
vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr }
vim.api.nvim_create_autocmd("CursorMoved", {
group = augroup,
buffer = bufnr,
callback = function()
require("nvim-treesitter-playground.internal")._highlight_playground_node_debounced(bufnr)
end,
desc = "TSPlayground: highlight playground node debounce",
})
vim.api.nvim_create_autocmd("BufLeave", {
group = augroup,
buffer = bufnr,
callback = function()
require("nvim-treesitter-playground.internal").clear_playground_highlights(bufnr)
end,
desc = "TSPlayground: clear playground highlights",
})
end
function M.detach(bufnr)
clear_entry(bufnr)
vim.cmd(string.format("autocmd! TreesitterPlayground_%d CursorMoved", bufnr))
vim.cmd(string.format("autocmd! TreesitterPlayground_%d BufLeave", bufnr))
vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr, event = { "CursorMoved", "BufLeave" } }
end
return M

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