]>
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 | ||
9a55edb7 | 27 | public static CompilationUnit parse(IDocument document) { |
247e81c5 | 28 | return ParseUtils.parse(document.get().toCharArray()); |
9a55edb7 EK |
29 | } |
30 | ||
247e81c5 | 31 | private static CompilationUnit parse(char[] chars) { |
db6d3183 EK |
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 | ||
9a55edb7 | 44 | public static TextSelection stripTextSelection(IDocument document, TextSelection textSelection) { |
3894a465 EK |
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) { | |
9a55edb7 | 60 | try { |
3894a465 | 61 | return document.get(textSelection.getOffset(), textSelection.getLength()); |
9a55edb7 EK |
62 | } catch (BadLocationException e) { |
63 | throw new RuntimeException(e); | |
64 | } | |
3894a465 | 65 | } |
9a55edb7 | 66 | |
3894a465 | 67 | private static int numberOfPrecedingWhitespaceCharacters(final String text) { |
9a55edb7 | 68 | int lstrip = 0; |
3894a465 | 69 | while (lstrip < text.length() && Character.isWhitespace(text.charAt(lstrip))) |
9a55edb7 | 70 | lstrip++; |
3894a465 EK |
71 | return lstrip; |
72 | } | |
9a55edb7 | 73 | |
3894a465 EK |
74 | private static int numberOfSuccessiveWhitespaceCharacters(final String text) { |
75 | if (textContainsOnlyWhitespace(text)) | |
76 | return 0; | |
77 | ||
9a55edb7 | 78 | int rstrip = 0; |
3894a465 EK |
79 | int rIndex = text.length() - 1; |
80 | while (rstrip < text.length() && Character.isWhitespace(text.charAt(rIndex--))) | |
9a55edb7 | 81 | rstrip++; |
3894a465 EK |
82 | return rstrip; |
83 | } | |
9a55edb7 | 84 | |
3894a465 EK |
85 | private static boolean textContainsOnlyWhitespace(final String text) { |
86 | return text.trim().length() == 0; | |
9a55edb7 EK |
87 | } |
88 | ||
89 | public static String getNodeDebugText(ASTNode node) { | |
90 | NaiveASTFlattener printer = new NaiveASTFlattener(); | |
91 | node.accept(printer); | |
92 | return printer.getResult(); | |
93 | } | |
94 | ||
18447975 | 95 | public static String getNodeText(ASTNode node) { |
9a55edb7 | 96 | try { |
18447975 EK |
97 | return getCompilationUnitAndGetTextFromSource(node); |
98 | } catch (JavaModelException e) { | |
99 | throw new RuntimeException(e); | |
9a55edb7 EK |
100 | } catch (BadLocationException e) { |
101 | throw new RuntimeException(e); | |
102 | } | |
103 | } | |
104 | ||
18447975 EK |
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(); | |
02079d76 | 113 | assertThat(rootNode, instanceOf(CompilationUnit.class)); |
18447975 EK |
114 | return (CompilationUnit) rootNode; |
115 | } | |
116 | ||
117 | private static IDocument getDocument(CompilationUnit compilationUnit) throws JavaModelException { | |
118 | return new Document(compilationUnit.getTypeRoot().getSource()); | |
119 | } | |
120 | ||
1d39ea7d | 121 | static NodeFinder createNodeFinder(CompilationUnit cu, ITextSelection selection) { |
0311e2cb EK |
122 | return new NodeFinder(cu.getRoot(), selection.getOffset(), selection.getLength()); |
123 | } | |
3894a465 | 124 | } |