]> git.uio.no Git - ifi-stolz-refaktor.git/blobdiff - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/ParseUtils.java
CompilationUnitCacheManager: refactoring
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / ParseUtils.java
index be43b4e2ca003ac1e7e3a659184bf3cfa5e8a1a4..84e90ab74cdae705ac7840ef0b650af79b94c15f 100644 (file)
@@ -1,40 +1,46 @@
 package no.uio.ifi.refaktor.utils;
 
+import static no.uio.ifi.refaktor.assertion.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) {
-               ASTParser parser = ParseUtils.createParser();
+               return CompilationUnitCacheManager.getParsedCompilationUnitFor(unit);
+       }
+
+       static CompilationUnit parseUncached(ICompilationUnit unit) {
+               ASTParser parser = createParser();
                parser.setSource(unit);
                return (CompilationUnit) parser.createAST(null);
        }
 
        public static CompilationUnit parse(IDocument document) {
-               return ParseUtils.parse(document.get());
+               return ParseUtils.parse(document.get().toCharArray());
        }
 
-       public static CompilationUnit parse(String text) {
-               return ParseUtils.parse(text.toCharArray());
-       }
-
-       public static CompilationUnit parse(char[] chars) {
+       private static CompilationUnit parse(char[] chars) {
                ASTParser parser = ParseUtils.createParser();
                parser.setSource(chars);
                return (CompilationUnit) parser.createAST(null);
        }
 
-       public static ASTParser createParser() {
+       private static ASTParser createParser() {
                ASTParser parser = ASTParser.newParser(AST.JLS4);
                parser.setKind(ASTParser.K_COMPILATION_UNIT);
                parser.setResolveBindings(true);
@@ -42,26 +48,48 @@ public class ParseUtils {
        }
 
        public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) {
-               String text;
+               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 {
-                       text = document.get(textSelection.getOffset(), textSelection.getLength());
+                       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 (Character.isWhitespace(text.charAt(lstrip)))
+               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 rIdx = text.length() - 1;
-               while (Character.isWhitespace(text.charAt(rIdx--)))
+               int rIndex = text.length() - 1;
+               while (rstrip < text.length() && Character.isWhitespace(text.charAt(rIndex--)))
                        rstrip++;
+               return rstrip;
+       }
 
-               int newOffset = textSelection.getOffset() + lstrip;
-               int newLength = textSelection.getLength() - (lstrip + rstrip);
-
-               return new TextSelection(document, newOffset, newLength);
+       private static boolean textContainsOnlyWhitespace(final String text) {
+               return text.trim().length() == 0;
        }
 
        public static String getNodeDebugText(ASTNode node) {
@@ -70,13 +98,33 @@ public class ParseUtils {
                return printer.getResult();
        }
 
-       public static String getNodeText(ASTNode node, IDocument document) {
+       public static String getNodeText(ASTNode node) {
                try {
-                       return document.get(node.getStartPosition(), node.getLength());
+                       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());
+       }
+}
\ No newline at end of file