1 package no.uio.ifi.refaktor.utils;
3 import no.uio.ifi.refaktor.utils.caching.CompilationUnitCacheManager;
5 import org.eclipse.jdt.core.ICompilationUnit;
6 import org.eclipse.jdt.core.JavaModelException;
7 import org.eclipse.jdt.core.dom.AST;
8 import org.eclipse.jdt.core.dom.ASTNode;
9 import org.eclipse.jdt.core.dom.ASTParser;
10 import org.eclipse.jdt.core.dom.CompilationUnit;
11 import org.eclipse.jdt.core.dom.NodeFinder;
12 import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener;
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.Document;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.ITextSelection;
17 import org.eclipse.jface.text.TextSelection;
19 @SuppressWarnings("restriction")
20 public class ParseUtils {
22 public static CompilationUnit parse(ICompilationUnit unit) {
23 return CompilationUnitCacheManager.getParsedCompilationUnitFor(unit);
26 public static CompilationUnit parseUncached(ICompilationUnit unit) {
27 ASTParser parser = createParser();
28 parser.setSource(unit);
29 return (CompilationUnit) parser.createAST(null);
32 public static CompilationUnit parse(IDocument document) {
33 return ParseUtils.parse(document.get().toCharArray());
36 private static CompilationUnit parse(char[] chars) {
37 ASTParser parser = ParseUtils.createParser();
38 parser.setSource(chars);
39 return (CompilationUnit) parser.createAST(null);
42 private static ASTParser createParser() {
43 ASTParser parser = ASTParser.newParser(AST.JLS4);
44 parser.setResolveBindings(true);
48 public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) {
49 final String text = getSelectedTextFromDocument(textSelection, document);
52 return null; // VS was here, TODO: return textSelection? EK was here
54 int lstrip = numberOfPrecedingWhitespaceCharacters(text);
55 int rstrip = numberOfSuccessiveWhitespaceCharacters(text);
57 int newOffset = textSelection.getOffset() + lstrip;
58 int newLength = textSelection.getLength() - (lstrip + rstrip);
60 return new TextSelection(document, newOffset, newLength);
63 private static String getSelectedTextFromDocument(TextSelection textSelection, IDocument document) {
65 return document.get(textSelection.getOffset(), textSelection.getLength());
66 } catch (BadLocationException e) {
67 throw new RuntimeException(e);
71 private static int numberOfPrecedingWhitespaceCharacters(final String text) {
73 while (lstrip < text.length() && Character.isWhitespace(text.charAt(lstrip)))
78 private static int numberOfSuccessiveWhitespaceCharacters(final String text) {
79 if (textContainsOnlyWhitespace(text))
83 int rIndex = text.length() - 1;
84 while (rstrip < text.length() && Character.isWhitespace(text.charAt(rIndex--)))
89 private static boolean textContainsOnlyWhitespace(final String text) {
90 return text.trim().length() == 0;
93 public static String getNodeDebugText(ASTNode node) {
94 NaiveASTFlattener printer = new NaiveASTFlattener();
96 return printer.getResult();
99 public static String getNodeText(ASTNode node) {
101 return getCompilationUnitAndGetTextFromSource(node);
102 } catch (JavaModelException e) {
103 throw new RuntimeException(e);
104 } catch (BadLocationException e) {
105 throw new RuntimeException(e);
109 private static String getCompilationUnitAndGetTextFromSource(ASTNode node)
110 throws JavaModelException, BadLocationException {
111 IDocument document = getDocument(getCompilationUnit(node));
112 return document.get(node.getStartPosition(), node.getLength());
115 private static CompilationUnit getCompilationUnit(ASTNode node) {
116 ASTNode rootNode = node.getRoot();
117 assert rootNode instanceof CompilationUnit;
118 return (CompilationUnit) rootNode;
121 private static IDocument getDocument(CompilationUnit compilationUnit) throws JavaModelException {
122 return new Document(compilationUnit.getTypeRoot().getSource());
125 public static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) {
126 return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength());