forked from sourcegraph/scip-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectIndexer.java
More file actions
159 lines (129 loc) · 6.19 KB
/
Copy pathProjectIndexer.java
File metadata and controls
159 lines (129 loc) · 6.19 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
package lsifjava;
import spoon.MavenLauncher;
import spoon.reflect.declaration.CtElement;
import spoon.support.compiler.SpoonPom;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.google.common.collect.Sets;
public class ProjectIndexer {
public int numFiles;
public int numDefinitions;
private Arguments arguments;
private Emitter emitter;
public ProjectIndexer(Arguments arguments, Emitter emitter) {
this.arguments = arguments;
this.emitter = emitter;
}
public void index() throws IOException {
emitter.emitVertex("metaData", Util.mapOf(
"version", "0.4.0",
"positionEncoding", "utf-16",
"projectRoot", String.format("file://%s", Paths.get(arguments.projectRoot).toFile().getCanonicalPath()),
"toolInfo", Util.mapOf("name", "lsif-java", "version", "0.1.0")
));
String projectId = emitter.emitVertex("project", Util.mapOf(
"kind", "java"
));
MavenLauncher spoon = new MavenLauncher(arguments.projectRoot, MavenLauncher.SOURCE_TYPE.ALL_SOURCE/* , Pattern.compile(".+") */);
spoon.getEnvironment().setIgnoreDuplicateDeclarations(true);
spoon.getEnvironment().setComplianceLevel(9);
spoon.buildModel();
Set<String> allFiles = new HashSet<>();
Set<String> visitedFiles = new HashSet<>();
if(arguments.verbose) {
FileVisitor<Path> filetreeWalker = new FileVisitor<Path>() {
@Override
public FileVisitResult postVisitDirectory(Path arg0, IOException arg1) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path arg0, BasicFileAttributes arg1) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes meta) throws IOException {
if (meta.isDirectory()) return FileVisitResult.CONTINUE;
String filename = path.getFileName().toString();
if (filename.endsWith(".java") && !filename.equals("package-info.java") && !filename.equals("module-info.java"))
allFiles.add(path.toFile().getCanonicalPath());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path arg0, IOException arg1) throws IOException {
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(Paths.get(arguments.projectRoot), filetreeWalker);
System.out.print("\nDISCOVERED MODULES\n-------------------------------------------------\n");
printModule(spoon.getPomFile());
for (SpoonPom pom : spoon.getPomFile().getModules()) {
printModule(pom);
for (SpoonPom child : pom.getModules()) {
printModule(child);
}
}
System.out.println("\nDISCOVERED SOURCE DIRECTORIES\n-------------------------------------------------");
System.out.println(spoon.getPomFile().getSourceDirectories().stream().map(row -> {
String path = null; try { path = String.join(",", row.getCanonicalPath().replaceFirst("^"+arguments.projectRoot, ".")); } catch (IOException e) {}; return path;
}).sorted().collect(Collectors.joining("\n")));
}
Map<String, CtElement> typeByFile = new HashMap<>();
for (CtElement el : spoon.getModel().getAllTypes()) {
visitedFiles.add(el.getPosition().getFile().getAbsolutePath());
typeByFile.put(el.getPosition().getFile().getPath(), el);
}
Map<String, DocumentIndexer> indexers = new HashMap<>();
for (Map.Entry<String, CtElement> pathname : typeByFile.entrySet()) {
indexers.put(pathname.getKey(), new DocumentIndexer(
arguments.projectRoot,
arguments.verbose,
pathname.getKey(),
pathname.getValue(),
projectId,
emitter,
indexers
));
}
for (DocumentIndexer indexer : indexers.values()) {
indexer.preIndex();
}
if(arguments.verbose)
System.out.println("\nEMITTING DEFINITIONS\n-------------------------------------------------");
for (DocumentIndexer indexer : indexers.values()) {
indexer.visitDefinitions();
}
if(arguments.verbose)
System.out.println("\nEMITTING REFERENCES\n-------------------------------------------------");
for (DocumentIndexer indexer : indexers.values()) {
indexer.visitReferences();
}
for (DocumentIndexer indexer : indexers.values()) {
indexer.postIndex();
}
for (DocumentIndexer indexer : indexers.values()) {
numFiles++;
numDefinitions += indexer.numDefinitions();
}
if(arguments.verbose) {
System.out.println("\nMISSED JAVA FILES\n-------------------------------------------------");
System.out.println(
Sets.difference(allFiles, visitedFiles)
.stream()
.map(row -> String.join(",", row))
.sorted()
.collect(Collectors.joining("\n")));
System.out.println("All: " + allFiles.size() + " Visited: " + visitedFiles.size());
}
}
private void printModule(SpoonPom pom) throws IOException {
System.out.println("Found module '" + pom.getName() + "' at " + Paths.get(pom.getPath()).getParent().toFile().getCanonicalPath().replaceFirst("^"+arguments.projectRoot, "."));
}
}