Skip to content

Commit e99245e

Browse files
committed
Send results to system.out instead of storing and then outputting. With that philosophy, don't cache everything in a map, either. Also, don't bother with non-under-cursor import.
1 parent 24479da commit e99245e

4 files changed

Lines changed: 43 additions & 75 deletions

File tree

SublimeJava.java

Lines changed: 38 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ private static boolean isPackage(String packageName)
282282
return false;
283283
}
284284

285-
private static Map<String, Set<String>> importMap = new HashMap<String, Set<String>>();
286285
private static final String CLASS_NAME_RE = "(?:$|.)?(\\w+).class";
287286
private static final String DIGITS_CLASS_NAME_RE = "\\d+";
288287
private static final String SUBLIME_JAVA_CLASS_RE = "SublimeJava";
@@ -295,27 +294,24 @@ private static boolean isPackage(String packageName)
295294
sublimeJavaClassPattern = Pattern.compile(SUBLIME_JAVA_CLASS_RE);
296295
}
297296

298-
private static void addToImportMap(String classFileName)
297+
private static String getImport(String classFileName, String searchClass)
299298
{
300299
Matcher classnameMatcher = classnamePattern.matcher(classFileName);
301300
if (classnameMatcher.find())
302301
{
303302
String classname = classnameMatcher.group(1);
304-
Matcher digitsClassnameMatcher = digitsClassnamePattern.matcher(classname);
305-
if (!digitsClassnameMatcher.find())
303+
if ((searchClass != null && searchClass.equals(classname)) ||
304+
(searchClass == null && !digitsClassnamePattern.matcher(classname).matches()))
306305
{
307-
if (!importMap.containsKey(classname))
308-
{
309-
importMap.put(classname, new HashSet<String>());
310-
}
311306
String fullClassname = classFileName.replace("/", ".").replace(".class", "");
312307
if (fullClassname.startsWith("."))
313308
{
314309
fullClassname = fullClassname.substring(1);
315310
}
316-
importMap.get(classname).add(fullClassname);
311+
return fullClassname;
317312
}
318313
}
314+
return null;
319315
}
320316

321317
private static String[] getClasspathEntries()
@@ -356,90 +352,65 @@ private static URL getUrlFromClasspathEntry(ClassLoader classLoader, String clas
356352
return url;
357353
}
358354

359-
private static Set<File> getClassFilesNotInJar(File current)
355+
private static void printImportsNotInJar(File current, File root, String classname, Set<String> possibleImports)
360356
{
361-
Set<File> classFiles = new HashSet<File>();
362-
if (current.isFile() && current.getName().endsWith(".class"))
357+
Matcher classnameMatcher = classnamePattern.matcher(current.getName());
358+
if (current.isFile() && classnameMatcher.matches())
363359
{
364-
classFiles.add(current);
360+
if (classname == null || classname.equals(classnameMatcher.group(1)))
361+
{
362+
String classFileName = current.getAbsolutePath().substring(root.getAbsolutePath().length());
363+
printPossibleImport(getImport(classFileName, classname), possibleImports);
364+
}
365365
}
366366
else if (current.isDirectory())
367367
{
368368
for (File file : current.listFiles())
369369
{
370-
classFiles.addAll(getClassFilesNotInJar(file));
370+
printImportsNotInJar(file, root, classname, possibleImports);
371371
}
372372
}
373-
return classFiles;
374373
}
375374

376375
private static void getPossibleImports(String classname)
377376
throws IOException
378377
{
379-
boolean importMapPopulated = importMap.size() > 0;
380-
381-
if (!importMapPopulated)
378+
Set<String> possibleImports = new HashSet<String>();
379+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
380+
for (String s : getClasspathEntries())
382381
{
383-
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
384-
for (String s : getClasspathEntries())
382+
URL url = getUrlFromClasspathEntry(classLoader, s, "");
383+
if (url == null)
384+
continue;
385+
System.err.println("s: " + s);
386+
System.err.println("url: " + url);
387+
388+
String filename = URLDecoder.decode(url.getFile(), "UTF-8");
389+
if (url.getProtocol().equals("jar"))
385390
{
386-
URL url = getUrlFromClasspathEntry(classLoader, s, "");
387-
if (url == null)
388-
continue;
389-
System.err.println("s: " + s);
390-
System.err.println("url: " + url);
391+
filename = filename.substring(5, filename.indexOf("!"));
391392

392-
String filename = URLDecoder.decode(url.getFile(), "UTF-8");
393-
if (url.getProtocol().equals("jar"))
393+
JarFile jf = new JarFile(filename);
394+
Enumeration<JarEntry> entries = jf.entries();
395+
while (entries.hasMoreElements())
394396
{
395-
filename = filename.substring(5, filename.indexOf("!"));
397+
String imp = getImport(entries.nextElement().getName(), classname);
398+
printPossibleImport(imp, possibleImports);
396399

397-
JarFile jf = new JarFile(filename);
398-
Enumeration<JarEntry> entries = jf.entries();
399-
while (entries.hasMoreElements())
400-
{
401-
addToImportMap(entries.nextElement().getName());
402-
}
403-
}
404-
else
405-
{
406-
File folder = new File(filename);
407-
File[] classFiles = getClassFilesNotInJar(folder).toArray(new File[0]);
408-
for (File classFile : classFiles)
409-
{
410-
addToImportMap(classFile.getAbsolutePath().substring(folder.getAbsolutePath().length()));
411-
}
412400
}
413401
}
414-
}
415-
416-
if (classname != null)
417-
{
418-
Set<String> possibleImports = importMap.get(classname);
419-
if (possibleImports != null)
420-
{
421-
printImports(possibleImports);
422-
}
423-
}
424-
else
425-
{
426-
for (Set<String> imports : importMap.values())
402+
else
427403
{
428-
printImports(imports);
404+
File folder = new File(filename);
405+
printImportsNotInJar(folder, folder, classname, possibleImports);
429406
}
430407
}
431-
432408
}
433409

434-
private static void printImports(Set<String> imports)
410+
private static void printPossibleImport(String fullClassname, Set<String> possibleImports)
435411
{
436-
for (String impClass : imports)
437-
{
438-
if (!sublimeJavaClassPattern.matcher(impClass).matches())
439-
{
440-
System.out.println(impClass);
441-
}
442-
}
412+
if (fullClassname != null && possibleImports.add(fullClassname))
413+
System.out.println(fullClassname);
443414
}
444415

445416
private static void completePackage(String packageName)
@@ -588,7 +559,7 @@ else if (args[0].equals("-findclass"))
588559
for (String pack : packages)
589560
{
590561
String classname = getClassname(pack, args[1]);
591-
while (!found && classname.indexOf('.') != -1)
562+
while (!found && classname != null && classname.indexOf('.') != -1)
592563
{
593564
int idx = classname.lastIndexOf('.');
594565
classname = classname.substring(0, idx) + "$" + classname.substring(idx+1);

SublimeJava.sublime-commands

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

classopener.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import webbrowser
3+
import sublime
34

45
path_to_full = lambda path: '.'.join(path.split('/'))
56
remove_dollar = lambda classname: classname.replace('$$', '.')
@@ -29,8 +30,10 @@ def do_open(result):
2930

3031
if classname is not None and len(options) == 1:
3132
do_open(0)
32-
else:
33+
elif len(options) > 1:
3334
self.window.show_quick_panel([t[0] for t in options], do_open)
35+
else:
36+
sublime.message_dialog("No classes could be found. Check your %s project setting." % self.setting_name)
3437

3538
def _scan_dir(self, base, classname_to_find=None):
3639
return []

sublimejava.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ def do_import(index):
158158
if index != -1:
159159
self._insert_import(imports[index], edit)
160160

161-
if len(imports) == 1:
162-
do_import(0)
163-
elif len(imports) > 1:
161+
if len(imports) > 0:
164162
view.window().show_quick_panel(imports, do_import)
165163
else:
166164
sublime.error_message("No classes found to import for name %s" % classname)

0 commit comments

Comments
 (0)