-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlua.lua
298 lines (281 loc) · 7.28 KB
/
lua.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
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
local dap = require('dap')
local api = vim.api
dap.adapters.nlua = function(callback, conf)
local adapter = {
type = 'server',
host = '127.0.0.1',
port = conf.port
}
if conf.start_neovim then
local start_opts = conf.start_neovim
conf.start_neovim = nil
local handle
local pid_or_err
local opts = {
args = {
'-e',
vim.v.progpath,
'-c',
string.format("lua require('osv').launch({ port = %s })", conf.port),
start_opts.fname or api.nvim_buf_get_name(0),
},
cwd = start_opts.cwd or vim.fn.getcwd(),
detached = true,
}
handle, pid_or_err = vim.loop.spawn('/usr/bin/alacritty', opts, function(code)
handle:close()
assert(code == 0, "Exit code must be 0, not " .. tostring(code))
end)
if not handle then
error(pid_or_err)
end
local timer = vim.loop.new_timer()
timer:start(1000, 0, function()
timer:stop()
timer:close()
vim.schedule(function()
callback(adapter)
end)
end)
else
callback(adapter)
end
end
dap.adapters["local-lua"] = {
type = 'executable',
command = 'local-lua-dbg',
enrich_config = function(config, on_config)
if not config["extensionPath"] then
local c = vim.deepcopy(config)
c.extensionPath = "/usr/lib/node_modules/local-lua-debugger-vscode/"
on_config(c)
else
on_config(config)
end
end
}
local function free_port()
local tcp = vim.loop.new_tcp()
tcp:bind('127.0.0.1', 0)
local port = tcp:getsockname().port
tcp:shutdown()
tcp:close()
return port
end
dap.configurations.lua = {
{
type = "nlua",
request = "attach",
name = "New instance (current file)",
port = free_port,
start_neovim = {}
},
{
type = "nlua",
request = "attach",
name = "New instance (dotfiles)",
port = free_port,
start_neovim = {
cwd = os.getenv('HOME') .. '/dotfiles',
fname = 'vim/.config/nvim/init.lua',
}
},
{
type = "nlua",
request = "attach",
name = "New instance (crate/crate)",
port = free_port,
start_neovim = {
cwd = os.getenv('HOME') .. '/dev/crate/crate',
fname = 'server/src/test/java/io/crate/planner/PlannerTest.java',
}
},
{
type = "nlua",
request = "attach",
name = "New instance (neovim/neovim)",
port = free_port,
start_neovim = {
cwd = os.getenv('HOME') .. '/dev/neovim/neovim',
fname = 'src/nvim/main.c',
}
},
{
type = 'nlua',
request = 'attach',
name = 'Attach',
port = function()
return assert(tonumber(vim.fn.input('Port: ')), 'Port is required')
end
},
{
name = 'Current file (local-lua-dbg, nlua)',
type = 'local-lua',
request = 'launch',
cwd = '${workspaceFolder}',
program = {
lua = 'nlua.lua',
file = '${file}',
},
verbose = true,
args = {},
},
{
name = 'Busted current file (local-lua-dbg, nlua)',
type = 'local-lua',
request = 'launch',
cwd = '${workspaceFolder}',
program = {
command = "busted",
},
verbose = true,
args = {
"${file}"
},
},
{
name = 'Current file (local-lua-dbg, lua)',
type = 'local-lua',
request = 'launch',
cwd = '${workspaceFolder}',
program = {
lua = 'lua5.1',
file = '${file}',
},
args = {},
},
}
if vim.loop.fs_stat(".stylua.toml") then
vim.bo.formatprg = "stylua -"
end
local lsp = require('me.lsp')
local config = lsp.mk_config {
name = "luals",
cmd = {'lua-language-server'},
root_dir = lsp.find_root({'.git'}),
settings = {
Lua = {
diagnostics = {
globals = {'vim', 'it', 'describe'}
},
runtime = {
version = "LuaJIT",
pathStrict = true,
},
workspace = {
library = {
"${3rd}/luv/library",
os.getenv("HOME") .. "/dev/neovim/neovim/runtime/lua/",
os.getenv("HOME") .. "/.config/nvim/pack/plugins/start/nvim-dap/lua",
os.getenv("HOME") .. "/.config/nvim/pack/plugins/start/nvim-fzy/lua",
os.getenv("HOME") .. "/.config/nvim/pack/plugins/start/nvim-qwahl/lua",
os.getenv("HOME") .. "/.config/nvim/pack/plugins/start/nvim-lsp-compl/lua",
os.getenv("HOME") .. "/.config/nvim/pack/plugins/start/nvim-lint/lua",
os.getenv("HOME") .. "/.config/nvim/pack/plugins/start/nvim-jdtls/lua",
os.getenv("HOME") .. "/.config/nvim/pack/plugins/opt/neodev.nvim/types/nightly",
},
checkThirdParty = false,
},
telemetry = {
enable = false,
},
}
}
}
vim.lsp.start(config)
if vim.loop.fs_stat("lemmy.sh") then
local bufnr = api.nvim_get_current_buf()
local group = vim.api.nvim_create_augroup('lemmy-' .. bufnr, { clear = true })
api.nvim_create_autocmd('BufWritePost', {
command = 'silent !./lemmy.sh',
buffer = bufnr,
group = group
})
end
local function find_test()
local bufnr = api.nvim_get_current_buf()
local lang = "lua"
local end_row = api.nvim_win_get_cursor(0)[1]
local query = vim.treesitter.query.parse(lang, [[
((function_call
name: (identifier) @name (#any-of? @name "describe" "it")
arguments: (arguments
((string) @str)
)
))
]])
local function get_text(root)
local start_row = root:range()
for id, node in query:iter_captures(root, bufnr, start_row, end_row) do
local name = query.captures[id]
local text = vim.treesitter.get_node_text(node, bufnr)
assert(text and type(text) == "string")
if name == "str" then
-- strip leading and trailing quotes
return text:sub(2, #text - 1)
end
end
return nil
end
local node = vim.treesitter.get_node()
local path = {}
while node ~= nil do
if node:type() == "function_call" then
local text = get_text(node)
if text then
table.insert(path, text)
end
end
node = node:parent()
end
for i = 1, math.floor(#path * 0.5) do
local tmp = path[i]
path[i] = path[#path - (i - 1)]
path[#path - (i - 1)] = tmp
end
return path
end
if config.root_dir and vim.endswith(config.root_dir, "neovim/neovim") then
local function run_test()
local path = find_test()
vim.cmd.split()
local fname = api.nvim_buf_get_name(0)
local filter = table.concat(path, " ")
vim.cmd.term(string.format(
[[TEST_FILE="%s" make functionaltest TEST_FILTER="%s"]],
fname,
filter
))
end
vim.keymap.set("n", "<leader>dn", run_test)
vim.keymap.set("n", "<leader>df", function()
vim.cmd.split()
vim.cmd.term(string.format(
[[TEST_FILE="%s" make functionaltest]],
api.nvim_buf_get_name(0)
))
end)
else
vim.keymap.set("n", "<leader>dn", function()
local path = find_test()
local name = table.concat(path, " ")
dap.run({
name = name,
type = "local-lua",
request = "launch",
cwd = "${workspaceFolder}",
program = {
command = "busted",
},
args = {
api.nvim_buf_get_name(0),
'--filter="' .. name .. '"',
}
})
end)
vim.keymap.set("n", "<leader>df", function()
vim.cmd.split()
local fname = api.nvim_buf_get_name(0)
vim.cmd.term('~/.luarocks/bin/busted "' .. fname .. '"')
end)
end