]>
Commit | Line | Data |
---|---|---|
db6d3183 EK |
1 | package no.uio.ifi.refaktor.utils; |
2 | ||
171cf942 | 3 | import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat; |
02079d76 EK |
4 | import static org.hamcrest.CoreMatchers.instanceOf; |
5 | ||
db6d3183 | 6 | import org.eclipse.jdt.core.ICompilationUnit; |
18447975 | 7 | import org.eclipse.jdt.core.JavaModelException; |
db6d3183 | 8 | import org.eclipse.jdt.core.dom.AST; |
9a55edb7 | 9 | import org.eclipse.jdt.core.dom.ASTNode; |
db6d3183 EK |
10 | import org.eclipse.jdt.core.dom.ASTParser; |
11 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
c519ffef | 12 | import org.eclipse.jdt.core.dom.NodeFinder; |
9a55edb7 EK |
13 | import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener; |
14 | import org.eclipse.jface.text.BadLocationException; | |
18447975 | 15 | import org.eclipse.jface.text.Document; |
9a55edb7 | 16 | import org.eclipse.jface.text.IDocument; |
c519ffef | 17 | import org.eclipse.jface.text.ITextSelection; |
9a55edb7 | 18 | import org.eclipse.jface.text.TextSelection; |
db6d3183 | 19 | |
9a55edb7 | 20 | @SuppressWarnings("restriction") |
db6d3183 EK |
21 | public class ParseUtils { |
22 | ||
23 | public static CompilationUnit parse(ICompilationUnit unit) { | |
99928b93 | 24 | return CompilationUnitCacheManager.getParsedCompilationUnitFor(unit); |
db6d3183 EK |
25 | } |
26 | ||
fa9cfed2 EK |
27 | static CompilationUnit parseUncached(ICompilationUnit unit) { |
28 | ASTParser parser = createParser(); | |
29 | parser.setSource(unit); | |
30 | return (CompilationUnit) parser.createAST(null); | |
31 | } | |
32 | ||
9a55edb7 | 33 | public static CompilationUnit parse(IDocument document) { |
247e81c5 | 34 | return ParseUtils.parse(document.get().toCharArray()); |
9a55edb7 EK |
35 | } |
36 | ||
247e81c5 | 37 | private static CompilationUnit parse(char[] chars) { |
db6d3183 EK |
38 | ASTParser parser = ParseUtils.createParser(); |
39 | parser.setSource(chars); | |
40 | return (CompilationUnit) parser.createAST(null); | |
41 | } | |
42 | ||
fa9cfed2 | 43 | private static ASTParser createParser() { |
db6d3183 EK |
44 | ASTParser parser = ASTParser.newParser(AST.JLS4); |
45 | parser.setKind(ASTParser.K_COMPILATION_UNIT); | |
46 | parser.setResolveBindings(true); | |
47 | return parser; | |
48 | } | |
49 | ||
9a55edb7 | 50 | public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) { |
3894a465 EK |
51 | final String text = getSelectedTextFromDocument(textSelection, document); |
52 | ||
53 | if (text.isEmpty()) | |
54 | return null; // VS was here, TODO: return textSelection? EK was here | |
55 | ||
56 | int lstrip = numberOfPrecedingWhitespaceCharacters(text); | |
57 | int rstrip = numberOfSuccessiveWhitespaceCharacters(text); | |
58 | ||
59 | int newOffset = textSelection.getOffset() + lstrip; | |
60 | int newLength = textSelection.getLength() - (lstrip + rstrip); | |
61 | ||
62 | return new TextSelection(document, newOffset, newLength); | |
63 | } | |
64 | ||
65 | private static String getSelectedTextFromDocument(TextSelection textSelection, IDocument document) { | |
9a55edb7 | 66 | try { |
3894a465 | 67 | return document.get(textSelection.getOffset(), textSelection.getLength()); |
9a55edb7 EK |
68 | } catch (BadLocationException e) { |
69 | throw new RuntimeException(e); | |
70 | } | |
3894a465 | 71 | } |
9a55edb7 | 72 | |
3894a465 | 73 | private static int numberOfPrecedingWhitespaceCharacters(final String text) { |
9a55edb7 | 74 | int lstrip = 0; |
3894a465 | 75 | while (lstrip < text.length() && Character.isWhitespace(text.charAt(lstrip))) |
9a55edb7 | 76 | lstrip++; |
3894a465 EK |
77 | return lstrip; |
78 | } | |
9a55edb7 | 79 | |
3894a465 EK |
80 | private static int numberOfSuccessiveWhitespaceCharacters(final String text) { |
81 | if (textContainsOnlyWhitespace(text)) | |
82 | return 0; | |
83 | ||
9a55edb7 | 84 | int rstrip = 0; |
3894a465 EK |
85 | int rIndex = text.length() - 1; |
86 | while (rstrip < text.length() && Character.isWhitespace(text.charAt(rIndex--))) | |
9a55edb7 | 87 | rstrip++; |
3894a465 EK |
88 | return rstrip; |
89 | } | |
9a55edb7 | 90 | |
3894a465 EK |
91 | private static boolean textContainsOnlyWhitespace(final String text) { |
92 | return text.trim().length() == 0; | |
9a55edb7 EK |
93 | } |
94 | ||
95 | public static String getNodeDebugText(ASTNode node) { | |
96 | NaiveASTFlattener printer = new NaiveASTFlattener(); | |
97 | node.accept(printer); | |
98 | return printer.getResult(); | |
99 | } | |
100 | ||
18447975 | 101 | public static String getNodeText(ASTNode node) { |
9a55edb7 | 102 | try { |
18447975 EK |
103 | return getCompilationUnitAndGetTextFromSource(node); |
104 | } catch (JavaModelException e) { | |
105 | throw new RuntimeException(e); | |
9a55edb7 EK |
106 | } catch (BadLocationException e) { |
107 | throw new RuntimeException(e); | |
108 | } | |
109 | } | |
110 | ||
18447975 EK |
111 | private static String getCompilationUnitAndGetTextFromSource(ASTNode node) |
112 | throws JavaModelException, BadLocationException { | |
113 | IDocument document = getDocument(getCompilationUnit(node)); | |
114 | return document.get(node.getStartPosition(), node.getLength()); | |
115 | } | |
116 | ||
117 | private static CompilationUnit getCompilationUnit(ASTNode node) { | |
118 | ASTNode rootNode = node.getRoot(); | |
02079d76 | 119 | assertThat(rootNode, instanceOf(CompilationUnit.class)); |
18447975 EK |
120 | return (CompilationUnit) rootNode; |
121 | } | |
122 | ||
123 | private static IDocument getDocument(CompilationUnit compilationUnit) throws JavaModelException { | |
124 | return new Document(compilationUnit.getTypeRoot().getSource()); | |
125 | } | |
126 | ||
1d39ea7d | 127 | static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) { |
0311e2cb EK |
128 | return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength()); |
129 | } | |
3894a465 | 130 | } |