-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinit.lua
359 lines (325 loc) · 9.72 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
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
local global_options = {
identifiers = {"test", "it"},
terminal_cmd = ':vsplit | terminal',
path_to_jest_debug = 'node_modules/.bin/jest',
path_to_jest_run = 'jest',
stringCharacters = {"'", '"'},
expressions = {"call_expression"},
prepend = {"describe"},
regexStartEnd = true,
escapeRegex = true,
dap = {
type = 'node2',
request = 'launch',
args = { "--no-cache" },
sourceMaps = false,
protocol = 'inspector',
skipFiles = {'<node_internals>/**/*.js'},
console = 'integratedTerminal',
port = 9229,
disableOptimisticBPs = true
},
cache = { -- used to store the information about the last run
last_run = nil,
last_used_term_buf = nil
}
}
local ts_utils = require("nvim-treesitter.ts_utils")
local api = vim.api
local parsers = require "nvim-treesitter.parsers"
local function has_value (tab, val)
for index, value in ipairs(tab) do
if value == val then
return true
end
end
return false
end
-- test
function get_node_at_cursor_or_above(winnr)
winnr = winnr or 0
local cursor = api.nvim_win_get_cursor(winnr)
local cursor_range = { cursor[1] - 1, cursor[2] }
local buf = vim.api.nvim_win_get_buf(winnr)
local root_lang_tree = parsers.get_parser(buf)
if not root_lang_tree then
return
end
local root = ts_utils.get_root_for_position(cursor_range[1], cursor_range[2], root_lang_tree)
if not root then
return
end
-- Fix because comments won't yield the correct root
local cur_cursor_line = cursor_range[1]
while cur_cursor_line > 0 and root:type() ~= "program" do
cur_cursor_line = cur_cursor_line - 1
root = ts_utils.get_root_for_position(cur_cursor_line, 0, root_lang_tree)
end
local found = root:named_descendant_for_range(cursor_range[1], cursor_range[2], cursor_range[1], cursor_range[2])
return found
end
local function find_nearest_node_obj(identifiers, prepend, expressions)
local node = get_node_at_cursor_or_above()
while node do
local node_type = node:type()
if has_value(expressions, node_type) then
local node_text = vim.treesitter.get_node_text(node, 0)
local identifier = string.match(node_text, "^[a-zA-Z0-9]*")
if has_value(identifiers, identifier) then
return { node = node, from_identifier = true }
elseif has_value(prepend, identifier) then
return { node = node, from_identifier = false }
end
end
node = node:parent()
end
end
local function prepend_node(current_node, prepend, expressions)
local node = current_node:parent()
if not node then
return
end
while node do
local node_type = node:type()
if has_value(expressions, node_type) then
local node_text = vim.treesitter.get_node_text(node, 0)
local identifier = string.match(node_text, "^[a-zA-Z0-9]*")
if has_value(prepend, identifier) then
return node
end
end
node = node:parent()
end
end
local function remove_quotations(stringCharacters, str)
local result = str
for index, value in ipairs(stringCharacters) do
result = result:gsub("^".. value, ""):gsub(value .. "$", "")
end
return result
end
local function get_identifier(node, stringCharacters)
local child = node:child(1)
local arguments = child:child(1)
return remove_quotations(stringCharacters, vim.treesitter.get_node_text(arguments, 0))
end
local function regexEscape(str)
return vim.fn.escape(str, '!"().+-*?^[]$')
end
local function get_result(o)
local result
local nearest_node_obj = find_nearest_node_obj(o.identifiers, o.prepend, o.expressions)
if not nearest_node_obj or not nearest_node_obj.node then
print("Could not find any of the following: " .. table.concat(o.identifiers, ", ") .. ", " .. table.concat(o.prepend, ", "))
return
end
local nearest_node = nearest_node_obj.node
result = get_identifier(nearest_node, o.stringCharacters)
if o.prepend then
local node = prepend_node(nearest_node, o.prepend, o.expressions)
while node do
local parent_identifier = get_identifier(node, o.stringCharacters)
result = parent_identifier .. " " .. result
node = prepend_node(node, o.prepend, o.expressions)
end
end
if o.escapeRegex then
result = regexEscape(result)
end
if o.regexStartEnd then
result = "^" .. result
if nearest_node_obj.from_identifier then
result = result .. "$"
end
end
return result
end
local function debug_jest(o)
local result = o.result
local file = o.file
local dap = require('dap')
local type = o.dap.type
local request = o.dap.request
local cwd = o.dap.cwd
if cwd == nil then
cwd = vim.fn.getcwd()
end
local runtimeArgs = o.dap.runtimeArgs
local path_to_jest = o.path_to_jest or o.path_to_jest_debug -- o.path_to_jest is only for backwards compatibility
if runtimeArgs == nil then
if result then
runtimeArgs = {'--inspect-brk', '$path_to_jest', '--no-coverage', '-t', '$result', '--', '$file'}
else
runtimeArgs = {'--inspect-brk', '$path_to_jest', '--no-coverage', '--', '$file'}
end
end
local testTimeout = o.dap.testTimeout
if testTimeout ~= nil then
table.insert(runtimeArgs, 3, "--testTimeout=" .. testTimeout)
end
for key, value in pairs(runtimeArgs) do
if string.match(value, "$result") then
runtimeArgs[key] = value:gsub("$result", result)
end
if string.match(value, "$file") then
runtimeArgs[key] = runtimeArgs[key]:gsub("$file", file)
end
if string.match(value, "$path_to_jest") then
runtimeArgs[key] = value:gsub("$path_to_jest", path_to_jest)
end
end
local config = vim.tbl_deep_extend('force', o.dap, { type = type, request = request, cwd = cwd, runtimeArgs = runtimeArgs })
dap.run(config)
end
local function adjust_cmd(cmd, result, file)
local adjusted_cmd = cmd
if result and string.match(adjusted_cmd, "$result") then
adjusted_cmd = cmd:gsub("$result", result)
end
if string.match(adjusted_cmd, "$file") then
adjusted_cmd = adjusted_cmd:gsub("$file", file)
end
-- adjusted_cmd = adjusted_cmd:gsub("\\", "\\\\") -- needs double escaping
return adjusted_cmd
end
local function run(o)
local cmd
local result
if not o then
o = {}
end
local file = o.file
local options = vim.tbl_deep_extend('force', global_options, o)
if options.run_last then
if options.cache.last_run == nil then
print("You must run some test(s) before")
return
end
result = options.cache.last_run.result
file = options.cache.last_run.file
cmd = options.cache.last_run.cmd
end
if options.cmd then
cmd = options.cmd
end
if cmd == nil then
if options.run_file == true then
cmd = (options.path_to_jest or options.path_to_jest_run) .. " -- $file"
else
cmd = (options.path_to_jest or options.path_to_jest_run) .. " -t '$result' -- $file"
end
end
if file == nil then
file = vim.fn.expand('%:p')
end
if not options.run_last and not options.run_file then
result = get_result(options)
if not result then return end
end
global_options.cache.last_run = { result = result, file = file, cmd = cmd }
if options.escapeRegex==nil or options.escapeRegex then
file = regexEscape(file)
end
if options.func then
return options.func(vim.tbl_deep_extend('force', options, { result = result, file = file }))
end
-- local adjusted_cmd = vim.fn.escape(vim.fn.escape(adjust_cmd(cmd, result, file), "\\"), '\\')
local adjusted_cmd = vim.fn.escape(adjust_cmd(cmd, result, file), '\\')
local terminal_cmd = options.terminal_cmd
if global_options.cache.last_used_term_buf ~= nil and api.nvim_buf_is_valid(global_options.cache.last_used_term_buf) then
local term_buf_win = false
for _, win in pairs(api.nvim_tabpage_list_wins(0)) do
if api.nvim_win_get_buf(win) == global_options.cache.last_used_term_buf then
term_buf_win = true
api.nvim_set_current_win(win)
end
end
if not term_buf_win then
api.nvim_buf_delete(global_options.cache.last_used_term_buf, {force=true})
api.nvim_command(terminal_cmd)
global_options.cache.last_used_term_buf = vim.api.nvim_get_current_buf()
end
else
api.nvim_command(terminal_cmd)
global_options.cache.last_used_term_buf = vim.api.nvim_get_current_buf()
end
local chan_id
for _, chan in pairs(vim.api.nvim_list_chans()) do
if chan.buffer == global_options.cache.last_used_term_buf then
chan_id = chan.id
end
end
vim.api.nvim_chan_send(chan_id, adjusted_cmd .. '\n')
end
-- options = vim.tbl_deep_extend('force', options, opts)
local function terminate(cb)
local dap = require('dap')
if dap.terminate then
dap.terminate(nil, nil, function()
cb()
end)
else
dap.disconnect({ terminateDebuggee = true })
dap.close()
end
end
local function debug(o)
if o == nil then
o = {}
end
if o.func == nil then
o.func = debug_jest
end
--terminate(function()
return run(o)
--end)
end
local function debug_last(o)
-- dap.run_last() would also work, but we want freely exchange it with run
if o == nil then
o = {}
end
if o.func == nil then
o.func = debug_jest
end
o.run_last = true
--terminate(function()
return run(o)
--end)
end
local function run_file(o)
if o == nil then
o = {}
end
o.run_file = true
return run(o)
end
local function debug_file(o)
if o == nil then
o = {}
end
if o.func == nil then
o.func = debug_jest
end
o.run_file = true
return run(o)
end
local function run_last(o)
if o == nil then
o = {}
end
o.run_last = true
return run(o)
end
local function setup(o)
global_options = vim.tbl_deep_extend('force', global_options, o)
end
return {
setup = setup,
run = run,
run_last = run_last,
run_file = run_file,
debug = debug,
debug_last = debug_last,
debug_file = debug_file,
}