Skip to content

Commit

Permalink
fix(nvim): replaces split_args with dap.utils equivalent
Browse files Browse the repository at this point in the history
  • Loading branch information
Diaoul committed Oct 10, 2024
1 parent edb7785 commit 9c9c136
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .config/nvim/lua/diaoul/plugins/coding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ return {

-- configuration
return {
auto_brackets = {}, -- configure any filetype to auto add brackets
auto_brackets = { "python" }, -- configure any filetype to auto add brackets
completion = {
completeopt = "menu,menuone,noinsert" .. (auto_select and "" or ",noselect"),
},
Expand Down
55 changes: 6 additions & 49 deletions .config/nvim/lua/diaoul/plugins/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ return {
},
opts = {},
config = function(_, opts)
local dap = require("dap")
local daputils = require("dap.utils")
local dapui = require("dapui")

-- setup dap config by VSCode launch.json file
require("dap.ext.vscode").load_launchjs()

-- setup dapui
local dapui = require("dapui")
dapui.setup(opts)

-- open and close windows automatically
local dap = require("dap")
dap.listeners.after.event_initialized["dapui_config"] = function()
dapui.open()
end
Expand All @@ -30,60 +32,14 @@ return {
dapui.close()
end

--- @param args string
--- @return table
local function split_args(args)
local lpeg = vim.lpeg

local P, S, C, Cc, Ct = lpeg.P, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Ct

--- @param id string
--- @param patt vim.lpeg.Capture
--- @return vim.lpeg.Pattern
local function token(id, patt)
return Ct(Cc(id) * C(patt))
end

local single_quoted = P("'") * ((1 - S("'\r\n\f\\")) + (P("\\") * 1)) ^ 0 * "'"
local double_quoted = P('"') * ((1 - S('"\r\n\f\\')) + (P("\\") * 1)) ^ 0 * '"'

local whitespace = token("whitespace", S("\r\n\f\t ") ^ 1)
local word = token("word", (1 - S("' \r\n\f\t\"")) ^ 1)
local string = token("string", single_quoted + double_quoted)

local pattern = Ct((string + whitespace + word) ^ 0)

local t = {}
local tokens = lpeg.match(pattern, args)

-- somehow, this did not work out
if tokens == nil or type(tokens) == "integer" then
return t
end

for _, tok in ipairs(tokens) do
if tok[1] ~= "whitespace" then
if tok[1] == "string" then
-- cut off quotes and replace escaped quotes
local v, _ = tok[2]:sub(2, -2):gsub("\\(['\"])", "%1")
table.insert(t, v)
else
table.insert(t, tok[2])
end
end
end

return t
end

-- add debugpy capability to specify args as string
dap.listeners.on_config["debugpy"] = function(config)
-- copy the config and split the args if it is a string
local c = {}

for k, v in pairs(vim.deepcopy(config)) do
if k == "args" and type(v) == "string" then
c[k] = split_args(v)
c[k] = daputils.splitstr(v)
else
c[k] = v
end
Expand Down Expand Up @@ -112,6 +68,7 @@ return {
automatic_installation = true,
},
},

-- python
{
"mfussenegger/nvim-dap-python",
Expand Down

0 comments on commit 9c9c136

Please sign in to comment.