-
Notifications
You must be signed in to change notification settings - Fork 50
/
example.lua
186 lines (166 loc) · 6.33 KB
/
example.lua
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
---@diagnostic disable: unused-local, unused-function, undefined-field
-----------------------------------------providerSelector-------------------------------------------
local function selectProviderWithFt()
local ftMap = {
vim = 'indent',
python = {'indent'},
git = ''
}
require('ufo').setup({
provider_selector = function(bufnr, filetype, buftype)
-- return a table with string elements: 1st is name of main provider, 2nd is fallback
-- return a string type: use ufo inner providers
-- return a string in a table: like a string type above
-- return empty string '': disable any providers
-- return `nil`: use default value {'lsp', 'indent'}
-- return a function: it will be involved and expected return `UfoFoldingRange[]|Promise`
-- if you prefer treesitter provider rather than lsp,
-- return ftMap[filetype] or {'treesitter', 'indent'}
return ftMap[filetype]
end
})
end
-- lsp->treesitter->indent
local function selectProviderWithChainByDefault()
local ftMap = {
vim = 'indent',
python = {'indent'},
git = ''
}
---@param bufnr number
---@return Promise
local function customizeSelector(bufnr)
local function handleFallbackException(err, providerName)
if type(err) == 'string' and err:match('UfoFallbackException') then
return require('ufo').getFolds(bufnr, providerName)
else
return require('promise').reject(err)
end
end
return require('ufo').getFolds(bufnr, 'lsp'):catch(function(err)
return handleFallbackException(err, 'treesitter')
end):catch(function(err)
return handleFallbackException(err, 'indent')
end)
end
require('ufo').setup({
provider_selector = function(bufnr, filetype, buftype)
return ftMap[filetype] or customizeSelector
end
})
end
local function selectProviderWithFunction()
---@param bufnr number
---@return UfoFoldingRange[]
local function customizeSelector(bufnr)
local res = {}
table.insert(res, {startLine = 1, endLine = 3})
table.insert(res, {startLine = 5, endLine = 10})
return res
end
local ftMap = {
vim = 'indent',
python = {'indent'},
git = customizeSelector
}
require('ufo').setup({
provider_selector = function(bufnr, filetype, buftype)
return ftMap[filetype]
end
})
end
-----------------------------------------providerSelector-------------------------------------------
------------------------------------------enhanceAction---------------------------------------------
local function peekOrHover()
local winid = require('ufo').peekFoldedLinesUnderCursor()
if winid then
local bufnr = vim.api.nvim_win_get_buf(winid)
local keys = {'a', 'i', 'o', 'A', 'I', 'O', 'gd', 'gr'}
for _, k in ipairs(keys) do
-- Add a prefix key to fire `trace` action,
-- if Neovim is 0.8.0 before, remap yourself
vim.keymap.set('n', k, '<CR>' .. k, {noremap = false, buffer = bufnr})
end
else
-- coc.nvim
vim.fn.CocActionAsync('definitionHover')
-- nvimlsp
vim.lsp.buf.hover()
end
end
local function goPreviousClosedAndPeek()
require('ufo').goPreviousClosedFold()
require('ufo').peekFoldedLinesUnderCursor()
end
local function goNextClosedAndPeek()
require('ufo').goNextClosedFold()
require('ufo').peekFoldedLinesUnderCursor()
end
local function applyFoldsAndThenCloseAllFolds(providerName)
require('async')(function()
local bufnr = vim.api.nvim_get_current_buf()
-- make sure buffer is attached
require('ufo').attach(bufnr)
-- getFolds return Promise if providerName == 'lsp'
local ranges = await(require('ufo').getFolds(bufnr, providerName))
if not vim.tbl_isempty(ranges) then
local ok = require('ufo').applyFolds(bufnr, ranges)
if ok then
require('ufo').closeAllFolds()
end
end
end)
end
------------------------------------------enhanceAction---------------------------------------------
---------------------------------------setFoldVirtTextHandler---------------------------------------
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)
-- str width returned from truncate() may less than 2nd argument, need padding
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 function customizeFoldText()
-- global handler
require('ufo').setup({
fold_virt_text_handler = handler
})
end
local function customizeBufFoldText()
-- buffer scope handler
-- will override global handler if it is existed
local bufnr = vim.api.nvim_get_current_buf()
require('ufo').setFoldVirtTextHandler(bufnr, handler)
end
local function inspectVirtTextForFoldedLines()
require('ufo').setup({
enable_get_fold_virt_text = true,
fold_virt_text_handler = function(virtText, lnum, endLnum, width, truncate, ctx)
for i = lnum, endLnum do
print('lnum: ', i, ', virtText: ', vim.inspect(ctx.get_fold_virt_text(i)))
end
return virtText
end
})
end
---------------------------------------setFoldVirtTextHandler---------------------------------------