-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: grepping or replacing in cwd instead of the directory of the file (
#293) Fixes a bug where grepping or replacing with `<c-s>` or `<c-g>` would pick Neovim's current working directory instead of the directory of the file. This needs another look after sxyazi/yazi#1314 is resolved. Maybe it can be improved.
- Loading branch information
1 parent
7062de6
commit aee19fb
Showing
5 changed files
with
264 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
local assert = require('luassert') | ||
local utils = require('yazi.utils') | ||
|
||
describe('dir_of helper function', function() | ||
describe( | ||
"when files and directories don't exist on disk (only in Neovim)", | ||
function() | ||
it('can detect the dir of a file', function() | ||
local d = utils.dir_of('/my-tmp/file1') | ||
assert.is_equal('/my-tmp', d.filename) | ||
end) | ||
|
||
it('can detect the dir of a directory', function() | ||
-- I think it just thinks directories are files because it cannot know | ||
-- better. But this is still a good default. | ||
local d = utils.dir_of('/my-tmp/dir1') | ||
assert.is_equal('/my-tmp', d.filename) | ||
end) | ||
end | ||
) | ||
|
||
describe('when files and directories exist on disk', function() | ||
local base_dir = os.tmpname() -- create a temporary file with a unique name | ||
|
||
before_each(function() | ||
assert( | ||
base_dir:match('/tmp/'), | ||
"base_dir is not under `/tmp/`, it's too dangerous to continue" | ||
) | ||
os.remove(base_dir) | ||
vim.fn.mkdir(base_dir, 'p') | ||
end) | ||
|
||
after_each(function() | ||
vim.fn.delete(base_dir, 'rf') | ||
end) | ||
|
||
it('can get the directory of a file', function() | ||
local file = vim.fs.joinpath(base_dir, 'abc.txt') | ||
local d = utils.dir_of(file) | ||
assert.is_equal(base_dir, d.filename) | ||
end) | ||
|
||
it('can get the directory of a directory', function() | ||
local dir = vim.fs.joinpath(base_dir, 'dir1') | ||
vim.fn.mkdir(dir) | ||
local d = utils.dir_of(dir) | ||
|
||
assert.is_equal(dir, d.filename) | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
local assert = require('luassert') | ||
local config_module = require('yazi.config') | ||
local keybinding_helpers = require('yazi.keybinding_helpers') | ||
local match = require('luassert.match') | ||
local stub = require('luassert.stub') | ||
|
||
describe('keybinding_helpers', function() | ||
describe('grep_in_directory', function() | ||
it('should grep in the parent directory for a file', function() | ||
local config = config_module.default() | ||
local s = stub(config.integrations, 'grep_in_directory') | ||
|
||
keybinding_helpers.grep_in_directory(config, '/tmp/file') | ||
|
||
assert.stub(s).was_called_with('/tmp') | ||
end) | ||
|
||
it('should grep in the directory when a directory is passed', function() | ||
local config = config_module.default() | ||
local s = stub(config.integrations, 'grep_in_directory') | ||
|
||
keybinding_helpers.grep_in_directory(config, '/tmp') | ||
|
||
assert.stub(s).was_called_with('/tmp') | ||
end) | ||
|
||
it('should not crash if the integration is disabled', function() | ||
local config = config_module.default() | ||
config.integrations.grep_in_directory = nil | ||
|
||
keybinding_helpers.grep_in_directory(config, '/tmp/file') | ||
end) | ||
end) | ||
|
||
describe('grep_in_selected_files', function() | ||
it('should call `integrations.grep_in_selected_files`', function() | ||
local config = config_module.default() | ||
|
||
local results = {} | ||
config.integrations.grep_in_selected_files = function(paths) | ||
results = paths | ||
end | ||
|
||
keybinding_helpers.grep_in_selected_files( | ||
config, | ||
{ '/tmp/file1', '/tmp/file2' } | ||
) | ||
|
||
assert.equals(2, #results) | ||
assert.are.same( | ||
{ '/tmp/file1', '/tmp/file2' }, | ||
vim | ||
.iter(results) | ||
:map(function(a) | ||
return a.filename | ||
end) | ||
:totable() | ||
) | ||
end) | ||
end) | ||
|
||
describe('replace_in_directory', function() | ||
it( | ||
"when a file is passed, should replace in the file's directory", | ||
function() | ||
local config = config_module.default() | ||
|
||
local stub_replace = stub(config.integrations, 'replace_in_directory') | ||
|
||
keybinding_helpers.replace_in_directory(config, '/tmp/file') | ||
|
||
assert.stub(stub_replace).was_called_with(match.is_truthy()) | ||
assert.equals('/tmp', stub_replace.calls[1].vals[1].filename) | ||
end | ||
) | ||
|
||
it('when a directory is passed, should replace in the directory', function() | ||
local config = config_module.default() | ||
|
||
local stub_replace = stub(config.integrations, 'replace_in_directory') | ||
|
||
keybinding_helpers.replace_in_directory(config, '/tmp') | ||
|
||
assert.stub(stub_replace).was_called_with(match.is_truthy()) | ||
assert.equals('/tmp', stub_replace.calls[1].vals[1].filename) | ||
end) | ||
end) | ||
|
||
describe('replace_in_selected_files', function() | ||
it('should call `integrations.replace_in_selected_files`', function() | ||
local config = config_module.default() | ||
|
||
local results = {} | ||
config.integrations.replace_in_selected_files = function(paths) | ||
results = paths | ||
end | ||
|
||
keybinding_helpers.replace_in_selected_files( | ||
config, | ||
{ '/tmp/file1', '/tmp/file2' } | ||
) | ||
|
||
assert.equals(2, #results) | ||
assert.are.same( | ||
{ '/tmp/file1', '/tmp/file2' }, | ||
vim | ||
.iter(results) | ||
:map(function(a) | ||
return a.filename | ||
end) | ||
:totable() | ||
) | ||
end) | ||
end) | ||
end) |