forked from ternjs/tern_for_sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tern.py
599 lines (520 loc) · 18.8 KB
/
tern.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# Sublime Text plugin for Tern
import sublime, sublime_plugin
import os, sys, platform, subprocess, webbrowser, json, re, time, atexit
try:
# python 2
from utils.renderer import create_arghints_renderer
except:
from .utils.renderer import create_arghints_renderer
windows = platform.system() == "Windows"
python3 = sys.version_info[0] > 2
is_st2 = int(sublime.version()) < 3000
def is_js_file(view):
return view.score_selector(sel_end(view.sel()[0]), "source.js") > 0
files = {}
arghints_enabled = False
arghints_renderer = None
arg_completion_enabled = False
tern_command = None
tern_arguments = []
def on_deactivated(view):
pfile = files.get(view.file_name(), None)
if pfile and pfile.dirty:
send_buffer(pfile, view)
def on_selection_modified(view):
if not arghints_enabled: return
pfile = get_pfile(view)
if pfile is not None: show_argument_hints(pfile, view)
class Listeners(sublime_plugin.EventListener):
def on_close(self, view):
files.pop(view.file_name(), None)
def on_deactivated(self, view):
if is_st2: on_deactivated(view)
def on_deactivated_async(self, view):
on_deactivated(view)
def on_modified(self, view):
pfile = files.get(view.file_name(), None)
if pfile: pfile_modified(pfile, view)
def on_selection_modified(self, view):
if is_st2: on_selection_modified(view)
def on_selection_modified_async(self, view):
on_selection_modified(view)
def on_query_completions(self, view, prefix, _locations):
if view.score_selector(sel_start(view.sel()[0]), 'string.quoted, comment') > 0: return None
pfile = get_pfile(view)
if pfile is None: return None
completions, fresh = ensure_completions_cached(pfile, view)
if completions is None: return None
if not fresh:
completions = [c for c in completions if c[1].startswith(prefix)]
return completions
class ProjectFile(object):
def __init__(self, name, view, project):
self.project = project
self.name = name
self.dirty = view.is_dirty()
self.cached_completions = None
self.cached_arguments = None
self.showing_arguments = False
self.last_modified = 0
class Project(object):
def __init__(self, dir):
self.dir = dir
self.port = None
self.proc = None
self.last_failed = 0
self.disabled = False
def __del__(self):
kill_server(self)
def get_pfile(view):
if not is_js_file(view): return None
fname = view.file_name()
if fname is None: return None
if fname in files:
pfile = files[fname]
if pfile.project.disabled: return None
return pfile
pdir = project_dir(fname)
if pdir is None: return None
project = None
for f in files.values():
if f.project.dir == pdir:
project = f.project
break
if project is None: project = Project(pdir)
pfile = files[fname] = ProjectFile(fname, view, project)
if project.disabled: return None
return pfile
def project_dir(fname):
dir = os.path.dirname(fname)
if not os.path.isdir(dir): return None
cur = dir
while True:
parent = os.path.dirname(cur[:-1])
if not parent:
break
if os.path.isfile(os.path.join(cur, ".tern-project")):
return cur
cur = parent
return dir
def pfile_modified(pfile, view):
pfile.dirty = True
now = time.time()
if now - pfile.last_modified > .5:
pfile.last_modified = now
if is_st2:
sublime.set_timeout(lambda: maybe_save_pfile(pfile, view, now), 5000)
else:
sublime.set_timeout_async(lambda: maybe_save_pfile(pfile, view, now), 5000)
if pfile.cached_completions and sel_start(view.sel()[0]) < pfile.cached_completions[0]:
pfile.cached_completions = None
if pfile.cached_arguments and sel_start(view.sel()[0]) < pfile.cached_arguments[0]:
pfile.cached_arguments = None
def maybe_save_pfile(pfile, view, timestamp):
if pfile.last_modified == timestamp and pfile.dirty:
send_buffer(pfile, view)
def server_port(project, ignored=None):
if project.port is not None and project.port != ignored:
return (project.port, True)
if project.port == ignored:
kill_server(project)
port_file = os.path.join(project.dir, ".tern-port")
if os.path.isfile(port_file):
port = int(open(port_file, "r").read())
if port != ignored:
project.port = port
return (port, True)
started = start_server(project)
if started is not None:
project.port = started
return (started, False)
def start_server(project):
if not tern_command: return None
if time.time() - project.last_failed < 30: return None
env = None
if platform.system() == "Darwin":
env = os.environ.copy()
env["PATH"] += ":/usr/local/bin"
proc = subprocess.Popen(tern_command + tern_arguments, cwd=project.dir, env=env,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=windows)
output = ""
while True:
line = proc.stdout.readline().decode("utf-8")
if not line:
sublime.error_message("Failed to start server" + (output and ":\n" + output))
project.last_failed = time.time()
return None
match = re.match("Listening on port (\\d+)", line)
if match:
project.proc = proc
return int(match.group(1))
else:
output += line
def kill_server(project):
if project.proc is None: return
project.proc.stdin.close()
project.proc.wait()
project.proc = None
def relative_file(pfile):
return pfile.name[len(pfile.project.dir) + 1:]
def buffer_fragment(view, pos):
region = None
for js_region in view.find_by_selector("source.js"):
if js_region.a <= pos and js_region.b >= pos:
region = js_region
break
if region is None: return sublime.Region(pos, pos)
start = view.line(max(region.a, pos - 1000)).a
if start < pos - 1500: start = pos - 1500
cur = start
min_indent = 10000
while True:
next = view.find("\\bfunction\\b", cur)
if next is None or next.b > pos or (next.a == -1 and next.b == -1): break
line = view.line(next.a)
if line.a < pos - 1500: line = sublime.Region(pos - 1500, line.b)
indent = count_indentation(view.substr(line))
if indent < min_indent:
min_indent = indent
start = line.a
cur = line.b
return sublime.Region(start, min(pos + 500, region.b))
def count_indentation(line):
count, pos = (0, 0)
while pos < len(line):
ch = line[pos]
if ch == " ": count += 1
elif ch == "\t": count += 4
else: break
pos += 1
return count
def sel_start(sel):
return min(sel.a, sel.b)
def sel_end(sel):
return max(sel.a, sel.b)
class Req_Error(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message
localhost = (windows and "127.0.0.1") or "localhost"
def make_request_py2():
import urllib2
opener = urllib2.build_opener(urllib2.ProxyHandler({}))
def f(port, doc):
try:
req = opener.open("http://" + localhost + ":" + str(port) + "/", json.dumps(doc), 1)
return json.loads(req.read())
except urllib2.HTTPError as error:
raise Req_Error(error.read())
return f
def make_request_py3():
import urllib.request, urllib.error
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def f(port, doc):
try:
req = opener.open("http://" + localhost + ":" + str(port) + "/", json.dumps(doc).encode("utf-8"), 1)
return json.loads(req.read().decode("utf-8"))
except urllib.error.URLError as error:
raise Req_Error(error.read().decode("utf-8"))
return f
if python3:
make_request = make_request_py3()
else:
make_request = make_request_py2()
def view_js_text(view):
text, pos = ("", 0)
for region in view.find_by_selector("source.js"):
if region.a > pos: text += ";" + re.sub(r'[^\n]', " ", view.substr(sublime.Region(pos + 1, region.a)))
text += view.substr(region)
pos = region.b
return text
def run_command(view, query, pos=None, fragments=True, silent=False):
pfile = get_pfile(view)
if pfile is None or pfile.project.disabled: return
if isinstance(query, str): query = {"type": query}
if (pos is None): pos = view.sel()[0].b
port, port_is_old = server_port(pfile.project)
if port is None: return
doc = {"query": query, "files": []}
if not pfile.dirty:
fname, sending_file = (relative_file(pfile), False)
if fragments and view.size() > 8000:
region = buffer_fragment(view, pos)
doc["files"].append({"type": "part",
"name": relative_file(pfile),
"offset": region.a,
"text": view.substr(region)})
pos -= region.a
fname, sending_file = ("#0", False)
else:
doc["files"].append({"type": "full",
"name": relative_file(pfile),
"text": view_js_text(view)})
fname, sending_file = ("#0", True)
query["file"] = fname
query["end"] = pos
data = None
try:
data = make_request(port, doc)
except Req_Error as e:
if not silent: report_error(str(e), pfile.project)
return None
except:
pass
if data is None and port_is_old:
try:
port = server_port(pfile.project, port)[0]
if port is None: return
data = make_request(port, doc)
if data is None: return None
except Exception as e:
if not silent: report_error(str(e), pfile.project)
if sending_file: pfile.dirty = False
return data
def send_buffer(pfile, view):
port = server_port(pfile.project)[0]
if port is None: return False
try:
make_request(port,
{"files": [{"type": "full",
"name": relative_file(pfile),
"text": view_js_text(view)}]})
pfile.dirty = False
return True
except:
return False
def report_error(message, project):
if sublime.ok_cancel_dialog(message, "Disable Tern"):
project.disabled = True
def completion_icon(type):
if type is None or type == "?": return " (?)"
if type.startswith("fn("): return " (fn)"
if type.startswith("["): return " ([])"
if type == "number": return " (num)"
if type == "string": return " (str)"
if type == "bool": return " (bool)"
return " (obj)"
def fn_completion_icon(arguments):
return " (fn/"+str(len(arguments))+")"
# create auto complete string from list arguments
def create_arg_str(arguments):
arg_str = ""
k = 1
for argument in arguments:
arg_str += "${" + str(k) + ":" + argument + "}, "
k += 1
return arg_str[0:-2]
# parse the type to get the arguments
def get_arguments(type):
type = type[3:type.find(')')] + ",'"
arg_list = []
arg_start = 0
arg_end = 0
depth = 0
arg_already = False
for ch in type:
if depth == 0 and ch == ',':
if arg_already:
arg_already = False
elif arg_start != arg_end:
arg_list.append(type[arg_start:arg_end])
arg_start = arg_end+1
elif depth == 0 and ch == ':':
arg_already = True
arg_list.append(type[arg_start:arg_end])
elif ch == '{' or ch == '(' or ch == '[':
depth += 1
elif ch == '}' or ch == ')' or ch == ']':
depth -= 1
elif ch == ' ':
arg_start = arg_end + 1
arg_end += 1
return arg_list
def ensure_completions_cached(pfile, view):
pos = view.sel()[0].b
if pfile.cached_completions is not None:
c_start, c_word, c_completions = pfile.cached_completions
if c_start <= pos:
slice = view.substr(sublime.Region(c_start, pos))
if slice.startswith(c_word) and not re.match(".*\\W", slice):
return (c_completions, False)
data = run_command(view, {"type": "completions", "types": True})
if data is None: return (None, False)
completions = []
completions_arity = []
for rec in data["completions"]:
rec_name = rec.get('name').replace('$', '\\$')
rec_type = rec.get("type", None)
if arg_completion_enabled:
if completion_icon(rec_type) == " (fn)":
arguments = get_arguments(rec_type)
fn_name = rec_name + "(" + create_arg_str(arguments) + ")"
completions.append((rec.get("name") + fn_completion_icon(arguments), fn_name))
for i in range(len(arguments) - 1, -1, -1):
fn_name = rec_name + "(" + create_arg_str(arguments[0:i]) + ")"
completions_arity.append((rec.get("name") + fn_completion_icon(arguments[0:i]), fn_name))
else:
completions.append((rec.get("name") + completion_icon(rec_type), rec_name))
else:
completions.append((rec_name + completion_icon(rec_type), rec_name))
# put the auto completions of functions with lower arity at the bottom of the autocomplete list
# so they don't clog up the autocompeltions at the top of the list
completions = completions + completions_arity
pfile.cached_completions = (data["start"], view.substr(sublime.Region(data["start"], pos)), completions)
return (completions, True)
def locate_call(view):
sel = view.sel()[0]
if sel.a != sel.b: return (None, 0)
context = view.substr(sublime.Region(max(0, sel.b - 500), sel.b))
pos = len(context)
depth = argpos = 0
while pos > 0:
pos -= 1
ch = context[pos]
if ch == "}" or ch == ")" or ch == "]":
depth += 1
elif ch == "{" or ch == "(" or ch == "[":
if depth > 0: depth -= 1
elif ch == "(": return (pos + sel.b - len(context), argpos)
else: return (None, 0)
elif ch == "," and depth == 0:
argpos += 1
return (None, 0)
def show_argument_hints(pfile, view):
call_start, argpos = locate_call(view)
if call_start is None: return render_argument_hints(pfile, view, None, 0)
if pfile.cached_arguments is not None and pfile.cached_arguments[0] == call_start:
return render_argument_hints(pfile, view, pfile.cached_arguments[1], argpos)
data = run_command(view, {"type": "type", "preferFunction": True}, call_start, silent=True)
if data is not None:
parsed = parse_function_type(data)
if parsed is not None:
parsed['url'] = data.get('url', None)
parsed['doc'] = data.get('doc', None)
pfile.cached_arguments = (call_start, parsed)
render_argument_hints(pfile, view, parsed, argpos)
def render_argument_hints(pfile, view, ftype, argpos):
if ftype is None:
arghints_renderer.clean(pfile, view)
else:
arghints_renderer.render(pfile, view, ftype, argpos)
def parse_function_type(data):
type = data["type"]
if not re.match("fn\\(", type): return None
pos = 3
args, retval = ([], None)
while pos < len(type) and type[pos] != ")":
colon = type.find(":", pos)
name = "?"
if colon != -1:
name = type[pos:colon]
if not re.match("[\\w_$]+$", name): name = "?"
else: pos = colon + 2
type_start = pos
depth = 0
while pos < len(type):
ch = type[pos]
if ch == "(" or ch == "[" or ch == "{":
depth += 1
elif ch == ")" or ch == "]" or ch == "}":
if depth > 0: depth -= 1
else: break
elif ch == "," and depth == 0:
break
pos += 1
args.append((name, type[type_start:pos]))
if type[pos] == ",": pos += 2
if type[pos:pos + 5] == ") -> ":
retval = type[pos + 5:]
return {"name": data.get("exprName", None) or data.get("name", None) or "fn",
"args": args,
"retval": retval}
jump_stack = []
class TernArghintCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
self.view.insert(edit, 0, args.get('msg', ''))
class TernJumpToDef(sublime_plugin.TextCommand):
def run(self, edit, **args):
data = run_command(self.view, {"type": "definition", "lineCharPositions": True})
if data is None: return
file = data.get("file", None)
if file is not None:
# Found an actual definition
row, col = self.view.rowcol(self.view.sel()[0].b)
cur_pos = self.view.file_name() + ":" + str(row + 1) + ":" + str(col + 1)
jump_stack.append(cur_pos)
if len(jump_stack) > 50: jump_stack.pop(0)
real_file = (os.path.join(get_pfile(self.view).project.dir, file) +
":" + str(data["start"]["line"] + 1) + ":" + str(data["start"]["ch"] + 1))
sublime.active_window().open_file(real_file, sublime.ENCODED_POSITION)
else:
url = data.get("url", None)
if url is None:
sublime.error_message("Could not find a definition")
else:
webbrowser.open(url)
class TernJumpBack(sublime_plugin.TextCommand):
def run(self, edit, **args):
if len(jump_stack) > 0:
sublime.active_window().open_file(jump_stack.pop(), sublime.ENCODED_POSITION)
class TernSelectVariable(sublime_plugin.TextCommand):
def run(self, edit, **args):
data = run_command(self.view, "refs", fragments=False)
if data is None: return
file = relative_file(get_pfile(self.view))
shown_error = False
regions = []
for ref in data["refs"]:
if ref["file"] != file:
if not shown_error:
sublime.error_message("Not all uses of this variable are file-local. Selecting only local ones.")
shown_error = True
else:
regions.append(sublime.Region(ref["start"], ref["end"]))
self.view.sel().clear()
for r in regions: self.view.sel().add(r)
plugin_dir = os.path.abspath(os.path.dirname(__file__))
def plugin_loaded():
global arghints_enabled, arghints_renderer, tern_command, tern_arguments
global arg_completion_enabled
settings = sublime.load_settings("Preferences.sublime-settings")
arghints_enabled = settings.get("tern_argument_hints", False)
arg_completion_enabled = settings.get("tern_argument_completion", False)
if arghints_enabled:
if "show_popup" in dir(sublime.View):
default_arghints_type = "tooltip"
else:
default_arghints_type = "status"
arghints_type = settings.get("tern_argument_hints_type", default_arghints_type)
arghints_renderer = create_arghints_renderer(arghints_type)
tern_arguments = settings.get("tern_arguments", [])
if not isinstance(tern_arguments, list):
tern_arguments = [tern_arguments]
tern_command = settings.get("tern_command", None)
if tern_command is None:
if not os.path.isdir(os.path.join(plugin_dir, "node_modules/tern")):
if sublime.ok_cancel_dialog(
"It appears Tern has not been installed. Do you want tern_for_sublime to try and install it? "
"(Note that this will only work if you already have node.js and npm installed on your system.)"
"\n\nTo get rid of this dialog, either uninstall tern_for_sublime, or set the tern_command setting.",
"Yes, install."):
try:
if hasattr(subprocess, "check_output"):
subprocess.check_output(["npm", "install"], cwd=plugin_dir)
else:
subprocess.check_call(["npm", "install"], cwd=plugin_dir)
except subprocess.CalledProcessError as e:
msg = "Installation failed. Try doing 'npm install' manually in " + plugin_dir + "."
if hasattr(e, "output"):
msg += " Error message was:\n\n" + e.output
sublime.error_message(msg)
return
tern_command = ["node", os.path.join(plugin_dir, "node_modules/tern/bin/tern"), "--no-port-file"]
def cleanup():
for f in files.values():
kill_server(f.project)
atexit.register(cleanup)
if is_st2:
sublime.set_timeout(plugin_loaded, 500)