Skip to content

Commit

Permalink
refactor: DRY up fallback finders
Browse files Browse the repository at this point in the history
Because we might be wanting to change the behavior soon, it would be
nice to be able to change in in _one_ place instead of three.

Specifically, as per:

- wincent/command-t#393 (comment)

we might be putting something in the prompt window title to indicate
what happened.
  • Loading branch information
Padmamanickam authored and wincent committed Sep 5, 2022
1 parent be40c89 commit a4360e1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
11 changes: 11 additions & 0 deletions lua/wincent/commandt/private/finders/fallback.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- SPDX-FileCopyrightText: Copyright 2022-present Greg Hurrell and contributors.
-- SPDX-License-Identifier: BSD-2-Clause

-- A lazy fallback wrapper (to avoid the cost of scanning until actually needed)
-- around the built-in file finder.
return function(finder, directory, options)
return function()
finder.fallback = require('wincent.commandt.private.finders.file')(directory ~= '' and directory or '.', options)
return finder.fallback
end
end
10 changes: 1 addition & 9 deletions lua/wincent/commandt/private/finders/find.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ local ffi = require('ffi')
-- TODO: remember cached directories
return function(directory, options)
local finder = {}
-- Use a thunk to avoid cost of fallback scanning until actually needed.
finder.fallback = (
(function(d, o)
return function()
finder.fallback = require('wincent.commandt.private.finders.file')(d ~= '' and d or '.', o)
return finder.fallback
end
end)(directory, options)
)
finder.fallback = require('wincent.commandt.private.finders.fallback')(finder, directory, options)
if vim.startswith(directory, './') then
directory = directory:sub(3, -1)
end
Expand Down
10 changes: 1 addition & 9 deletions lua/wincent/commandt/private/finders/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ local ffi = require('ffi')
-- TODO: remember cached directories
return function(directory, options)
local finder = {}
-- Use a thunk to avoid cost of fallback scanning until actually needed.
finder.fallback = (
(function(d, o)
return function()
finder.fallback = require('wincent.commandt.private.finders.file')(d ~= '' and d or '.', o)
return finder.fallback
end
end)(directory, options)
)
finder.fallback = require('wincent.commandt.private.finders.fallback')(finder, directory, options)
if directory ~= '' then
directory = vim.fn.shellescape(directory)
end
Expand Down
10 changes: 1 addition & 9 deletions lua/wincent/commandt/private/finders/rg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ local ffi = require('ffi')
-- TODO: remember cached directories
return function(directory, options)
local finder = {}
-- Use a thunk to avoid cost of fallback scanning until actually needed.
finder.fallback = (
(function(d, o)
return function()
finder.fallback = require('wincent.commandt.private.finders.file')(d ~= '' and d or '.', o)
return finder.fallback
end
end)(directory, options)
)
finder.fallback = require('wincent.commandt.private.finders.fallback')(finder, directory, options)
if vim.startswith(directory, './') then
directory = directory:sub(3, -1)
end
Expand Down

0 comments on commit a4360e1

Please sign in to comment.