aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTill Bungert <tillbungert@gmail.com>2024-01-27 01:38:56 +0100
committerGitHub <noreply@github.com>2024-01-27 08:38:56 +0800
commit0892c080d16776366a2fe289f9083cdc532ec56c (patch)
treeea18dcaebb1c94a7ac641b1181f4c4cdf76f7691
parenta9df0c5ce6caa5e623c3140a80baf4b3c1ce07db (diff)
downloadrneovim-0892c080d16776366a2fe289f9083cdc532ec56c.tar.gz
rneovim-0892c080d16776366a2fe289f9083cdc532ec56c.tar.bz2
rneovim-0892c080d16776366a2fe289f9083cdc532ec56c.zip
revert: "feat(treesitter): add foldtext with treesitter highlighting"
This reverts commit 9ce1623 in favor of #20750.
-rw-r--r--runtime/doc/news.txt5
-rw-r--r--runtime/doc/treesitter.txt10
-rw-r--r--runtime/lua/vim/treesitter.lua12
-rw-r--r--runtime/lua/vim/treesitter/_fold.lua93
-rw-r--r--test/functional/treesitter/fold_spec.lua123
5 files changed, 2 insertions, 241 deletions
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt
index a7c4e8147c..2212b9910b 100644
--- a/runtime/doc/news.txt
+++ b/runtime/doc/news.txt
@@ -123,7 +123,8 @@ BREAKING CHANGES IN HEAD *news-breaking-dev*
The following breaking changes were made during the development cycle to
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
queries.
• 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`,
Windows `explorer`, Linux `xdg-open`, etc.)
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt
index 956b7d6ae5..0f4462b109 100644
--- a/runtime/doc/treesitter.txt
+++ b/runtime/doc/treesitter.txt
@@ -652,16 +652,6 @@ foldexpr({lnum}) *vim.treesitter.foldexpr()*
Return: ~
(`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()*
get_captures_at_cursor({winnr})
Returns a list of highlight capture names under the cursor
diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua
index 3c91be7acf..9d96ab33fa 100644
--- a/runtime/lua/vim/treesitter.lua
+++ b/runtime/lua/vim/treesitter.lua
@@ -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
diff --git a/runtime/lua/vim/treesitter/_fold.lua b/runtime/lua/vim/treesitter/_fold.lua
index 735627d29f..d96cc966de 100644
--- a/runtime/lua/vim/treesitter/_fold.lua
+++ b/runtime/lua/vim/treesitter/_fold.lua
@@ -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
diff --git a/test/functional/treesitter/fold_spec.lua b/test/functional/treesitter/fold_spec.lua
index ac9d227bb6..9428432f66 100644
--- a/test/functional/treesitter/fold_spec.lua
+++ b/test/functional/treesitter/fold_spec.lua
@@ -675,126 +675,3 @@ t2]])
}
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)