Skip to content

Commit

Permalink
perf(debugpy): simplify venv detection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jint-lzxy committed Apr 4, 2024
1 parent 0bec5c5 commit 65d580e
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions lua/modules/configs/tool/dap/clients/python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
-- https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings
return function()
local dap = require("dap")
local is_windows = require("core.global").is_windows
local data_dir = require("core.global").data_dir
local python = is_windows and data_dir .. "../mason/packages/debugpy/venv/Scripts/pythonw.exe"
or data_dir .. "../mason/packages/debugpy/venv/bin/python"
local utils = require("modules.utils.dap")

local function is_empty(s)
return s == nil or s == ""
end
local is_windows = require("core.global").is_windows
local debugpy_root = require("mason-registry").get_package("debugpy"):get_install_path()

dap.adapters.python = function(callback, config)
if config.request == "attach" then
Expand All @@ -25,7 +19,8 @@ return function()
else
callback({
type = "executable",
command = python,
command = is_windows and debugpy_root .. "/venv/Scripts/pythonw.exe"
or debugpy_root .. "/venv/bin/python",
args = { "-m", "debugpy.adapter" },
options = { source_filetype = "python" },
})
Expand All @@ -41,9 +36,9 @@ return function()
console = "integratedTerminal",
program = utils.input_file_path(),
pythonPath = function()
if not is_empty(vim.env.CONDA_PREFIX) then
return is_windows and vim.env.CONDA_PREFIX .. "/Scripts/pythonw.exe"
or vim.env.CONDA_PREFIX .. "/bin/python"
local venv = vim.env.CONDA_PREFIX
if venv then
return is_windows and venv .. "/Scripts/pythonw.exe" or venv .. "/bin/python"
else
return is_windows and "pythonw.exe" or "python3"
end
Expand All @@ -58,25 +53,18 @@ return function()
console = "integratedTerminal",
program = utils.input_file_path(),
pythonPath = function()
-- Prefer the venv that is defined by the designated environment variable.
local cwd, venv = vim.fn.getcwd(), os.getenv("VIRTUAL_ENV")
if
venv
and (
vim.fn.executable(venv .. "/bin/python") == 1
or vim.fn.executable(venv .. "/Scripts/pythonw.exe") == 1
)
then
return is_windows and venv .. "/Scripts/pythonw.exe" or venv .. "/bin/python"
elseif
(vim.fn.executable(cwd .. "/venv/bin/python") == 1)
or (vim.fn.executable(cwd .. "/venv/Scripts/pythonw.exe") == 1)
then
return is_windows and cwd .. "/venv/Scripts/pythonw.exe" or cwd .. "/venv/bin/python"
elseif
(vim.fn.executable(cwd .. "/.venv/bin/python") == 1)
or (vim.fn.executable(cwd .. "/.venv/Scripts/pythonw.exe") == 1)
then
return is_windows and cwd .. "/.venv/Scripts/pythonw.exe" or cwd .. "/.venv/bin/python"
local python = venv and (is_windows and venv .. "/Scripts/pythonw.exe" or venv .. "/bin/python") or ""
if vim.fn.executable(python) == 1 then
return python
end

-- Otherwise, fall back to check if there are any local venvs available.
venv = vim.fn.isdirectory(cwd .. "/venv") == 1 and cwd .. "/venv" or cwd .. "/.venv"
python = is_windows and venv .. "/Scripts/pythonw.exe" or venv .. "/bin/python"
if vim.fn.executable(python) == 1 then
return python
else
return is_windows and "pythonw.exe" or "python3"
end
Expand Down

0 comments on commit 65d580e

Please sign in to comment.