Skip to content

Commit 230e1af

Browse files
committed
command to import class under cursor. pull in get_class_under_cursor from open_java_class branch and change it to get active view rather than passing it. getting of classpath entries is completely refactored into separate method. get class files not in jars. for consistency, command on repl and everything involved is some variation of "get_possible_imports".
1 parent d7b42ec commit 230e1af

3 files changed

Lines changed: 81 additions & 38 deletions

File tree

SublimeJava.java

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,11 @@ private static boolean isPackage(String packageName)
242242
Package p = Package.getPackage(packageName);
243243
if (p != null)
244244
return true;
245-
ArrayList<String> paths = new ArrayList<String>();
246-
paths.add("java/lang/String.class");
247-
for (String s : System.getProperty("java.class.path").split(System.getProperty("path.separator")))
248-
{
249-
if (!paths.contains(s))
250-
paths.add(s);
251-
}
252245

253246
packageName = packageName.replace(".", "/");
254247

255248
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
256-
for (String s : paths)
249+
for (String s : getClasspathEntries())
257250
{
258251
URL url = classLoader.getResource(s + "/" + packageName);
259252
if (url != null)
@@ -322,7 +315,7 @@ private static void addToImportMap(String classFileName)
322315
}
323316
}
324317

325-
protected static String[] getClasspathEntries()
318+
private static String[] getClasspathEntries()
326319
{
327320
Set<String> paths = new HashSet<String>();
328321
paths.add("java/lang/String.class");
@@ -333,7 +326,7 @@ protected static String[] getClasspathEntries()
333326
return paths.toArray(new String[0]);
334327
}
335328

336-
protected static URL getUrlFromClasspathEntry(ClassLoader classLoader, String classpathEntry, String packagePath)
329+
private static URL getUrlFromClasspathEntry(ClassLoader classLoader, String classpathEntry, String packagePath)
337330
{
338331
URL url = null;
339332
if (classpathEntry.endsWith(".class"))
@@ -360,16 +353,31 @@ protected static URL getUrlFromClasspathEntry(ClassLoader classLoader, String cl
360353
return url;
361354
}
362355

363-
protected static void importClass(String classname)
356+
private static Set<File> getClassFilesNotInJar(File current)
357+
{
358+
Set<File> classFiles = new HashSet<File>();
359+
if (current.isFile() && current.getName().endsWith(".class"))
360+
{
361+
classFiles.add(current);
362+
}
363+
else if (current.isDirectory())
364+
{
365+
for (File file : current.listFiles())
366+
{
367+
classFiles.addAll(getClassFilesNotInJar(file));
368+
}
369+
}
370+
return classFiles;
371+
}
372+
373+
private static void getPossibleImports(String classname)
364374
throws IOException
365375
{
366376
boolean importMapPopulated = importMap.size() > 0;
367377

368378
if (!importMapPopulated) {
369-
String[] paths = getClasspathEntries();
370-
371379
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
372-
for (String s : paths)
380+
for (String s : getClasspathEntries())
373381
{
374382
URL url = getUrlFromClasspathEntry(classLoader, s, "");
375383
if (url == null)
@@ -406,37 +414,18 @@ protected static void importClass(String classname)
406414
{
407415
for (String impClass : possibleImports)
408416
{
409-
System.out.println(impClass + "\tclass" + sep + impClass);
417+
System.out.println(impClass);
410418
}
411419
}
412420
}
413421

414-
protected static Set<File> getClassFilesNotInJar(File current)
415-
{
416-
Set<File> classFiles = new HashSet<File>();
417-
if (current.isFile() && current.getName().endsWith(".class"))
418-
{
419-
classFiles.add(current);
420-
}
421-
else if (current.isDirectory())
422-
{
423-
for (File file : current.listFiles())
424-
{
425-
classFiles.addAll(getClassFilesNotInJar(file));
426-
}
427-
}
428-
return classFiles;
429-
}
430-
431-
protected static void completePackage(String packageName)
422+
private static void completePackage(String packageName)
432423
throws IOException
433424
{
434-
String[] paths = getClasspathEntries();
435-
436425
String packagePath = packageName.replace(".", "/");
437426

438427
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
439-
for (String s : paths)
428+
for (String s : getClasspathEntries())
440429
{
441430
URL url = getUrlFromClasspathEntry(classLoader, s, packagePath);
442431
if (url == null)
@@ -528,9 +517,9 @@ public static void main(String... unusedargs)
528517
System.err.println("quitting upon request");
529518
return;
530519
}
531-
else if (args[0].equals("-importclass"))
520+
else if (args[0].equals("-possibleimports"))
532521
{
533-
importClass(args[1]);
522+
getPossibleImports(args[1]);
534523
continue;
535524
}
536525
else if (args[0].equals("-findclass"))

SublimeJava.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
[
2+
{
3+
"caption": "Import Java Class Under Cursor",
4+
"command": "import_java_class"
5+
}
26
]

sublimejava.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ def return_completions(self, comp):
110110
ret.append((self.fixnames(display), self.fixnames(insert)))
111111
return super(SublimeJavaCompletion, self).return_completions(ret)
112112

113+
def get_class_under_cursor(self):
114+
view = sublime.active_window().active_view()
115+
data = view.substr(sublime.Region(0,view.size()))
116+
word = view.substr(view.word(view.sel()[0].begin()))
117+
return self.find_absolute_of_type(data, data, word)
118+
119+
def get_possible_imports(self, classname):
120+
imports = []
121+
if self.get_class_under_cursor() == classname:
122+
stdout = self.run_completion("-possibleimports;;--;;%s" % classname)
123+
imports = sorted(stdout.split("\n")[:-1])
124+
return imports
125+
126+
113127
comp = SublimeJavaCompletion()
114128

115129

@@ -125,3 +139,39 @@ def on_query_context(self, view, key, operator, operand, match_all):
125139
return comp.is_supported_language(view)
126140
else:
127141
return comp.on_query_context(view, key, operator, operand, match_all)
142+
143+
144+
class ImportJavaClassCommand(sublime_plugin.TextCommand):
145+
146+
def run(self, edit):
147+
view = self.view
148+
classname = view.substr(view.word(view.sel()[0].begin()))
149+
imports = comp.get_possible_imports(classname);
150+
151+
def do_import(index):
152+
self._insert_import(imports[index], edit)
153+
154+
if len(imports) == 1:
155+
do_import(0)
156+
elif len(imports) > 1:
157+
view.window().show_quick_panel(imports, do_import)
158+
else:
159+
sublime.error_message("No classes found to import for name %s" % classname)
160+
161+
def _insert_import(self, full_classname, edit):
162+
insert_point = 0
163+
newlines_prepend = 0
164+
newlines_append = 1
165+
all_imports = self.view.find_all('import( static)? ([\w\.]+)\.([\w]+|\*);')
166+
if len(all_imports) > 0:
167+
insert_point = all_imports[-1].b
168+
newlines_prepend = 1
169+
newlines_append = 0
170+
else:
171+
package_dec = self.view.find('package ([\w]+.)*\w+;', 0)
172+
if package_dec is not None:
173+
insert_point = package_dec.b
174+
newlines_prepend = 2
175+
newlines_append = 0
176+
import_statement = "%simport %s;%s" % ("\n" * newlines_prepend, full_classname.replace("$", "."), "\n" * newlines_append)
177+
self.view.insert(edit, insert_point, import_statement)

0 commit comments

Comments
 (0)