forked from quarnster/SublimeJava
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsublimejava.py
More file actions
Latest commit
222 lines (172 loc) · 7.85 KB
/
Copy pathsublimejava.py
File metadata and controls
222 lines (172 loc) · 7.85 KB
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
"""
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.
"""
importsublime
importsublime_plugin
importos
importre
fromsublimecompletioncommonimportcompletioncommon
reload(completioncommon)
importclassopener
reload(classopener)
classSublimeJavaDotComplete(completioncommon.CompletionCommonDotComplete):
pass
classSublimeJavaCompletion(completioncommon.CompletionCommon):
def__init__(self):
super(SublimeJavaCompletion, self).__init__("SublimeJava.sublime-settings", os.path.dirname(os.path.abspath(__file__)))
self.regex= [
(re.compile(r"\[I([,)}]|$)"), r"int[]\1"),
(re.compile(r"\[F([,)}]|$)"), r"float[]\1"),
(re.compile(r"\[Z([,)}]|$)"), r"boolean[]\1"),
(re.compile(r"\[B([,)}]|$)"), r"byte[]\1"),
(re.compile(r"\[C([,)}]|$)"), r"char[]\1"),
(re.compile(r"\[S([,)}]|$)"), r"short[]\1"),
(re.compile(r"\[J([,)}]|$)"), r"long[]\1"),
(re.compile(r"\[D([,)}]|$)"), r"double[]\1"),
(re.compile(r"\[\L?([\w\./]+)(;)?"), r"\1[]")]
defshow_error(self, msg):
ifself.get_setting("sublimejava_no_visual_errors", False):
printmsg
else:
sublime.error_message(msg+"\n\nDisable visual error message dialogues with setting:\nsublimejava_no_visual_errors: true")
defget_packages(self, data, thispackage, type):
packages=re.findall(r"(?:^|\n)[ \t]*import[ \t]+(.*);", data)
packages.append("java.lang.*")
packages.append("") # for int, boolean, etc
forpackageinpackages:
idx=type.find(".")
ifidx==-1:
idx=len(type)
subtype=type[:idx]
ifre.search("[\.\$]{1}%s$"%subtype, package):
# Explicit imports, we want these to have the highest
# priority when searching for the absolute type, so
# insert them at the top of the package list.
# Both the .* version and not is added so that
# blah.<searchedForClass> and blah$<searchedForClass>
# is tested
add=package[:-(len(subtype)+1)]
packages.insert(0, add+".*")
packages.insert(1, add)
break
packages.append(thispackage+".*")
returnpackages
defget_cmd(self):
classpath=self.get_setting("sublimejava_classpath", ["."])
newclasspath= []
window=sublime.active_window()
forpathinclasspath:
newclasspath.append(self.expand_path(path, window))
classpath=newclasspath
classpath.insert(0, ".")
classpath=os.pathsep.join(classpath)
return"java -classpath \"%s\" SublimeJava"%classpath
defis_supported_language(self, view):
ifview.is_scratch() ornotself.get_setting("sublimejava_enabled", True):
returnFalse
language=self.get_language(view)
returnlanguage=="java"orlanguage=="jsp"
defsub(self, regex, sub, data):
olddata=data
data=regex.sub(sub, data)
whiledata!=olddata:
olddata=data
data=regex.sub(sub, data)
returndata
deffixnames(self, data):
forregex, replaceinself.regex:
data=self.sub(regex, replace, data)
returndata
defreturn_completions(self, comp):
ret= []
fordisplay, insertincomp:
ret.append((self.fixnames(display), self.fixnames(insert)))
returnsuper(SublimeJavaCompletion, self).return_completions(ret)
defget_class_under_cursor(self):
view=sublime.active_window().active_view()
data=view.substr(sublime.Region(0, view.size()))
word=view.substr(view.word(view.sel()[0].begin()))
returnself.find_absolute_of_type(data, data, word)
defget_possible_imports(self, classname):
imports= []
ifclassnameisnotNone:
stdout=self.run_completion("-possibleimports;;--;;%s"%classname)
imports=sorted(stdout.split("\n")[:-1])
returnimports
comp=SublimeJavaCompletion()
classSublimeJava(sublime_plugin.EventListener):
defon_query_completions(self, view, prefix, locations):
returncomp.on_query_completions(view, prefix, locations)
defon_query_context(self, view, key, operator, operand, match_all):
ifkey=="sublimejava.dotcomplete":
returncomp.get_setting(key.replace(".", "_"), True)
elifkey=="sublimejava.supported_language":
returncomp.is_supported_language(view)
else:
returncomp.on_query_context(view, key, operator, operand, match_all)
MSG_NO_CLASSES_FOUND="No classes found to import for name %s."
MSG_ALREADY_IMPORTED="Class %s has either already been imported, is \
in the current package, or is in the default package."
RE_IMPORT="import( static)? ([\w\.]+)\.([\w]+|\*);"
RE_PACKAGE="package ([\w]+.)*\w+;"
classImportJavaClassCommand(sublime_plugin.TextCommand):
defrun(self, edit):
view=self.view
classname=view.substr(view.word(view.sel()[0].begin()))
"""
if comp.get_class_under_cursor():
comp.show_error(MSG_ALREADY_IMPORTED % classname)
return
"""
imports=comp.get_possible_imports(classname)
defdo_import(index):
ifindex!=-1:
self._insert_import(imports[index], edit)
iflen(imports) >0:
view.window().show_quick_panel(imports, do_import)
else:
comp.show_error(MSG_NO_CLASSES_FOUND%classname)
def_insert_import(self, full_classname, edit):
insert_point=0
newlines_prepend=0
newlines_append=1
all_imports_region=self.view.find_all(RE_IMPORT)
iflen(all_imports_region) >0:
insert_point=all_imports_region[-1].b
newlines_prepend=1
newlines_append=0
else:
package_declaration_region=self.view.find(RE_PACKAGE, 0)
ifpackage_declaration_regionisnotNone:
insert_point=package_declaration_region.b
newlines_prepend=2
newlines_append=0
import_classname=full_classname.replace("$", ".")
import_statement="%simport %s;%s"% ("\n"*newlines_prepend,
import_classname,
"\n"*newlines_append)
self.view.insert(edit, insert_point, import_statement)
classOpenJavaSourceCommand(sublime_plugin.WindowCommand):
defrun(self, under_cursor=False):
classopener.JavaSourceOpener(comp,
self.window.active_view(),
under_cursor).show()
classOpenJavaDocCommand(sublime_plugin.WindowCommand):
defrun(self, under_cursor=False):
classopener.JavaDocOpener(comp,
self.window.active_view(),
under_cursor).show()