forked from FourierTransformer/LuaComplete-Sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaComplete.py
192 lines (150 loc) · 5.98 KB
/
LuaComplete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import sublime, sublime_plugin
from subprocess import Popen, PIPE, call, STDOUT
import sys, time
state = {}
def is_server_running(quick=False):
# this is probably enough to see if everything is running
if quick:
if "server" in state:
return True
else:
return False
if "server" in state:
if state["server"].poll() == None:
return True
else:
return False
else:
return False
def start_server():
global state
# print("starting server")
if not is_server_running():
# clean up any old instances that may be running...
stop_server()
state["server"] = Popen(state["server_command"], shell=True)
# else:
# print("LuaComplete: server already running")
def stop_server():
# try to cleanly bring it down.
shutdown = Popen(state["client_command"] + " -x", shell=True)
shutdown.wait(.5)
# if the command fails, and it's still running. terminate it.
if shutdown.returncode != 0:
if is_server_running():
state["server"].terminate()
def create_completion(completion):
(name, completion_type) = completion.split(":")
completion_type = completion_type.strip()
completion = name
# add the '(' for functions!
if completion_type.startswith("function"):
completion = name + "("
# it's a Lua func and params have been found
# if "|" in completion_type
# doing this for the speed!
if len(completion_type) >= 10:
# split out the function params
params = completion_type[11:].split()
# set the completion type to just the start
completion_type = completion_type[0:9] + "()"
# figure this thing out
completion = completion + ", ".join([ "${{{num}:{name}}}".format(num=num+1, name=val) for (num, val) in enumerate(params)])
completion = completion + ")"
# for c funcs, we can't do completion
else:
completion = completion + "$1)"
return "{0}\t{1}".format(name, completion_type), completion
class LuaComplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
position = locations[0]
scopes = view.scope_name(position).split()
if ('source.lua' not in scopes or state["enabled"] == False):
return None
# load the server if it's not running.
if not is_server_running(quick=True):
start_server()
# we can only autocomplete certain things
current_char = view.substr(position-1)
if current_char not in [":", ".", "[", "("]:
return None
# build the main command
command = "{client} -i -c {pos}".format(client=state["client_command"], pos=str(position))
# append the filename if it exists
file_name = view.file_name()
if file_name is not None:
command = command + " -f '{0}'".format(file_name)
# get all the window vars
package_folders = []
window_vars = view.window().extract_variables()
if "folder" in window_vars:
package_folders.append(window_vars["folder"])
if state["additional_includes"]:
package_folders.append(state["additional_includes"])
# did we find a folder to add?
if package_folders:
command = command + " -r '{0}'".format(';'.join(package_folders))
# get the file contents
file_contents = view.substr(sublime.Region(0, view.size())).encode('utf8')
# send it to the client
# print(command)
client = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
# print(file_contents)
# print(position)
# send communicate on stdin to the client
output = client.communicate(file_contents)[0].decode('utf-8')
# print("returncode", client.returncode)
if client.returncode == 0:
view.set_status("a", "")
output = output.splitlines()
output_type = output[0]
# print(output_type)
# main output is on lines 1 and below
output = output[1:]
# print(output)
if output_type == "table":
return [ create_completion(x) for x in output ]
else:
view.set_status("a", "The lua-complete client failed to return")
# potentially retry the command or restart the server if his happens.
def __exit__(self, type, value, traceback):
stop_server()
# start and stop are really only used for debug
# class StartServerCommand(sublime_plugin.ApplicationCommand):
# def run(self):
# start_server()
# class StopServerCommand(sublime_plugin.ApplicationCommand):
# def run(self):
# stop_server()
class ClearCacheCommand(sublime_plugin.ApplicationCommand):
def run(self):
stop_server()
start_server()
class DisableCommand(sublime_plugin.ApplicationCommand):
def run(self):
global state
state["enabled"] = False
class EnableCommand(sublime_plugin.ApplicationCommand):
def run(self):
global state
state["enabled"] = True
def plugin_loaded():
global state
state["settings"] = sublime.load_settings("LuaComplete.sublime-settings")
# strip out the path/port
path = state["settings"].get("path")
if path is None:
path = "lua-complete"
port = state["settings"].get("port")
if port is None:
port = 24548
# figure out if it's enabled
enabled = state["settings"].get("enabled")
if enabled is None:
enabled = True
# setup the command.
state["server_command"] = "{path} server -p {port}".format(path=path, port=port)
state["client_command"] = "{path} client -p {port}".format(path=path, port=port)
state["enabled"] = enabled
# get any additional include locations
state["additional_includes"] = state["settings"].get("additional_includes")