aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2021-10-17 22:04:53 +0800
committerzeertzjq <zeertzjq@outlook.com>2021-10-17 22:04:53 +0800
commite91dee5c21ffca22ac821336ad23bce6339b5f7c (patch)
tree255c0c0e08203f8af3badb29d858460a0bcee6ee
parenteed89d5e0c3bd6870e6e9959ccfdbf99549dce6b (diff)
downloadrneovim-e91dee5c21ffca22ac821336ad23bce6339b5f7c.tar.gz
rneovim-e91dee5c21ffca22ac821336ad23bce6339b5f7c.tar.bz2
rneovim-e91dee5c21ffca22ac821336ad23bce6339b5f7c.zip
vim-patch:8.1.0602: DirChanged is also triggered when directory didn't change
Problem: DirChanged is also triggered when the directory didn't change. (Daniel Hahler) Solution: Compare the current with the new directory. (closes vim/vim#3697) https://github.com/vim/vim/commit/2caad3fbbdbf1486a176c9f6bfbc3d9be90e09f7
-rw-r--r--src/nvim/ex_docmd.c5
-rw-r--r--src/nvim/testdir/test_autochdir.vim8
-rw-r--r--src/nvim/testdir/test_autocmd.vim33
-rw-r--r--test/functional/autocmd/dirchanged_spec.lua36
4 files changed, 67 insertions, 15 deletions
diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c
index 5d83e2218b..b81aab9ffe 100644
--- a/src/nvim/ex_docmd.c
+++ b/src/nvim/ex_docmd.c
@@ -7811,10 +7811,11 @@ void ex_cd(exarg_T *eap)
break;
}
- if (vim_chdir(new_dir)) {
+ bool dir_differs = prev_dir == NULL || STRCMP(prev_dir, new_dir) != 0;
+ if (dir_differs && vim_chdir(new_dir)) {
EMSG(_(e_failed));
} else {
- post_chdir(scope, true);
+ post_chdir(scope, dir_differs);
// Echo the new current directory if the command was typed.
if (KeyTyped || p_verbose >= 5) {
ex_pwd(eap);
diff --git a/src/nvim/testdir/test_autochdir.vim b/src/nvim/testdir/test_autochdir.vim
index d071f4b325..3d1ba084c6 100644
--- a/src/nvim/testdir/test_autochdir.vim
+++ b/src/nvim/testdir/test_autochdir.vim
@@ -8,11 +8,19 @@ func Test_set_filename()
let cwd = getcwd()
call test_autochdir()
set acd
+
+ let s:li = []
+ autocmd DirChanged auto call add(s:li, "autocd")
+ autocmd DirChanged auto call add(s:li, expand("<afile>"))
+
new
w samples/Xtest
call assert_equal("Xtest", expand('%'))
call assert_equal("samples", substitute(getcwd(), '.*/\(\k*\)', '\1', ''))
+ call assert_equal(["autocd", getcwd()], s:li)
+
bwipe!
+ au! DirChanged
set noacd
exe 'cd ' . cwd
call delete('samples/Xtest')
diff --git a/src/nvim/testdir/test_autocmd.vim b/src/nvim/testdir/test_autocmd.vim
index a2ed3ae677..93083f55c7 100644
--- a/src/nvim/testdir/test_autocmd.vim
+++ b/src/nvim/testdir/test_autocmd.vim
@@ -1334,13 +1334,16 @@ function s:Before_test_dirchanged()
augroup END
let s:li = []
let s:dir_this = getcwd()
- let s:dir_other = s:dir_this . '/foo'
- call mkdir(s:dir_other)
+ let s:dir_foo = s:dir_this . '/foo'
+ call mkdir(s:dir_foo)
+ let s:dir_bar = s:dir_this . '/bar'
+ call mkdir(s:dir_bar)
endfunc
function s:After_test_dirchanged()
exe 'cd' s:dir_this
- call delete(s:dir_other, 'd')
+ call delete(s:dir_foo, 'd')
+ call delete(s:dir_bar, 'd')
augroup test_dirchanged
autocmd!
augroup END
@@ -1350,10 +1353,12 @@ function Test_dirchanged_global()
call s:Before_test_dirchanged()
autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
- exe 'cd' s:dir_other
- call assert_equal(["cd:", s:dir_other], s:li)
- exe 'lcd' s:dir_other
- call assert_equal(["cd:", s:dir_other], s:li)
+ exe 'cd' s:dir_foo
+ call assert_equal(["cd:", s:dir_foo], s:li)
+ exe 'cd' s:dir_foo
+ call assert_equal(["cd:", s:dir_foo], s:li)
+ exe 'lcd' s:dir_bar
+ call assert_equal(["cd:", s:dir_foo], s:li)
call s:After_test_dirchanged()
endfunc
@@ -1361,10 +1366,12 @@ function Test_dirchanged_local()
call s:Before_test_dirchanged()
autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
- exe 'cd' s:dir_other
+ exe 'cd' s:dir_foo
call assert_equal([], s:li)
- exe 'lcd' s:dir_other
- call assert_equal(["lcd:", s:dir_other], s:li)
+ exe 'lcd' s:dir_bar
+ call assert_equal(["lcd:", s:dir_bar], s:li)
+ exe 'lcd' s:dir_bar
+ call assert_equal(["lcd:", s:dir_bar], s:li)
call s:After_test_dirchanged()
endfunc
@@ -1379,9 +1386,9 @@ function Test_dirchanged_auto()
set acd
exe 'cd ..'
call assert_equal([], s:li)
- exe 'edit ' . s:dir_other . '/Xfile'
- call assert_equal(s:dir_other, getcwd())
- call assert_equal(["auto:", s:dir_other], s:li)
+ exe 'edit ' . s:dir_foo . '/Xfile'
+ call assert_equal(s:dir_foo, getcwd())
+ call assert_equal(["auto:", s:dir_foo], s:li)
set noacd
bwipe!
call s:After_test_dirchanged()
diff --git a/test/functional/autocmd/dirchanged_spec.lua b/test/functional/autocmd/dirchanged_spec.lua
index 6f2da24cf2..2e1aff3914 100644
--- a/test/functional/autocmd/dirchanged_spec.lua
+++ b/test/functional/autocmd/dirchanged_spec.lua
@@ -113,6 +113,42 @@ describe('autocmd DirChanged', function()
eq(2, eval('g:cdcount'))
end)
+ it('does not trigger if diretory has not changed', function()
+ command('lcd '..dirs[1])
+ eq({cwd=dirs[1], scope='window', changed_window=false}, eval('g:ev'))
+ eq(1, eval('g:cdcount'))
+ command('let g:ev = {}')
+ command('lcd '..dirs[1])
+ eq({}, eval('g:ev'))
+ eq(1, eval('g:cdcount'))
+
+ command('tcd '..dirs[2])
+ eq({cwd=dirs[2], scope='tab', changed_window=false}, eval('g:ev'))
+ eq(2, eval('g:cdcount'))
+ command('let g:ev = {}')
+ command('tcd '..dirs[2])
+ eq({}, eval('g:ev'))
+ eq(2, eval('g:cdcount'))
+
+ command('cd '..dirs[3])
+ eq({cwd=dirs[3], scope='global', changed_window=false}, eval('g:ev'))
+ eq(3, eval('g:cdcount'))
+ command('let g:ev = {}')
+ command('cd '..dirs[3])
+ eq({}, eval('g:ev'))
+ eq(3, eval('g:cdcount'))
+
+ command('set autochdir')
+
+ command('split '..dirs[1]..'/foo')
+ eq({cwd=dirs[1], scope='window', changed_window=false}, eval('g:ev'))
+ eq(4, eval('g:cdcount'))
+ command('let g:ev = {}')
+ command('split '..dirs[1]..'/bar')
+ eq({}, eval('g:ev'))
+ eq(4, eval('g:cdcount'))
+ end)
+
it("is triggered by switching to win/tab with different CWD #6054", function()
command('lcd '..dirs[3]) -- window 3
command('split '..dirs[2]..'/foo') -- window 2