Repository navigation
Expand file tree
/
Copy pathnvim-ufo.lua
More file actions
85 lines (77 loc) · 2.37 KB
/
Copy pathnvim-ufo.lua
File metadata and controls
85 lines (77 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- local status, ufoPlugIn = pcall(require, "ufo")
-- if not status then
-- vim.notify("没有找到 ufo")
-- return
-- end
vim.o.foldcolumn = '1'
-- Using ufo provider need a large value, feel free to decrease the value
vim.o.foldlevel = 99
vim.o.foldlevelstart = 99
vim.o.foldenable = true
local bufnr = vim.api.nvim_get_current_buf()
local handler = function(virtText, lnum, endLnum, width, truncate)
local newVirtText = {}
local suffix = (' %d '):format(endLnum - lnum)
local sufWidth = vim.fn.strdisplaywidth(suffix)
local targetWidth = width - sufWidth
local curWidth = 0
for _, chunk in ipairs(virtText) do
local chunkText = chunk[1]
local chunkWidth = vim.fn.strdisplaywidth(chunkText)
if targetWidth > curWidth + chunkWidth then
table.insert(newVirtText, chunk)
else
chunkText = truncate(chunkText, targetWidth - curWidth)
local hlGroup = chunk[2]
table.insert(newVirtText, {chunkText, hlGroup})
chunkWidth = vim.fn.strdisplaywidth(chunkText)
if curWidth + chunkWidth < targetWidth then
suffix = suffix .. (' '):rep(targetWidth - curWidth - chunkWidth)
end
break
end
curWidth = curWidth + chunkWidth
end
table.insert(newVirtText, {suffix, 'MoreMsg'})
return newVirtText
end
local config = {
enable_get_fold_virt_text = true,
open_fold_hl_timeout = 150,
fold_virt_text_handler = handler,
preview = {
win_config = {
winhighlight = 'Normal:Folded',
winblend = 0
},
mappings = {
scrollU = '<C-u>',
scrollD = '<C-d>'
}
},
provider_selector = function()
return {'treesitter', 'indent'}
end
}
local M = {
'kevinhwang91/nvim-ufo',
config = function ()
local ufo = require('ufo')
ufo.setup(config)
ufo.setFoldVirtTextHandler(bufnr, handler)
vim.keymap.set('n', 'zR', ufo.openAllFolds)
vim.keymap.set('n', 'zM', ufo.closeAllFolds)
vim.keymap.set('n', 'zr', ufo.openFoldsExceptKinds)
-- closeAllFolds == closeFoldsWith(0)
vim.keymap.set('n', 'zm', ufo.closeFoldsWith)
vim.keymap.set('n', 'K', function()
local winid = ufo.peekFoldedLinesUnderCursor()
if not winid then
vim.fn.CocActionAsync('definitionHover')
vim.lsp.buf.hover()
end
end)
end,
dependencies = { {'kevinhwang91/promise-async'} }
}
return M