public static String getBodyText(CompilationInfo cinfo, MethodTree tree) { SourcePositions sp = cinfo.getTrees().getSourcePositions(); BlockTree body = tree.getBody(); int start = (int) sp.getStartPosition(cinfo.getCompilationUnit(), body); int end = (int) sp.getEndPosition(cinfo.getCompilationUnit(), body); // get body text from source text return cinfo.getText().substring(start, end); }
private Pretty(CompilationInfo info, TreePath path, CodeStyle cs, int startOffset, int endOffset, boolean templateEdit) { this(info.getText(), info.getTokenHierarchy().tokenSequence(JavaTokenId.language()), path, info.getTrees().getSourcePositions(), cs, startOffset, endOffset, cs.getRightMargin()); this.templateEdit = templateEdit; }
public static String getArgumentSource(CompilationInfo cinfo, ExpressionTree exprTree) { long end = cinfo.getTrees().getSourcePositions().getEndPosition(cinfo.getCompilationUnit(), exprTree); long start = cinfo.getTrees().getSourcePositions().getStartPosition(cinfo.getCompilationUnit(), exprTree); return cinfo.getText().substring((int)start, (int)end); } }
int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), cls); int caret = context.getCaretLocation(); String code = context.getInfo().getText(); if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) { return null;
int startPos = (int) sourcePositions.getStartPosition(tp.getCompilationUnit(), method); int caret = context.getCaretLocation(); String code = context.getInfo().getText(); if (startPos < 0 || caret < 0 || caret < startPos || caret >= code.length()) { return null;
public static LinkedList<Diff> reformat(CompilationInfo info, TreePath path, CodeStyle cs, int startOffset, int endOffset, boolean templateEdit, int firstLineIndent) { Pretty pretty = new Pretty(info, path, cs, startOffset, endOffset, templateEdit); if (pretty.indent >= 0) { if (firstLineIndent >= 0) pretty.indent = pretty.lastIndent = firstLineIndent; pretty.scan(path, null); } if (path.getLeaf().getKind() == Tree.Kind.COMPILATION_UNIT) { CompilationUnitTree cut = (CompilationUnitTree) path.getLeaf(); List<? extends Tree> typeDecls = cut.getTypeDecls(); int size = typeDecls.size(); int cnt = size > 0 && org.netbeans.api.java.source.TreeUtilities.CLASS_TREE_KINDS.contains(typeDecls.get(size - 1).getKind()) ? cs.getBlankLinesAfterClass() : 1; if (cnt < 1) cnt = 1; String s = pretty.getNewlines(cnt); pretty.tokens.moveEnd(); pretty.tokens.movePrevious(); if (pretty.tokens.token().id() != WHITESPACE) { if (!pretty.tokens.token().text().toString().endsWith(s)) { String text = info.getText(); pretty.diffs.addFirst(new Diff(text.length(), text.length(), s)); } } else if (!s.contentEquals(pretty.tokens.token().text())) { pretty.diffs.addFirst(new Diff(pretty.tokens.offset(), pretty.tokens.offset() + pretty.tokens.token().length(), s)); } } return pretty.diffs; }
public DiffContext(CompilationInfo copy, Set<Tree> syntheticTrees) { this.tokenSequence = copy.getTokenHierarchy().tokenSequence(JavaTokenId.language()); this.mainCode = this.origText = copy.getText(); this.style = getCodeStyle(copy); this.context = JavaSourceAccessor.getINSTANCE().getJavacTask(copy).getContext(); this.mainUnit = this.origUnit = (JCCompilationUnit) copy.getCompilationUnit(); this.trees = copy.getTrees(); this.doc = copy.getSnapshot().getSource().getDocument(false); //TODO: true or false? this.positionConverter = copy.getPositionConverter(); this.file = copy.getFileObject(); this.syntheticTrees = syntheticTrees; this.textLength = copy.getSnapshot() == null ? Integer.MAX_VALUE : copy.getSnapshot().getOriginalOffset(copy.getSnapshot().getText().length()); this.blockSequences = new BlockSequences(this.tokenSequence, doc, textLength); this.forceInitialComment = false; }