1
1
from vroom .vim import CONFIGFILE
2
2
from vroom .vim import Communicator as VimCommunicator
3
3
import subprocess
4
+ import tempfile
4
5
import time
5
6
import neovim
6
7
import os
7
8
8
9
class Communicator (VimCommunicator ):
9
10
"""Object to communicate with a Neovim server."""
10
11
12
+ _listen_addr = None
13
+
11
14
def __init__ (self , args , env , writer ):
12
15
self .writer = writer .commands
13
16
self .args = args
@@ -16,7 +19,6 @@ def __init__(self, args, env, writer):
16
19
'-u' , args .vimrc ,
17
20
'-c' , 'set shell=' + args .shell ,
18
21
'-c' , 'source %s' % CONFIGFILE ]
19
- env ['NVIM_LISTEN_ADDRESS' ] = args .servername
20
22
self .env = env
21
23
self ._cache = {}
22
24
@@ -29,13 +31,21 @@ def Quit(self):
29
31
30
32
def Start (self ):
31
33
"""Starts Neovim"""
34
+ if self ._listen_addr is not None :
35
+ raise InvocationError ('Called Start on already-running neovim instance' )
36
+ tmpdir = tempfile .mkdtemp ()
37
+ self ._listen_addr = os .path .join (tmpdir , 'nvim.pipe' )
38
+ # Legacy env var, used by nvim <0.8
39
+ self .env ['NVIM_LISTEN_ADDRESS' ] = self ._listen_addr
40
+ self .start_command += ['--listen' , self ._listen_addr ]
41
+
32
42
self .process = subprocess .Popen (self .start_command , env = self .env )
33
43
start_time = time .time ()
34
44
# Wait at most 5s for the Neovim socket
35
- while not os .path .exists (self .args . servername ) \
45
+ while not os .path .exists (self ._listen_addr ) \
36
46
and time .time () - start_time < 5 :
37
47
time .sleep (0.01 )
38
- self .nvim = neovim .attach ('socket' , path = self .args . servername )
48
+ self .nvim = neovim .attach ('socket' , path = self ._listen_addr )
39
49
40
50
def Communicate (self , command , extra_delay = 0 ):
41
51
"""Sends a command to Neovim.
@@ -101,5 +111,12 @@ def Kill(self):
101
111
"""Kills the Neovim process and removes the socket"""
102
112
VimCommunicator .Kill (self )
103
113
104
- if os .path .exists (self .args .servername ):
105
- os .remove (self .args .servername )
114
+ if os .path .exists (self ._listen_addr ):
115
+ os .remove (self ._listen_addr )
116
+
117
+
118
+ class InvocationError (Exception ):
119
+ """Raised when there's a problem starting or interacting with neovim instance.
120
+ """
121
+ is_fatal = True
122
+
0 commit comments