From 3a76cd484c02e0d9e71d998012db6d04ce7a360e Mon Sep 17 00:00:00 2001 From: dlond Date: Mon, 26 May 2025 00:10:00 +1200 Subject: [PATCH] fix refactor pt 4 --- lua/custom/plugins/debug.lua | 10 ++--- lua/custom/utils.lua | 79 +++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/lua/custom/plugins/debug.lua b/lua/custom/plugins/debug.lua index d680ee8c..67b48a85 100644 --- a/lua/custom/plugins/debug.lua +++ b/lua/custom/plugins/debug.lua @@ -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, diff --git a/lua/custom/utils.lua b/lua/custom/utils.lua index 7d5ab3c8..b94a5772 100644 --- a/lua/custom/utils.lua +++ b/lua/custom/utils.lua @@ -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', '', 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, }