Skip to content

Commit 1ab98e8

Browse files
Leandrosjustinmk
authored andcommitted
set process-global CWD on DirChanged #296
close #295 close neovim/neovim#3636
1 parent 70dfc84 commit 1ab98e8

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

docs/installation.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Using pip
88

99
You can install the package without being root by adding the ``--user`` flag::
1010

11-
pip2 install neovim
12-
pip3 install neovim
11+
pip2 install --user neovim
12+
pip3 install --user neovim
1313

1414
.. note::
1515

1616
If you only use one of python2 or python3,
1717
it is enough to install that version.
1818

19-
If you follow Neovim master,
19+
If you follow Neovim HEAD,
2020
make sure to upgrade the ``python-client`` when you upgrade Neovim::
2121

2222
pip2 install --upgrade neovim

neovim/plugin/script_host.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def __init__(self, nvim):
4545
self.legacy_vim = LegacyVim.from_nvim(nvim)
4646
sys.modules['vim'] = self.legacy_vim
4747

48+
# Handle DirChanged. #296
49+
nvim.command(
50+
'autocmd DirChanged * call rpcrequest({}, "python_chdir", v:event)'
51+
.format(nvim.channel_id), async=True)
52+
# XXX: Avoid race condition.
53+
# https://github.com/neovim/python-client/pull/296#issuecomment-358970531
54+
os.chdir(nvim.eval('getcwd()', async=False))
55+
4856
def setup(self, nvim):
4957
"""Setup import hooks and global streams.
5058
@@ -153,6 +161,11 @@ def python_eval(self, expr):
153161
"""Handle the `pyeval` vim function."""
154162
return eval(expr, self.module.__dict__)
155163

164+
@rpc_export('python_chdir', sync=True)
165+
def python_chdir(self, args):
166+
"""Handle working directory changes."""
167+
os.chdir(args['cwd'])
168+
156169
def _set_current_range(self, start, stop):
157170
current = self.legacy_vim.current
158171
current.range = current.buffer.range(start, stop)

test/test_vim.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
import os, tempfile
2+
import os, sys, tempfile
33
from nose.tools import with_setup, eq_ as eq, ok_ as ok
44
from test_common import vim, cleanup
55

@@ -15,7 +15,7 @@ def source(code):
1515
def test_command():
1616
fname = tempfile.mkstemp()[1]
1717
vim.command('new')
18-
vim.command('edit %s' % fname)
18+
vim.command('edit {}'.format(fname))
1919
# skip the "press return" state, which does not handle deferred calls
2020
vim.input('\r')
2121
vim.command('normal itesting\npython\napi')
@@ -162,3 +162,20 @@ def test_hash():
162162
eq(d[vim.current.buffer], "alpha")
163163
vim.command('winc w')
164164
eq(d[vim.current.buffer], "beta")
165+
166+
167+
@with_setup(setup=cleanup)
168+
def test_cwd():
169+
pycmd = 'python'
170+
if sys.version_info >= (3, 0):
171+
pycmd = 'python3'
172+
173+
vim.command('{} import os'.format(pycmd))
174+
cwd_before = vim.command_output('{} print(os.getcwd())'.format(pycmd))
175+
176+
vim.command('cd test')
177+
cwd_vim = vim.command_output('pwd')
178+
cwd_python = vim.command_output('{} print(os.getcwd())'.format(pycmd))
179+
eq(cwd_vim, cwd_python)
180+
ok(cwd_python != cwd_before)
181+

0 commit comments

Comments
 (0)