-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.lua
More file actions
72 lines (64 loc) · 2.06 KB
/
python.lua
File metadata and controls
72 lines (64 loc) · 2.06 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
local dap = require("dap")
--- @return string|nil
local function get_python_path()
-- always prioritize python from venv
local cwd = vim.fn.getcwd()
if vim.fn.executable(cwd .. "/venv/bin/python") == 1 then
return cwd .. "/venv/bin/python"
elseif vim.fn.executable(cwd .. "/.venv/bin/python") == 1 then
return cwd .. "/.venv/bin/python"
end
-- if not in venv, then use (if available) globally installed python3(.exe)
if vim.fn.has("win64") == 1 then
-- on Windows, vim.fn.executable("python3") returns 0 even if it is in PATH
if vim.fn.executable("python") == 1 then
return "python"
end
else -- linux, macos
if vim.fn.executable("python3") == 1 then
return "python3"
elseif vim.fn.executable("/usr/bin/python3") == 1 then
return "/usr/bin/python3"
end
end
-- otherwise prompt the user for python3 executable path
--- @type string
local chosen_path
vim.ui.input(
{ prompt = "provide absolute path to Python (to run debugpy): " },
function(result)
chosen_path = result
end
)
if vim.fn.executable(chosen_path) == 1 then
return chosen_path
else
vim.notify("you have provided an invalid Python path", vim.log.levels.ERROR)
return nil
end
end
dap.adapters.python = function(cb, _)
local python_path = get_python_path()
cb({
type = "executable",
-- to directly provide a python path, replace the python_path with "your-path-to-python",
command = python_path,
args = { "-m", "debugpy.adapter" },
options = {
source_filetype = "python",
},
})
end
dap.adapters.debugpy = dap.adapters.python
-- make sure not to override other C# DAP configs
if dap.configurations.python == nil then
dap.configurations.python = {}
end
table.insert(dap.configurations.python, {
type = "python",
request = "launch",
name = "Launch file",
-- options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings
program = "${file}", -- This configuration will launch the current file if used.
pythonPath = get_python_path(),
})