This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrystalAutoComplete.py
143 lines (114 loc) · 4.45 KB
/
CrystalAutoComplete.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
import os
import sublime
import sublime_plugin
import re
import subprocess
import json
from subprocess import Popen, PIPE
settings = None
class Settings:
def __init__(self):
package_settings = sublime.load_settings("CrystalAutoComplete.sublime-settings")
package_settings.add_on_change("cracker", settings_changed)
package_settings.add_on_change("search_paths", settings_changed)
self.cracker_bin = package_settings.get("cracker", "cracker")
self.search_paths = package_settings.get("search_paths", [])
self.package_settings = package_settings
def unload(self):
self.package_settings.clear_on_change("cracker")
self.package_settings.clear_on_change("search_paths")
def plugin_loaded():
global settings
settings = Settings()
def plugin_unloaded():
global settings
if settings != None:
settings.unload()
settings = None
def settings_changed():
global settings
if settings != None:
settings.unload()
settings = None
settings = Settings()
class Result:
def __init__(self, parts):
self.path = parts["path"]
self.name = parts["name"]
self.file = parts["file"]
self.type = parts["type"]
self.location = parts.get("location", "")
self.signature = parts["signature"]
def run_cracker(view, loc):
# Retrieve the entire buffer
region = sublime.Region(0, loc)
content = view.substr(region)
cmd_list = []
cmd_list.insert(0, settings.cracker_bin)
cmd_list.append("client")
cmd_list.append("--context")
# Run cracker
process = Popen(cmd_list, stdout=PIPE, stderr=PIPE, stdin=PIPE,
bufsize=1,
universal_newlines=True)
(output, err) = process.communicate(input=content, timeout=2)
exit_code = process.wait()
# Parse results
results = []
if exit_code == 0:
raw = json.loads(output)
if raw["status"] == "success":
raw_results = raw["results"]
for r in raw_results:
r["path"] = view.file_name()
results.append(Result(r))
pass
else:
print("CMD: '%s' failed: exit_code:" % ' '.join(cmd_list), exit_code, output, err)
return results
class CrystalAutocomplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
# Check if this is a Crystal source file. This check
# relies on the Crystal syntax formatting extension
# being installed - https://github.com/crystal-lang/sublime-crystal
if view.match_selector(locations[0], "source.crystal"):
try:
raw_results = run_cracker(view, locations[0])
except FileNotFoundError:
print("Unable to find cracker executable (check settings)")
return
results = []
regexp = '[\.#](.+)\('
for r in raw_results:
if r.type == "Function":
if r.name.find("#") != -1:
trigger = r.name.split("#")[1]
else:
trigger = r.name.split(".")[1]
contents = trigger.split("(")[0]
if r.name.find("()") == -1:
contents = contents + '('
else:
trigger = r.name
contents = r.name
results.append([trigger, contents])
if len(results) > 0:
# return results
return (results, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
#class CrystalGotoDefinitionCommand(sublime_plugin.TextCommand):
# def run(self, edit):
# # Get the buffer location in correct format for cracker
# row, col = self.view.rowcol(self.view.sel()[0].begin())
# row += 1
#
# results = run_cracker(self.view, ["find-definition", str(row), str(col)])
#
# if len(results) == 1:
# result = results[0]
# path = result.path
# # On Windows the cracker will return the paths without the drive
# # letter and we need the letter for the open_file to work.
# if sublime.platform() == 'windows' and not re.compile('^\w\:').match(path):
# path = 'c:' + path
# encoded_path = "{0}:{1}:{2}".format(path, result.row, result.column)
# self.view.window().open_file(encoded_path, sublime.ENCODED_POSITION)