mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
DirChanged: avoid redundant events on 'autochdir'
This commit is contained in:
parent
d9fcbc2cfb
commit
c5e61b41a5
@ -6960,8 +6960,8 @@ void post_chdir(CdScope scope)
|
||||
}
|
||||
}
|
||||
|
||||
char curdir[MAXPATHL];
|
||||
if (os_dirname((char_u *)curdir, MAXPATHL) != OK) {
|
||||
char cwd[MAXPATHL];
|
||||
if (os_dirname((char_u *)cwd, MAXPATHL) != OK) {
|
||||
return;
|
||||
}
|
||||
switch (scope) {
|
||||
@ -6971,17 +6971,17 @@ void post_chdir(CdScope scope)
|
||||
globaldir = NULL;
|
||||
break;
|
||||
case kCdScopeTab:
|
||||
curtab->tp_localdir = (char_u *)xstrdup(curdir);
|
||||
curtab->tp_localdir = (char_u *)xstrdup(cwd);
|
||||
break;
|
||||
case kCdScopeWindow:
|
||||
curwin->w_localdir = (char_u *)xstrdup(curdir);
|
||||
curwin->w_localdir = (char_u *)xstrdup(cwd);
|
||||
break;
|
||||
case kCdScopeInvalid:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
shorten_fnames(true);
|
||||
do_autocmd_dirchanged(curdir, scope);
|
||||
do_autocmd_dirchanged(cwd, scope);
|
||||
}
|
||||
|
||||
/// `:cd`, `:tcd`, `:lcd`, `:chdir`, `:tchdir` and `:lchdir`.
|
||||
|
@ -1566,14 +1566,25 @@ void do_autocmd_dirchanged(char *new_dir, CdScope scope)
|
||||
/// @return OK or FAIL
|
||||
int vim_chdirfile(char_u *fname)
|
||||
{
|
||||
char_u dir[MAXPATHL];
|
||||
char dir[MAXPATHL];
|
||||
|
||||
STRLCPY(dir, fname, MAXPATHL);
|
||||
*path_tail_with_sep(dir) = NUL;
|
||||
if (os_chdir((char *)dir) != 0) {
|
||||
*path_tail_with_sep((char_u *)dir) = NUL;
|
||||
|
||||
if (os_dirname(NameBuff, sizeof(NameBuff)) != OK) {
|
||||
NameBuff[0] = NUL;
|
||||
}
|
||||
|
||||
if (os_chdir(dir) != 0) {
|
||||
return FAIL;
|
||||
}
|
||||
do_autocmd_dirchanged((char *)dir, kCdScopeWindow);
|
||||
|
||||
#ifdef BACKSLASH_IN_FILENAME
|
||||
slash_adjust(dir);
|
||||
#endif
|
||||
if (!strequal(dir, (char *)NameBuff)) {
|
||||
do_autocmd_dirchanged(dir, kCdScopeWindow);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
@ -20,34 +20,36 @@ describe('autocmd DirChanged', function()
|
||||
|
||||
before_each(function()
|
||||
clear()
|
||||
command('autocmd DirChanged * let [g:getcwd_rv, g:event, g:amatch, g:cdcount] '
|
||||
..' = [getcwd(), copy(v:event), expand("<amatch>"), 1 + get(g:, "cdcount", 0)]'
|
||||
..[[ | let g:event['cwd'] = substitute(g:event['cwd'], '\\', '/', 'g') ]])
|
||||
command('autocmd DirChanged * let [g:getcwd, g:ev, g:amatch, g:cdcount] '
|
||||
..' = [getcwd(), copy(v:event), expand("<amatch>"), 1 + get(g:, "cdcount", 0)]')
|
||||
-- Normalize path separators.
|
||||
command([[autocmd DirChanged * let g:ev['cwd'] = substitute(g:ev['cwd'], '\\', '/', 'g')]])
|
||||
command([[autocmd DirChanged * let g:getcwd = substitute(g:getcwd, '\\', '/', 'g')]])
|
||||
end)
|
||||
|
||||
it('sets v:event', function()
|
||||
command('lcd '..dirs[1])
|
||||
eq({cwd=dirs[1], scope='window'}, eval('g:event'))
|
||||
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
|
||||
eq(1, eval('g:cdcount'))
|
||||
|
||||
command('tcd '..dirs[2])
|
||||
eq({cwd=dirs[2], scope='tab'}, eval('g:event'))
|
||||
eq({cwd=dirs[2], scope='tab'}, eval('g:ev'))
|
||||
eq(2, eval('g:cdcount'))
|
||||
|
||||
command('cd '..dirs[3])
|
||||
eq({cwd=dirs[3], scope='global'}, eval('g:event'))
|
||||
eq({cwd=dirs[3], scope='global'}, eval('g:ev'))
|
||||
eq(3, eval('g:cdcount'))
|
||||
end)
|
||||
|
||||
it('sets getcwd() during event #6260', function()
|
||||
command('lcd '..dirs[1])
|
||||
eq(dirs[1], eval('g:getcwd_rv'))
|
||||
eq(dirs[1], eval('g:getcwd'))
|
||||
|
||||
command('tcd '..dirs[2])
|
||||
eq(dirs[2], eval('g:getcwd_rv'))
|
||||
eq(dirs[2], eval('g:getcwd'))
|
||||
|
||||
command('cd '..dirs[3])
|
||||
eq(dirs[3], eval('g:getcwd_rv'))
|
||||
eq(dirs[3], eval('g:getcwd'))
|
||||
end)
|
||||
|
||||
it('disallows recursion', function()
|
||||
@ -55,7 +57,7 @@ describe('autocmd DirChanged', function()
|
||||
-- Set up a _nested_ handler.
|
||||
command('autocmd DirChanged * nested lcd '..dirs[3])
|
||||
command('lcd '..dirs[1])
|
||||
eq({cwd=dirs[1], scope='window'}, eval('g:event'))
|
||||
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
|
||||
eq(1, eval('g:cdcount'))
|
||||
-- autocmd changed to dirs[3], but did NOT trigger another DirChanged.
|
||||
eq(dirs[3], eval('getcwd()'))
|
||||
@ -73,22 +75,22 @@ describe('autocmd DirChanged', function()
|
||||
end)
|
||||
|
||||
it('does not trigger if :cd fails', function()
|
||||
command('let g:event = {}')
|
||||
command('let g:ev = {}')
|
||||
|
||||
local status1, err1 = pcall(function()
|
||||
command('lcd '..dirs[1] .. '/doesnotexist')
|
||||
end)
|
||||
eq({}, eval('g:event'))
|
||||
eq({}, eval('g:ev'))
|
||||
|
||||
local status2, err2 = pcall(function()
|
||||
command('lcd '..dirs[2] .. '/doesnotexist')
|
||||
end)
|
||||
eq({}, eval('g:event'))
|
||||
eq({}, eval('g:ev'))
|
||||
|
||||
local status3, err3 = pcall(function()
|
||||
command('lcd '..dirs[3] .. '/doesnotexist')
|
||||
end)
|
||||
eq({}, eval('g:event'))
|
||||
eq({}, eval('g:ev'))
|
||||
|
||||
eq(false, status1)
|
||||
eq(false, status2)
|
||||
@ -103,10 +105,12 @@ describe('autocmd DirChanged', function()
|
||||
command('set autochdir')
|
||||
|
||||
command('split '..dirs[1]..'/foo')
|
||||
eq({cwd=dirs[1], scope='window'}, eval('g:event'))
|
||||
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
|
||||
|
||||
command('split '..dirs[2]..'/bar')
|
||||
eq({cwd=dirs[2], scope='window'}, eval('g:event'))
|
||||
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
|
||||
|
||||
eq(2, eval('g:cdcount'))
|
||||
end)
|
||||
|
||||
it("is triggered by switching to win/tab with different CWD #6054", function()
|
||||
@ -117,16 +121,16 @@ describe('autocmd DirChanged', function()
|
||||
command('lcd '..dirs[1])
|
||||
|
||||
command('2wincmd w') -- window 2
|
||||
eq({cwd=dirs[2], scope='window'}, eval('g:event'))
|
||||
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
|
||||
|
||||
eq(4, eval('g:cdcount'))
|
||||
command('tabnew') -- tab 2 (tab-local CWD)
|
||||
eq(4, eval('g:cdcount')) -- same CWD, no DirChanged event
|
||||
command('tcd '..dirs[3])
|
||||
command('tabnext') -- tab 1 (no tab-local CWD)
|
||||
eq({cwd=dirs[2], scope='window'}, eval('g:event'))
|
||||
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
|
||||
command('tabnext') -- tab 2
|
||||
eq({cwd=dirs[3], scope='tab'}, eval('g:event'))
|
||||
eq({cwd=dirs[3], scope='tab'}, eval('g:ev'))
|
||||
eq(7, eval('g:cdcount'))
|
||||
|
||||
command('tabnext') -- tab 1
|
||||
@ -134,21 +138,20 @@ describe('autocmd DirChanged', function()
|
||||
eq(9, eval('g:cdcount'))
|
||||
command('tabnext') -- tab 2 (has the *same* CWD)
|
||||
eq(9, eval('g:cdcount')) -- same CWD, no DirChanged event
|
||||
|
||||
end)
|
||||
|
||||
it('is triggered by nvim_set_current_dir()', function()
|
||||
request('nvim_set_current_dir', dirs[1])
|
||||
eq({cwd=dirs[1], scope='global'}, eval('g:event'))
|
||||
eq({cwd=dirs[1], scope='global'}, eval('g:ev'))
|
||||
|
||||
request('nvim_set_current_dir', dirs[2])
|
||||
eq({cwd=dirs[2], scope='global'}, eval('g:event'))
|
||||
eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
|
||||
|
||||
local status, err = pcall(function()
|
||||
request('nvim_set_current_dir', '/doesnotexist')
|
||||
end)
|
||||
eq(false, status)
|
||||
eq('Failed to change directory', string.match(err, ': (.*)'))
|
||||
eq({cwd=dirs[2], scope='global'}, eval('g:event'))
|
||||
eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
|
||||
end)
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user