-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
dap-python.lua
384 lines (326 loc) · 11.5 KB
/
dap-python.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
---@mod dap-python Python extension for nvim-dap
local api = vim.api
local M = {}
--- Test runner to use by default. Default is "unittest". See |dap-python.test_runners|
--- Override this to set a different runner:
--- ```
--- require('dap-python').test_runner = "pytest"
--- ```
---@type string name of the test runner
M.test_runner = 'unittest'
--- Table to register test runners.
--- Built-in are test runners for unittest, pytest and django.
--- The key is the test runner name, the value a function to generate the
--- module name to run and its arguments. See |TestRunner|
---@type table<string, TestRunner>
M.test_runners = {}
local function prune_nil(items)
return vim.tbl_filter(function(x) return x end, items)
end
local is_windows = function()
return vim.loop.os_uname().sysname:find("Windows", 1, true) and true
end
local get_python_path = function()
local venv_path = os.getenv('VIRTUAL_ENV')
if venv_path then
if is_windows() then
return venv_path .. '\\Scripts\\python.exe'
end
return venv_path .. '/bin/python'
end
return nil
end
local enrich_config = function(config, on_config)
if not config.pythonPath and not config.python then
config.pythonPath = get_python_path()
end
on_config(config)
end
local default_setup_opts = {
include_configs = true,
console = 'integratedTerminal',
pythonPath = nil,
}
local default_test_opts = {
console = 'integratedTerminal'
}
local function load_dap()
local ok, dap = pcall(require, 'dap')
assert(ok, 'nvim-dap is required to use dap-python')
return dap
end
---@private
function M.test_runners.unittest(classname, methodname)
local path = vim.fn.expand('%:r:gs?/?.?')
local test_path = table.concat(prune_nil({path, classname, methodname}), '.')
local args = {'-v', test_path}
return 'unittest', args
end
---@private
function M.test_runners.pytest(classname, methodname)
local path = vim.fn.expand('%:p')
local test_path = table.concat(prune_nil({path, classname, methodname}), '::')
-- -s "allow output to stdout of test"
local args = {'-s', test_path}
return 'pytest', args
end
---@private
function M.test_runners.django(classname, methodname)
local path = vim.fn.expand('%:r:gs?/?.?')
local test_path = table.concat(prune_nil({path, classname, methodname}), '.')
local args = {'test', test_path}
return 'django', args
end
--- Register the python debug adapter
---@param adapter_python_path string|nil Path to the python interpreter. Path must be absolute or in $PATH and needs to have the debugpy package installed. Default is `python3`
---@param opts SetupOpts See |SetupOpts|
function M.setup(adapter_python_path, opts)
local dap = load_dap()
adapter_python_path = adapter_python_path and vim.fn.expand(vim.fn.trim(adapter_python_path)) or 'python3'
opts = vim.tbl_extend('keep', opts or {}, default_setup_opts)
dap.adapters.python = function(cb, config)
if config.request == 'attach' then
local port = (config.connect or config).port
cb({
type = 'server';
port = assert(port, '`connect.port` is required for a python `attach` configuration');
host = (config.connect or config).host or '127.0.0.1';
enrich_config = enrich_config;
options = {
source_filetype = 'python',
}
})
else
cb({
type = 'executable';
command = adapter_python_path;
args = { '-m', 'debugpy.adapter' };
enrich_config = enrich_config;
options = {
source_filetype = 'python',
}
})
end
end
if opts.include_configs then
dap.configurations.python = dap.configurations.python or {}
table.insert(dap.configurations.python, {
type = 'python';
request = 'launch';
name = 'Launch file';
program = '${file}';
console = opts.console;
pythonPath = opts.pythonPath,
})
table.insert(dap.configurations.python, {
type = 'python';
request = 'launch';
name = 'Launch file with arguments';
program = '${file}';
args = function()
local args_string = vim.fn.input('Arguments: ')
return vim.split(args_string, " +")
end;
console = opts.console;
pythonPath = opts.pythonPath,
})
table.insert(dap.configurations.python, {
type = 'python';
request = 'attach';
name = 'Attach remote';
connect = function()
local host = vim.fn.input('Host [127.0.0.1]: ')
host = host ~= '' and host or '127.0.0.1'
local port = tonumber(vim.fn.input('Port [5678]: ')) or 5678
return { host = host, port = port }
end;
})
end
end
local function get_nodes(query_text, predicate)
local end_row = api.nvim_win_get_cursor(0)[1]
local ft = api.nvim_buf_get_option(0, 'filetype')
assert(ft == 'python', 'test_method of dap-python only works for python files, not ' .. ft)
local query = vim.treesitter.parse_query(ft, query_text)
assert(query, 'Could not parse treesitter query. Cannot find test')
local parser = vim.treesitter.get_parser(0)
local root = (parser:parse()[1]):root()
local nodes = {}
for _, node in query:iter_captures(root, 0, 0, end_row) do
if predicate(node) then
table.insert(nodes, node)
end
end
return nodes
end
local function get_function_nodes()
local query_text = [[
(function_definition
name: (identifier) @name) @definition.function
]]
return get_nodes(query_text, function(node)
return node:type() == 'identifier'
end)
end
local function get_class_nodes()
local query_text = [[
(class_definition
name: (identifier) @name) @definition.class
]]
return get_nodes(query_text, function(node)
return node:type() == 'identifier'
end)
end
local function get_node_text(node)
local row1, col1, row2, col2 = node:range()
if row1 == row2 then
row2 = row2 + 1
end
local lines = api.nvim_buf_get_lines(0, row1, row2, true)
if #lines == 1 then
return (lines[1]):sub(col1 + 1, col2)
end
return table.concat(lines, '\n')
end
local function get_parent_classname(node)
local parent = node:parent()
while parent do
local type = parent:type()
if type == 'class_definition' then
for child in parent:iter_children() do
if child:type() == 'identifier' then
return get_node_text(child)
end
end
end
parent = parent:parent()
end
end
---@param opts DebugOpts
local function trigger_test(classname, methodname, opts)
local test_runner = opts.test_runner or M.test_runner
local runner = M.test_runners[test_runner]
if not runner then
vim.notify('Test runner `' .. test_runner .. '` not supported', vim.log.levels.WARN)
return
end
assert(type(runner) == "function", "Test runner must be a function")
local module, args = runner(classname, methodname)
local config = {
name = table.concat(prune_nil({classname, methodname}), '.'),
type = 'python',
request = 'launch',
module = module,
args = args,
console = opts.console
}
load_dap().run(vim.tbl_extend('force', config, opts.config or {}))
end
local function closest_above_cursor(nodes)
local result
for _, node in pairs(nodes) do
if not result then
result = node
else
local node_row1, _, _, _ = node:range()
local result_row1, _, _, _ = result:range()
if node_row1 > result_row1 then
result = node
end
end
end
return result
end
--- Run test class above cursor
---@param opts DebugOpts See |DebugOpts|
function M.test_class(opts)
opts = vim.tbl_extend('keep', opts or {}, default_test_opts)
local class_node = closest_above_cursor(get_class_nodes())
if not class_node then
print('No suitable test class found')
return
end
local class = get_node_text(class_node)
trigger_test(class, nil, opts)
end
--- Run the test method above cursor
---@param opts DebugOpts See |DebugOpts|
function M.test_method(opts)
opts = vim.tbl_extend('keep', opts or {}, default_test_opts)
local function_node = closest_above_cursor(get_function_nodes())
if not function_node then
print('No suitable test method found')
return
end
local class = get_parent_classname(function_node)
local function_name = get_node_text(function_node)
trigger_test(class, function_name, opts)
end
--- Strips extra whitespace at the start of the lines
--
-- >>> remove_indent({' print(10)', ' if True:', ' print(20)'})
-- {'print(10)', 'if True:', ' print(20)'}
local function remove_indent(lines)
local offset = nil
for _, line in ipairs(lines) do
local first_non_ws = line:find('[^%s]') or 0
if first_non_ws >= 1 and (not offset or first_non_ws < offset) then
offset = first_non_ws
end
end
if offset > 1 then
return vim.tbl_map(function(x) return string.sub(x, offset) end, lines)
else
return lines
end
end
--- Debug the selected code
---@param opts DebugOpts
function M.debug_selection(opts)
opts = vim.tbl_extend('keep', opts or {}, default_test_opts)
local start_row, _ = unpack(api.nvim_buf_get_mark(0, '<'))
local end_row, _ = unpack(api.nvim_buf_get_mark(0, '>'))
local lines = api.nvim_buf_get_lines(0, start_row - 1, end_row, false)
local code = table.concat(remove_indent(lines), '\n')
local config = {
type = 'python',
request = 'launch',
code = code,
console = opts.console
}
load_dap().run(vim.tbl_extend('force', config, opts.config or {}))
end
---@class PathMapping
---@field localRoot string
---@field remoteRoot string
---@class DebugpyConfig
---@field django boolean|nil Enable django templates. Default is `false`
---@field gevent boolean|nil Enable debugging of gevent monkey-patched code. Default is `false`
---@field jinja boolean|nil Enable jinja2 template debugging. Default is `false`
---@field justMyCode boolean|nil Debug only user-written code. Default is `true`
---@field pathMappings PathMapping[]|nil Map of local and remote paths.
---@field pyramid boolean|nil Enable debugging of pyramid applications
---@field redirectOutput boolean|nil Redirect output to debug console. Default is `false`
---@field showReturnValue boolean|nil Shows return value of function when stepping
---@field sudo boolean|nil Run program under elevated permissions. Default is `false`
---@class DebugpyLaunchConfig : DebugpyConfig
---@field module string|nil Name of the module to debug
---@field program string|nil Absolute path to the program
---@field code string|nil Code to execute in string form
---@field python string[]|nil Path to python executable and interpreter arguments
---@field args string[]|nil Command line arguments passed to the program
---@field console DebugpyConsole See |DebugpyConsole|
---@field cwd string|nil Absolute path to the working directory of the program being debugged.
---@field env table|nil Environment variables defined as key value pair
---@field stopOnEntry boolean|nil Stop at first line of user code.
---@class DebugOpts
---@field console DebugpyConsole See |DebugpyConsole|
---@field test_runner "unittest"|"pytest"|"django"|string name of the test runner. Default is |dap-python.test_runner|
---@field config DebugpyConfig Overrides for the configuration
---@class SetupOpts
---@field include_configs boolean Add default configurations
---@field console DebugpyConsole See |DebugpyConsole|
---@field pythonPath string|nil Path to python interpreter. Uses interpreter from `VIRTUAL_ENV` environment variable or `adapter_python_path` by default
---@alias TestRunner fun(classname: string, methodname: string): string module, string[] args
---@alias DebugpyConsole "internalConsole"|"integratedTerminal"|"externalTerminal"|nil
return M