Skip to content

Commit db84024

Browse files
CsCherrYYjdneo
andauthored
feat: Support referenceResults and referenceLinks (#74)
Co-authored-by: Sheng Chen <[email protected]>
1 parent 763e1a1 commit db84024

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/indexer/Indexer.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
package com.microsoft.java.lsif.core.internal.indexer;
77

88
import java.io.File;
9+
import java.util.ArrayList;
910
import java.util.HashMap;
1011
import java.util.LinkedList;
1112
import java.util.List;
1213
import java.util.Map;
14+
import java.util.Map.Entry;
1315
import java.util.concurrent.ExecutorService;
1416
import java.util.concurrent.Executors;
1517

@@ -49,12 +51,14 @@
4951
import com.microsoft.java.lsif.core.internal.protocol.Document;
5052
import com.microsoft.java.lsif.core.internal.protocol.Event;
5153
import com.microsoft.java.lsif.core.internal.protocol.Group;
54+
import com.microsoft.java.lsif.core.internal.protocol.ItemEdge;
5255
import com.microsoft.java.lsif.core.internal.protocol.PackageInformation;
5356
import com.microsoft.java.lsif.core.internal.protocol.Project;
5457
import com.microsoft.java.lsif.core.internal.protocol.Group.ConflictResolution;
5558
import com.microsoft.java.lsif.core.internal.visitors.DiagnosticVisitor;
5659
import com.microsoft.java.lsif.core.internal.visitors.DocumentVisitor;
5760
import com.microsoft.java.lsif.core.internal.visitors.LsifVisitor;
61+
import com.microsoft.java.lsif.core.internal.visitors.SymbolData;
5862
import com.microsoft.java.lsif.core.internal.visitors.VisitorUtils;
5963

6064
import io.reactivex.Observable;
@@ -133,6 +137,7 @@ private void buildIndex(IPath path, IProgressMonitor monitor, LsifService lsif)
133137

134138
dumpParallelly(sourceList, threadPool, projVertex, lsif, hasPackageInformation, monitor);
135139

140+
emitReferenceEdges(lsif);
136141
VisitorUtils.endAllDocument(lsif);
137142
LsifEmitter.getInstance().emit(
138143
lsif.getVertexBuilder().event(Event.EventScope.Project, Event.EventKind.END, projVertex.getId()));
@@ -174,6 +179,22 @@ private List<ICompilationUnit> getAllSourceFiles(IJavaProject javaProject) throw
174179
return res;
175180
}
176181

182+
private void emitReferenceEdges(LsifService lsif) {
183+
for (Entry<SymbolData, List<SymbolData>> entry : Repository.getInstance().getReferenceResultsMap().entrySet()) {
184+
SymbolData fromSymbolData = entry.getKey();
185+
List<String> referenceResultsInVs = new ArrayList<String>();
186+
List<String> referenceLinksInVs = new ArrayList<String>();
187+
for (SymbolData symbolData : entry.getValue()) {
188+
referenceResultsInVs.add(symbolData.getReferenceResult().getId());
189+
referenceLinksInVs.add(symbolData.getGroupMoniker().getId());
190+
}
191+
LsifEmitter.getInstance().emit(lsif.getEdgeBuilder().item(fromSymbolData.getReferenceResult(), referenceResultsInVs, fromSymbolData.getDocument(),
192+
ItemEdge.ItemEdgeProperties.REFERENCE_RESULTS));
193+
LsifEmitter.getInstance().emit(lsif.getEdgeBuilder().item(fromSymbolData.getReferenceResult(), referenceLinksInVs, fromSymbolData.getDocument(),
194+
ItemEdge.ItemEdgeProperties.REFERENCE_LINKS));
195+
}
196+
}
197+
177198
/**
178199
* Generate and emit the package information of the given project and return if
179200
* the project is published.

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/indexer/Repository.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import java.io.File;
99
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
1012
import java.util.Map;
1113
import java.util.concurrent.ConcurrentHashMap;
1214

@@ -56,6 +58,10 @@ public class Repository {
5658
// Value: PackageInformation
5759
private Map<String, MavenProject> mavenProjectMap = new ConcurrentHashMap<>();
5860

61+
// Key: SymbolData
62+
// Value: List<SymbolData>
63+
private Map<SymbolData, List<SymbolData>> referenceResultsMap = new ConcurrentHashMap<>();
64+
5965
private Repository() {
6066
}
6167

@@ -199,4 +205,17 @@ private void addMavenProject(String id, MavenProject mavenProject) {
199205
this.mavenProjectMap.put(id, mavenProject);
200206
}
201207

208+
public synchronized void addReferenceResults(SymbolData from, SymbolData to) {
209+
if (this.referenceResultsMap.containsKey(from)) {
210+
this.referenceResultsMap.get(from).add(to);
211+
} else {
212+
this.referenceResultsMap.put(from, Arrays.asList(to));
213+
}
214+
215+
}
216+
217+
public Map<SymbolData, List<SymbolData>> getReferenceResultsMap() {
218+
return this.referenceResultsMap;
219+
}
220+
202221
}

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/protocol/ItemEdge.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static class ItemEdgeProperties {
3838
public static final String DEFINITIONS = "definitions";
3939
public static final String REFERENCES = "references";
4040
public static final String REFERENCE_RESULTS = "referenceResults";
41+
public static final String REFERENCE_LINKS = "referenceLinks";
4142
public static final String IMPLEMENTATION_RESULTS = "implementationResults";
4243
}
4344
}

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/visitors/LsifVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void resolve(int startPosition, int length, boolean needResolveImpl, int
107107
symbolData.resolveMoniker(lsif, element, modifier, hasPackageInformation);
108108

109109
/* Resolve definition */
110-
symbolData.resolveDefinition(lsif, definitionLocation);
110+
symbolData.resolveDefinition(lsif, element, definitionLocation);
111111

112112
/* Resolve typeDefinition */
113113
symbolData.resolveTypeDefinition(lsif, docVertex, sourceLspRange);

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/visitors/SymbolData.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.apache.maven.scm.provider.ScmUrlUtils;
1717
import org.apache.maven.shared.utils.StringUtils;
1818
import org.eclipse.core.runtime.IPath;
19+
import org.eclipse.core.runtime.NullProgressMonitor;
1920
import org.eclipse.jdt.core.IClassFile;
2021
import org.eclipse.jdt.core.IClasspathEntry;
2122
import org.eclipse.jdt.core.IField;
@@ -32,9 +33,11 @@
3233
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
3334
import org.eclipse.jdt.launching.JavaRuntime;
3435
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
36+
import org.eclipse.jdt.ls.core.internal.handlers.FindLinksHandler;
3537
import org.eclipse.lsp4j.Hover;
3638
import org.eclipse.lsp4j.Location;
3739

40+
import com.microsoft.java.lsif.core.internal.JdtlsUtils;
3841
import com.microsoft.java.lsif.core.internal.emitter.LsifEmitter;
3942
import com.microsoft.java.lsif.core.internal.indexer.LsifService;
4043
import com.microsoft.java.lsif.core.internal.indexer.Repository;
@@ -72,6 +75,18 @@ public SymbolData(Project project, Document document) {
7275
this.document = document;
7376
}
7477

78+
public Document getDocument() {
79+
return this.document;
80+
}
81+
82+
public ReferenceResult getReferenceResult() {
83+
return this.referenceResult;
84+
}
85+
86+
public Moniker getGroupMoniker() {
87+
return this.groupMoniker;
88+
}
89+
7590
synchronized public void ensureResultSet(LsifService lsif, Range sourceRange) {
7691
if (this.resultSet == null) {
7792
ResultSet resultSet = lsif.getVertexBuilder().resultSet();
@@ -137,7 +152,7 @@ synchronized public void generateExportMoniker(LsifService lsif, String identifi
137152
LsifEmitter.getInstance().emit(lsif.getEdgeBuilder().attach(this.schemeMoniker, this.groupMoniker));
138153
}
139154

140-
synchronized public void resolveDefinition(LsifService lsif, Location definitionLocation) {
155+
synchronized public void resolveDefinition(LsifService lsif, IJavaElement element, Location definitionLocation) {
141156
if (this.definitionResolved) {
142157
return;
143158
}
@@ -148,9 +163,37 @@ synchronized public void resolveDefinition(LsifService lsif, Location definition
148163
DefinitionResult defResult = VisitorUtils.ensureDefinitionResult(lsif, this.resultSet);
149164
LsifEmitter.getInstance().emit(lsif.getEdgeBuilder().item(defResult, definitionRange, document,
150165
ItemEdge.ItemEdgeProperties.DEFINITIONS));
166+
if (element instanceof IMethod) {
167+
IMethod mostAbstractMethod = getMostAbstractDeclaration((IMethod) element);
168+
if (mostAbstractMethod != null) {
169+
Location abstractDefinitionLocation = JdtlsUtils.getElementLocation(mostAbstractMethod);
170+
String id = VisitorUtils.createSymbolKey(abstractDefinitionLocation);
171+
Document abstractDefinitionDocument = Repository.getInstance().enlistDocument(lsif,
172+
abstractDefinitionLocation.getUri(), this.project);
173+
SymbolData abstractSymbolData = Repository.getInstance().enlistSymbolData(id,
174+
abstractDefinitionDocument, this.project);
175+
Repository.getInstance().addReferenceResults(this, abstractSymbolData);
176+
}
177+
}
151178
this.definitionResolved = true;
152179
}
153180

181+
private IMethod getMostAbstractDeclaration(IMethod method) {
182+
IMethod current = null;
183+
try {
184+
while (method != null) {
185+
method = FindLinksHandler.findOverriddenMethod(method, new NullProgressMonitor());
186+
if (method == null) {
187+
return current;
188+
}
189+
current = method;
190+
}
191+
} catch (JavaModelException e) {
192+
return null;
193+
}
194+
return null;
195+
}
196+
154197
synchronized public void resolveTypeDefinition(LsifService lsif, Document docVertex,
155198
org.eclipse.lsp4j.Range sourceLspRange) {
156199
if (this.typeDefinitionResolved) {
@@ -190,8 +233,7 @@ synchronized public void resolveImplementation(LsifService lsif, Document docVer
190233
this.implementationResolved = true;
191234
}
192235

193-
synchronized public void resolveReference(LsifService lsif, Document sourceDocument, Location definitionLocation,
194-
Range sourceRange) {
236+
synchronized public void resolveReference(LsifService lsif, Document sourceDocument, Location definitionLocation, Range sourceRange) {
195237
if (this.referenceResult == null) {
196238
ReferenceResult referenceResult = VisitorUtils.ensureReferenceResult(lsif, this.resultSet);
197239
this.referenceResult = referenceResult;

0 commit comments

Comments
 (0)