-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathwinbar.lua
More file actions
289 lines (249 loc) · 7.33 KB
/
winbar.lua
File metadata and controls
289 lines (249 loc) · 7.33 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
local api = vim.api
local config = require('lspsaga').config.symbol_in_winbar
local ui = require('lspsaga').config.ui
local util = require('lspsaga.util')
local symbol = require('lspsaga.symbol')
local get_kind_icon = require('lspsaga.lspkind').get_kind_icon
local function bar_prefix()
return {
prefix = '%#Saga',
sep = '%#SagaSep#' .. config.separator .. '%*',
}
end
local function path_in_bar(buf)
local ft = vim.bo[buf].filetype
local icon, hl
if ui.devicon then
icon, hl = util.icon_from_devicon(ft)
end
local bar = bar_prefix()
local items = {}
local folder
if ui.foldericon then
folder = ui.winbar_prefix .. get_kind_icon(302)[2]
end
for item in util.path_itera(buf) do
if string.find(item, '%%') then
item = item:gsub('%%', '%%%%')
end
item = #items == 0
and '%#' .. (hl or 'SagaFileIcon') .. '#' .. (icon and icon .. ' ' or '') .. '%*' .. bar.prefix .. 'FileName#' .. item
or bar.prefix
.. 'Folder#'
.. (folder and folder or ui.winbar_prefix)
.. '%*'
.. bar.prefix
.. 'FolderName#'
.. item
.. '%*'
items[#items + 1] = item
if #items > config.folder_level then
break
end
end
local barstr = ''
for i = #items, 1, -1 do
barstr = barstr .. items[i] .. (i > 1 and bar.sep or '')
end
return barstr
end
--@private
local function pos_before(line, col, pos)
return line < pos.line or (line == pos.line and col < pos.character)
end
--@private
local function pos_after(line, col, pos)
return line > pos.line or (line == pos.line and col > pos.character)
end
--@private
local function binary_search(tbl, line, col)
local left = 1
local right = #tbl
while left <= right do
local mid = bit.rshift(left + right, 1)
local range = tbl[mid].range or tbl[mid].location.range
if not range then
return
end
if pos_before(line, col, range.start) then
right = mid - 1
elseif pos_after(line, col, range['end']) then
left = mid + 1
else
return mid
end
end
end
local function stl_escape(str)
return str:gsub('%%', '')
end
-- pylsp
local function has_container(node, elements)
local bar = bar_prefix()
local type, icon = unpack(get_kind_icon(5))
elements[#elements + 1] = string.format('%s%s#%s%s', bar.prefix, type, icon, node.containerName)
end
local function insert_elements(buf, node, elements)
if config.hide_keyword and symbol:node_is_keyword(buf, node) then
return
end
local type, icon = unpack(get_kind_icon(node.kind))
local bar = bar_prefix()
if node.name:find('%%') then
node.name = stl_escape(node.name)
end
if node.containerName then
has_container(node, elements)
end
if config.color_mode then
local node_context = string.format('%s%s#%s%s', bar.prefix, type, icon, node.name)
elements[#elements + 1] = node_context
else
elements[#elements + 1] =
string.format('%s%s#%s%sWord#%s', bar.prefix, type, icon, bar.prefix, node.name)
end
end
--@private
local function find_in_node(buf, tbl, line, col, elements)
local mid = binary_search(tbl, line, col)
if not mid then
return
end
local node = tbl[mid]
insert_elements(buf, tbl[mid], elements)
if node.children ~= nil and next(node.children) ~= nil then
find_in_node(buf, node.children, line, col, elements)
end
end
--@private
local function render_symbol_winbar(buf, symbols)
if api.nvim_get_current_buf() ~= buf then
return
end
-- don't show in float window.
local cur_win = api.nvim_get_current_win()
local winconf = api.nvim_win_get_config(cur_win)
if #winconf.relative > 0 then
return
end
local cursor = api.nvim_win_get_cursor(cur_win)
local current_line, current_col = cursor[1], cursor[2]
local winbar_str = config.show_file and path_in_bar(buf) or ''
local winbar_elements = {}
find_in_node(buf, symbols, current_line - 1, current_col, winbar_elements)
local lens, over_idx = 0, 0
local max_width = math.floor(api.nvim_win_get_width(cur_win) * 0.9)
for i, item in pairs(winbar_elements) do
local s = vim.split(item, '#')
lens = lens + api.nvim_strwidth(s[3]) + api.nvim_strwidth(config.separator)
if lens > max_width then
over_idx = i
lens = 0
end
end
if over_idx > 0 then
winbar_elements = { unpack(winbar_elements, over_idx) }
table.insert(winbar_elements, 1, '...')
end
local bar = bar_prefix()
local str = table.concat(winbar_elements, bar.sep)
if config.show_file and next(winbar_elements) ~= nil then
str = bar.sep .. str
end
winbar_str = winbar_str .. str
if config.enable and api.nvim_win_get_height(cur_win) - 1 > 1 then
if #winbar_str == 0 then
winbar_str = bar_prefix().prefix .. ' #'
end
api.nvim_set_option_value('winbar', winbar_str, { scope = 'local', win = cur_win })
end
return winbar_str
end
local function file_bar(buf)
local winid = api.nvim_get_current_win()
local winconf = api.nvim_win_get_config(winid)
if #winconf.relative ~= 0 then
return
end
if config.show_file then
api.nvim_set_option_value('winbar', path_in_bar(buf), { scope = 'local', win = winid })
else
api.nvim_set_option_value(
'winbar',
bar_prefix().prefix .. ' #',
{ scope = 'local', win = winid }
)
end
end
local function ignored(bufname)
for _, pattern in ipairs(util.as_table(config.ignore_patterns)) do
if bufname:find(pattern) then
return true
end
end
return false
end
local function init_winbar(buf)
if vim.o.diff or config.ignore_patterns and ignored(api.nvim_buf_get_name(buf)) then
return
end
file_bar(buf)
api.nvim_create_autocmd('User', {
pattern = 'SagaSymbolUpdate',
callback = function(opt)
local curbuf = api.nvim_get_current_buf()
if
vim.bo[opt.buf].buftype == 'nofile'
or curbuf ~= opt.data.bufnr
or #opt.data.symbols == 0
then
local curwin = api.nvim_get_current_win()
if not config.show_file and vim.wo[curwin].winbar ~= '' then
vim.wo[curwin].winbar = ''
end
return
end
render_symbol_winbar(opt.buf, opt.data.symbols)
end,
desc = 'Lspsaga get and show symbols',
})
api.nvim_create_autocmd({ 'CursorMoved' }, {
group = api.nvim_create_augroup('SagaWinbar' .. buf, { clear = true }),
buffer = buf,
callback = function(args)
local res = not util.nvim_ten() and symbol:get_buf_symbols(args.buf)
or require('lspsaga.symbol.head'):get_buf_symbols(args.buf)
if res and res.symbols and not vim.tbl_isempty(res.symbols) then
render_symbol_winbar(args.buf, res.symbols)
end
end,
desc = 'Lspsaga symbols render and request',
})
end
local function get_bar()
local curbuf = api.nvim_get_current_buf()
local res = not util.nvim_ten() and symbol:get_buf_symbols(curbuf)
or require('lspsaga.symbol.head'):get_buf_symbols(curbuf)
if res and res.symbols then
return render_symbol_winbar(curbuf, res.symbols)
end
end
local function toggle()
local curbuf = api.nvim_get_current_buf()
local ok, g = pcall(api.nvim_get_autocmds, {
group = 'SagaWinbar' .. curbuf,
event = { 'CursorMoved' },
buffer = curbuf,
})
if ok then
vim.opt_local.winbar = ''
api.nvim_del_augroup_by_id(g[1].group)
return
end
init_winbar(curbuf)
end
return {
init_winbar = init_winbar,
get_bar = get_bar,
toggle = toggle,
}