Skip to content

Commit 8707394

Browse files
committed
refactor: consolidate, replacing on_directory() with get_directory()
1 parent f5630ba commit 8707394

File tree

8 files changed

+39
-51
lines changed

8 files changed

+39
-51
lines changed

lua/wincent/commandt/finder.lua

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ local relativize = require('wincent.commandt.private.relativize')
1111
local ui = nil
1212

1313
local function file_finder(directory)
14-
directory = vim.trim(directory)
15-
if directory == '' then
16-
directory = require('wincent.commandt.private.get_directory')()
17-
end
14+
directory = require('wincent.commandt.get_directory')(directory)
1815
pushd(directory)
1916
local options = require('wincent.commandt.private.options'):get()
2017
local finder = require('wincent.commandt.private.finders.file')('.', options)
@@ -33,10 +30,7 @@ local function file_finder(directory)
3330
end
3431

3532
local function watchman_finder(directory)
36-
directory = vim.trim(directory)
37-
if directory == '' then
38-
directory = require('wincent.commandt.private.get_directory')()
39-
end
33+
directory = require('wincent.commandt.get_directory')(directory)
4034
local options = require('wincent.commandt.private.options'):get()
4135
local finder = require('wincent.commandt.private.finders.watchman')(directory, options)
4236

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
2+
-- SPDX-License-Identifier: BSD-2-Clause
3+
4+
--- Infers which directory a scanner should operate in.
5+
---
6+
--- - If passed a non-blank `directory`, returns that.
7+
--- - Otherwise, based on the `'traverse'` setting, either returns the nearest
8+
--- SCM root directory, or the current working directory.
9+
---
10+
--- @param directory string | nil
11+
--- @return string
12+
local function get_directory(directory)
13+
if directory and vim.trim(directory) ~= '' then
14+
return directory
15+
else
16+
local options = require('wincent.commandt.private.options'):get()
17+
local find_root = require('wincent.commandt.private.find_root')
18+
if options.traverse == 'file' then
19+
local file = vim.fn.expand('%:p:h') -- If no current file, returns current dir.
20+
return find_root(file, options.root_markers)
21+
elseif options.traverse == 'pwd' then
22+
return find_root(vim.fn.getcwd(), options.root_markers)
23+
else
24+
return vim.fn.getcwd()
25+
end
26+
end
27+
end
28+
29+
return get_directory

lua/wincent/commandt/on_directory.lua

Lines changed: 0 additions & 18 deletions
This file was deleted.

lua/wincent/commandt/private/finders/fd.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local on_directory = require('wincent.commandt.on_directory')
4+
local get_directory = require('wincent.commandt.get_directory')
55
local on_open = require('wincent.commandt.on_open')
66
local popd = require('wincent.commandt.popd')
77
local pushd = require('wincent.commandt.pushd')
@@ -18,7 +18,7 @@ local fd = {
1818
return options.scanners.fd.max_files
1919
end,
2020
on_close = popd,
21-
on_directory = on_directory,
21+
on_directory = get_directory,
2222
open = on_open,
2323
}
2424

lua/wincent/commandt/private/finders/find.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local on_directory = require('wincent.commandt.on_directory')
4+
local get_directory = require('wincent.commandt.get_directory')
55
local on_open = require('wincent.commandt.on_open')
66
local popd = require('wincent.commandt.popd')
77
local pushd = require('wincent.commandt.pushd')
@@ -18,7 +18,7 @@ local find = {
1818
return options.scanners.find.max_files
1919
end,
2020
on_close = popd,
21-
on_directory = on_directory,
21+
on_directory = get_directory,
2222
open = on_open,
2323
}
2424

lua/wincent/commandt/private/finders/git.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local on_directory = require('wincent.commandt.on_directory')
4+
local get_directory = require('wincent.commandt.get_directory')
55
local on_open = require('wincent.commandt.on_open')
66

77
local git = {
@@ -26,7 +26,7 @@ local git = {
2626
max_files = function(options)
2727
return options.scanners.git.max_files
2828
end,
29-
on_directory = on_directory,
29+
on_directory = get_directory,
3030
open = on_open,
3131
}
3232

lua/wincent/commandt/private/finders/rg.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
22
-- SPDX-License-Identifier: BSD-2-Clause
33

4-
local on_directory = require('wincent.commandt.on_directory')
4+
local get_directory = require('wincent.commandt.get_directory')
55
local on_open = require('wincent.commandt.on_open')
66
local popd = require('wincent.commandt.popd')
77
local pushd = require('wincent.commandt.pushd')
@@ -18,7 +18,7 @@ local rg = {
1818
return options.scanners.rg.max_files
1919
end,
2020
on_close = popd,
21-
on_directory = on_directory,
21+
on_directory = get_directory,
2222
open = on_open,
2323
}
2424

lua/wincent/commandt/private/get_directory.lua

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)