revert: "feat(treesitter): add foldtext with treesitter highlighting"

This reverts commit 9ce1623 in favor of #20750.
This commit is contained in:
Till Bungert
2024-01-27 01:38:56 +01:00
committed by GitHub
parent a9df0c5ce6
commit 0892c080d1
5 changed files with 2 additions and 241 deletions

View File

@@ -123,7 +123,8 @@ BREAKING CHANGES IN HEAD *news-breaking-dev*
The following breaking changes were made during the development cycle to The following breaking changes were made during the development cycle to
unreleased features on Nvim HEAD. unreleased features on Nvim HEAD.
... Removed `vim.treesitter.foldtext` as transparent foldtext is now supported
https://github.com/neovim/neovim/pull/20750
• ... • ...
============================================================================== ==============================================================================
@@ -223,8 +224,6 @@ The following new APIs and features were added.
• |vim.treesitter.query.edit()| allows live editing of treesitter • |vim.treesitter.query.edit()| allows live editing of treesitter
queries. queries.
• Improved error messages for query parsing. • Improved error messages for query parsing.
• |vim.treesitter.foldtext()| applies treesitter highlighting to
foldtext.
• |vim.ui.open()| opens URIs using the system default handler (macOS `open`, • |vim.ui.open()| opens URIs using the system default handler (macOS `open`,
Windows `explorer`, Linux `xdg-open`, etc.) Windows `explorer`, Linux `xdg-open`, etc.)

View File

@@ -652,16 +652,6 @@ foldexpr({lnum}) *vim.treesitter.foldexpr()*
Return: ~ Return: ~
(`string`) (`string`)
foldtext() *vim.treesitter.foldtext()*
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`)
*vim.treesitter.get_captures_at_cursor()* *vim.treesitter.get_captures_at_cursor()*
get_captures_at_cursor({winnr}) get_captures_at_cursor({winnr})
Returns a list of highlight capture names under the cursor Returns a list of highlight capture names under the cursor

View File

@@ -517,16 +517,4 @@ function M.foldexpr(lnum)
return require('vim.treesitter._fold').foldexpr(lnum) return require('vim.treesitter._fold').foldexpr(lnum)
end 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 return M

View File

@@ -397,97 +397,4 @@ api.nvim_create_autocmd('OptionSet', {
end end
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 return M

View File

@@ -675,126 +675,3 @@ t2]])
} }
end) end)
end) end)
describe('treesitter foldtext', function()
local test_text = [[
void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *))
{
int width = INT_MAX, height = INT_MAX;
bool ext_widgets[kUIExtCount];
for (UIExtension i = 0; (int)i < kUIExtCount; i++) {
ext_widgets[i] = true;
}
bool inclusive = ui_override();
for (size_t i = 0; i < ui_count; i++) {
UI *ui = uis[i];
width = MIN(ui->width, width);
height = MIN(ui->height, height);
foo = BAR(ui->bazaar, bazaar);
for (UIExtension j = 0; (int)j < kUIExtCount; j++) {
ext_widgets[j] &= (ui->ui_ext[j] || inclusive);
}
}
}]]
local screen
before_each(function()
screen = Screen.new(60, 5)
screen:set_default_attr_ids({
[0] = { foreground = Screen.colors.Blue, bold = true },
[1] = { foreground = Screen.colors.DarkBlue, background = Screen.colors.LightGray },
[2] = {
bold = true,
background = Screen.colors.LightGray,
foreground = Screen.colors.SeaGreen,
},
[3] = { foreground = Screen.colors.DarkCyan, background = Screen.colors.LightGray },
[4] = { foreground = Screen.colors.SlateBlue, background = Screen.colors.LightGray },
[5] = { bold = true, background = Screen.colors.LightGray, foreground = Screen.colors.Brown },
[6] = { background = Screen.colors.Red1 },
[7] = { foreground = Screen.colors.DarkBlue, background = Screen.colors.Red },
[8] = { foreground = Screen.colors.Brown, bold = true, background = Screen.colors.Red },
[9] = { foreground = Screen.colors.SlateBlue, background = Screen.colors.Red },
[10] = { bold = true },
})
screen:attach()
end)
it('displays highlighted content', function()
command([[set foldmethod=manual foldtext=v:lua.vim.treesitter.foldtext() updatetime=50]])
insert(test_text)
exec_lua([[vim.treesitter.get_parser(0, "c")]])
feed('ggVGzf')
screen:expect {
grid = [[
{4:^void}{1: }{3:qsort}{4:(void}{1: }{5:*}{3:base}{4:,}{1: }{4:size_t}{1: }{3:nel}{4:,}{1: }{4:size_t}{1: }{3:width}{4:,}{1: }{4:int}{1: }{4:(}{5:*}{3:compa}|
{0:~ }|*3
|
]],
}
end)
it('handles deep nested captures', function()
command([[set foldmethod=manual foldtext=v:lua.vim.treesitter.foldtext() updatetime=50]])
insert([[
function FoldInfo.new()
return setmetatable({
start_counts = {},
stop_counts = {},
levels0 = {},
levels = {},
}, FoldInfo)
end]])
exec_lua([[vim.treesitter.get_parser(0, "lua")]])
feed('ggjVGkzfgg')
screen:expect {
grid = [[
^function FoldInfo.new() |
{1: }{5:return}{1: }{4:setmetatable({}{1:·····································}|
end |
{0:~ }|
|
]],
}
command('hi! Visual guibg=Red')
feed('GVgg')
screen:expect {
grid = [[
^f{6:unction FoldInfo.new()} |
{7: }{8:return}{7: }{9:setmetatable({}{7:·····································}|
{6:end} |
{0:~ }|
{10:-- VISUAL LINE --} |
]],
}
feed('10l<C-V>')
screen:expect {
grid = [[
{6:function F}^oldInfo.new() |
{7: }{8:return}{7: }{9:se}{4:tmetatable({}{1:·····································}|
{6:end} |
{0:~ }|
{10:-- VISUAL BLOCK --} |
]],
}
end)
it('falls back to default', function()
command([[set foldmethod=manual foldtext=v:lua.vim.treesitter.foldtext()]])
insert(test_text)
feed('ggVGzf')
screen:expect {
grid = [[
{1:^+-- 19 lines: void qsort(void *base, size_t nel, size_t widt}|
{0:~ }|*3
|
]],
}
end)
end)