package no.uio.ifi.refaktor.utils; import static no.uio.ifi.refaktor.RefaktorAssert.assertThat; import static org.hamcrest.CoreMatchers.instanceOf; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.NodeFinder; import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.Document; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.TextSelection; @SuppressWarnings("restriction") public class ParseUtils { public static CompilationUnit parse(ICompilationUnit unit) { return CompilationUnitCacheManager.getParsedCompilationUnitFor(unit); } public static CompilationUnit parse(IDocument document) { return ParseUtils.parse(document.get().toCharArray()); } private static CompilationUnit parse(char[] chars) { ASTParser parser = ParseUtils.createParser(); parser.setSource(chars); return (CompilationUnit) parser.createAST(null); } public static ASTParser createParser() { ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); return parser; } public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) { final String text = getSelectedTextFromDocument(textSelection, document); if (text.isEmpty()) return null; // VS was here, TODO: return textSelection? EK was here int lstrip = numberOfPrecedingWhitespaceCharacters(text); int rstrip = numberOfSuccessiveWhitespaceCharacters(text); int newOffset = textSelection.getOffset() + lstrip; int newLength = textSelection.getLength() - (lstrip + rstrip); return new TextSelection(document, newOffset, newLength); } private static String getSelectedTextFromDocument(TextSelection textSelection, IDocument document) { try { return document.get(textSelection.getOffset(), textSelection.getLength()); } catch (BadLocationException e) { throw new RuntimeException(e); } } private static int numberOfPrecedingWhitespaceCharacters(final String text) { int lstrip = 0; while (lstrip < text.length() && Character.isWhitespace(text.charAt(lstrip))) lstrip++; return lstrip; } private static int numberOfSuccessiveWhitespaceCharacters(final String text) { if (textContainsOnlyWhitespace(text)) return 0; int rstrip = 0; int rIndex = text.length() - 1; while (rstrip < text.length() && Character.isWhitespace(text.charAt(rIndex--))) rstrip++; return rstrip; } private static boolean textContainsOnlyWhitespace(final String text) { return text.trim().length() == 0; } public static String getNodeDebugText(ASTNode node) { NaiveASTFlattener printer = new NaiveASTFlattener(); node.accept(printer); return printer.getResult(); } public static String getNodeText(ASTNode node) { try { return getCompilationUnitAndGetTextFromSource(node); } catch (JavaModelException e) { throw new RuntimeException(e); } catch (BadLocationException e) { throw new RuntimeException(e); } } private static String getCompilationUnitAndGetTextFromSource(ASTNode node) throws JavaModelException, BadLocationException { IDocument document = getDocument(getCompilationUnit(node)); return document.get(node.getStartPosition(), node.getLength()); } private static CompilationUnit getCompilationUnit(ASTNode node) { ASTNode rootNode = node.getRoot(); assertThat(rootNode, instanceOf(CompilationUnit.class)); return (CompilationUnit) rootNode; } private static IDocument getDocument(CompilationUnit compilationUnit) throws JavaModelException { return new Document(compilationUnit.getTypeRoot().getSource()); } static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) { return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength()); } }