Skip to content

Commit b2aea4e

Browse files
committed
Can now find full class names for java.lang and import package.*;
1 parent 844f44a commit b2aea4e

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

SublimeJava.class

463 Bytes
Binary file not shown.

SublimeJava.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,40 @@
2525
import java.lang.reflect.Method;
2626
import java.lang.reflect.Member;
2727
import java.net.URL;
28+
import java.io.BufferedReader;
29+
import java.io.InputStreamReader;
30+
2831

2932
public class SublimeJava
3033
{
3134
public static void main(String... args)
3235
{
3336
try
3437
{
38+
if (args[0].equals("-findclass"))
39+
{
40+
String line = null;
41+
try
42+
{
43+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
44+
while ((line = in.readLine()) != null)
45+
{
46+
try
47+
{
48+
Class<?> c = Class.forName(line + "." + args[1]);
49+
System.out.println("" + c.getName());
50+
return;
51+
}
52+
catch (Exception e)
53+
{
54+
}
55+
}
56+
}
57+
catch (Exception e)
58+
{
59+
}
60+
return;
61+
}
3562
Class<?> c = Class.forName(args[1]);
3663
String filter = "";
3764
if (args.length >= 3)

sublimejava.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def find_type_of_variable(self, data, variable):
4040
# Variable not defined in this class...
4141
return None
4242

43+
def get_cmd(self):
44+
return "java -classpath .:/Users/quarnster/android/android-sdk-mac_86/platforms/android-14/android.jar SublimeJava"
45+
4346
def find_absolute_of_type(self, data, type):
4447
match = re.search("class %s" % type, data)
4548
if not match is None:
@@ -52,26 +55,35 @@ def find_absolute_of_type(self, data, type):
5255
match = re.search(regex, data)
5356
if not match is None:
5457
return "%s.%s" % (match.group(1), type)
55-
return type
5658

57-
def run_java(self, cmd):
59+
# Couldn't find the absolute name of this class so try to
60+
# see if it's in one of the packages imported as
61+
# "import package.*;", or in java.lang
62+
#
63+
packages = re.findall("[ \t]*import[ \t]+(.*)\.\*;", data)
64+
packages.append("java.lang")
65+
output = self.run_java("%s -findclass %s" % (self.get_cmd(), type), "\n".join(packages))
66+
return output.strip()
67+
68+
def run_java(self, cmd, stdin=None):
5869
scriptdir = os.path.dirname(os.path.abspath(__file__))
5970
proc = subprocess.Popen(
6071
cmd,
6172
cwd=scriptdir,
6273
shell=True,
63-
stdout=subprocess.PIPE
74+
stdout=subprocess.PIPE,
75+
stdin=subprocess.PIPE
6476
)
65-
stdout, stderr = proc.communicate()
77+
stdout, stderr = proc.communicate(stdin)
6678
return stdout
6779

6880
def complete_class(self, absolute_classname, prefix):
69-
stdout = self.run_java("java -classpath .:/Users/quarnster/android/android-sdk-mac_86/platforms/android-14/android.jar SublimeJava -complete %s %s" % (absolute_classname, prefix))
81+
stdout = self.run_java("%s -complete %s %s" % (self.get_cmd(), absolute_classname, prefix))
7082
ret = [tuple(line.split(";")) for line in stdout.split("\n")[:-1]]
7183
return sorted(ret, key=lambda a: a[0])
7284

7385
def get_return_type(self, absolute_classname, prefix):
74-
stdout = self.run_java("java -classpath .:/Users/quarnster/android/android-sdk-mac_86/platforms/android-14/android.jar SublimeJava -returntype %s %s" % (absolute_classname, prefix))
86+
stdout = self.run_java("%s -returntype %s %s" % (self.get_cmd(), absolute_classname, prefix))
7587
return stdout.strip()
7688

7789
def on_query_completions(self, view, prefix, locations):
@@ -93,6 +105,7 @@ def on_query_completions(self, view, prefix, locations):
93105
t = self.find_type_of_variable(data, var)
94106
print "type is %s" % t
95107
t = self.find_absolute_of_type(data, t)
108+
96109
print "absolute is %s" % (t)
97110

98111
idx = before.find(".")

0 commit comments

Comments
 (0)