Skip to content

Commit

Permalink
fixup! fixup! fixup! feat(utils): Implement pick_files()
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed May 23, 2024
1 parent e4287d3 commit cd1456c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doc/dap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ pick_process({opts}) *dap.utils.pick_process*
pick_file({opts}) *dap.utils.pick_file*
Show a prompt to select a file.
Returns the path to the selected file.
Requires nvim 0.10+
Requires nvim 0.10+ or a `find` executable

Parameters:
{opts} optional table with:
Expand Down
34 changes: 22 additions & 12 deletions lua/dap/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ end
---@param opts {filter?: string|(fun(name: string):boolean), executables?: boolean}
---@return string[]
local function get_files(path, opts)
if not vim.fs.dir then
error("pick_file requires nvim-0.10 with vim.fs.dir support")
end
local filter = function(_) return true end
if opts.filter then
if type(opts.filter) == "string" then
Expand All @@ -207,7 +204,7 @@ local function get_files(path, opts)
error('opts.filter must be a string or a function')
end
end
if opts.executables then
if opts.executables and vim.fs.dir then
local f = filter
local uv = vim.uv or vim.loop
local user_execute = tonumber("00100", 8)
Expand All @@ -220,19 +217,32 @@ local function get_files(path, opts)
end
end

local files = {}
for name, type in vim.fs.dir(path, { depth = 50 }) do
if type == "file" and filter(name) then
table.insert(files, path .. "/" .. name)
if vim.fs.dir then
local files = {}
for name, type in vim.fs.dir(path, { depth = 50 }) do
if type == "file" and filter(name) then
table.insert(files, path .. "/" .. name)
end
end
return files
end


local cmd = {"find", path, "-type", "f"}
if opts.executables then
-- The order of options matters!
table.insert(cmd, "-executable")
end
return files
table.insert(cmd, "-follow")

local output = vim.fn.system(cmd)
return vim.tbl_filter(filter, vim.split(output, '\n'))
end


--- Show a prompt to select a file.
--- Returns the path to the selected file.
--- Requires nvim 0.10+
--- Requires nvim 0.10+ or a `find` executable
---
--- Takes an optional `opts` table with following options:
---
Expand All @@ -247,7 +257,7 @@ end
--- </pre>
---@param opts? {filter?: string|(fun(name: string): boolean), executables?: boolean}
---
---@return thread|string|nil
---@return thread|string|nil|table
function M.pick_file(opts)
opts = opts or {}
opts.executables = opts.executables == nil and true or opts.executables
Expand All @@ -260,7 +270,7 @@ function M.pick_file(opts)
local label_fn = function(filepath)
return vim.fn.fnamemodify(filepath, ":.")
end
return pick(files, prompt, label_fn)
return pick(files, prompt, label_fn) or require("dap").ABORT
end


Expand Down

0 comments on commit cd1456c

Please sign in to comment.