commit
86d3ec6024
@ -1,52 +1,84 @@
|
|||||||
local parsers = require "nvim-treesitter.parsers"
|
local highlighter = require("vim.treesitter.highlighter")
|
||||||
local queries = require'nvim-treesitter.query'
|
local ts_utils = require "nvim-treesitter.ts_utils"
|
||||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
|
||||||
|
|
||||||
local hlmap = vim.treesitter.highlighter.hl_map
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local function is_highlight_name(capture_name)
|
function M.get_treesitter_hl()
|
||||||
local firstc = string.sub(capture_name, 1, 1)
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
return firstc ~= string.lower(firstc)
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.show_hl_captures()
|
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
|
||||||
local lang = parsers.get_buf_lang(bufnr)
|
|
||||||
|
|
||||||
if not lang then return end
|
|
||||||
|
|
||||||
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||||
row = row - 1
|
row = row - 1
|
||||||
|
|
||||||
local parser = parsers.get_parser(bufnr, lang)
|
local self = highlighter.active[buf]
|
||||||
if not parser then return function() end end
|
|
||||||
|
|
||||||
parser = parser:language_for_range({row, col, row, col})
|
if not self then return {} end
|
||||||
|
|
||||||
local matches = {}
|
local matches = {}
|
||||||
local query = queries.get_query(parser:lang(), 'highlights')
|
|
||||||
|
self.tree:for_each_tree(function(tstree, tree)
|
||||||
for _, tree in ipairs(parser:trees()) do
|
if not tstree then return end
|
||||||
local root = tree:root()
|
|
||||||
local start_row, _, end_row, _ = root:range()
|
local root = tstree:root()
|
||||||
|
local root_start_row, _, root_end_row, _ = root:range()
|
||||||
for _, match in query:iter_matches(root, bufnr, start_row, end_row) do
|
|
||||||
for id, node in pairs(match) do
|
-- Only worry about trees within the line range
|
||||||
if ts_utils.is_in_node_range(node, row, col) then
|
if root_start_row > row or root_end_row < row then return end
|
||||||
local c = query.captures[id] -- name of the capture in the query
|
|
||||||
if c ~= nil then
|
local query = self:get_query(tree:lang())
|
||||||
table.insert(matches, '@'..c..' -> '..(is_highlight_name(c) and c or (hlmap[c] or 'nil')))
|
|
||||||
|
-- Some injected languages may not have highlight queries.
|
||||||
|
if not query:query() then return end
|
||||||
|
|
||||||
|
local iter = query:query():iter_captures(root, self.bufnr, row, row + 1)
|
||||||
|
|
||||||
|
for capture, node in iter do
|
||||||
|
local hl = query.hl_cache[capture]
|
||||||
|
|
||||||
|
if hl and ts_utils.is_in_node_range(node, row, col) then
|
||||||
|
local c = query._query.captures[capture] -- name of the capture in the query
|
||||||
|
if c ~= nil then
|
||||||
|
local general_hl = query:_get_hl_from_capture(capture)
|
||||||
|
local line = "* **@" .. c .. "** -> " .. hl
|
||||||
|
if general_hl ~= hl then
|
||||||
|
line = line .. " -> **" .. general_hl .. "**"
|
||||||
end
|
end
|
||||||
|
table.insert(matches, line)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end, true)
|
||||||
|
return matches
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.get_syntax_hl()
|
||||||
|
local line = vim.fn.line(".")
|
||||||
|
local col = vim.fn.col(".")
|
||||||
|
local matches = {}
|
||||||
|
for _, i1 in ipairs(vim.fn.synstack(line, col)) do
|
||||||
|
local i2 = vim.fn.synIDtrans(i1)
|
||||||
|
local n1 = vim.fn.synIDattr(i1, "name")
|
||||||
|
local n2 = vim.fn.synIDattr(i2, "name")
|
||||||
|
table.insert(matches, "* " .. n1 .. " -> **" .. n2 .. "**")
|
||||||
end
|
end
|
||||||
if #matches == 0 then
|
return matches
|
||||||
matches = {"No tree-sitter matches found!"}
|
end
|
||||||
|
|
||||||
|
function M.show_hl_captures()
|
||||||
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
|
local lines = {}
|
||||||
|
local matches
|
||||||
|
if highlighter.active[buf] then
|
||||||
|
table.insert(lines, "# Treesitter")
|
||||||
|
matches = M.get_treesitter_hl()
|
||||||
|
else
|
||||||
|
table.insert(lines, "# Syntax")
|
||||||
|
matches = M.get_syntax_hl()
|
||||||
end
|
end
|
||||||
vim.lsp.util.open_floating_preview(matches, "treesitter-hl-captures")
|
if #matches == 0 then table.insert(lines, "* No highlight groups found") end
|
||||||
|
table.insert(lines, "")
|
||||||
|
for _, line in ipairs(matches) do table.insert(lines, line) end
|
||||||
|
vim.lsp.util.open_floating_preview(lines, "markdown",
|
||||||
|
{ border = "single", pad_left = 4, pad_right = 4 })
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
Loading…
Reference in New Issue