Implemented code folder imports. Fixes #3732

This commit is contained in:
Manindra Moharana
2015-09-13 23:58:00 -07:00
parent 0c3364d00f
commit ff039cf482
3 changed files with 74 additions and 29 deletions

View File

@@ -1308,6 +1308,19 @@ public class ASTGenerator {
//log("Doesn't exist in imp package: " + impS.getImportName());
}
for (ImportStatement impS : errorCheckerService.codeFolderImports) {
String temp = impS.getPackageName();
if (impS.isStarredImport() && className.indexOf('.') == -1) {
temp = impS.getPackageName() + "." + className;
}
tehClass = loadClass(temp);
if (tehClass != null) {
log(tehClass.getName() + " located.");
return tehClass;
}
//log("Doesn't exist in (code folder) imp package: " + impS.getImportName());
}
PdePreprocessor p = new PdePreprocessor(null);
for (String impS : p.getCoreImports()) {
tehClass = loadClass(impS.substring(0,impS.length()-1) + className);
@@ -3512,6 +3525,13 @@ public class ASTGenerator {
return false;
}
}
for (ImportStatement impS : errorCheckerService.codeFolderImports) {
if (impName.toLowerCase().startsWith(impS.getPackageName().toLowerCase())) {
return false;
}
}
if (JavaMode.suggestionsMap == null
|| JavaMode.suggestionsMap.keySet().size() == 0) {
log("SuggestionsMap is null or empty, won't be able to trim class names");

View File

@@ -202,6 +202,11 @@ public class ErrorCheckerService implements Runnable {
*/
protected ArrayList<ImportStatement> previousImports = new ArrayList<ImportStatement>();
/**
* List of import statements for any .jar files in the code folder.
*/
protected ArrayList<ImportStatement> codeFolderImports = new ArrayList<ImportStatement>();
/**
* Teh Preprocessor
*/
@@ -243,7 +248,7 @@ public class ErrorCheckerService implements Runnable {
initParser();
//initializeErrorWindow();
xqpreproc = new XQPreprocessor();
xqpreproc = new XQPreprocessor(this);
PdePreprocessor pdePrepoc = new PdePreprocessor(null);
defaultImportsOffset = pdePrepoc.getCoreImports().length +
pdePrepoc.getDefaultImports().length + 1;
@@ -652,6 +657,10 @@ public class ErrorCheckerService implements Runnable {
loadCompClass = false;
}
// for(URL cpUrl: classPath) {
// Messages.log("CP jar: " + cpUrl.getPath());
// }
if (compilerSettings == null) {
prepareCompilerSetting();
}
@@ -844,37 +853,36 @@ public class ErrorCheckerService implements Runnable {
classpathJars.add(new File(pathItem).toURI().toURL());
}
} catch (Exception e) {
if (library == null && !codeFolderChecked) {
// Look around in the code folder for jar files
if (editor.getSketch().hasCodeFolder()) {
File codeFolder = editor.getSketch().getCodeFolder();
Messages.log("Encountered " + e + " while adding library to classpath");
}
}
// get a list of .jar files in the "code" folder
// (class files in subfolders should also be picked up)
String codeFolderClassPath = Util.contentsToClassPath(codeFolder);
codeFolderChecked = true;
// huh? doesn't this mean .length() == 0? [fry]
if (codeFolderClassPath.equalsIgnoreCase("")) {
String message = String.format("Cannot find \"%s\" library in code folder. Line %d in tab %s%n",
entry, impstat.getLineNumber(),
editor.getSketch().getCode(impstat.getTab()).getPrettyName());
Messages.log(message);
} else {
String codeFolderPath[] =
// Look around in the code folder for jar files and them too
if (editor.getSketch().hasCodeFolder()) {
File codeFolder = editor.getSketch().getCodeFolder();
// get a list of .jar files in the "code" folder
// (class files in subfolders should also be picked up)
String codeFolderClassPath = Util.contentsToClassPath(codeFolder);
codeFolderChecked = true;
// huh? doesn't this mean .length() == 0? [fry]
if (!codeFolderClassPath.equalsIgnoreCase("")) {
Messages.log("Sketch has a code folder. Adding its jars");
String codeFolderPath[] =
PApplet.split(codeFolderClassPath.substring(1).trim(),
File.pathSeparatorChar);
try {
for (String pathItem : codeFolderPath) {
classpathJars.add(new File(pathItem).toURI().toURL());
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
File.pathSeparatorChar);
try {
for (String pathItem : codeFolderPath) {
classpathJars.add(new File(pathItem).toURI().toURL());
Messages.log("Addind cf jar: " + pathItem);
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
// Also add jars specified in mode's search path

View File

@@ -20,6 +20,7 @@
package processing.mode.java.pdex;
import java.io.File;
import java.util.List;
import java.util.Map;
@@ -38,6 +39,8 @@ import org.eclipse.jface.text.Document;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
import processing.app.Messages;
import processing.app.Util;
import processing.data.StringList;
import processing.mode.java.preproc.PdePreprocessor;
@@ -50,12 +53,13 @@ import processing.mode.java.preproc.PdePreprocessor;
public class XQPreprocessor {
private ASTRewrite rewrite = null;
private List<ImportStatement> extraImports;
private ErrorCheckerService ecs;
private String[] coreImports;
private String[] defaultImports;
public XQPreprocessor() {
public XQPreprocessor(ErrorCheckerService errorCheckerService) {
ecs = errorCheckerService;
PdePreprocessor p = new PdePreprocessor(null);
defaultImports = p.getDefaultImports();
coreImports = p.getCoreImports();
@@ -111,6 +115,19 @@ public class XQPreprocessor {
for (String imp : defaultImports) {
imports.append("import " + imp + ";");
}
if (ecs.getEditor().getSketch().getCodeFolder().exists()) {
StringList codeFolderPackages = null;
String codeFolderClassPath = Util.contentsToClassPath(ecs.getEditor().getSketch().getCodeFolder());
codeFolderPackages = Util.packageListFromClassPath(codeFolderClassPath);
if (codeFolderPackages != null) {
ecs.codeFolderImports.clear();
for (String item : codeFolderPackages) {
// Messages.log("CF import " + item);
imports.append("import " + item + ".*;");
ecs.codeFolderImports.add(new ImportStatement("import " + item + ".*;",0,0));
}
}
}
return imports.join("\n") + "\n";
}