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