completor.vim icon indicating copy to clipboard operation
completor.vim copied to clipboard

Add support for Swift via SourceKittenDaemon

Open drewdeponte opened this issue 9 years ago • 1 comments

SourceKittenDaemon works great on MacOS to provide what I believe is the necessary information to implement completion. It is done through a RESTish api. Details on the super simple protocol are as follows.

https://github.com/terhechte/SourceKittenDaemon/blob/master/Protocol.org

I am just not clear exactly on what the intended API is that you are providing to add support for other languages.

I am also not sure what the details of the sync and daemon options on the completor are really for.

I tried to look and see how to implement support for this. But, it wasn't super clear.

I know from just looking at the code I need to provide something like the following. However, the following causes vim to hang indefinitely when it triggers a completion. I can see the completion request show up in the SourceKittenDaemon logs. So, the request seems to be happening.

Anyways, any help you could provide would be great!!

import vim

from completor import Completor

class Swift(Completor):
    filetype = 'swift'
    trigger = r'(?:\w{2,}\w*|\.\w*)$'

    def offset(self):
        line, col = vim.current.window.cursor
        line2byte = vim.Function('line2byte')
        return line2byte(line) + col - 1

    def format_cmd(self):
        binary = self.get_option('completor_swiftsourcekittenclient_binary') or 'sourcekittenclient'
        return [binary, '{}'.format(self.tempname), self.offset()]

    def parse(self, items):
        print("DREW")
        print(items)
        print("DREW END")
        res = []
        # for item in items:
        #     parts = item.split(b',,')
        #     res.append({
        #         'word': parts[1],
        #         'menu': parts[2]
        #     })
        return res

sourcekittenclient

#!/usr/bin/env python

import sys
import urllib2

class SourceKittenDaemon(object):
    def __init__(self, port):
        self.__port = port

    def complete(self, path, offset):
        request = urllib2.Request("http://localhost:%d/complete" % self.__port)
        request.add_header("X-Path", path)
        request.add_header("X-Offset", offset)
        response = urllib2.urlopen(request).read()
        return response

s = SourceKittenDaemon(8081)
sys.stdout.write(s.complete(sys.argv[1], sys.argv[2]))

I guess it would also be helpful to know that I am running SourceKittenDaemon independently because I just want to see the thing work before I invest time getting the plugin to manage the life cycle of the SourceKittenDaemon and its configuration.

drewdeponte avatar Dec 15 '16 09:12 drewdeponte

The Swift implementation is located at https://github.com/maralla/completor-swift.

maralla avatar Nov 29 '17 12:11 maralla