Skip to content

Commit 844f44a

Browse files
committed
Initial commit
0 parents  commit 844f44a

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

README.creole

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
=== Description ===
2+
A plugin that provides an Java code completion view inside of Sublime Text 2.
3+
4+
=== Known issues and feature requests ===
5+
Please go [[https://github.com/quarnster/SublimeJava/issues?sort=created&direction=desc&state=open|here]] to see the currently known issues and feature request, or to file a new.
6+
7+
=== Prerequisites ===
8+
# You need to have java installed on your system and the java binary in your path
9+
10+
=== Installation ===
11+
# The easiest way to install this plugin is via the excellent Package Control Plugin
12+
## See http://wbond.net/sublime_packages/package_control#Installation
13+
### Once package control has been installed, bring up the command palette (cmd+shift+P or ctrl+shift+P)
14+
### Type Install and select "Package Control: Install Package"
15+
### Select SublimeJava from the list. Package Control will keep it automatically updated for you
16+
## If you don't want to use package control, you can manually install it
17+
### Go to your packages directory and type:
18+
#### git clone https://github.com/quarnster/SublimeJava SublimeJava
19+
20+
=== Usage ===
21+
Once installed it'll try to provide reasonable completion suggestions when you trigger autocompletion.
22+
23+
24+
=== License ===
25+
This plugin is using the zlib license
26+
27+
{{{
28+
Copyright (c) 2012 Fredrik Ehnbom
29+
30+
This software is provided 'as-is', without any express or implied
31+
warranty. In no event will the authors be held liable for any damages
32+
arising from the use of this software.
33+
34+
Permission is granted to anyone to use this software for any purpose,
35+
including commercial applications, and to alter it and redistribute it
36+
freely, subject to the following restrictions:
37+
38+
1. The origin of this software must not be misrepresented; you must not
39+
claim that you wrote the original software. If you use this software
40+
in a product, an acknowledgment in the product documentation would be
41+
appreciated but is not required.
42+
43+
2. Altered source versions must be plainly marked as such, and must not be
44+
misrepresented as being the original software.
45+
46+
3. This notice may not be removed or altered from any source
47+
distribution.
48+
}}}

SublimeJava.class

2.38 KB
Binary file not shown.

SublimeJava.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright (c) 2012 Fredrik Ehnbom
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
12+
1. The origin of this software must not be misrepresented; you must not
13+
claim that you wrote the original software. If you use this software
14+
in a product, an acknowledgment in the product documentation would be
15+
appreciated but is not required.
16+
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
20+
3. This notice may not be removed or altered from any source
21+
distribution.
22+
*/
23+
import java.lang.reflect.Constructor;
24+
import java.lang.reflect.Field;
25+
import java.lang.reflect.Method;
26+
import java.lang.reflect.Member;
27+
import java.net.URL;
28+
29+
public class SublimeJava
30+
{
31+
public static void main(String... args)
32+
{
33+
try
34+
{
35+
Class<?> c = Class.forName(args[1]);
36+
String filter = "";
37+
if (args.length >= 3)
38+
filter = args[2];
39+
if (args[0].equals("-complete"))
40+
{
41+
for (Field f : c.getFields())
42+
{
43+
String str = f.getName();
44+
if (!str.startsWith(filter))
45+
continue;
46+
47+
String rep = str + "\t" + f.getType().getName();
48+
System.out.println(rep + ";" + str);
49+
}
50+
for (Method m : c.getMethods())
51+
{
52+
String str = m.getName();
53+
if (!str.startsWith(filter))
54+
continue;
55+
str += "(";
56+
String ins = str;
57+
int count = 1;
58+
for (Class c2 : m.getParameterTypes())
59+
{
60+
if (count > 1)
61+
{
62+
str += ", ";
63+
ins += ", ";
64+
}
65+
String n = c2.getName();
66+
str += n;
67+
ins += "${"+count + ":" + n + "}";
68+
count++;
69+
}
70+
str += ")\t" + m.getReturnType().getName();
71+
ins += ")";
72+
System.out.println(str + ";" + ins);
73+
}
74+
}
75+
else
76+
{
77+
for (Field f : c.getFields())
78+
{
79+
if (filter.equals(c.getName()))
80+
{
81+
System.out.println("" + f.getType());
82+
return;
83+
}
84+
}
85+
for (Method m : c.getMethods())
86+
{
87+
if (filter.equals(m.getName()))
88+
{
89+
System.out.println("" + m.getReturnType().getName());
90+
return;
91+
}
92+
}
93+
}
94+
}
95+
catch (ClassNotFoundException x)
96+
{
97+
}
98+
}
99+
}

sublimejava.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"""
2+
Copyright (c) 2012 Fredrik Ehnbom
3+
4+
This software is provided 'as-is', without any express or implied
5+
warranty. In no event will the authors be held liable for any damages
6+
arising from the use of this software.
7+
8+
Permission is granted to anyone to use this software for any purpose,
9+
including commercial applications, and to alter it and redistribute it
10+
freely, subject to the following restrictions:
11+
12+
1. The origin of this software must not be misrepresented; you must not
13+
claim that you wrote the original software. If you use this software
14+
in a product, an acknowledgment in the product documentation would be
15+
appreciated but is not required.
16+
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
20+
3. This notice may not be removed or altered from any source
21+
distribution.
22+
"""
23+
import sublime
24+
import sublime_plugin
25+
import re
26+
import subprocess
27+
import os.path
28+
29+
30+
class SublimeJava(sublime_plugin.EventListener):
31+
32+
def find_type_of_variable(self, data, variable):
33+
print variable
34+
regex = "(\w[^( \t]+)[ \t]+%s[ \t]*(\;|,|\)|=|:).*$" % variable
35+
print regex
36+
match = re.search(regex, data, re.MULTILINE)
37+
if not match is None:
38+
return match.group(1)
39+
else:
40+
# Variable not defined in this class...
41+
return None
42+
43+
def find_absolute_of_type(self, data, type):
44+
match = re.search("class %s" % type, data)
45+
if not match is None:
46+
# Class is defined in this file, return package of the file
47+
package = re.search("[ \t]*package (.*);", data)
48+
if package is None:
49+
return type
50+
return "%s.%s" % (package.group(1), type)
51+
regex = "[ \t]*import[ \t]+(.*)\.%s" % type
52+
match = re.search(regex, data)
53+
if not match is None:
54+
return "%s.%s" % (match.group(1), type)
55+
return type
56+
57+
def run_java(self, cmd):
58+
scriptdir = os.path.dirname(os.path.abspath(__file__))
59+
proc = subprocess.Popen(
60+
cmd,
61+
cwd=scriptdir,
62+
shell=True,
63+
stdout=subprocess.PIPE
64+
)
65+
stdout, stderr = proc.communicate()
66+
return stdout
67+
68+
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))
70+
ret = [tuple(line.split(";")) for line in stdout.split("\n")[:-1]]
71+
return sorted(ret, key=lambda a: a[0])
72+
73+
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))
75+
return stdout.strip()
76+
77+
def on_query_completions(self, view, prefix, locations):
78+
line = view.substr(sublime.Region(view.full_line(locations[0]).begin(), locations[0]))
79+
before = line
80+
if len(prefix) > 0:
81+
before = line[:-len(prefix)]
82+
if re.search("[ \t]+$", before):
83+
before = ""
84+
elif re.search("\.$", before):
85+
# Member completion
86+
data = view.substr(sublime.Region(0, locations[0]))
87+
before = re.search("[^ \t]+\.$", before).group(0)
88+
89+
idx = before.find(".")
90+
var = before[:idx].strip()
91+
before = before[idx+1:]
92+
print "var is %s" % var
93+
t = self.find_type_of_variable(data, var)
94+
print "type is %s" % t
95+
t = self.find_absolute_of_type(data, t)
96+
print "absolute is %s" % (t)
97+
98+
idx = before.find(".")
99+
while idx != -1:
100+
sub = before[:idx]
101+
idx2 = sub.find("(")
102+
if idx2 >= 0:
103+
sub = sub[:idx2]
104+
105+
n = self.get_return_type(t, sub)
106+
print "%s.%s = %s" % (t, sub, n)
107+
t = n
108+
before = before[idx+1:]
109+
idx = before.find(".")
110+
111+
print "completing %s.%s" % (t, prefix)
112+
113+
return self.complete_class(t, prefix)
114+
115+
print "here"
116+
return []

0 commit comments

Comments
 (0)