Skip to content

Commit 78fc932

Browse files
committed
feat: implement traverse support for :CommandT
Rather hacky, so want to test this a bit to make sure it works, probably refactor it to make it less hacky, and then roll out coverage to the other file-oriented finder types. In the end, treating explicit path arguments as overriding the inferred root directory; ie. `:CommandT foo` or `:CommandT ..` won't do traversal, but `:CommandT` will. In all cases, this behaves as if you `cd`'d into the root (either explicitly specified or found via traversal) ran the command, and the `cd`'d back. The match listing shows candidates relative to that root.
1 parent def57ea commit 78fc932

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lua/wincent/commandt/init.lua

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,13 +615,34 @@ end
615615

616616
commandt.file_finder = function(directory)
617617
directory = vim.trim(directory)
618+
local previous_cwd = vim.uv.cwd()
619+
local next_cwd = previous_cwd
618620
if directory == '' then
619-
directory = '.'
621+
next_cwd = get_directory()
620622
end
621623
local ui = require('wincent.commandt.private.ui')
622624
local options = commandt.options()
623-
local finder = require('wincent.commandt.private.finders.file')(directory, options)
624-
ui.show(finder, merge(options, { name = 'file' }))
625+
if previous_cwd ~= next_cwd then
626+
vim.fn.chdir(get_directory())
627+
end
628+
629+
local finder = require('wincent.commandt.private.finders.file')('.', options)
630+
ui.show(finder, merge(options, {
631+
name = 'file',
632+
on_open = function(result)
633+
if previous_cwd ~= next_cwd then
634+
result = vim.fs.normalize(
635+
vim.fs.joinpath(next_cwd, result)
636+
)
637+
end
638+
return result
639+
end,
640+
on_close = function()
641+
if previous_cwd ~= next_cwd then
642+
vim.fn.chdir(previous_cwd)
643+
end
644+
end,
645+
}))
625646
end
626647

627648
local report_errors = function(errors, heading)

lua/wincent/commandt/private/ui.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ local cmdline_enter_autocmd = nil
1212
local current_finder = nil -- Reference to avoid premature garbage collection.
1313
local current_window = nil
1414
local match_listing = nil
15+
local on_close = nil
16+
local on_open = nil
1517
local prompt = nil
1618
local results = nil
1719
local selected = nil
@@ -61,17 +63,26 @@ local close = function()
6163
current_window = nil
6264
vim.api.nvim_set_current_win(win)
6365
end
66+
if on_close then
67+
on_close()
68+
on_close = nil
69+
end
6470
end
6571

6672
ui.open = function(kind)
6773
close()
6874
if results and #results > 0 then
69-
-- Defer, to give autocommands a chance to run.
7075
local result = results[selected]
76+
if on_open then
77+
result = on_open(result)
78+
end
79+
80+
-- Defer, to give autocommands a chance to run.
7181
vim.defer_fn(function()
7282
current_finder.open(result, kind)
7383
end, 0)
7484
end
85+
on_open = nil
7586
end
7687

7788
ui.show = function(finder, options)
@@ -80,6 +91,9 @@ ui.show = function(finder, options)
8091

8192
current_window = vim.api.nvim_get_current_win()
8293

94+
on_close = options.on_close
95+
on_open = options.on_open
96+
8397
-- Temporarily override global settings.
8498
-- For now just 'hlsearch', but may add more later (see
8599
-- ruby/command-t/lib/command-t/match_window.rb)

0 commit comments

Comments
 (0)