Skip to content

Instantly share code, notes, and snippets.

@derekwyatt
Created August 30, 2011 23:35
Show Gist options
  • Save derekwyatt/1182400 to your computer and use it in GitHub Desktop.
Save derekwyatt/1182400 to your computer and use it in GitHub Desktop.
This is an ENSIME broker to be used for Vim integration to ENSIME... just a prototype at this point
#!/usr/bin/python
import socket
import re
from time import sleep
from os import system
from threading import Thread
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-c", "--command", dest="command", help="The full shell command line to execute")
parser.add_option("-p", "--port", dest="port", help="The port number of the running ENSIME server")
parser.add_option("-f", "--fifo", dest="fifo", help="The FIFO used to communicate with the client script input")
parser.add_option("-r", "--respond", dest="resp", help="The command to run when responses come in")
(options, args) = parser.parse_args()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', int(options.port)))
class ENSIMESender:
def __init__(self, socket):
self.socket = socket
self.sendSequence = 1
def send(self, data):
tosend = "(%s %d)" % (data.rstrip('\n'), self.sendSequence)
tosend = "%06x%s" % (len(tosend), tosend)
self.socket.send(tosend)
self.sendSequence = self.sendSequence + 1
class ENSIMEReceiver(Thread):
def __init__(self, socket, command):
Thread.__init__(self)
self.sock = socket
self.command = command
def run(self):
respnum = 1
while 1:
header = int(self.sock.recv(6), 16)
swank = self.sock.recv(header)
outfilename = "/tmp/ensime.broker." + str(respnum)
outfile = open(outfilename, "w")
outfile.write(swank + "\n")
outfile.close()
system(re.sub(r'%F', outfilename, self.command))
respnum = respnum + 1
class ClientReceiver(Thread):
def __init__(self, fifo, sender):
Thread.__init__(self)
self.fifo = fifo
self.sender = sender
def run(self):
done = 0
while done == 0:
f = open(self.fifo, 'r')
message = f.readline()
f.close
if message.startswith("quit"):
done = 1
else:
sender.send(message)
# Main
ensimeReceiver = ENSIMEReceiver(s, options.resp)
ensimeReceiver.setDaemon(True)
ensimeReceiver.start()
sender = ENSIMESender(s)
clientReceiver = ClientReceiver(options.fifo, sender)
clientReceiver.start()
clientReceiver.join()
@derekwyatt
Copy link
Author

This uses the same idea as what Marc Weber has in (https://github.com/MarcWeber/vim-addon-async) but that implementation was causing me a world of hurt, so it was just quicker to invent a new one.

Eventually this python script will start the ENSIME server as well, but for now it just takes the port of the pre-running server. Once it's up and running, you start this broker like this (assuming the ENSIME server is running on port 6666):

mkfifo /tmp/broker.input
broker.py -f /tmp/broker.input -p 6666 -r "gvim --remote-send \":call ensime#HandleIncomingMessage('context', '%F')<cr>\""

Now you can talk to the ENSIME server by just echoing commands to the fifo:

echo ':swank-rpc (swank:connection-info)' >> /tmp/broker.input

That will send to the ENSIME server and then the broker will send the response(s) back to the running vim session by calling the command passed in to -r.

This is VERY KLUNKY!!! I can't see a better way to go though... I think Marc had the right idea, but the implementation wasn't working for me very well. This is definitely an area where Vim itself could help us out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment