mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 03:48:18 +00:00
revert: "feat(treesitter): add foldtext with treesitter highlighting"
This reverts commit 9ce1623
in favor of #20750.
This commit is contained in:
@@ -517,16 +517,4 @@ function M.foldexpr(lnum)
|
||||
return require('vim.treesitter._fold').foldexpr(lnum)
|
||||
end
|
||||
|
||||
--- Returns the highlighted content of the first line of the fold or falls back to |foldtext()|
|
||||
--- if no treesitter parser is found. Can be set directly to 'foldtext':
|
||||
---
|
||||
--- ```lua
|
||||
--- vim.wo.foldtext = 'v:lua.vim.treesitter.foldtext()'
|
||||
--- ```
|
||||
---
|
||||
---@return { [1]: string, [2]: string[] }[] | string
|
||||
function M.foldtext()
|
||||
return require('vim.treesitter._fold').foldtext()
|
||||
end
|
||||
|
||||
return M
|
||||
|
@@ -397,97 +397,4 @@ api.nvim_create_autocmd('OptionSet', {
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
---@package
|
||||
---@return { [1]: string, [2]: string[] }[]|string
|
||||
function M.foldtext()
|
||||
local foldstart = vim.v.foldstart
|
||||
local bufnr = api.nvim_get_current_buf()
|
||||
|
||||
---@type boolean, LanguageTree
|
||||
local ok, parser = pcall(ts.get_parser, bufnr)
|
||||
if not ok then
|
||||
return vim.fn.foldtext()
|
||||
end
|
||||
|
||||
local query = ts.query.get(parser:lang(), 'highlights')
|
||||
if not query then
|
||||
return vim.fn.foldtext()
|
||||
end
|
||||
|
||||
local tree = parser:parse({ foldstart - 1, foldstart })[1]
|
||||
|
||||
local line = api.nvim_buf_get_lines(bufnr, foldstart - 1, foldstart, false)[1]
|
||||
if not line then
|
||||
return vim.fn.foldtext()
|
||||
end
|
||||
|
||||
---@type { [1]: string, [2]: string[], range: { [1]: integer, [2]: integer } }[] | { [1]: string, [2]: string[] }[]
|
||||
local result = {}
|
||||
|
||||
local line_pos = 0
|
||||
|
||||
for id, node, metadata in query:iter_captures(tree:root(), 0, foldstart - 1, foldstart) do
|
||||
local name = query.captures[id]
|
||||
local start_row, start_col, end_row, end_col = node:range()
|
||||
|
||||
local priority = tonumber(metadata.priority or vim.highlight.priorities.treesitter)
|
||||
|
||||
if start_row == foldstart - 1 and end_row == foldstart - 1 then
|
||||
-- check for characters ignored by treesitter
|
||||
if start_col > line_pos then
|
||||
table.insert(result, {
|
||||
line:sub(line_pos + 1, start_col),
|
||||
{},
|
||||
range = { line_pos, start_col },
|
||||
})
|
||||
end
|
||||
line_pos = end_col
|
||||
|
||||
local text = line:sub(start_col + 1, end_col)
|
||||
table.insert(result, { text, { { '@' .. name, priority } }, range = { start_col, end_col } })
|
||||
end
|
||||
end
|
||||
|
||||
local i = 1
|
||||
while i <= #result do
|
||||
-- find first capture that is not in current range and apply highlights on the way
|
||||
local j = i + 1
|
||||
while
|
||||
j <= #result
|
||||
and result[j].range[1] >= result[i].range[1]
|
||||
and result[j].range[2] <= result[i].range[2]
|
||||
do
|
||||
for k, v in ipairs(result[i][2]) do
|
||||
if not vim.tbl_contains(result[j][2], v) then
|
||||
table.insert(result[j][2], k, v)
|
||||
end
|
||||
end
|
||||
j = j + 1
|
||||
end
|
||||
|
||||
-- remove the parent capture if it is split into children
|
||||
if j > i + 1 then
|
||||
table.remove(result, i)
|
||||
else
|
||||
-- highlights need to be sorted by priority, on equal prio, the deeper nested capture (earlier
|
||||
-- in list) should be considered higher prio
|
||||
if #result[i][2] > 1 then
|
||||
table.sort(result[i][2], function(a, b)
|
||||
return a[2] < b[2]
|
||||
end)
|
||||
end
|
||||
|
||||
result[i][2] = vim.tbl_map(function(tbl)
|
||||
return tbl[1]
|
||||
end, result[i][2])
|
||||
result[i] = { result[i][1], result[i][2] }
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
return M
|
||||
|
Reference in New Issue
Block a user