This repository was archived by the owner on Oct 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathhover.lua
More file actions
387 lines (356 loc) · 11.5 KB
/
hover.lua
File metadata and controls
387 lines (356 loc) · 11.5 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
-- define some hover related function modified from neovim source code
local vim = vim
local validate = vim.validate
local api = vim.api
local opt = require 'completion.option'
local manager = require 'completion.manager'
local M = {}
local function ok_or_nil(status, ...)
if not status then return end
return ...
end
local function npcall(fn, ...)
return ok_or_nil(pcall(fn, ...))
end
local function find_window_by_var(name, value)
for _, win in ipairs(api.nvim_list_wins()) do
if npcall(api.nvim_win_get_var, win, name) == value then
return win
end
end
end
M.focusable_float = function(unique_name, fn)
if npcall(api.nvim_win_get_var, 0, unique_name) then
return api.nvim_command("wincmd p")
end
local bufnr = api.nvim_get_current_buf()
do
local win = find_window_by_var(unique_name, bufnr)
if win then
api.nvim_win_close(win, true)
end
end
local pbufnr, pwinnr = fn()
if pbufnr then
api.nvim_win_set_var(pwinnr, unique_name, bufnr)
return pbufnr, pwinnr
end
end
---------------------------------
-- floating window for hover --
---------------------------------
local make_floating_popup_options = function(width, height, opts)
validate {
opts = { opts, 't', true };
}
opts = opts or {}
validate {
["opts.offset_x"] = { opts.offset_x, 'n', true };
["opts.offset_y"] = { opts.offset_y, 'n', true };
}
local lines_above = vim.fn.winline() - 1
local lines_below = vim.fn.winheight(0) - lines_above
local col
if lines_above < lines_below then
height = math.min(lines_below, height)
else
height = math.min(lines_above, height)
end
if opts.align == 'right' then
col = opts.col + opts.width
else
col = opts.col - width - 1
end
local default_border = {
{"", "NormalFloat"},
{"", "NormalFloat"},
{"", "NormalFloat"},
{" ", "NormalFloat"},
{"", "NormalFloat"},
{"", "NormalFloat"},
{"", "NormalFloat"},
{" ", "NormalFloat"},
}
return {
col = col,
height = height,
relative = 'editor',
row = opts.row,
focusable = false,
style = 'minimal',
width = width,
border = vim.g.completion_popup_border or default_border
}
end
local fancy_floating_markdown = function(contents, opts)
local pad_left = opts and opts.pad_left
local pad_right = opts and opts.pad_right
local stripped = {}
local highlights = {}
local max_width
if opts.align == 'right' then
local columns = api.nvim_get_option('columns')
max_width = columns - opts.col - opts.width
else
max_width = opts.col - 1
end
do
local i = 1
while i <= #contents do
local line = contents[i]
local ft = line:match("^```([a-zA-Z0-9_]*)$")
if ft then
local start = #stripped
i = i + 1
while i <= #contents do
line = contents[i]
if line == "```" then
i = i + 1
break
end
if #line > max_width then
while #line > max_width do
local trimmed_line = string.sub(line, 1, max_width)
local index = trimmed_line:reverse():find(" ")
if index == nil or index > #trimmed_line/2 then
break
else
table.insert(stripped, string.sub(line, 1, max_width-index))
line = string.sub(line, max_width-index+2, #line)
end
end
table.insert(stripped, line)
else
table.insert(stripped, line)
end
i = i + 1
end
table.insert(highlights, {
ft = ft;
start = start + 1;
finish = #stripped + 1 - 1
})
else
if #line > max_width then
while #line > max_width do
local trimmed_line = string.sub(line, 1, max_width)
-- local index = math.max(trimmed_line:reverse():find(" "), trimmed_line:reverse():find("/"))
local index = trimmed_line:reverse():find(" ")
if index == nil or index > #trimmed_line/2 then
break
else
table.insert(stripped, string.sub(line, 1, max_width-index))
line = string.sub(line, max_width-index+2, #line)
end
end
table.insert(stripped, line)
else
table.insert(stripped, line)
end
i = i + 1
end
end
end
local width = 0
for i, v in ipairs(stripped) do
v = v:gsub("\r", "")
if pad_left then v = (" "):rep(pad_left)..v end
if pad_right then v = v..(" "):rep(pad_right) end
stripped[i] = v
width = math.max(width, #v)
end
if opts.align == 'right' then
local columns = api.nvim_get_option('columns')
if opts.col + opts.row + width > columns then
width = columns - opts.col - opts.width -1
end
else
if width > opts.col then
width = opts.col - 1
end
end
local insert_separator = true
if insert_separator then
for i, h in ipairs(highlights) do
h.start = h.start + i - 1
h.finish = h.finish + i - 1
if h.finish + 1 <= #stripped then
table.insert(stripped, h.finish + 1, string.rep("─", width))
end
end
end
-- Make the floating window.
local height = #stripped
local bufnr = api.nvim_create_buf(false, true)
local winnr
if opt.get_option('docked_hover') == 1 then
if height > opt.get_option('docked_maximum_size') then
height = opt.get_option('docked_maximum_size')
elseif height < opt.get_option('docked_minimum_size') then
height = opt.get_option('docked_minimum_size')
end
local row
if vim.fn.winline() > api.nvim_get_option('lines')/2 then
row = 0
else
row = api.nvim_get_option('lines') - height
end
winnr = api.nvim_open_win(bufnr, false, {
col = 0,
height = height,
relative = 'editor',
row = row,
focusable = true,
style = 'minimal',
width = api.nvim_get_option('columns'),
})
else
local opt = make_floating_popup_options(width, height, opts)
if opt.width <= 0 then return end
winnr = api.nvim_open_win(bufnr, false, opt)
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, stripped)
-- setup a variable for floating window, fix #223
vim.api.nvim_buf_set_var(bufnr, "lsp_floating", true)
local cwin = vim.api.nvim_get_current_win()
vim.api.nvim_set_current_win(winnr)
vim.cmd("ownsyntax markdown")
local idx = 1
local function highlight_region(ft, start, finish)
if ft == '' then return end
local name = ft..idx
idx = idx + 1
local lang = "@"..ft:upper()
-- TODO(ashkan): better validation before this.
if not pcall(vim.cmd, string.format("syntax include %s syntax/%s.vim", lang, ft)) then
return
end
vim.cmd(string.format("syntax region %s start=+\\%%%dl+ end=+\\%%%dl+ contains=%s", name, start, finish + 1, lang))
end
for _, h in ipairs(highlights) do
highlight_region(h.ft, h.start, h.finish)
end
vim.api.nvim_set_current_win(cwin)
return bufnr, winnr
end
local function handler_function(_, ...)
if vim.fn.pumvisible() == 1 then
local method = select(1, ...)
local result = select(2, ...)
if type(method) == 'table' then
result = select(1, ...)
method = select(2, ...).method
end
M.focusable_float(method, function()
if not (result and result.contents) then
-- return { 'No information available' }
return
end
local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(result.contents)
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then
-- return { 'No information available' }
return
end
local bufnr, winnr
-- modified to open hover window align to popupmenu
local position = vim.fn.pum_getpos()
-- Set max width option to avoid overlapping with popup menu
local total_column = api.nvim_get_option('columns')
local align
local col = position['col']
if position['col'] < total_column/2 then
align = 'right'
if position['scrollbar'] then
col = col + 1
end
else
align = 'left'
end
bufnr, winnr = fancy_floating_markdown(markdown_lines, {
pad_left = 0; pad_right = 1;
col = col; width = position['width']; row = position['row']-1;
align = align
})
M.winnr = winnr
if winnr ~= nil and api.nvim_win_is_valid(winnr) then
vim.lsp.util.close_preview_autocmd({"CursorMoved", "BufHidden", "InsertCharPre"}, winnr)
end
local hover_len = #vim.api.nvim_buf_get_lines(bufnr,0,-1,false)[1]
local win_width = vim.api.nvim_win_get_width(0)
if hover_len > win_width then
vim.api.nvim_win_set_width(winnr,math.min(hover_len,win_width))
vim.api.nvim_win_set_height(winnr,math.ceil(hover_len/win_width))
vim.wo[winnr].wrap = true
end
return bufnr, winnr
end)
end
end
M.autoOpenHoverInPopup = function()
if vim.fn.pumvisible() ~= 1 then return end
local bufnr = api.nvim_get_current_buf()
if api.nvim_call_function('pumvisible', {}) == 1 then
-- Auto open hover
local items = api.nvim_call_function('complete_info', {{"eval", "selected", "items", "user_data"}})
if items['selected'] ~= manager.selected then
manager.textHover = true
if M.winnr ~= nil and api.nvim_win_is_valid(M.winnr) then
api.nvim_win_close(M.winnr, true)
end
M.winnr = nil
end
if manager.textHover == true and items['selected'] ~= -1 then
if items['selected'] == -2 then
items['selected'] = 0
end
local item = items['items'][items['selected']+1]
local user_data = item['user_data']
if user_data ~= nil and #user_data ~= 0 then
user_data = vim.fn.json_decode(item['user_data'])
end
if user_data ~= nil and user_data['lsp'] == nil then
if user_data['hover'] ~= nil and type(user_data['hover']) == 'string' and #user_data['hover'] ~= 0 then
local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(user_data['hover'])
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
local position = vim.fn.pum_getpos()
-- Set max width option to avoid overlapping with popup menu
local total_column = api.nvim_get_option('columns')
local align
if position['col'] < total_column/2 then
align = 'right'
else
align = 'left'
end
local _, winnr = fancy_floating_markdown(markdown_lines, {
pad_left = 1; pad_right = 1;
col = position['col']; width = position['width']; row = position['row']-1;
align = align
})
M.winnr = winnr
end
else
local has_hover = false
for _, value in pairs(vim.lsp.buf_get_clients(0)) do
if value.resolved_capabilities.hover then
has_hover = true
break
end
end
if not has_hover then return end
local row, col = unpack(api.nvim_win_get_cursor(0))
row = row - 1
local line = api.nvim_buf_get_lines(0, row, row+1, true)[1]
col = vim.str_utfindex(line, col)
local params = {
textDocument = vim.lsp.util.make_text_document_params();
position = { line = row; character = col-string.len(item.word); }
}
vim.lsp.buf_request(bufnr, 'textDocument/hover', params, handler_function)
end
manager.textHover = false
end
manager.selected = items['selected']
end
end
return M