Skip to content

Commit

Permalink
set process-global CWD on DirChanged neovim#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 01ec74b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
6 changes: 3 additions & 3 deletions docs/installation.rst
Original file line number Diff line number Diff line change
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
11 changes: 11 additions & 0 deletions neovim/plugin/script_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ 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)
os.chdir(nvim.eval('getcwd()', async=False))

def setup(self, nvim):
"""Setup import hooks and global streams.
Expand Down Expand Up @@ -153,6 +159,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
23 changes: 22 additions & 1 deletion test/test_vim.py
Original file line number Diff line number Diff line change
@@ -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 Down Expand Up @@ -162,3 +162,24 @@ 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():
# Detect whether we're running under python2 or python3.
IS_PYTHON3 = sys.version_info >= (3, 0)
if IS_PYTHON3:
pycmd = 'python3'
else:
pycmd = 'python'

vim.command('%s import os' % pycmd)
vim.command('cd test')
cwd_vim = vim.command_output('pwd')
if IS_PYTHON3:
cwd_python = vim.command_output('%s print(os.getcwd())' % pycmd)
else:
cwd_python = vim.command_output('%s print os.getcwd()' % pycmd)

eq(cwd_vim, cwd_python)

0 comments on commit 01ec74b

Please sign in to comment.