forked from sourcegraph/scip-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentIndexer.java
More file actions
327 lines (281 loc) · 12 KB
/
DocumentIndexer.java
File metadata and controls
327 lines (281 loc) · 12 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import com.github.javaparser.Position;
import com.github.javaparser.Range;
import spoon.reflect.code.*;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.*;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtScanner;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class DocumentIndexer {
private String projectRoot;
private boolean contents;
private String pathname;
private CtElement spoonElement;
private String projectId;
private Emitter emitter;
private Map<String, DocumentIndexer> indexers;
private String documentId;
private Set<String> rangeIds = new HashSet<>();
private Map<Range, DefinitionMeta> definitions = new HashMap<>();
public DocumentIndexer(
String projectRoot,
boolean contents,
String pathname,
CtElement spoonElement,
String projectId,
Emitter emitter,
Map<String, DocumentIndexer> indexers
) {
this.projectRoot = projectRoot;
this.contents = contents;
this.pathname = pathname;
this.spoonElement = spoonElement;
this.projectId = projectId;
this.emitter = emitter;
this.indexers = indexers;
}
public int numDefinitions() {
return definitions.size();
}
public void visitDefinitions() {
this.spoonElement.accept(new DefinitionsVisitor());
}
public void visitReferences() {
this.spoonElement.accept(new ReferencesVisitor());
}
public void preIndex() {
Map<String, Object> args = Util.mapOf(
"languageId", "java",
"uri", String.format("file://%s", Paths.get(pathname).toAbsolutePath().toString())
);
if (contents) {
try {
List<String> lines = Files.readAllLines(Paths.get(pathname), StandardCharsets.UTF_8);
args = Util.union(args, Util.mapOf("contents", String.join("\n", lines)));
} catch (IOException ex) {
throw new RuntimeException(String.format("Failed to read file %s", pathname));
}
}
this.documentId = emitter.emitVertex("document", args);
}
public void postIndex() {
for (DefinitionMeta meta : definitions.values()) {
linkUses(meta, documentId);
}
emitter.emitEdge("contains", Util.mapOf("outV", projectId, "inVs", new String[]{documentId}));
emitter.emitEdge("contains", Util.mapOf("outV", documentId, "inVs", rangeIds.stream().sorted().toArray()));
}
private void linkUses(DefinitionMeta meta, String documentId) {
String resultId = emitter.emitVertex("referenceResult", Util.mapOf());
emitter.emitEdge("textDocument/references", Util.mapOf("outV", meta.resultSetId, "inV", resultId));
emitter.emitEdge("item", Util.mapOf(
"property", "definitions",
"outV", resultId,
"inVs", new String[]{meta.rangeId},
"document", documentId
));
for (Map.Entry<String, Set<String>> entry : meta.referenceRangeIds.entrySet()) {
emitter.emitEdge("item", Util.mapOf(
"property", "references",
"outV", resultId,
"inVs", entry.getValue().stream().sorted().toArray(),
"document", entry.getKey()
));
}
}
// TODO add support for more language constructs:
// https://github.com/INRIA/spoon/blob/master/src/main/java/spoon/reflect/visitor/CtScanner.java
private class DefinitionsVisitor extends CtScanner {
@Override
public <T> void visitCtParameter(CtParameter<T> el) {
super.visitCtParameter(el);
if (el.getPosition() instanceof NoSourcePosition) {
return;
}
emitDefinition(mkRange(el.getPosition()), mkDoc(el.getType(), el.getDocComment()));
}
@Override
public <T> void visitCtLocalVariable(CtLocalVariable<T> el) {
super.visitCtLocalVariable(el);
emitDefinition(mkRange(el.getPosition()), mkDoc(el.getType(), el.getDocComment()));
}
@Override
public <T> void visitCtCatchVariable(CtCatchVariable<T> el) {
super.visitCtCatchVariable(el);
emitDefinition(mkRange(el.getPosition()), mkDoc(el.getType(), el.getDocComment()));
}
@Override
public <T> void visitCtMethod(CtMethod<T> el) {
super.visitCtMethod(el);
emitDefinition(nameRange(el), mkDoc(el.getType(), el.getDocComment()));
}
@Override
public <T> void visitCtField(CtField<T> el) {
super.visitCtField(el);
emitDefinition(nameRange(el), mkDoc(el.getType(), el.getDocComment()));
}
@Override
public <T> void visitCtClass(CtClass<T> el) {
super.visitCtClass(el);
emitDefinition(nameRange(el), el.getDocComment());
}
private void emitDefinition(Range range, String doc) {
System.out.println("DEF " + pathname + ":" + humanRange(range));
String hoverId = emitter.emitVertex("hoverResult", Util.mapOf(
"result", Util.mapOf(
"contents", Util.mapOf(
"kind", "markdown",
"value", doc
)
)
));
String resultSetId = emitter.emitVertex("resultSet", Util.mapOf());
emitter.emitEdge("textDocument/hover", Util.mapOf("outV", resultSetId, "inV", hoverId));
String rangeId = emitter.emitVertex("range", createRange(range));
emitter.emitEdge("next", Util.mapOf("outV", rangeId, "inV", resultSetId));
rangeIds.add(rangeId);
definitions.put(range, new DefinitionMeta(rangeId, resultSetId)); // + contents?
}
}
private class ReferencesVisitor extends CtScanner {
@Override
public <T> void visitCtVariableRead(CtVariableRead<T> el) {
super.visitCtVariableRead(el);
if (el.getVariable().getDeclaration() == null) {
return;
}
if (el.getPosition() instanceof NoSourcePosition) {
return;
}
emitUse(
mkRange(el.getPosition()),
mkRange(el.getVariable().getDeclaration().getPosition()),
el.getVariable().getDeclaration().getPosition().getFile().getPath()
);
}
@Override
public <T> void visitCtInvocation(CtInvocation<T> el) {
super.visitCtInvocation(el);
if (el.getPosition() instanceof NoSourcePosition) {
return;
}
if (el.getExecutable() == null || el.getExecutable().getDeclaration() == null || el.getExecutable().getDeclaration().getPosition() instanceof NoSourcePosition) {
return;
}
Range useRange = identifierRange(el, el.getExecutable().getSimpleName());
emitUse(
useRange,
nameRange(el.getExecutable().getDeclaration()),
el.getExecutable().getDeclaration().getPosition().getFile().getPath()
);
}
@Override
public <T> void visitCtFieldRead(CtFieldRead<T> el) {
super.visitCtFieldRead(el);
if (el.getPosition() instanceof NoSourcePosition) {
return;
}
Range useRange = identifierRange(el, el.getVariable().getSimpleName());
CtField decl = el.getVariable().getDeclaration();
if (decl == null) {
return;
}
emitUse(
useRange,
nameRange(decl),
decl.getPosition().getFile().getPath()
);
}
@Override
public <T> void visitCtTypeReference(CtTypeReference<T> el) {
super.visitCtTypeReference(el);
if (el.getPosition() instanceof NoSourcePosition) {
return;
}
CtType decl = el.getDeclaration();
if (decl == null) {
return;
}
emitUse(
mkRange(el.getPosition()),
nameRange(decl),
decl.getPosition().getFile().getPath()
);
}
private void emitUse(Range use, Range def, String defPath) {
DocumentIndexer indexer = indexers.get(defPath);
String link = pathname + ":" + humanRange(use) + " -> " + defPath + ":" + humanRange(def);
System.out.println("Linking use to definition: " + link);
DefinitionMeta meta = indexer.definitions.get(def);
if (meta == null) {
System.out.println("WARNING Skipping linking use to definition: " + link);
return;
}
String rangeId = emitter.emitVertex("range", createRange(use));
emitter.emitEdge("next", Util.mapOf("outV", rangeId, "inV", meta.resultSetId));
if (meta.definitionResultId == null) {
String resultId = emitter.emitVertex("definitionResult", Util.mapOf());
emitter.emitEdge("textDocument/definition", Util.mapOf("outV", meta.resultSetId, "inV", resultId));
meta.definitionResultId = resultId;
}
emitter.emitEdge("item", Util.mapOf(
"outV", meta.definitionResultId,
"inVs", new String[]{meta.rangeId},
"document", indexer.documentId
));
rangeIds.add(rangeId);
Set<String> set = meta.referenceRangeIds.getOrDefault(documentId, new HashSet<>());
set.add(rangeId);
meta.referenceRangeIds.put(documentId, set);
}
}
private Range nameRange(CtNamedElement a) {
return nameRange(a.getPosition(), a.getSimpleName());
}
private Range nameRange(SourcePosition a, String name) {
return Range.range(
a.getLine(),
a.getColumn(),
a.getLine(),
a.getColumn() + name.length()
);
}
private Range nameRange(Range a, String name) {
return Range.range(
a.begin.line,
a.begin.column,
a.end.line,
a.end.column + name.length()
);
}
private String mkDoc(CtTypeReference t, String docComment) {
return "```java\n" + t + "\n```" + (docComment.equals("") ? "" : "\n---\n" + docComment);
}
private String humanRange(Range r) {
return r.begin.line + ":" + r.begin.column + "-" + r.end.line + ":" + r.end.column;
}
private Range mkRange(SourcePosition pos) {
return Range.range(pos.getLine(), pos.getColumn(), pos.getEndLine(), pos.getEndColumn());
}
private Map<String, Object> createRange(Range range) {
return Util.mapOf("start", createPosition(range.begin), "end", createPosition(range.end));
}
private Map<String, Object> createPosition(Position position) {
return Util.mapOf("line", position.line - 1, "character", position.column - 1);
}
private Range identifierRange(CtTargetedExpression a, String b) {
// Ugh, we only want the range for the identifier, not the whole expression.
// This will probably break on fields/methods that are spread across lines.
// +1 for `.`, +1 because (I'm guessing) end is inclusive
if (a.getTarget() == null || a.getTarget().getPosition() instanceof NoSourcePosition) {
return mkRange(a.getPosition());
}
SourcePosition p = a.getTarget().getPosition();
return Range.range(p.getEndLine(), p.getEndColumn() + 1 + 1, p.getEndLine(), p.getEndColumn() + 1 + 1 + b.length());
}
}