in codeanalyzer/src/main/java/nl/obren/sokrates/sourcecode/lang/vb/VisualBasicHeuristicUnitsExtractor.java [23:61]
public List<UnitInfo> extractUnits(SourceFile sourceFile) {
List<UnitInfo> units = new ArrayList<>();
CommentsAndEmptyLinesCleaner cleaner = analyzer.getCommentsAndEmptyLinesCleaner();
List<String> lines = Arrays.asList(cleaner.cleanKeepEmptyLines(sourceFile.getContent()).split("\n"));
List<String> unitEnds = Arrays.asList("End Sub", "End Function");
int loc = 0;
int endLine = 0;
for (int i = lines.size() - 1; i > 0; i--) {
String line = lines.get(i);
for (String unitEnd : unitEnds) {
if (line.trim().equalsIgnoreCase(unitEnd)) {
endLine = i;
loc = 0;
String prefix = line.substring(0, line.indexOf(unitEnd));
String body = line;
String cleanedBody = line;
i--;
while (i >= 0) {
line = lines.get(i);
body = line + "\n" + body;
if (line.length() > prefix.length() && line.startsWith(prefix) && line.charAt(prefix.length()) != ' ') {
units.add(0, getUnitInfo(sourceFile, loc, endLine, i, line, body, cleanedBody));
break;
} else {
if (!line.trim().isEmpty()) {
cleanedBody = line + "\n" + cleanedBody;
loc += 1;
}
}
i--;
}
}
}
}
return units;
}