-
Notifications
You must be signed in to change notification settings - Fork 2
/
OmniSharp.py
80 lines (63 loc) · 2.89 KB
/
OmniSharp.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
import urllib2, urllib, urlparse, json, os, sublime_plugin, sublime
class OmniSharp(sublime_plugin.EventListener):
word_list = []
def on_pre_save(self, view):
self.view = view
if self.is_dotnet_file(view.scope_name(view.sel()[0].begin())):
js = self.get_response('/syntaxerrors')
if len(js['Errors']) > 0 :
view.set_status('message', 'Syntax errors. View the console for details')
print js['Errors']
else :
view.set_status('message', '')
def on_modified(self, view):
self.view = view
sublime.set_timeout(lambda: self.load_completions(view), 3)
def load_completions(self, view):
scope_name = view.scope_name(view.sel()[0].begin())
if self.is_dotnet_file(scope_name) :
parameters = {}
location = self.view.sel()[0]
parameters['wordToComplete'] = self.view.substr(self.view.word(location.a))
completions = self.get_response('/autocomplete', parameters)
for completion in completions:
self.word_list.append(completion['CompletionText'])
def is_dotnet_file(self, scope):
return ".cs" in scope
def get_autocomplete_list(self, word):
autocomplete_list = []
for w in self.word_list:
try:
if word.lower() in w.lower():
autocomplete_list.append((w, w))
except UnicodeDecodeError:
continue
return autocomplete_list
# gets called when auto-completion pops up.
def on_query_completions(self, view, prefix, locations):
scope_name = sublime.windows()[0].active_view().scope_name(sublime.windows()[0].active_view().sel()[0].begin())
return self.get_autocomplete_list(prefix)
def get_response(self, endpoint, additionalParameters=None):
parameters = {}
location = self.view.sel()[0]
cursor = self.view.rowcol(location.begin())
parameters['line'] = cursor[0] + 1
parameters['column'] = cursor[1] + 1
parameters['buffer'] = '\r\n'.join(self.task_input()[:])
parameters['filename'] = self.view.file_name()
if additionalParameters != None:
parameters.update(additionalParameters)
target = urlparse.urljoin('http://localhost:2000/', endpoint)
parameters = urllib.urlencode(parameters)
response = urllib2.urlopen(target, parameters)
js = response.read()
if(js != ''):
return json.loads(js)
def task_input(self):
selections = [region for region in self.view.sel() if not region.empty()]
if len(selections) == 0:
self.regions = [sublime.Region(0, self.view.size())]
else:
self.regions = selections
page_text = [self.view.substr(region).encode('utf-8') for region in self.regions]
return page_text