Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -5,21 +5,34 @@

package com.microsoft.java.lsif.core.internal;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JDTUtils.LocationType;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;

public final class JdtlsUtils {

private static final String JDT_SCHEME = "jdt";

private JdtlsUtils() {
}

Expand All @@ -29,15 +42,15 @@ public final static Location getElementLocation(IJavaElement element) {
ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (compilationUnit != null || (cf != null && cf.getSourceRange() != null)) {
targetLocation = JdtlsUtils.fixLocation(element, JDTUtils.toLocation(element),
targetLocation = JdtlsUtils.fixLocation(element, toLocation(element),
element.getJavaProject());
}
if (element instanceof IMember && ((IMember) element).getClassFile() != null) {
} else if (element instanceof IMember && ((IMember) element).getClassFile() != null) {
targetLocation = JdtlsUtils.fixLocation(element,
JDTUtils.toLocation(((IMember) element).getClassFile()), element.getJavaProject());
toLocation(((IMember) element).getClassFile(), 0, 0), element.getJavaProject());
}
} catch (CoreException ex) {
}

return targetLocation;
}

Expand Down Expand Up @@ -68,6 +81,86 @@ public final static Location fixLocation(IJavaElement element, Location location
return location;
}

/**
* Creates a location for a given java element. Element can be a
* {@link ICompilationUnit} or {@link IClassFile}
*
* @param element
* @return location or null
* @throws JavaModelException
*/
public static Location toLocation(IJavaElement element) throws JavaModelException {
return toLocation(element, LocationType.NAME_RANGE);
}

/**
* Creates a location for a given java element. Unlike {@link #toLocation} this
* method can be called to return with a range that contains surrounding
* comments (method body), not just the name of the Java element. Element can be
* a {@link ICompilationUnit} or {@link IClassFile}
*
* @param element
* @param type the range type. The {@link LocationType#NAME_RANGE name} or
* {@link LocationType#FULL_RANGE full} range.
* @return location or null
* @throws JavaModelException
*/
public static Location toLocation(IJavaElement element, LocationType type) throws JavaModelException {
ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (unit == null && cf == null) {
return null;
}
if (element instanceof ISourceReference) {
ISourceRange nameRange = getNameRange(element);
if (SourceRange.isAvailable(nameRange)) {
if (cf == null) {
return JDTUtils.toLocation(unit, nameRange.getOffset(), nameRange.getLength());
} else {
return toLocation(cf, nameRange.getOffset(), nameRange.getLength());
}
}
}
return null;
}

/**
* Creates location to the given offset and length for the class file.
*
* @param unit
* @param offset
* @param length
* @return location
* @throws JavaModelException
*/
public static Location toLocation(IClassFile classFile, int offset, int length) throws JavaModelException {
String uriString = toUri(classFile);
if (uriString != null) {
Range range = JDTUtils.toRange(classFile, offset, length);
return new Location(uriString, range);
}
return null;
}

public static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
ISourceRange nameRange = null;
if (element instanceof IMember) {
IMember member = (IMember) element;
nameRange = member.getNameRange();
if ((!SourceRange.isAvailable(nameRange))) {
nameRange = member.getSourceRange();
}
} else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
nameRange = ((ISourceReference) element).getNameRange();
} else if (element instanceof ISourceReference) {
nameRange = ((ISourceReference) element).getSourceRange();
}
if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
nameRange = getNameRange(element.getParent());
}
return nameRange;
}

/**
* Normalize the URI to the same format as the client.
*/
Expand All @@ -80,4 +173,19 @@ public final static String normalizeUri(String uri) {
}
return uri;
}

public static String toUri(IClassFile classFile) {
String packageName = classFile.getParent().getElementName();
String jarName = classFile.getParent().getParent().getElementName();
String uriString = null;
try {
uriString = new URI(
JDT_SCHEME, "contents", JDTUtils.PATH_SEPARATOR + jarName + JDTUtils.PATH_SEPARATOR + packageName
+ JDTUtils.PATH_SEPARATOR + classFile.getElementName(),
classFile.getHandleIdentifier(), null).toASCIIString();
} catch (URISyntaxException e) {
LanguageServerIndexerPlugin.logException("Error generating URI for class ", e);
}
return uriString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@
import com.microsoft.java.lsif.core.internal.protocol.Project;
import com.microsoft.java.lsif.core.internal.task.ASTTask;
import com.microsoft.java.lsif.core.internal.task.TaskType;
import com.microsoft.java.lsif.core.internal.visitors.DefinitionVisitor;
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.HoverVisitor;
import com.microsoft.java.lsif.core.internal.visitors.ImplementationsVisitor;
import com.microsoft.java.lsif.core.internal.visitors.ReferencesVisitor;
import com.microsoft.java.lsif.core.internal.visitors.TypeDefinitionVisitor;
import com.microsoft.java.lsif.core.internal.visitors.LsifVisitor;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
Expand All @@ -61,7 +57,6 @@ public Indexer() {
public void generateLsif() throws JavaModelException {
NullProgressMonitor monitor = new NullProgressMonitor();
IPath path = this.handler.initialize();

initializeJdtls();
LsifService lsif = new LsifService();

Expand Down Expand Up @@ -160,31 +155,13 @@ private void dumpParallely(ConcurrentLinkedQueue<ASTTask> taskQueue, ExecutorSer
.flatMap(item -> Observable.just(item).observeOn(Schedulers.from(threadPool)).map(task -> {
switch (task.getTaskType()) {
case DEFINITION:
DefinitionVisitor definitionVisitor = new DefinitionVisitor(lsif, task.getContext());
task.getContext().getCompilationUnit().accept(definitionVisitor);
LsifVisitor lsifVisitor = new LsifVisitor(lsif, task.getContext());
task.getContext().getCompilationUnit().accept(lsifVisitor);
break;
case DIAGNOSTIC:
DiagnosticVisitor diagnosticVisitor = new DiagnosticVisitor(lsif, task.getContext());
diagnosticVisitor.enlist();
break;
case HOVER:
HoverVisitor hoverVisitor = new HoverVisitor(lsif, task.getContext());
task.getContext().getCompilationUnit().accept(hoverVisitor);
break;
case IMPLEMENTATION:
ImplementationsVisitor implementationVisitor = new ImplementationsVisitor(lsif,
task.getContext());
task.getContext().getCompilationUnit().accept(implementationVisitor);
break;
case REFERENCE:
ReferencesVisitor referencesVisitor = new ReferencesVisitor(lsif, task.getContext());
task.getContext().getCompilationUnit().accept(referencesVisitor);
break;
case TYPEDEFINITION:
TypeDefinitionVisitor typeDefinitionVisitor = new TypeDefinitionVisitor(lsif,
task.getContext());
task.getContext().getCompilationUnit().accept(typeDefinitionVisitor);
break;
}
return 0;
})).blockingSubscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.lsp4j.Hover;

import com.microsoft.java.lsif.core.internal.JdtlsUtils;
import com.microsoft.java.lsif.core.internal.emitter.LsifEmitter;
import com.microsoft.java.lsif.core.internal.protocol.Document;
import com.microsoft.java.lsif.core.internal.protocol.HoverResult;
import com.microsoft.java.lsif.core.internal.protocol.Range;
import com.microsoft.java.lsif.core.internal.protocol.ResultSet;
import com.microsoft.java.lsif.core.internal.protocol.SymbolData;

public class Repository {

Expand All @@ -29,13 +26,9 @@ public class Repository {
// LSIF: range
private Map<String, Map<org.eclipse.lsp4j.Range, Range>> rangeMap = new ConcurrentHashMap<>();

// Key: Range
// Value: ResultSet that range refers to
private Map<Range, ResultSet> resultSetMap = new ConcurrentHashMap<>();

// Key: Hash Code of the Hover Content
// Value: HoverResult
private Map<Integer, HoverResult> hoverResultMap = new ConcurrentHashMap<>();
// Key: definition meta-data id
// Value: SymbolData
private Map<String, SymbolData> symbolDataMap = new ConcurrentHashMap<>();

private Repository() {
}
Expand All @@ -60,18 +53,6 @@ public synchronized Document enlistDocument(LsifService service, String uri) {
return targetDocument;
}

public synchronized ResultSet enlistResultSet(LsifService service, Range range) {
ResultSet resultSet = findResultSetByRange(range);
if (resultSet == null) {
resultSet = service.getVertexBuilder().resultSet();
addResultSet(range, resultSet);
LsifEmitter.getInstance().emit(resultSet);
LsifEmitter.getInstance().emit(service.getEdgeBuilder().refersTo(range, resultSet));
}

return resultSet;
}

public synchronized Range enlistRange(LsifService service, Document docVertex,
org.eclipse.lsp4j.Range lspRange) {
Range range = findRange(docVertex.getUri(), lspRange);
Expand All @@ -84,21 +65,19 @@ public synchronized Range enlistRange(LsifService service, Document docVertex,
return range;
}

public synchronized HoverResult enlistHoverResult(LsifService service, Hover hover) {
int contentHash = hover.getContents().hashCode();
HoverResult hoverResult = findHoverResultByHashCode(contentHash);
if (hoverResult == null) {
hoverResult = service.getVertexBuilder().hoverResult(hover);
LsifEmitter.getInstance().emit(hoverResult);
addHoverResult(contentHash, hoverResult);
}
return hoverResult;
}

public Range enlistRange(LsifService service, String uri, org.eclipse.lsp4j.Range lspRange) {
return enlistRange(service, enlistDocument(service, uri), lspRange);
}

public synchronized SymbolData enlistSymbolData(String id) {
SymbolData symbolData = findSymbolDataById(id);
if (symbolData == null) {
symbolData = new SymbolData();
addSymbolData(id, symbolData);
}
return symbolData;
}

private void addDocument(Document doc) {
this.documentMap.put(doc.getUri(), doc);
}
Expand All @@ -109,12 +88,8 @@ private void addRange(Document owner, org.eclipse.lsp4j.Range lspRange, Range ra
ranges.putIfAbsent(lspRange, range);
}

private void addResultSet(Range range, ResultSet resultSet) {
this.resultSetMap.put(range, resultSet);
}

private void addHoverResult(int hashCode, HoverResult hoverResult) {
this.hoverResultMap.put(hashCode, hoverResult);
private void addSymbolData(String id, SymbolData symbolData) {
this.symbolDataMap.put(id, symbolData);
}

private Document findDocumentByUri(String uri) {
Expand All @@ -129,11 +104,7 @@ private Range findRange(String uri, org.eclipse.lsp4j.Range lspRange) {
return null;
}

private ResultSet findResultSetByRange(Range range) {
return this.resultSetMap.getOrDefault(range, null);
}

public HoverResult findHoverResultByHashCode(int hashCode) {
return this.hoverResultMap.getOrDefault(hashCode, null);
private SymbolData findSymbolDataById(String id) {
return this.symbolDataMap.getOrDefault(id, null);
}
}
Loading