diff --git a/lua/custom/plugins/debug.lua b/lua/custom/plugins/debug.lua index e2fdbc0d..4fc1ac66 100644 --- a/lua/custom/plugins/debug.lua +++ b/lua/custom/plugins/debug.lua @@ -68,7 +68,12 @@ return { name = 'Launch C/C++ (lldb-dap)', type = 'lldb', request = 'launch', - program = require('custom.utils').pick_executable, + program = function() + local utils = require 'custom.utils' + local co = utils.pick_executable '${workspaceFolder}/build' + local ok, result = coroutine.resume(co) + return ok and result or nil + end, cwd = '${workspaceFolder}', stopOnEntry = false, args = {}, diff --git a/lua/custom/utils.lua b/lua/custom/utils.lua index 1f206215..7d5ab3c8 100644 --- a/lua/custom/utils.lua +++ b/lua/custom/utils.lua @@ -1,46 +1,46 @@ return { - pick_executable = function() + pick_executable = function(start_dir) + local scan = require 'plenary.scandir' + local Path = require 'plenary.path' + 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, + }) + 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 entry_maker = function(entry) - return { - value = entry, - display = entry, - ordinal = entry, - } - end - - local co = coroutine.running() - pickers - .new({}, { - prompt_title = 'Select binary', - finder = finders.new_oneshot_job { - 'fd', - '--type', - 'f', - '--exec-batch', - 'test', - '-x', - '{}', - ';', - 'echo', - '{}', - '', - 'build/', - }, - sorter = conf.generic_sorter {}, - attach_mappings = function(_, map) - map('i', '', function(bufnr) - local entry = require('telescope.actions.state').get_selected_entry() - require('telescope.actions').close(bufnr) - coroutine.resume(co, entry.value) - end) - return true - end, - }) - :find() - return coroutine.yield() + 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) end, }