-
Notifications
You must be signed in to change notification settings - Fork 12
/
Completions.py
140 lines (128 loc) · 6.01 KB
/
Completions.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
import sublime
import sublime_plugin
import threading
import urllib.request, urllib.parse
import json
import re
SETTINGS_FILE = 'Kulture.sublime-settings'
AC_OPTS = sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
class KKultureComplete (sublime_plugin.TextCommand):
def run(self, edit, action = None, **args):
print("DOT_COMPLETE:::::Code Reached")
self.view.insert(edit, self.view.sel()[0].begin(), args['key'])
self.view.run_command('auto_complete')
return True
class KKultureCompletion( sublime_plugin.EventListener):
def __init__(self):
self.result = {}
self.cache = []
self.thread = RetrievePackageNames(5)
self.thread.start()
while(self.thread.is_alive()):
pass
response = self.thread.response
if not response:
print (self.thread.message)
return
for package in response:
t = (package['Id'],package['Id'])
# if t not in self.result:
regex = r'([-a-zA-Z0-9\.]+)\/([-a-zA-Z0-9\.]+)$'
match = re.search(regex, package['__metadata']['media_src'])
try:
if (match.group(1),match.group(1)) not in self.cache:
self.cache.append((match.group(1), match.group(1)))
try:
self.result[match.group(1)].append((match.group(2), match.group(2)))
except KeyError as e:
self.result[match.group(1)] = []
self.result[match.group(1)].append((match.group(2), match.group(2)))
except AttributeError as e:
pass
def on_query_completions(self, view, prefix, locations):
pos = locations[0]
scopes = view.scope_name(pos).split()
if "source.json" not in scopes:
return []
else:
doc = view.substr(sublime.Region(0,view.size()))
whitespaces = 0;
for index, char in enumerate(doc[0:pos]):
if char == '\n' or char == '\r' or char == ' ' or char == '\t':
whitespaces += 1
doc = doc.replace('\n', '').replace('\r','').replace(' ', '').replace('\t', '')
pos = pos - whitespaces
depth = -1
tokens = []
token_regex = re.compile(r'"([-a-zA-Z0-9+]+)":{')
for index, char in enumerate(doc):
if char == '{':
depth += 1
token_regex = r'"([-a-zA-Z0-9+]+)":{$'
match = re.search(token_regex, doc[0:index+1])
try:
tokens.append(match.group(1))
except AttributeError as e:
pass
if char == '}':
depth -= 1
try:
tokens.pop()
except IndexError as e:
pass
if index==pos:
if (depth == 1 and tokens[0] == 'dependencies'):
# Version number
version_regex = r'(?:,|{|\[])"([-a-zA-Z0-9.*]+)":"[-a-zA-Z0-9.*]*$'
try:
package_name = re.search(version_regex, doc[0:index]).group(1)
return (self.result[package_name], AC_OPTS)
except AttributeError as e:
pass
except KeyError as e:
# TODO: Not in cache. Make HTTP request to feth completions on that package
return AC_OPTS
# Package name
return (self.cache, AC_OPTS)
elif depth == 0:
# In future, get completions dynamically from http://schemastore.org/schemas/json/project
# Replace word completions with snippets
return ([('version', 'version" : "$1"'),
('dependencies', 'dependencies" : {\n\t"$1": "$2"\n}'),
('commands', 'commands" : {\n\t"$1": "$2"\n}'),
('configurations', 'configurations" : {\n\t"$1": {}\n}'),
('compilationOptions', 'compilationOptions'),
('frameworks', 'frameworks" : {\n\t"$1": "$2"\n}'),
('description', 'description" : "$1"'),
('authors', 'authors" : [\n\t"$1"\n]'),
('code', 'code" : {\n\t"$1": "$2"\n}'),
('shared', 'shared" : {\n\t"$1": "$2"\n}'),
('exclude', 'exclude" : {\n\t"$1": "$2"\n}'),
('preprocess', 'preprocess" : {\n\t"$1": "$2"\n}'),
('resources', 'resources')
], AC_OPTS)
else:
break
return AC_OPTS
class RetrievePackageNames(threading.Thread):
def __init__(self,timeout):
self.timeout = timeout
self.response = None
threading.Thread.__init__(self)
def run(self):
try:
request = urllib.request.Request("https://www.myget.org/F/aspnetvnext/api/v2/Packages()?"
+ "$select=Id&"
+ "$format=json&"
+ "orderby=DownloadCount&"
+ "$top=100",
headers={"User-Agent": "Sublime"})
http_file = urllib.request.urlopen(request, timeout=self.timeout)
self.response = json.loads(http_file.read().decode('utf-8'))['d']
return
except (urllib.request.HTTPError) as e:
self.message = '%s: HTTP error %s contacting API' % (__name__, str(e.code))
except (urllib.request.URLError) as e:
self.message = '%s: URL error %s contacting API' % (__name__, str(e.reason))
self.response = False
return