-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcompletesharp.py
133 lines (112 loc) · 5.28 KB
/
completesharp.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
"""
Copyright (c) 2012 Fredrik Ehnbom
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
"""
import sublime_plugin
import sublime
import os.path
import re
import glob
import imp
import sys
completioncommon = imp.load_source("completioncommon", os.path.join(os.path.dirname(os.path.abspath(__file__)), "sublimecompletioncommon/completioncommon.py"))
completioncommon.reload(completioncommon)
class CompleteSharpDotComplete(completioncommon.CompletionCommonDotComplete):
pass
class CompleteSharpCompletion(completioncommon.CompletionCommon):
def __init__(self):
super(CompleteSharpCompletion, self).__init__("CompleteSharp.sublime-settings", os.path.dirname(os.path.abspath(__file__)))
self.replacements = {"int": "System.Int32", "string": "System.String", "char": "System.Char", "void": "System.Void", "long": "System.Int64", "short": "System.Int16"}
def find_absolute_of_type(self, data, full_data, type, template_args=[]):
idx1 = type.find("+")
idx2 = type.find("`")
extra = ""
if idx2 != -1 and idx1 != -1 and idx1 < idx2:
end = type[idx2:]
extra = type[idx1:idx2]
type = "%s%s%s" % (type[:idx1], end, extra)
if template_args != None and len(template_args):
type += "`%d" % len(template_args)
if type in self.replacements:
type = self.replacements[type]
return super(CompleteSharpCompletion, self).find_absolute_of_type(data, full_data, type)
def error_thread(self):
try:
errors = "The following assemblies could not be loaded, please consider manually adding them to your completesharp_assemblies list:\n\n"
while True:
if self.completion_proc.poll() != None:
break
line = self.completion_proc.stderr.readline().decode(sys.getdefaultencoding()).strip()
if line.startswith("Couldn't find assembly: "):
asm = line[line.find(":")+2:]
if not asm in errors:
errors += asm + "\n"
sublime.set_timeout(lambda: sublime.error_message(errors), 0)
print("stderr: %s" % line)
finally:
pass
def get_packages(self, data, thispackage, type):
packages = re.findall("[ \t]*using[ \t]+(.*);", data)
packages.append("System")
packages.append("")
return packages
def filter(self, typename, var, isstatic, data, indata):
filterregex = self.get_setting("completesharp_filterregex", r"^(get_|set_|op_|add_|remove_|<)")
try:
filterregex = re.compile(filterregex)
except:
sublime.error_message("Invalid filter regular expression. Please modify your completesharp_filterregex setting")
filterregex = None
ret = []
for d in indata:
# get_ and set_ are mostly associated with properties
if filterregex and filterregex.search(d[0]) != None:
continue
elif len(d) == 3 and self.is_static(d[2]) and var != None:
continue
ret.append(d)
return super(CompleteSharpCompletion, self).filter(typename, var, isstatic, data, ret)
def get_cmd(self):
extra = self.get_setting("completesharp_assemblies", [])
newextra = []
window = sublime.active_window()
for path in extra:
newextra.extend(glob.glob(self.expand_path(path, window, False)))
extra = newextra
cmd = ""
q = "\""
if sublime.platform() != "windows":
cmd = self.get_setting("completesharp_mono_path", "mono") + " "
q = "\'"
cmd = "%sCompleteSharp.exe %s%s%s" % (cmd, q, ";;--;;".join(extra), q)
return cmd
def is_supported_language(self, view):
if view.is_scratch():
return False
language = self.get_language(view)
return language == "cs"
comp = CompleteSharpCompletion()
class CompleteSharp(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
ret = comp.on_query_completions(view, prefix, locations)
return ret
def on_query_context(self, view, key, operator, operand, match_all):
if key == "completesharp.dotcomplete":
return comp.get_setting(key.replace(".", "_"), True)
elif key == "completesharp.supported_language":
return comp.is_supported_language(view)
else:
return comp.on_query_context(view, key, operator, operand, match_all)