]> 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 cd39758f246683dc768ba0b27f24101e124ae908..84e90ab74cdae705ac7840ef0b650af79b94c15f 100644 (file)
@@ -1,5 +1,7 @@
 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;
@@ -19,7 +21,11 @@ import org.eclipse.jface.text.TextSelection;
 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);
        }
@@ -34,7 +40,7 @@ public class ParseUtils {
                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,29 +48,48 @@ public class ParseUtils {
        }
 
        public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) {
-               final 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);
                }
+       }
 
-               if (text.length() == 0)
-                       return null; // VS was here
-
+       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) {
@@ -91,7 +116,7 @@ public class ParseUtils {
 
        private static CompilationUnit getCompilationUnit(ASTNode node) {
                ASTNode rootNode = node.getRoot();
-               assert rootNode instanceof CompilationUnit;
+               assertThat(rootNode, instanceOf(CompilationUnit.class));
                return (CompilationUnit) rootNode;
        }
 
@@ -102,4 +127,4 @@ public class ParseUtils {
        static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) {
                return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength());
        }
-}
+}
\ No newline at end of file