generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinit.lua
196 lines (174 loc) · 4.32 KB
/
init.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
187
188
189
190
191
192
193
194
195
196
local Utils = require("magenta.utils")
local Options = require("magenta.options")
require("magenta.actions")
local M = {}
M.setup = function(opts)
Options.set_options(opts)
M.start(true)
require("magenta.keymaps").default_keymaps()
end
M.testSetup = function()
-- do not start. The test runner will start the process for us.
vim.api.nvim_set_keymap(
"n",
"<leader>m",
":Magenta toggle<CR>",
{silent = true, noremap = true, desc = "Toggle Magenta window"}
)
end
M.start = function(silent)
if not silent then
vim.notify("magenta: init", vim.log.levels.INFO)
end
local __filename = debug.getinfo(1, "S").source:sub(2)
local plugin_root = vim.fn.fnamemodify(__filename, ":p:h:h:h") .. "/"
local env = {
IS_DEV = false,
LOG_LEVEL = "info"
}
local job_id =
vim.fn.jobstart(
"npm run start -s",
{
cwd = plugin_root,
stdin = "null",
on_exit = Utils.log_exit(env.LOG_LEVEL),
on_stdout = Utils.log_job(env.LOG_LEVEL, false),
on_stderr = Utils.log_job(env.LOG_LEVEL, true),
env = env
}
)
if job_id <= 0 then
vim.api.nvim_err_writeln("Failed to start magenta server. Error code: " .. job_id)
return
end
end
local normal_commands = {
"abort",
"clear",
"context-files",
"profile",
"start-inline-edit",
"toggle",
}
local visual_commands = {
"start-inline-edit-selection",
"paste-selection",
}
M.bridge = function(channelId)
vim.api.nvim_create_user_command(
"Magenta",
function(opts)
vim.rpcnotify(channelId, "magentaCommand", opts.args)
end,
{
nargs = "+",
range = true,
desc = "Execute Magenta command",
complete = function(ArgLead, CmdLine)
local commands = CmdLine:match("^'<,'>") and visual_commands or normal_commands
if ArgLead == '' then
return commands
end
-- Filter based on ArgLead
return vim.tbl_filter(function(cmd)
return cmd:find('^' .. ArgLead)
end, commands)
end
}
)
vim.api.nvim_create_autocmd(
"WinClosed",
{
pattern = "*",
callback = function()
vim.rpcnotify(channelId, "magentaWindowClosed", {})
end
}
)
M.listenToBufKey = function(bufnr, vimKey)
vim.keymap.set(
"n",
vimKey,
function()
vim.rpcnotify(channelId, "magentaKey", vimKey)
end,
{buffer = bufnr, noremap = true, silent = true}
)
end
M.lsp_response = function(requestId, response)
vim.rpcnotify(channelId, "magentaLspResponse", {requestId, response})
end
local opts = Options.options
return {
profiles = opts.profiles,
active_profile = opts.profiles[0],
sidebar_position = opts.sidebar_position
}
end
M.wait_for_lsp_attach = function(bufnr, capability, timeout_ms)
-- Default timeout of 1000ms if not specified
timeout_ms = timeout_ms or 1000
return vim.wait(
timeout_ms,
function()
local clients = vim.lsp.get_active_clients({bufnr = bufnr})
for _, client in ipairs(clients) do
if client.server_capabilities[capability] then
return true
end
end
return false
end
)
end
M.lsp_hover_request = function(requestId, bufnr, row, col)
local success = M.wait_for_lsp_attach(bufnr, "hoverProvider", 1000)
if not success then
M.lsp_response(requestId, "Timeout waiting for LSP client with hoverProvider to attach")
return
end
vim.lsp.buf_request_all(
bufnr,
"textDocument/hover",
{
textDocument = {
uri = vim.uri_from_bufnr(bufnr)
},
position = {
line = row,
character = col
}
},
function(responses)
M.lsp_response(requestId, responses)
end
)
end
M.lsp_references_request = function(requestId, bufnr, row, col)
local success = M.wait_for_lsp_attach(bufnr, "referencesProvider", 1000)
if not success then
M.lsp_response(requestId, "Timeout waiting for LSP client with referencesProvider to attach")
return
end
vim.lsp.buf_request_all(
bufnr,
"textDocument/references",
{
textDocument = {
uri = vim.uri_from_bufnr(bufnr)
},
position = {
line = row,
character = col
},
context = {
includeDeclaration = true
}
},
function(responses)
M.lsp_response(requestId, responses)
end
)
end
return M