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
2 changes: 2 additions & 0 deletions com.microsoft.java.lsif.core/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry exported="true" kind="lib" path="lib/rxjava-2.2.8.jar"/>
<classpathentry exported="true" kind="lib" path="lib/reactive-streams-1.0.0.jar"/>
<classpathentry kind="src" path="src/"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
4 changes: 2 additions & 2 deletions com.microsoft.java.lsif.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.12.0",
org.eclipse.jdt.ls.core;bundle-version="0.32.0",
org.apache.commons.io;bundle-version="2.2.0"
Export-Package: com.microsoft.java.lsif.core.internal
Bundle-ClassPath: lib/jsoup-1.9.2.jar,
lib/remark-1.0.0.jar,
Bundle-ClassPath: lib/rxjava-2.2.8.jar,
lib/reactive-streams-1.0.0.jar,
.
Bundle-Vendor: %Bundle-Vendor
Automatic-Module-Name: org.eclipse.jdt.ls.core
3 changes: 2 additions & 1 deletion com.microsoft.java.lsif.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ bin.includes = META-INF/,\
plugin.xml,\
lifecycle-mapping-metadata.xml,\
plugin.properties,\
pom.xml
pom.xml,\
lib/
src.includes = src/,\
META-INF/,\
plugin.xml,\
Expand Down
28 changes: 28 additions & 0 deletions com.microsoft.java.lsif.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,32 @@
<artifactId>com.microsoft.java.lsif.core</artifactId>
<packaging>eclipse-plugin</packaging>
<name>${base.name} :: Core</name>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.8</version>
</artifactItem>
<artifactItem>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
<version>1.0.0</version>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@
package com.microsoft.java.lsif.core.internal.indexer;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;

import com.microsoft.java.lsif.core.internal.IConstant;

public final class ASTUtil {

/**
Expand All @@ -39,49 +32,36 @@ public static CompilationUnit createAST(final ITypeRoot input, final IProgressMo

final CompilationUnit root[] = new CompilationUnit[1];

SafeRunner.run(new ISafeRunnable() {
@Override
public void run() {
try {
if (progressMonitor != null && progressMonitor.isCanceled()) {
return;
}
if (input instanceof ICompilationUnit) {
ICompilationUnit cu = (ICompilationUnit) input;
if (cu.isWorkingCopy()) {
root[0] = cu.reconcile(IASTSharedValues.SHARED_AST_LEVEL, true, null, progressMonitor);
}
}
if (root[0] == null) {
final ASTParser parser = newASTParser();
parser.setSource(input);
root[0] = (CompilationUnit) parser.createAST(progressMonitor);
}
// mark as unmodifiable
ASTNodes.setFlagsToAST(root[0], ASTNode.PROTECT);
} catch (OperationCanceledException ex) {
return;
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
return;
try {
if (progressMonitor != null && progressMonitor.isCanceled()) {
return null;
}
if (input instanceof ICompilationUnit) {
ICompilationUnit cu = (ICompilationUnit) input;
if (cu.isWorkingCopy()) {
root[0] = cu.reconcile(IASTSharedValues.SHARED_AST_LEVEL, true, null, progressMonitor);
}
}

@Override
public void handleException(Throwable ex) {
IStatus status = new Status(IStatus.ERROR, IConstant.PLUGIN_ID, IStatus.OK,
"Error in JDT Core during AST creation", ex); //$NON-NLS-1$
JavaLanguageServerPlugin.log(status);
if (root[0] == null) {
final ASTParser parser = newASTParser();
parser.setSource(input);
root[0] = (CompilationUnit) parser.createAST(progressMonitor);
}
});
// mark as unmodifiable
ASTNodes.setFlagsToAST(root[0], ASTNode.PROTECT);
} catch (OperationCanceledException ex) {
return null;
} catch (Throwable e) {
JavaLanguageServerPlugin.logException("Error in JDT Core during AST creation", e);
return null;
}
return root[0];
}

public static ASTParser newASTParser() {
final ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
parser.setResolveBindings(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setStatementsRecovery(IASTSharedValues.SHARED_AST_STATEMENT_RECOVERY);
parser.setBindingsRecovery(IASTSharedValues.SHARED_BINDING_RECOVERY);
return parser;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
/* --------------------------------------------------------------------------------------------
* 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.indexer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.lsp4j.ClientCapabilities;

import com.microsoft.java.lsif.core.internal.LanguageServerIndexerPlugin;
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.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.ProtocolVisitor;
import com.microsoft.java.lsif.core.internal.visitors.ReferencesVisitor;
import com.microsoft.java.lsif.core.internal.visitors.TypeDefinitionVisitor;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

public class Indexer {

private WorkspaceHandler handler;

//@formatter:off
private List<ProtocolVisitor> visitors = Arrays.asList(
new DefinitionVisitor(),
new TypeDefinitionVisitor(),
new ImplementationsVisitor(),
new ReferencesVisitor(),
new HoverVisitor());
//@formatter:on

public Indexer() {
this.handler = new WorkspaceHandler(System.getProperty("intellinav.repo.path"));
}
Expand Down Expand Up @@ -78,72 +77,112 @@ private void buildIndex(IPath path, IProgressMonitor monitor, LsifService lsif)

IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

final ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

for (IProject proj : projects) {
if (proj == null) {
return;
}

IJavaProject javaProject = JavaCore.create(proj);
if (!javaProject.exists()) {
continue;
}

Project projVertex = lsif.getVertexBuilder().project();
LsifEmitter.getInstance().emit(projVertex);
IClasspathEntry[] references = javaProject.getRawClasspath();
for (IClasspathEntry reference : references) {
if (reference.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
IPackageFragmentRoot[] fragmentRoots = javaProject.findPackageFragmentRoots(reference);
for (IPackageFragmentRoot fragmentRoot : fragmentRoots) {
for (IJavaElement child : fragmentRoot.getChildren()) {
IPackageFragment fragment = (IPackageFragment) child;
if (fragment.hasChildren()) {
for (IJavaElement sourceFile : fragment.getChildren()) {
if (!sourceFile.exists()) {
continue;
}
CompilationUnit cu = ASTUtil.createAST((ITypeRoot) sourceFile, monitor);

IndexerContext currentContext = new IndexerContext(lsif, null,
(ITypeRoot) sourceFile, JavaLanguageServerPlugin.getPreferencesManager());

Document docVertex = (new DocumentVisitor(currentContext, projVertex))
.enlist(sourceFile);
currentContext.setDocVertex(docVertex);

List<CompletableFuture<Void>> completableFutures = new ArrayList<>();
for (ProtocolVisitor vis : this.visitors) {
vis.setContext(currentContext);
completableFutures.add(CompletableFuture.runAsync(() -> {
cu.accept(vis);
}));
}

DiagnosticVisitor diagnosticVisitor = new DiagnosticVisitor(currentContext, cu);
completableFutures.add(CompletableFuture.runAsync(() -> {
diagnosticVisitor.enlist();
}));
try {
CompletableFuture
.allOf(completableFutures
.toArray(new CompletableFuture[completableFutures.size()]))
.get();
} catch (InterruptedException | ExecutionException e) {
LanguageServerIndexerPlugin.logException("Exception occurs when indexing: ",
e);
}

List<ICompilationUnit> sourceList = getAllSourceFiles(javaProject);

ConcurrentLinkedQueue<ASTTask> taskQueue = new ConcurrentLinkedQueue<>();
buildASTParallely(sourceList, taskQueue, threadPool, projVertex, lsif,
monitor);

dumpParallely(taskQueue, threadPool, lsif);
}

threadPool.shutdown();
}

private void initializeJdtls() {
Map<String, Object> extendedClientCapabilities = new HashMap<>();
extendedClientCapabilities.put("classFileContentsSupport", false);
JavaLanguageServerPlugin.getPreferencesManager().updateClientPrefences(new ClientCapabilities(),
extendedClientCapabilities);
}

private List<ICompilationUnit> getAllSourceFiles(IJavaProject javaProject) throws JavaModelException {
List<ICompilationUnit> res = new LinkedList<>();
IClasspathEntry[] references = javaProject.getRawClasspath();
for (IClasspathEntry reference : references) {
if (reference.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
IPackageFragmentRoot[] fragmentRoots = javaProject.findPackageFragmentRoots(reference);
for (IPackageFragmentRoot fragmentRoot : fragmentRoots) {
for (IJavaElement child : fragmentRoot.getChildren()) {
IPackageFragment fragment = (IPackageFragment) child;
if (fragment.hasChildren()) {
for (IJavaElement sourceFile : fragment.getChildren()) {
if (sourceFile.exists() && sourceFile instanceof ICompilationUnit) {
res.add((ICompilationUnit) sourceFile);
}
}
}
}
}
}
}
return res;
}

private void initializeJdtls() {
Map<String, Object> extendedClientCapabilities = new HashMap<>();
extendedClientCapabilities.put("classFileContentsSupport", false);
JavaLanguageServerPlugin.getPreferencesManager().updateClientPrefences(new ClientCapabilities(),
extendedClientCapabilities);
private void buildASTParallely(List<ICompilationUnit> sourceList,
ConcurrentLinkedQueue<ASTTask> taskQueue, ExecutorService threadPool,
Project projVertex, LsifService lsif, IProgressMonitor monitor) {
Observable.fromIterable(sourceList)
.flatMap(item -> Observable.just(item).observeOn(Schedulers.from(threadPool)).map(sourceFile -> {
CompilationUnit cu = ASTUtil.createAST(sourceFile, monitor);
Document docVertex = (new DocumentVisitor(lsif, projVertex)).enlist(sourceFile);
if (cu == null || docVertex == null) {
return 0;
}
IndexerContext context = new IndexerContext(docVertex, cu);
for (TaskType type : TaskType.values()) {
taskQueue.add(new ASTTask(type, context));
}
return 0;
})).blockingSubscribe();
}

private void dumpParallely(ConcurrentLinkedQueue<ASTTask> taskQueue, ExecutorService threadPool, LsifService lsif) {
Observable.fromIterable(taskQueue)
.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);
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();
}
}
Loading