use of com.sun.source.tree.LineMap in project meghanada-server by mopemope.
the class TreeAnalyzer method analyzeUnit.
private static Source analyzeUnit(CompilationUnitTree cut, Set<File> errorFiles) throws IOException {
LineMap lineMap = cut.getLineMap();
URI uri = cut.getSourceFile().toUri();
File file = new File(uri.normalize());
String path = file.getCanonicalPath();
Source source = new Source(path, lineMap);
if (errorFiles.contains(file)) {
source.hasCompileError = true;
}
SourceContext context = new SourceContext(source);
analyzeCompilationUnitTree(context, cut);
source.resetLineRange();
return source;
}
use of com.sun.source.tree.LineMap in project vertx-docgen by vert-x3.
the class JavaDocGenerator method renderSource.
public String renderSource(TreePath path, List<? extends Tree> trees, String source) {
CompilationUnitTree unit = path.getCompilationUnit();
int from = (int) docTrees.getSourcePositions().getStartPosition(unit, trees.get(0));
int to = (int) docTrees.getSourcePositions().getEndPosition(unit, trees.get(trees.size() - 1));
// Correct boundaries
while (from > 1 && source.charAt(from - 1) != '\n') {
from--;
}
while (to < source.length() && source.charAt(to) != '\n') {
to++;
}
String block = source.substring(from, to);
// Determine margin
int blockMargin = Integer.MAX_VALUE;
LineMap lineMap = unit.getLineMap();
for (Tree statement : trees) {
int statementStart = (int) docTrees.getSourcePositions().getStartPosition(unit, statement);
int lineStart = statementStart;
while (lineMap.getLineNumber(statementStart) == lineMap.getLineNumber(lineStart - 1)) {
lineStart--;
}
blockMargin = Math.min(blockMargin, statementStart - lineStart);
}
// Crop the fragment
StringBuilder fragment = new StringBuilder();
for (Iterator<String> sc = new Scanner(block).useDelimiter("\n"); sc.hasNext(); ) {
String line = sc.next();
int margin = Math.min(blockMargin, line.length());
line = line.substring(margin);
fragment.append(line);
if (sc.hasNext()) {
fragment.append('\n');
}
}
return fragment.toString();
}
Aggregations