Skip to content

Commit

Permalink
Merge pull request #92 from hjdivad/hjdivad/dap
Browse files Browse the repository at this point in the history
Neovim Debugger (DAP)
  • Loading branch information
hjdivad committed May 14, 2024
2 parents 737d1bb + 0f131fb commit 0a67697
Show file tree
Hide file tree
Showing 20 changed files with 733 additions and 100 deletions.
2 changes: 2 additions & 0 deletions packages/bash/bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ function __src_rename {

source $BASHRC_DIR/shell.bash

# export OATMEAL_OPENAI_TOKEN=""


# vim:ft=sh:
1 change: 1 addition & 0 deletions packages/kitty/config/kitty.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ map cmd+9 send_text all \x1b[20~
map cmd+0 send_text all \x1b[21~
# <F11> which will be mapped to :tabnew
map cmd+t send_text all \x1b[23~
map cmd+shift+semicolon send_text all \x1b[24~

#: You can also create shortcuts to go to specific tabs, with 1 being
#: the first tab, 2 the second tab and -1 being the previously active
Expand Down
2 changes: 2 additions & 0 deletions packages/nvim/config/lazy-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"headlines.nvim": { "branch": "master", "commit": "618ef1b2502c565c82254ef7d5b04402194d9ce3" },
"indent-blankline.nvim": { "branch": "master", "commit": "3d08501caef2329aba5121b753e903904088f7e6" },
"lazy.nvim": { "branch": "main", "commit": "bef521ac89c8d423f9d092e37b58e8af0c099309" },
"lua-toml": { "branch": "master", "commit": "13731a5dd48c8c314d2451760604810bd6221085" },
"lualine.nvim": { "branch": "master", "commit": "0a5a66803c7407767b799067986b4dc3036e1983" },
"markdown-preview.nvim": { "branch": "master", "commit": "a923f5fc5ba36a3b17e289dc35dc17f66d0548ee" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "05744f0f1967b5757bd05c08df4271ab8ec990aa" },
Expand Down Expand Up @@ -63,6 +64,7 @@
"rustaceanvim": { "branch": "master", "commit": "2eb8776df1aab03f514b38ddc39af57efbd8970b" },
"tailwindcss-colorizer-cmp.nvim": { "branch": "main", "commit": "3d3cd95e4a4135c250faf83dd5ed61b8e5502b86" },
"telescope-fzf-native.nvim": { "branch": "main", "commit": "9ef21b2e6bb6ebeaf349a0781745549bbb870d27" },
"telescope-tmux.nvim": { "branch": "master", "commit": "cf857c1d28f6a5b0fd78ecb9d7c03fe95aa8eb3e" },
"telescope.nvim": { "branch": "master", "commit": "fac83a556e7b710dc31433dec727361ca062dbe9" },
"todo-comments.nvim": { "branch": "main", "commit": "a7e39ae9e74f2c8c6dc4eea6d40c3971ae84752d" },
"tokyonight.nvim": { "branch": "main", "commit": "67afeaf7fd6ebba000633e89f63c31694057edde" },
Expand Down
44 changes: 43 additions & 1 deletion packages/nvim/config/lua/hjdivad_util/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local map_stack = require('hjdivad_util/map_stack')
local map_stack = require("hjdivad_util/map_stack")

local M = {}

Expand All @@ -24,4 +24,46 @@ function M.concat(...)
return result
end

function M.args_list(str)
local result = {}
local word = ""
local quote = nil

for i = 1, #str do
local char = str:sub(i, i)

if quote == nil then
if char == " " or char == "\t" then
if #word > 0 then
table.insert(result, word)
word = ""
end
elseif char == "'" then
quote = "'"
elseif char == '"' then
quote = '"'
else
word = word .. char
end
else
if char == quote then
quote = nil
if #word > 0 then
table.insert(result, word)
word = ""
end
else
word = word .. char
end
end
end

if #word > 0 then
table.insert(result, word)
word = ""
end

return result
end

return M
8 changes: 8 additions & 0 deletions packages/nvim/config/lua/hjdivad_util/sys.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local M = {}

---@param name string
function M.cache_path(name)
return vim.fn.stdpath('cache') .. '/' .. 'hjdivad/' .. name
end

return M
184 changes: 184 additions & 0 deletions packages/nvim/config/lua/hjdivad_util/venv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
---@class PythonEntryPoint
---@field name string
---@field pkg string
---@field fn string

-- see :he dap-configuration
-- see :dap-python.DebugpyLaunchConfig
---@class DapConfiguration
---@field type string python
---@field request string launch | attach
---@field name string user-friendly name of the configuration
---@field module? string
---@field program? string
---@field code? string code to execute
---@field args? string[] | nil command-line args for the program
---@field python? string[] path to python and interpreter arguments
---@field cwd? string
---@field env? table
---@field stopOnEntry? boolean

local TOML = require("toml")
local VenvSelector = require("venv-selector")

local Venv = {}
Venv.__index = Venv

function Venv:new()
local instance = {}
setmetatable(instance, Venv)
return instance
end
--
---@param search_start_dir? string
---@return string | nil
function Venv.find_pyproject_toml(search_start_dir)
-- Iterate upwards from search_start_dir
while search_start_dir ~= "" do
-- Check if pyproject.toml exists in the current directory
local pyproject_toml_path = search_start_dir .. "/pyproject.toml"
if vim.fn.filereadable(pyproject_toml_path) == 1 then
return pyproject_toml_path
end

-- Move up one directory
search_start_dir = vim.fn.fnamemodify(search_start_dir, ":h")
end

-- If no pyproject.toml is found, return nil
return nil
end

---@param search_start_dir string
function Venv.load_pyproject(search_start_dir)
local pyproject_path = Venv.find_pyproject_toml(search_start_dir)

if pyproject_path == nil then
return nil
end

local pyproject_config = Venv.parse_pyproject(pyproject_path)

return pyproject_config
end

---@param path string
function Venv.parse_pyproject(path)
local file_contents = io.open(path, "r"):read("*a")
local pyproject = TOML.parse(file_contents)
return pyproject
end

function Venv.extract_entrypoints(pyproject)
local result = {}

local p = pyproject
if p.project and p.project.scripts then
result = vim.tbl_extend("keep", result, p.project.scripts)
end

if p.tool then
if p.tool.poetry and p.tool.poetry.scripts then
result = vim.tbl_extend("keep", result, p.tool.poetry.scripts)
end

if p.tool.setuptools and p.tool.setuptools.entry_points then
result = vim.tbl_extend("keep", result, p.tool.setuptools.entry_points)
end

if p.tool.flit then
if p.tool.flit.scripts then
result = vim.tbl_extend("keep", result, p.tool.flipt.scripts)
end

if p.tool.flit.entrypoints then
result = vim.tbl_extend("keep", result, p.tool.flipt.entrypoints)
end
end
end

return result
end

function Venv.get_venv()
local venv_path = VenvSelector.get_active_venv()
if venv_path == nil then
-- try to load last venv
VenvSelector.retrieve_from_cache()
venv_path = VenvSelector.get_active_venv()
end

local python_path = VenvSelector.get_active_path()

return python_path, venv_path
end

function Venv.get_and_check_venv()
local python_path, venv_path = Venv.get_venv()

if venv_path == nil then
require("noice").redirect(function()
print("No python venv detected. Run :VenvSelect.")
end)
return
end

return python_path, venv_path
end

---@param name string
---@param args string[] | nil
function Venv.dap_configuration(name, cmd, args)
local python_path, venv_path = Venv.get_and_check_venv()

if venv_path == nil then
return {}
end

cmd = cmd or name

local program

if cmd:find('/') then
program = cmd
else
program = venv_path .. "/bin/" .. cmd
end


---@type DapConfiguration
local result = {
type = "python",
request = "launch",
name = name,
python = { python_path },
program = program,
}

if args ~= nil then
result.args = args
end

return result
end

---@return DapConfiguration[]
function Venv:dap_configurations()
local pyproject = Venv.load_pyproject(vim.fn.getcwd())
if pyproject == nil then
return {}
end

local python_path = Venv.get_and_check_venv()

if python_path == nil then
return {}
end

local entrypoints = Venv.extract_entrypoints(pyproject)
local entrypooint_runnables = vim.tbl_keys(entrypoints)
local configs = vim.tbl_map(Venv.dap_configuration, entrypooint_runnables)
return configs
end

return Venv:new()

0 comments on commit 0a67697

Please sign in to comment.