Created
April 19, 2017 17:32
-
-
Save sloat/6ef3a0cc582dc00208e8dde6cfc1d785 to your computer and use it in GitHub Desktop.
Preliminary work for typescript support in completor.vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
try: | |
from pipes import quote | |
except ImportError: | |
from shlex import quote | |
import json | |
import os | |
import vim | |
from completor import Completor, get_encoding | |
from completor.compat import to_bytes | |
class TypeScript(Completor): | |
filetype = 'typescript' | |
daemon = True | |
trigger = r'(?:\w{2,}\w*|\.\w*)$' | |
seq = 0 | |
files_opened = [] | |
def format_cmd(self): | |
binary = self.get_option('tsserver_binary') or 'tsserver' | |
return [binary] | |
def request(self): | |
self.seq += 1 | |
fname = quote(self.filename) | |
if fname not in self.files_opened: | |
self.files_opened.append(fname) | |
return json.dumps({ | |
"seq": self.seq, | |
"type": "request", | |
"command": "open", | |
"arguments": { | |
"file": fname | |
} | |
}) | |
line, col = self.cursor | |
m = self.trigger.search(self.input_data) | |
if not m: | |
return None | |
matchtext = m[0] | |
req = { | |
"seq": self.seq, | |
"type": "request", | |
"command": "completions", | |
"arguments": { | |
"file": fname, | |
"line": line, | |
"offset": col, | |
"prefix": matchtext, | |
# optional: prefix, projectFileName | |
}, | |
} | |
return json.dumps(req) | |
def is_message_end(self, msg): | |
if msg.startswith(b'Content-Length') or len(msg) == 0: | |
return False | |
return True | |
# items: list of bytes | |
def parse(self, items): | |
#input_data = to_bytes(self.input_data, get_encoding()) | |
completions = [] | |
for item in items: | |
try: | |
res = json.loads(item.decode('utf-8')) | |
except json.JSONDecodeError: | |
continue # ??? | |
if not res.get('success', False): | |
continue | |
for cpl in res.get('body', []): | |
name = cpl.get('name') | |
spec = cpl.get('kind') | |
if name and spec: | |
completions.append({ | |
'word': name, | |
'menu': spec, | |
}) | |
return completions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment