Skip to content

Commit

Permalink
feat: Add contents to the Document vertex (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Aug 14, 2019
1 parent 11211e8 commit 8e38d7a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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;
Expand All @@ -23,6 +22,9 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
import org.eclipse.jdt.core.util.ClassFormatException;
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;
Expand All @@ -31,6 +33,9 @@

public final class JdtlsUtils {

public static final String DISASSEMBLED_HEADER = " // Failed to get sources. Instead, stub sources have been generated by the disassembler.\n"
+ " // Implementation of methods is unavailable.\n";
private static final String LF = "\n";
private static final String JDT_SCHEME = "jdt";

private JdtlsUtils() {
Expand Down Expand Up @@ -161,19 +166,6 @@ public static ISourceRange getNameRange(IJavaElement element) throws JavaModelEx
return nameRange;
}

/**
* Normalize the URI to the same format as the client.
*/
public final static String normalizeUri(String uri) {
if (Platform.OS_WIN32.equals(Platform.getOS())) {
if (uri.startsWith("file:///") && uri.length() > 10 && Character.isUpperCase(uri.charAt(8))
&& uri.charAt(9) == ':') {
return "file:///" + Character.toLowerCase(uri.charAt(8)) + uri.substring(9);
}
}
return uri;
}

public static String toUri(IClassFile classFile) {
String packageName = classFile.getParent().getElementName();
String jarName = classFile.getParent().getParent().getElementName();
Expand All @@ -188,4 +180,52 @@ public static String toUri(IClassFile classFile) {
}
return uriString;
}

public static String getDocumentContent(String uriString) {
ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(uriString);
try {
if (compilationUnit != null) {
return compilationUnit.getSource();
}

IClassFile classFile = JDTUtils.resolveClassFile(uriString);
if (classFile != null) {
return getSource(classFile);
}
} catch (JavaModelException e) {
LanguageServerIndexerPlugin.logException("Error getting the class file source", e);
}

return "";
}

private static String getSource(IClassFile classFile) {
String classSource;
try {
classSource = classFile.getSource();
if (classSource != null) {
return classSource;
}
return getContent(classFile.getBytes());
} catch (JavaModelException e) {
LanguageServerIndexerPlugin.logException("Error getting the class file source", e);
}

return "";
}

private static String getContent(byte[] bytes) {
ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
String disassembledByteCode = null;
try {
disassembledByteCode = disassembler.disassemble(bytes, LF, ClassFileBytesDisassembler.WORKING_COPY);
if (disassembledByteCode != null) {
return DISASSEMBLED_HEADER + disassembledByteCode;
}
} catch (ClassFormatException e) {
LanguageServerIndexerPlugin.logException("Error disassembling", e);
}

return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

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

import java.util.Base64;

import org.eclipse.core.runtime.Platform;

public class LsifUtils {

private LsifUtils() {
}

/**
* Normalize the URI to the same format as the client.
*/
public final static String normalizeUri(String uri) {
if (Platform.OS_WIN32.equals(Platform.getOS())) {
if (uri.startsWith("file:///") && uri.length() > 10 && Character.isUpperCase(uri.charAt(8))
&& uri.charAt(9) == ':') {
return "file:///" + Character.toLowerCase(uri.charAt(8)) + uri.substring(9);
}
}
return uri;
}

public static String encodeToBase64(String input) {
return Base64.getEncoder().withoutPadding().encodeToString(input.getBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.microsoft.java.lsif.core.internal.JdtlsUtils;
import com.microsoft.java.lsif.core.internal.LsifUtils;
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.Range;
Expand Down Expand Up @@ -42,7 +42,7 @@ public static Repository getInstance() {
}

public synchronized Document enlistDocument(LsifService service, String uri) {
uri = JdtlsUtils.normalizeUri(uri);
uri = LsifUtils.normalizeUri(uri);
Document targetDocument = findDocumentByUri(uri);
if (targetDocument == null) {
targetDocument = service.getVertexBuilder().document(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import com.microsoft.java.lsif.core.internal.JdtlsUtils;
import com.microsoft.java.lsif.core.internal.LsifUtils;
import com.microsoft.java.lsif.core.internal.protocol.DefinitionResult;
import com.microsoft.java.lsif.core.internal.protocol.DiagnosticResult;
import com.microsoft.java.lsif.core.internal.protocol.Document;
Expand Down Expand Up @@ -44,8 +45,10 @@ public Project project() {
}

public Document document(String uri) {
uri = JdtlsUtils.normalizeUri(uri);
uri = LsifUtils.normalizeUri(uri);
Document res = new Document(generator.next(), uri);
String base64Contents = LsifUtils.encodeToBase64(JdtlsUtils.getDocumentContent(uri));
res.setContents(base64Contents);
return res;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Document extends Vertex {

private String languageId;

private String contents;

public Document(String id, String uri) {
super(id, Vertex.DOCUMENT);
this.uri = uri;
Expand All @@ -26,4 +28,12 @@ public String getUri() {
public String getLanguageId() {
return this.languageId;
}

public String getContents() {
return contents;
}

public void setContents(String contents) {
this.contents = contents;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import com.microsoft.java.lsif.core.internal.JdtlsUtils;
import com.microsoft.java.lsif.core.internal.LsifUtils;
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 @@ -96,7 +96,7 @@ public static void ensureImplementationResult(LsifService lsif, ResultSet result
/* reference */
public static boolean isDefinitionItself(Document sourceDoc, Range sourceRange, Document definitionDoc,
Range definitionRange) {
return JdtlsUtils.normalizeUri(definitionDoc.getUri()).equals(JdtlsUtils.normalizeUri(sourceDoc.getUri()))
return LsifUtils.normalizeUri(definitionDoc.getUri()).equals(LsifUtils.normalizeUri(sourceDoc.getUri()))
&& sourceRange.equals(definitionRange);
}

Expand Down

0 comments on commit 8e38d7a

Please sign in to comment.