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

View File

@ -1,46 +1,49 @@
return {
pick_executable = function(start_dir)
local scan = require 'plenary.scandir'
local Path = require 'plenary.path'
local results = {}
local function is_executable(path)
return vim.fn.filereadable(path) == 1 and vim.fn.executable(path) == 1
end
-- 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)
local function collect_executables(dir)
local files = vim.fn.globpath(dir, '**', true, true)
local binaries = {}
for _, path in ipairs(files) do
if is_executable(path) then
table.insert(binaries, path)
end
end,
})
end
return binaries
end
local pickers = require 'telescope.pickers'
local finders = require 'telescope.finders'
local conf = require('telescope.config').values
local actions = require 'telescope.actions'
local action_state = require 'telescope.actions.state'
local co = coroutine.running()
if not co then
error 'pick_executable must be called from a coroutine'
end
return coroutine.create(function(coro)
pickers
.new({}, {
prompt_title = 'Select Executable',
finder = finders.new_table {
results = results,
},
sorter = conf.generic_sorter {},
attach_mappings = function(_, map)
actions.select_default:replace(function(prompt_bufnr)
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
coroutine.resume(coro, selection[1])
end)
return true
end,
})
:find()
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({}, {
prompt_title = 'Select Executable',
finder = require('telescope.finders').new_table {
results = executables,
},
sorter = require('telescope.config').values.generic_sorter {},
attach_mappings = function(_, map)
map('i', '<CR>', function(prompt_bufnr)
local entry = require('telescope.actions.state').get_selected_entry()
require('telescope.actions').close(prompt_bufnr)
coroutine.resume(co, entry.value)
end)
return true
end,
})
:find()
return coroutine.yield()
end,
}