Skip to content

Commit 01ec74b

Browse files
Leandrosjustinmk
authored andcommitted
set process-global CWD on DirChanged neovim#296
1 parent 70dfc84 commit 01ec74b

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ 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+
os.chdir(nvim.eval('getcwd()', async=False))
53+
4854
def setup(self, nvim):
4955
"""Setup import hooks and global streams.
5056
@@ -153,6 +159,11 @@ def python_eval(self, expr):
153159
"""Handle the `pyeval` vim function."""
154160
return eval(expr, self.module.__dict__)
155161

162+
@rpc_export('python_chdir', sync=True)
163+
def python_chdir(self, args):
164+
"""Handle working directory changes."""
165+
os.chdir(args['cwd'])
166+
156167
def _set_current_range(self, start, stop):
157168
current = self.legacy_vim.current
158169
current.range = current.buffer.range(start, stop)

test/test_vim.py

Lines changed: 22 additions & 1 deletion
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

@@ -162,3 +162,24 @@ 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+
# Detect whether we're running under python2 or python3.
170+
IS_PYTHON3 = sys.version_info >= (3, 0)
171+
if IS_PYTHON3:
172+
pycmd = 'python3'
173+
else:
174+
pycmd = 'python'
175+
176+
vim.command('%s import os' % pycmd)
177+
vim.command('cd test')
178+
cwd_vim = vim.command_output('pwd')
179+
if IS_PYTHON3:
180+
cwd_python = vim.command_output('%s print(os.getcwd())' % pycmd)
181+
else:
182+
cwd_python = vim.command_output('%s print os.getcwd()' % pycmd)
183+
184+
eq(cwd_vim, cwd_python)
185+

0 commit comments

Comments
 (0)