1 package no.uio.ifi.refaktor.utils;
3 import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
6 import org.eclipse.jdt.core.ICompilationUnit;
7 import org.eclipse.jdt.core.JavaModelException;
8 import org.eclipse.jdt.core.dom.AST;
9 import org.eclipse.jdt.core.dom.ASTNode;
10 import org.eclipse.jdt.core.dom.ASTParser;
11 import org.eclipse.jdt.core.dom.CompilationUnit;
12 import org.eclipse.jdt.core.dom.NodeFinder;
13 import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.Document;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.ITextSelection;
18 import org.eclipse.jface.text.TextSelection;
20 @SuppressWarnings("restriction")
21 public class ParseUtils {
23 public static CompilationUnit parse(ICompilationUnit unit) {
24 return CompilationUnitCacheManager.getParsedCompilationUnitFor(unit);
27 public static CompilationUnit parse(IDocument document) {
28 return ParseUtils.parse(document.get().toCharArray());
31 private static CompilationUnit parse(char[] chars) {
32 ASTParser parser = ParseUtils.createParser();
33 parser.setSource(chars);
34 return (CompilationUnit) parser.createAST(null);
37 public static ASTParser createParser() {
38 ASTParser parser = ASTParser.newParser(AST.JLS4);
39 parser.setKind(ASTParser.K_COMPILATION_UNIT);
40 parser.setResolveBindings(true);
44 public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) {
45 final String text = getSelectedTextFromDocument(textSelection, document);
48 return null; // VS was here, TODO: return textSelection? EK was here
50 int lstrip = numberOfPrecedingWhitespaceCharacters(text);
51 int rstrip = numberOfSuccessiveWhitespaceCharacters(text);
53 int newOffset = textSelection.getOffset() + lstrip;
54 int newLength = textSelection.getLength() - (lstrip + rstrip);
56 return new TextSelection(document, newOffset, newLength);
59 private static String getSelectedTextFromDocument(TextSelection textSelection, IDocument document) {
61 return document.get(textSelection.getOffset(), textSelection.getLength());
62 } catch (BadLocationException e) {
63 throw new RuntimeException(e);
67 private static int numberOfPrecedingWhitespaceCharacters(final String text) {
69 while (lstrip < text.length() && Character.isWhitespace(text.charAt(lstrip)))
74 private static int numberOfSuccessiveWhitespaceCharacters(final String text) {
75 if (textContainsOnlyWhitespace(text))
79 int rIndex = text.length() - 1;
80 while (rstrip < text.length() && Character.isWhitespace(text.charAt(rIndex--)))
85 private static boolean textContainsOnlyWhitespace(final String text) {
86 return text.trim().length() == 0;
89 public static String getNodeDebugText(ASTNode node) {
90 NaiveASTFlattener printer = new NaiveASTFlattener();
92 return printer.getResult();
95 public static String getNodeText(ASTNode node) {
97 return getCompilationUnitAndGetTextFromSource(node);
98 } catch (JavaModelException e) {
99 throw new RuntimeException(e);
100 } catch (BadLocationException e) {
101 throw new RuntimeException(e);
105 private static String getCompilationUnitAndGetTextFromSource(ASTNode node)
106 throws JavaModelException, BadLocationException {
107 IDocument document = getDocument(getCompilationUnit(node));
108 return document.get(node.getStartPosition(), node.getLength());
111 private static CompilationUnit getCompilationUnit(ASTNode node) {
112 ASTNode rootNode = node.getRoot();
113 assertThat(rootNode, instanceOf(CompilationUnit.class));
114 return (CompilationUnit) rootNode;
117 private static IDocument getDocument(CompilationUnit compilationUnit) throws JavaModelException {
118 return new Document(compilationUnit.getTypeRoot().getSource());
121 static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) {
122 return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength());