]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/ParseUtils.java
CompilationUnitTextSelection: can now find local declaring types
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / ParseUtils.java
1 package no.uio.ifi.refaktor.utils;
2
3 import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat;
4 import static org.hamcrest.CoreMatchers.instanceOf;
5
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;
19
20 @SuppressWarnings("restriction")
21 public class ParseUtils {
22
23         public static CompilationUnit parse(ICompilationUnit unit) {
24                 return CompilationUnitCacheManager.getParsedCompilationUnitFor(unit);
25         }
26
27         public static CompilationUnit parse(IDocument document) {
28                 return ParseUtils.parse(document.get().toCharArray());
29         }
30
31         private static CompilationUnit parse(char[] chars) {
32                 ASTParser parser = ParseUtils.createParser();
33                 parser.setSource(chars);
34                 return (CompilationUnit) parser.createAST(null);
35         }
36
37         public static ASTParser createParser() {
38                 ASTParser parser = ASTParser.newParser(AST.JLS4);
39                 parser.setKind(ASTParser.K_COMPILATION_UNIT);
40                 parser.setResolveBindings(true);
41                 return parser;
42         }
43
44         public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) {
45                 final String text = getSelectedTextFromDocument(textSelection, document);
46
47                 if (text.isEmpty())
48                         return null; // VS was here, TODO: return textSelection? EK was here
49
50                 int lstrip = numberOfPrecedingWhitespaceCharacters(text);
51                 int rstrip = numberOfSuccessiveWhitespaceCharacters(text);
52
53                 int newOffset = textSelection.getOffset() + lstrip;
54                 int newLength = textSelection.getLength() - (lstrip + rstrip);
55
56                 return new TextSelection(document, newOffset, newLength);
57         }
58
59         private static String getSelectedTextFromDocument(TextSelection textSelection, IDocument document) {
60                 try {
61                         return document.get(textSelection.getOffset(), textSelection.getLength());
62                 } catch (BadLocationException e) {
63                         throw new RuntimeException(e);
64                 }
65         }
66
67         private static int numberOfPrecedingWhitespaceCharacters(final String text) {
68                 int lstrip = 0;
69                 while (lstrip < text.length() && Character.isWhitespace(text.charAt(lstrip)))
70                         lstrip++;
71                 return lstrip;
72         }
73
74         private static int numberOfSuccessiveWhitespaceCharacters(final String text) {
75                 if (textContainsOnlyWhitespace(text))
76                         return 0;
77                         
78                 int rstrip = 0;
79                 int rIndex = text.length() - 1;
80                 while (rstrip < text.length() && Character.isWhitespace(text.charAt(rIndex--)))
81                         rstrip++;
82                 return rstrip;
83         }
84
85         private static boolean textContainsOnlyWhitespace(final String text) {
86                 return text.trim().length() == 0;
87         }
88
89         public static String getNodeDebugText(ASTNode node) {
90                 NaiveASTFlattener printer = new NaiveASTFlattener();
91                 node.accept(printer);
92                 return printer.getResult();
93         }
94
95         public static String getNodeText(ASTNode node) {
96                 try {
97                         return getCompilationUnitAndGetTextFromSource(node);
98                 } catch (JavaModelException e) {
99                         throw new RuntimeException(e);
100                 } catch (BadLocationException e) {
101                         throw new RuntimeException(e);
102                 }
103         }
104
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());
109         }
110
111         private static CompilationUnit getCompilationUnit(ASTNode node) {
112                 ASTNode rootNode = node.getRoot();
113                 assertThat(rootNode, instanceOf(CompilationUnit.class));
114                 return (CompilationUnit) rootNode;
115         }
116
117         private static IDocument getDocument(CompilationUnit compilationUnit) throws JavaModelException {
118                 return new Document(compilationUnit.getTypeRoot().getSource());
119         }
120
121         static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) {
122                 return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength());
123         }
124 }