Repository navigation
Expand file tree
/
Copy pathTreePruner.java
More file actions
182 lines (139 loc) · 5.96 KB
/
Copy pathTreePruner.java
File metadata and controls
182 lines (139 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.javacs;
import com.sun.source.tree.*;
import com.sun.source.util.JavacTask;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.List;
import java.io.IOException;
import java.io.Reader;
import javax.tools.JavaFileObject;
/** Fix up the tree to make it easier to autocomplete, index */
class TreePruner {
private final JavacTask task;
TreePruner(JavacTask task) {
this.task = task;
}
void removeNonCursorMethodBodies(CompilationUnitTree source, int line, int column) {
long offset = source.getLineMap().getPosition(line, column);
doRemoveNonCursorMethodBodies(source, offset);
}
void removeAllMethodBodies(CompilationUnitTree source) {
doRemoveNonCursorMethodBodies(source, -1);
}
private void doRemoveNonCursorMethodBodies(CompilationUnitTree source, long offset) {
SourcePositions sourcePositions = Trees.instance(task).getSourcePositions();
class Pruner extends TreeScanner<Void, Void> {
@Override
public Void visitBlock(BlockTree node, Void aVoid) {
if (containsCursor(node)) super.visitBlock(node, aVoid);
else {
JCTree.JCBlock impl = (JCTree.JCBlock) node;
impl.stats = List.nil();
}
return null;
}
boolean containsCursor(Tree leaf) {
long start = sourcePositions.getStartPosition(source, leaf);
long end = sourcePositions.getEndPosition(source, leaf);
return start <= offset && offset <= end;
}
@Override
public Void visitErroneous(ErroneousTree node, Void nothing) {
return super.scan(node.getErrorTrees(), nothing);
}
}
new Pruner().scan(source, null);
}
/** Remove all statements after the statement the cursor is in */
void removeStatementsAfterCursor(CompilationUnitTree source, int line, int column) {
SourcePositions sourcePositions = Trees.instance(task).getSourcePositions();
long offset = source.getLineMap().getPosition(line, column);
class Pruner extends TreeScanner<Void, Void> {
@Override
public Void visitBlock(BlockTree node, Void aVoid) {
JCTree.JCBlock impl = (JCTree.JCBlock) node;
impl.stats = pruneStatements(impl.stats);
return super.visitBlock(node, aVoid);
}
@Override
public Void visitSwitch(SwitchTree node, Void aVoid) {
JCTree.JCSwitch impl = (JCTree.JCSwitch) node;
impl.cases = pruneStatements(impl.cases);
return super.visitSwitch(node, aVoid);
}
@Override
public Void visitCase(CaseTree node, Void aVoid) {
JCTree.JCCase impl = (JCTree.JCCase) node;
impl.stats = pruneStatements(impl.stats);
return super.visitCase(node, aVoid);
}
private <T extends Tree> List<T> pruneStatements(List<T> stats) {
int countStatements = 0;
boolean foundCursor = false;
// Scan up to statement containing cursor
while (countStatements < stats.size()) {
T s = stats.get(countStatements);
if (containsCursor(s)) foundCursor = true;
else if (foundCursor) break;
else this.scan(s, null);
countStatements++;
}
// Remove all statements after statement containing cursor
return stats.take(countStatements);
}
boolean containsCursor(Tree leaf) {
long start = sourcePositions.getStartPosition(source, leaf);
long end = sourcePositions.getEndPosition(source, leaf);
return start <= offset && offset <= end;
}
@Override
public Void visitErroneous(ErroneousTree node, Void nothing) {
return super.scan(node.getErrorTrees(), nothing);
}
}
new Pruner().scan(source, null);
}
/**
* Insert ';' after the users cursor so we recover from parse errors in a helpful way when doing
* autocomplete.
*/
static JavaFileObject putSemicolonAfterCursor(
JavaFileObject file, int cursorLine, int cursorCharacter) {
try (Reader reader = file.openReader(true)) {
StringBuilder acc = new StringBuilder();
int line = 1, character = 1;
// Go to the cursor
while (line < cursorLine || character < cursorCharacter) {
int next = reader.read();
if (next == -1)
throw new RuntimeException(
"End of file "
+ file
+ " before cursor "
+ cursorLine
+ ":"
+ cursorCharacter);
else if (next == '\n') {
line++;
character = 1;
} else character++;
acc.append((char) next);
}
// Go to the end of the line
while (true) {
int next = reader.read();
if (next == -1 || next == '\n') break;
acc.append((char) next);
}
acc.append(";\n");
for (int next = reader.read(); next > 0; next = reader.read()) {
acc.append((char) next);
}
return new StringFileObject(acc.toString(), file.toUri());
} catch (IOException e) {
throw ShowMessageException.error("Error reading " + file, e);
}
}
}