Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support referenceResults and referenceLinks #74

Merged
merged 5 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package com.microsoft.java.lsif.core.internal.indexer;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -49,12 +51,14 @@
import com.microsoft.java.lsif.core.internal.protocol.Document;
import com.microsoft.java.lsif.core.internal.protocol.Event;
import com.microsoft.java.lsif.core.internal.protocol.Group;
import com.microsoft.java.lsif.core.internal.protocol.ItemEdge;
import com.microsoft.java.lsif.core.internal.protocol.PackageInformation;
import com.microsoft.java.lsif.core.internal.protocol.Project;
import com.microsoft.java.lsif.core.internal.protocol.Group.ConflictResolution;
import com.microsoft.java.lsif.core.internal.visitors.DiagnosticVisitor;
import com.microsoft.java.lsif.core.internal.visitors.DocumentVisitor;
import com.microsoft.java.lsif.core.internal.visitors.LsifVisitor;
import com.microsoft.java.lsif.core.internal.visitors.SymbolData;
import com.microsoft.java.lsif.core.internal.visitors.VisitorUtils;

import io.reactivex.Observable;
Expand Down Expand Up @@ -133,6 +137,19 @@ private void buildIndex(IPath path, IProgressMonitor monitor, LsifService lsif)

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

for (Entry<SymbolData, List<SymbolData>> entry : Repository.getInstance().getReferenceResultsMap().entrySet()) {
CsCherrYY marked this conversation as resolved.
Show resolved Hide resolved
SymbolData fromSymbolData = entry.getKey();
List<String> referenceResultsInVs = new ArrayList<String>();
List<String> referenceLinksInVs = new ArrayList<String>();
for (SymbolData symbolData : entry.getValue()) {
referenceResultsInVs.add(symbolData.getReferenceResult().getId());
referenceLinksInVs.add(symbolData.getGroupMoniker().getId());
}
LsifEmitter.getInstance().emit(lsif.getEdgeBuilder().item(fromSymbolData.getReferenceResult(), referenceResultsInVs, fromSymbolData.getDocument(),
ItemEdge.ItemEdgeProperties.REFERENCE_RESULTS));
LsifEmitter.getInstance().emit(lsif.getEdgeBuilder().item(fromSymbolData.getReferenceResult(), referenceLinksInVs, fromSymbolData.getDocument(),
ItemEdge.ItemEdgeProperties.REFERENCE_LINKS));
}
VisitorUtils.endAllDocument(lsif);
LsifEmitter.getInstance().emit(
lsif.getVertexBuilder().event(Event.EventScope.Project, Event.EventKind.END, projVertex.getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

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

// Key: SymbolData
// Value: List<SymbolData>
private Map<SymbolData, List<SymbolData>> referenceResultsMap = new ConcurrentHashMap<>();

private Repository() {
}

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

public synchronized void addReferenceResults(SymbolData from, SymbolData to) {
if (this.referenceResultsMap.containsKey(from)) {
this.referenceResultsMap.get(from).add(to);
} else {
this.referenceResultsMap.put(from, Arrays.asList(to));
}

}

public Map<SymbolData, List<SymbolData>> getReferenceResultsMap() {
return this.referenceResultsMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static class ItemEdgeProperties {
public static final String DEFINITIONS = "definitions";
public static final String REFERENCES = "references";
public static final String REFERENCE_RESULTS = "referenceResults";
public static final String REFERENCE_LINKS = "referenceLinks";
public static final String IMPLEMENTATION_RESULTS = "implementationResults";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void resolve(int startPosition, int length, boolean needResolveImpl, int
}

/* Resolve reference */
symbolData.resolveReference(lsif, docVertex, definitionLocation, sourceRange);
symbolData.resolveReference(lsif, docVertex, element, definitionLocation, sourceRange);

/* Resolve hover */
symbolData.resolveHover(lsif, docVertex, sourceLspRange);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.apache.maven.scm.provider.ScmUrlUtils;
import org.apache.maven.shared.utils.StringUtils;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IField;
Expand All @@ -32,9 +33,11 @@
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.handlers.FindLinksHandler;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.Location;

import com.microsoft.java.lsif.core.internal.JdtlsUtils;
import com.microsoft.java.lsif.core.internal.emitter.LsifEmitter;
import com.microsoft.java.lsif.core.internal.indexer.LsifService;
import com.microsoft.java.lsif.core.internal.indexer.Repository;
Expand Down Expand Up @@ -72,6 +75,18 @@ public SymbolData(Project project, Document document) {
this.document = document;
}

public Document getDocument() {
return this.document;
}

public ReferenceResult getReferenceResult() {
return this.referenceResult;
}

public Moniker getGroupMoniker() {
return this.groupMoniker;
}

synchronized public void ensureResultSet(LsifService lsif, Range sourceRange) {
if (this.resultSet == null) {
ResultSet resultSet = lsif.getVertexBuilder().resultSet();
Expand Down Expand Up @@ -190,8 +205,8 @@ synchronized public void resolveImplementation(LsifService lsif, Document docVer
this.implementationResolved = true;
}

synchronized public void resolveReference(LsifService lsif, Document sourceDocument, Location definitionLocation,
Range sourceRange) {
synchronized public void resolveReference(LsifService lsif, Document sourceDocument, IJavaElement element,
Location definitionLocation, Range sourceRange) {
if (this.referenceResult == null) {
ReferenceResult referenceResult = VisitorUtils.ensureReferenceResult(lsif, this.resultSet);
this.referenceResult = referenceResult;
Expand All @@ -205,7 +220,35 @@ synchronized public void resolveReference(LsifService lsif, Document sourceDocum
if (!VisitorUtils.isDefinitionItself(sourceDocument, sourceRange, definitionDocument, definitionRange)) {
LsifEmitter.getInstance().emit(lsif.getEdgeBuilder().item(this.referenceResult, sourceRange, document,
ItemEdge.ItemEdgeProperties.REFERENCES));
} else if (element instanceof IMethod) {
IMethod mostAbstractMethod = getMostAbstractDeclaration((IMethod) element);
if (mostAbstractMethod != null) {
Location abstractDefinitionLocation = JdtlsUtils.getElementLocation(mostAbstractMethod);
String id = VisitorUtils.createSymbolKey(abstractDefinitionLocation);
Document abstractdefinitionDocument = Repository.getInstance().enlistDocument(lsif,
CsCherrYY marked this conversation as resolved.
Show resolved Hide resolved
abstractDefinitionLocation.getUri(), this.project);
SymbolData abstractSymbolData = Repository.getInstance().enlistSymbolData(id,
abstractdefinitionDocument, this.project);
Repository.getInstance().addReferenceResults(this, abstractSymbolData);
}
}
}

private IMethod getMostAbstractDeclaration(IMethod method) {
IMethod previous = null;
IMethod current = method;
try {
while (current != null) {
CsCherrYY marked this conversation as resolved.
Show resolved Hide resolved
current = FindLinksHandler.findOverriddenMethod(current, new NullProgressMonitor());
if (current == null) {
return previous;
}
previous = current;
}
} catch (JavaModelException e) {
return null;
}
return null;
}

synchronized public void resolveHover(LsifService lsif, Document docVertex,
Expand Down