-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrustowl.lua
More file actions
100 lines (91 loc) · 2.82 KB
/
rustowl.lua
File metadata and controls
100 lines (91 loc) · 2.82 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
local M = {}
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)
local clients = vim.lsp.get_clients({ bufnr = bufnr, name = "rustowl" })
for _, client in ipairs(clients) do
local line, col = table.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(_, result, _)
if result ~= nil then
for _, deco in ipairs(result["decorations"]) do
if deco["is_display"] == true then
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
end, bufnr)
end
end
local function rustowl_on_attach(hover, _, 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()
if not timer then
return
end
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
M.rustowl_cursor = function(...)
local args = { ... }
local bufnr = args[1] or vim.api.nvim_get_current_buf()
show_rustowl(bufnr)
end
local me = require("kide.melspconfig")
M.config = {
name = "rustowl",
cmd = { "cargo", "owlsp" },
root_dir = vim.fs.root(0, { ".git", "Cargo.toml" }),
filetypes = { "rust" },
on_attach = function(client, bufnr)
rustowl_on_attach(false, client, bufnr, 2000)
me.on_attach(client, bufnr)
end,
on_init = me.on_init,
capabilities = me.capabilities({}),
}
return M