public static ImportInfo getImportsTipperStr(String trimmedLine, boolean returnEvenEmpty) {
String importMsg = "";
if (!trimmedLine.startsWith("from") && !trimmedLine.startsWith("import")) {
return new ImportInfo("", false, false); // it is not an import
}
int fromIndex = trimmedLine.indexOf("from");
int importIndex = trimmedLine.indexOf("import");
boolean foundImportOnArray = false;
boolean foundFromOnArray = false;
// check if we have a from or an import.
if (fromIndex != -1 || importIndex != -1) {
trimmedLine = trimmedLine.replaceAll("#.*", ""); // remove comments
String[] strings = trimmedLine.split(" ");
if (fromIndex != -1 && importIndex == -1) {
if (strings.length > 2) {
// user has spaces as in 'from xxx uuu'
return new ImportInfo("", foundImportOnArray, foundFromOnArray);
}
}
for (int i = 0; i < strings.length; i++) {
if (strings[i].equals("import")) {
foundImportOnArray = true;
}
if (strings[i].equals("from")) {
foundFromOnArray = true;
}
if (strings[i].equals("from") == false && strings[i].equals("import") == false) {
if (importMsg.length() != 0) {
importMsg += '.';
}
importMsg += strings[i];
}
// now, if we have a from xxx import something, we'll always
// want to return only the xxx
if (fromIndex != -1 && importIndex == -1 && (foundImportOnArray || i == strings.length - 1)) {
if (importMsg.length() == 0) {
return ImportsSelection.doExistingOrEmptyReturn(returnEvenEmpty, importMsg, foundImportOnArray,
foundFromOnArray);
}
if (importMsg.startsWith(".")) {
return new ImportInfo(importMsg, foundImportOnArray, foundFromOnArray);
}
if (importMsg.indexOf(".") == -1) {
return ImportsSelection.doExistingOrEmptyReturn(returnEvenEmpty, importMsg, foundImportOnArray,
foundFromOnArray);
}
return new ImportInfo(importMsg.substring(0, importMsg.lastIndexOf(".") + 1), foundImportOnArray,
foundFromOnArray);
}
}
if (fromIndex != -1 && importIndex != -1) {
if (strings.length == 3) {
importMsg += '.';
}
}
} else {
return new ImportInfo("", foundImportOnArray, foundFromOnArray);
}
if (importMsg.indexOf(".") == -1) {
// we have only import fff or from iii (so, we're going for all
// imports).
return ImportsSelection.doExistingOrEmptyReturn(returnEvenEmpty, importMsg, foundImportOnArray,
foundFromOnArray);
}
if (fromIndex == -1 && importMsg.indexOf(',') != -1) {
// we have something like import xxx, yyy, ...
importMsg = importMsg.substring(importMsg.lastIndexOf(',') + 1, importMsg.length());
if (importMsg.startsWith(".")) {
importMsg = importMsg.substring(1);
}
int j = importMsg.lastIndexOf('.');
if (j != -1) {
importMsg = importMsg.substring(0, j);
return new ImportInfo(importMsg, foundImportOnArray, foundFromOnArray);
} else {
return ImportsSelection.doExistingOrEmptyReturn(returnEvenEmpty, importMsg, foundImportOnArray,
foundFromOnArray);
}
} else {
// now, we may still have something like 'unittest.test,' or
// 'unittest.test.,'
// so, we have to remove this comma (s).
int i;
boolean removed = false;
while ((i = importMsg.indexOf(',')) != -1) {
if (importMsg.charAt(i - 1) == '.') {
int j = importMsg.lastIndexOf('.');
importMsg = importMsg.substring(0, j);
}
int j = importMsg.lastIndexOf('.');
importMsg = importMsg.substring(0, j);
removed = true;
}
// if it is something like aaa.sss.bb : removes the bb because it is
// the qualifier
// if it is something like aaa.sss. : removes only the last point
if (!removed && importMsg.length() > 0 && importMsg.indexOf('.') != -1) {
importMsg = importMsg.substring(0, importMsg.lastIndexOf('.'));
}
return new ImportInfo(importMsg, foundImportOnArray, foundFromOnArray);
}
}