broke the tele picker, fix pt 1

This commit is contained in:
dlond 2025-05-26 01:13:05 +12:00
parent d2da11491d
commit 8a51315db4
2 changed files with 47 additions and 44 deletions

View File

@ -55,6 +55,7 @@ return {
}, },
config = function() config = function()
local dap = require 'dap' local dap = require 'dap'
local utils = require 'custom.utils'
-- 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)
@ -63,19 +64,15 @@ return {
command = 'lldb-dap', command = 'lldb-dap',
name = 'lldb-dap (Nix)', name = 'lldb-dap (Nix)',
} }
dap.configurations.cpp = { dap.configurations.cpp = {
{ {
name = 'Launch C/C++ (lldb-dap)', name = 'Launch C/C++ (lldb-dap)',
type = 'lldb', type = 'lldb',
request = 'launch', request = 'launch',
program = coroutine.wrap(function() program = function()
local exe = require('custom.utils').pick_executable(vim.fn.getcwd() .. '/build') return utils.pick_executable_for_dap(vim.fn.getcwd() .. '/build')
if not exe then end,
vim.notify('Debug session cancelled: executable not selected', vim.log.levels.INFO)
return nil
end
return exe
end),
cwd = '${workspaceFolder}', cwd = '${workspaceFolder}',
stopOnEntry = false, stopOnEntry = false,
args = {}, args = {},

View File

@ -1,64 +1,70 @@
local function is_executable(path) local async = require 'plenary.async.util'
-- Use `file` command to identify Mach-O or ELF binaries local pickers = require 'telescope.pickers'
local output = vim.fn.system { 'file', '-b', path } local finders = require 'telescope.finders'
return output:match 'Mach%-O' or output:match 'ELF' local sorters = require('telescope.config').values
end local actions = require 'telescope.actions'
local action_state = require 'telescope.actions.state'
local Path = require 'plenary.path'
local function collect_executables(dir) local function collect_executables(start_dir)
local files = vim.fn.globpath(dir, '**', true, true) local results = {}
local binaries = {} local fd = vim.fn.systemlist { 'fd', '--type', 'x', '--exec', 'file', '{}', start_dir }
for _, path in ipairs(files) do for _, line in ipairs(fd) do
if vim.fn.filereadable(path) == 1 and is_executable(path) and not path:match 'CMakeFiles' then local path, typeinfo = line:match '^(.-):%s*(.+)$'
table.insert(binaries, path) if path and not path:match 'CMakeFiles' and typeinfo and typeinfo:match 'Mach%-O' or typeinfo:match 'ELF' then
table.insert(results, path)
end end
end end
return binaries return results
end end
local function pick_executable(start_dir) local function pick_executable(start_dir, on_choice)
local co = coroutine.running()
if not co then
error 'pick_executable must be called from a coroutine'
end
local executables = collect_executables(start_dir) local executables = collect_executables(start_dir)
if #executables == 0 then if #executables == 0 then
vim.notify('No executables found in ' .. start_dir, vim.log.levels.WARN) vim.notify('No executables found in ' .. start_dir, vim.log.levels.WARN)
on_choice(nil)
return return
end end
local actions = require 'telescope.actions' pickers
local action_state = require 'telescope.actions.state'
require('telescope.pickers')
.new({}, { .new({}, {
prompt_title = 'Select Executable', prompt_title = 'Select Executable',
finder = require('telescope.finders').new_table { finder = finders.new_table { results = executables },
results = executables, sorter = sorters.generic_sorter {},
},
sorter = require('telescope.config').values.generic_sorter {},
attach_mappings = function(_, map) attach_mappings = function(_, map)
map('i', '<CR>', function(prompt_bufnr) actions.select_default:replace(function(prompt_bufnr)
local entry = action_state.get_selected_entry() local entry = action_state.get_selected_entry()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
coroutine.resume(co, entry.value) on_result(entry.value)
end) end)
map('i', '<Esc>', function(prompt_bufnr) map('i', '<Esc>', function(buf)
actions.close(prompt_bufnr) actions.close(buf)
coroutine.resume(co, nil) on_result(nil)
end) end)
map('n', 'q', function(prompt_bufnr) map('n', 'q', function(buf)
actions.close(prompt_bufnr) actions.close(buf)
coroutine.resume(co, nil) on_result(nil)
end) end)
return true return true
end, end,
}) })
:find() :find()
end
return coroutine.yield() local function pick_executable_for_dap(start_dir)
return async.void(function()
local co = coroutine.running()
pick_executable(start_dir, function(choice)
coroutine.resume(co, choice)
end)
local result = coroutine.yield()
if not result then
vim.notify('Debug launch cancelled', vim.log.levels.INFO)
end
return result
end)()
end end
return { return {
pick_executable = pick_executable, pick_executable_for_dap = pick_executable_for_dap,
} }