fix refactor pt 4

This commit is contained in:
dlond 2025-05-26 00:10:00 +12:00
parent a7e384d20c
commit 3a76cd484c
2 changed files with 46 additions and 43 deletions

View File

@ -55,6 +55,7 @@ return {
}, },
config = function() config = function()
local dap = require 'dap' local dap = require 'dap'
local pick_executable = require('custom.utils').pick_executable
-- Configure the LLDB DAP adapter for C/C++ -- Configure the LLDB DAP adapter for C/C++
-- Assumes 'lldb-dap' executable is in PATH (from pkgs.llvmPackages_XX.lldb) -- Assumes 'lldb-dap' executable is in PATH (from pkgs.llvmPackages_XX.lldb)
@ -69,11 +70,10 @@ return {
type = 'lldb', type = 'lldb',
request = 'launch', request = 'launch',
program = function() program = function()
local utils = require 'custom.utils' return coroutine.create(function()
local cwd = vim.fn.getcwd() local executable = pick_executable(ivim.fn.getcwd() .. '/build')
local co = utils.pick_executable 'build' coroutine.yield(executable)
local ok, result = coroutine.resume(co) end)
return ok and result or nil
end, end,
cwd = '${workspaceFolder}', cwd = '${workspaceFolder}',
stopOnEntry = false, stopOnEntry = false,

View File

@ -1,46 +1,49 @@
return { return {
pick_executable = function(start_dir) pick_executable = function(start_dir)
local scan = require 'plenary.scandir' local function is_executable(path)
local Path = require 'plenary.path' return vim.fn.filereadable(path) == 1 and vim.fn.executable(path) == 1
local results = {}
-- Recursively scan for files
scan.scan_dir(start_dir, {
hidden = true,
depth = 3,
add_dirs = false,
on_insert = function(file)
local file_type = vim.fn.system { 'file', '-b', file }
if file_type:match 'Mach%-O' or file_type:match 'ELF' then
table.insert(results, file)
end end
end,
})
local pickers = require 'telescope.pickers' local function collect_executables(dir)
local finders = require 'telescope.finders' local files = vim.fn.globpath(dir, '**', true, true)
local conf = require('telescope.config').values local binaries = {}
local actions = require 'telescope.actions' for _, path in ipairs(files) do
local action_state = require 'telescope.actions.state' if is_executable(path) then
table.insert(binaries, path)
end
end
return binaries
end
return coroutine.create(function(coro) local co = coroutine.running()
pickers if not co then
error 'pick_executable must be called from a coroutine'
end
local executables = collect_executables(start_dir)
if #executables == 0 then
vim.notify('No executables found in ' .. start_dir, vim.log.levels.WARN)
return
end
require('telescope.pickers')
.new({}, { .new({}, {
prompt_title = 'Select Executable', prompt_title = 'Select Executable',
finder = finders.new_table { finder = require('telescope.finders').new_table {
results = results, results = executables,
}, },
sorter = conf.generic_sorter {}, sorter = require('telescope.config').values.generic_sorter {},
attach_mappings = function(_, map) attach_mappings = function(_, map)
actions.select_default:replace(function(prompt_bufnr) map('i', '<CR>', function(prompt_bufnr)
local selection = action_state.get_selected_entry() local entry = require('telescope.actions.state').get_selected_entry()
actions.close(prompt_bufnr) require('telescope.actions').close(prompt_bufnr)
coroutine.resume(coro, selection[1]) coroutine.resume(co, entry.value)
end) end)
return true return true
end, end,
}) })
:find() :find()
end)
return coroutine.yield()
end, end,
} }