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;
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);
}
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);
}
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) {
private static CompilationUnit getCompilationUnit(ASTNode node) {
ASTNode rootNode = node.getRoot();
- assert rootNode instanceof CompilationUnit;
+ assertThat(rootNode, instanceOf(CompilationUnit.class));
return (CompilationUnit) rootNode;
}
static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) {
return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength());
}
-}
+}
\ No newline at end of file