Skip to content

Commit

Permalink
set process-global CWD on DirChanged #296
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandros authored and justinmk committed Apr 12, 2018
1 parent 70dfc84 commit 1ab98e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
6 changes: 3 additions & 3 deletions docs/installation.rst
Expand Up @@ -8,15 +8,15 @@ Using pip

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

pip2 install neovim
pip3 install neovim
pip2 install --user neovim
pip3 install --user neovim

.. note::

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

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

pip2 install --upgrade neovim
Expand Down
13 changes: 13 additions & 0 deletions neovim/plugin/script_host.py
Expand Up @@ -45,6 +45,14 @@ def __init__(self, nvim):
self.legacy_vim = LegacyVim.from_nvim(nvim)
sys.modules['vim'] = self.legacy_vim

# Handle DirChanged. #296
nvim.command(
'autocmd DirChanged * call rpcrequest({}, "python_chdir", v:event)'
.format(nvim.channel_id), async=True)
# XXX: Avoid race condition.
# https://github.com/neovim/python-client/pull/296#issuecomment-358970531
os.chdir(nvim.eval('getcwd()', async=False))

def setup(self, nvim):
"""Setup import hooks and global streams.
Expand Down Expand Up @@ -153,6 +161,11 @@ def python_eval(self, expr):
"""Handle the `pyeval` vim function."""
return eval(expr, self.module.__dict__)

@rpc_export('python_chdir', sync=True)
def python_chdir(self, args):
"""Handle working directory changes."""
os.chdir(args['cwd'])

def _set_current_range(self, start, stop):
current = self.legacy_vim.current
current.range = current.buffer.range(start, stop)
Expand Down
21 changes: 19 additions & 2 deletions test/test_vim.py
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import os, tempfile
import os, sys, tempfile
from nose.tools import with_setup, eq_ as eq, ok_ as ok
from test_common import vim, cleanup

Expand All @@ -15,7 +15,7 @@ def source(code):
def test_command():
fname = tempfile.mkstemp()[1]
vim.command('new')
vim.command('edit %s' % fname)
vim.command('edit {}'.format(fname))
# skip the "press return" state, which does not handle deferred calls
vim.input('\r')
vim.command('normal itesting\npython\napi')
Expand Down Expand Up @@ -162,3 +162,20 @@ def test_hash():
eq(d[vim.current.buffer], "alpha")
vim.command('winc w')
eq(d[vim.current.buffer], "beta")


@with_setup(setup=cleanup)
def test_cwd():
pycmd = 'python'
if sys.version_info >= (3, 0):
pycmd = 'python3'

vim.command('{} import os'.format(pycmd))
cwd_before = vim.command_output('{} print(os.getcwd())'.format(pycmd))

vim.command('cd test')
cwd_vim = vim.command_output('pwd')
cwd_python = vim.command_output('{} print(os.getcwd())'.format(pycmd))
eq(cwd_vim, cwd_python)
ok(cwd_python != cwd_before)

0 comments on commit 1ab98e8

Please sign in to comment.