-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathrustowl.lua
141 lines (125 loc) · 4.12 KB
/
rustowl.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
local lspconfig = require('lspconfig')
local util = require('lspconfig.util')
local configs = require('lspconfig.configs')
local hlns = vim.api.nvim_create_namespace('rustowl')
vim.api.nvim_set_hl(0, 'lifetime', { undercurl = true, sp = '#00cc00' })
vim.api.nvim_set_hl(0, 'imm_borrow', { undercurl = true, sp = '#0000cc' })
vim.api.nvim_set_hl(0, 'mut_borrow', { undercurl = true, sp = '#cc00cc' })
vim.api.nvim_set_hl(0, 'move', { undercurl = true, sp = '#cccc00' })
vim.api.nvim_set_hl(0, 'call', { undercurl = true, sp = '#cccc00' })
vim.api.nvim_set_hl(0, 'outlive', { undercurl = true, sp = '#cc0000' })
local function show_rustowl(bufnr)
bufnr = util.validate_bufnr(bufnr)
local clients = util.get_lsp_clients { bufnr = bufnr, name = 'rustowl' }
for _, client in ipairs(clients) do
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
client.request(
'rustowl/cursor',
{
position = {
line = line - 1,
character = col,
},
document = vim.lsp.util.make_text_document_params(),
},
function(err, result, ctx)
if result ~= nil then
for _, deco in ipairs(result['decorations']) do
local start = { deco['range']['start']['line'], deco['range']['start']['character'] }
local finish = { deco['range']['end']['line'], deco['range']['end']['character'] }
vim.highlight.range(
bufnr,
hlns,
deco['type'],
start,
finish,
{ regtype = "v", inclusive = true }
)
end
end
end,
bufnr
)
end
end
local function rustowl_on_attach(hover, client, bufnr, idle_time_ms)
local timer = nil
local augroup = vim.api.nvim_create_augroup('RustOwlCmd', { clear = true })
local function clear_timer()
if timer then
timer:stop()
timer:close()
timer = nil
end
end
local function start_timer()
clear_timer()
timer = vim.uv.new_timer()
timer:start(
idle_time_ms,
0,
vim.schedule_wrap(
function()
show_rustowl(bufnr)
end
)
)
end
vim.api.nvim_create_autocmd(
{ 'CursorMoved', 'CursorMovedI' },
{
group = augroup,
buffer = bufnr,
callback = function()
vim.api.nvim_buf_clear_namespace(bufnr, hlns, 0, -1)
if hover == true then
start_timer()
end
end
}
)
vim.api.nvim_create_autocmd(
'BufUnload',
{
group = augroup,
buffer = bufnr,
callback = clear_timer
}
)
start_timer()
end
if not configs.rustowl then
configs.rustowl = {
default_config = {
cmd = { 'cargo', 'owlsp' },
root_dir = lspconfig.util.root_pattern('Cargo.toml', '.git'),
filetypes = { 'rust' },
on_attach = function(client, bufnr)
end,
},
idle_time = 2000,
}
local orig_setup = lspconfig.rustowl.setup
lspconfig.rustowl.setup = function(user_opts)
user_opts = user_opts or {}
local user = user_opts.on_attach
local trigger = user_opts.trigger or {
hover = true,
}
local idle_time = tonumber(user_opts.idle_time) or 2000
user_opts.on_attach = function(client, bufnr)
rustowl_on_attach(trigger.hover, client, bufnr, idle_time)
if type(user) == 'function' then
user(client, bufnr)
end
end
orig_setup(user_opts)
end
end
return {
rustowl_cursor = function(...)
args = {...}
bufnr = args[1] or vim.api.nvim_get_current_buf()
show_rustowl(bufnr)
end,
}