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
146 lines (124 loc) · 5.49 KB
/
sublimejava.py
File metadata and controls
146 lines (124 loc) · 5.49 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
"""
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
import sublime_plugin
from os import pathsep
import os.path
import re
try:
from sublimecompletioncommon import completioncommon
reload(completioncommon)
except:
def hack(func):
# If there's a sublime.error_message before a window is open
# on Windows 7, it appears the main editor window
# is never opened...
class hackClass:
def __init__(self, func):
self.func = func
self.try_now()
def try_now(self):
if sublime.active_window() == None:
sublime.set_timeout(self.try_now, 500)
else:
self.func()
hackClass(func)
def showError():
sublime.error_message("""\
Unfortunately SublimeJava currently can't be installed \
via Package Control at the moment. Please see http://www.github.com/quarnster/SublimeJava \
for more details.""")
hack(showError)
class SublimeJavaDotComplete(completioncommon.CompletionCommonDotComplete):
pass
class SublimeJavaCompletion(completioncommon.CompletionCommon):
def __init__(self):
super(SublimeJavaCompletion, self).__init__("SublimeJava.sublime-settings", os.path.dirname(os.path.abspath(__file__)))
self.javaseparator = pathsep # just so that get_cmd references it. It's set "for real" later
self.javaseparator = self.run_completion("-separator").strip()
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[]")]
def get_packages(self, data, thispackage, type):
packages = re.findall(r"(?:^|\n)[ \t]*import[ \t]+(.*);", data)
packages.append("java.lang.*")
packages.append("") # for int, boolean, etc
for package in packages:
idx = type.find(".")
if idx == -1:
idx = len(type)
subtype = type[:idx]
if re.search("%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 + ".*")
return packages
def get_cmd(self):
classpath = "."
if self.javaseparator != None:
classpath = self.get_setting("sublimejava_classpath", ["."])
classpath.insert(0, ".")
classpath = self.javaseparator.join(classpath)
return "java -classpath %s SublimeJava" % classpath
def is_supported_language(self, view):
if view.is_scratch() or not self.get_setting("sublimejava_enabled", True):
return False
language = self.get_language(view)
return language == "java" or language == "jsp"
def sub(self, regex, sub, data):
olddata = data
data = regex.sub(sub, data)
while data != olddata:
olddata = data
data = regex.sub(sub, data)
return data
def fixnames(self, data):
for regex, replace in self.regex:
data = self.sub(regex, replace, data)
return data
def return_completions(self, comp):
ret = []
for display, insert in comp:
ret.append((self.fixnames(display), self.fixnames(insert)))
return super(SublimeJavaCompletion, self).return_completions(ret)
comp = SublimeJavaCompletion()
class SublimeJava(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
return comp.on_query_completions(view, prefix, locations)
def on_query_context(self, view, key, operator, operand, match_all):
if key == "sublimejava.dotcomplete":
return comp.get_setting(key.replace(".", "_"), True)
elif key == "sublimejava.supported_language":
return comp.is_supported_language(view)
else:
return comp.on_query_context(view, key, operator, operand, match_all)