Search in sources :

Example 1 with LineMap

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;
}
Also used : LineMap(com.sun.source.tree.LineMap) URI(java.net.URI) File(java.io.File)

Example 2 with LineMap

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();
}
Also used : Scanner(java.util.Scanner) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) MethodTree(com.sun.source.tree.MethodTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) BlockTree(com.sun.source.tree.BlockTree) StatementTree(com.sun.source.tree.StatementTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) LineMap(com.sun.source.tree.LineMap)

Aggregations

LineMap (com.sun.source.tree.LineMap)2 BlockTree (com.sun.source.tree.BlockTree)1 ClassTree (com.sun.source.tree.ClassTree)1 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)1 MethodTree (com.sun.source.tree.MethodTree)1 StatementTree (com.sun.source.tree.StatementTree)1 Tree (com.sun.source.tree.Tree)1 File (java.io.File)1 URI (java.net.URI)1 Scanner (java.util.Scanner)1