-
Notifications
You must be signed in to change notification settings - Fork 1
/
haxe_completion.py
178 lines (125 loc) · 5.07 KB
/
haxe_completion.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
# -*- coding: utf-8 -*-
import sys, os, subprocess, re, shlex
from subprocess import Popen, PIPE
import sublime, sublime_plugin
#plugin location
plugin_file = __file__
plugin_filepath = os.path.realpath(plugin_file)
plugin_path = os.path.dirname(plugin_filepath)
import Default
stexec = getattr( Default , "exec" )
ExecCommand = stexec.ExecCommand
AsyncProcess = stexec.AsyncProcess
try:
STARTUP_INFO = subprocess.STARTUPINFO()
STARTUP_INFO.dwFlags |= subprocess.STARTF_USESHOWWINDOW
STARTUP_INFO.wShowWindow = subprocess.SW_HIDE
except (AttributeError):
STARTUP_INFO = None
print("hello haxe completion")
_completionist_ = None
def plugin_unloaded():
if _completionist_:
_completionist_.shutdown()
class HaxeCompletionist( sublime_plugin.EventListener ):
def __init__(self):
HaxeCompletionist.current = self
global _completionist_
_completionist_ = self
self.process = None
print("[haxe completion] __init__")
def init(self, forced=False):
if not forced:
if self.process is not None:
return;
print("[haxe completion] init")
settings = sublime.load_settings('haxe_completion.sublime-settings')
#kill any existing server
self.shutdown(True)
#defaults
self.haxe_path = "haxe"
self.port = 6110
if settings.has("port") is True:
self.port = settings.get("port")
print("[haxe completion] load custom port as {}".format(self.port))
if settings.has("haxe_path") is True:
self.haxe_path = settings.get("haxe_path")
print("[haxe completion] load custom haxe path as {}".format(self.haxe_path))
print("[haxe completion] trying to start cache server `" + self.haxe_path + "` port:" + str(self.port))
#this only starts the completion cache host from haxe,
#then each request is faster, in get()
try:
self.process = run_process_bg([self.haxe_path, "--wait", str(self.port)])
#Popen( [ self.haxe_path, "-v", "--wait", str(self.port) ], env = os.environ.copy(), startupinfo=STARTUP_INFO)
print("[haxe completion] started with `" + self.haxe_path + "` port:" + str(self.port))
except(OSError, ValueError) as e:
reason = u'[haxe completion] error starting server and connecting to it: \n%s' % e
print(reason)
return None
def complete(self, cwd='', fname='', offset=0, hxml=[]):
print("[haxe completion] complete")
self.init()
view = sublime.active_window().active_view()
haxe_cmd = [
self.haxe_path,
"--no-output",
"--cwd", cwd,
"--connect", "127.0.0.1:" + str(self.port),
"--display", fname + "@" + str(offset)
]
# print("[haxe completion] haxe complete args " + str(haxe_cmd+hxml))
_proc, _result_buffer = run_process( haxe_cmd+hxml )
_result = ""
if _result_buffer:
_result = _result_buffer.decode('utf-8')
if _result:
_result = _result.strip()
if _proc:
try:
_proc.kill();
_proc.wait();
except:
pass
return _result
def reset(self):
print("[haxe completion] reset")
self.shutdown()
self.init()
def shutdown(self, forced=False):
if(forced == False):
print("[haxe completion] shutdown")
if self.process is not None :
self.process.terminate()
self.process.kill()
self.process.wait()
self.process = None
def run_process( args ):
_proc = run_process_bg(args)
return _proc, _proc.communicate()[0]
def run_process_bg( args ):
_proc = None
#this shell_cmd is not used by windows
shell_cmd = ""
for arg in args:
#make sure lines from the hxml file don't trip up the shell
shell_cmd += shlex.quote(arg) + " "
if sys.platform == "win32":
_proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=STARTUP_INFO)
elif sys.platform == "darwin":
# Use a login shell on OSX, otherwise the users expected env vars won't be setup
_proc = subprocess.Popen(["/bin/bash", "-l", "-c", shell_cmd], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, startupinfo=STARTUP_INFO, shell=False,
preexec_fn=os.setsid)
elif sys.platform == "linux":
# Explicitly use /bin/bash on Linux, to keep Linux and OSX as
# similar as possible. A login shell is explicitly not used for
# linux, as it's not required
_proc = subprocess.Popen(["/bin/bash", "-c", shell_cmd], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, startupinfo=STARTUP_INFO, shell=False,
preexec_fn=os.setsid)
return _proc
class HaxeCompletionResetCommand( sublime_plugin.WindowCommand ):
def run( self ) :
global _completionist_
view = sublime.active_window().active_view()
_completionist_.reset()