Skip to content

Commit

Permalink
Merge pull request #42173 from mindula/refactor-ls-package-loader
Browse files Browse the repository at this point in the history
[master] Refactor ls package loader
  • Loading branch information
KavinduZoysa authored Mar 7, 2024
2 parents 2ce4236 + c70e143 commit 60357b2
Show file tree
Hide file tree
Showing 35 changed files with 693 additions and 475 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public interface InitializationOptions {
*/
String KEY_ENABLE_INLAY_HINTS = "enableInlayHints";

/**
* Whether the LS should index packages from BallerinaUserHome.
*/
String KEY_ENABLE_INDEX_PACKAGES = "enableIndexPackages";

/**
* Whether the client supports memory usage monitor.
*/
Expand Down Expand Up @@ -98,6 +103,13 @@ public interface InitializationOptions {
*/
boolean isEnableInlayHints();

/**
* Returns if the LS has indexed BallerinaUserHome.
*
* @return True if supported, false otherwise
*/
boolean isEnableIndexPackages();

/**
* Returns if the client supports memory usage monitor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.ballerinalang.langserver.commons.registration.BallerinaInitializeParams;
import org.ballerinalang.langserver.commons.registration.BallerinaInitializeResult;
import org.ballerinalang.langserver.commons.service.spi.ExtendedLanguageServerService;
import org.ballerinalang.langserver.completions.providers.context.util.ServiceTemplateGenerator;
import org.ballerinalang.langserver.config.ClientConfigListener;
import org.ballerinalang.langserver.config.LSClientConfig;
import org.ballerinalang.langserver.config.LSClientConfigHolder;
Expand Down Expand Up @@ -216,11 +215,9 @@ public void initialized(InitializedParams params) {
if (lsClientCapabilities.getInitializationOptions().isEnableLightWeightMode()) {
return;
}
//Initialize Service Template Generator.
ServiceTemplateGenerator.getInstance(this.serverContext);
CentralPackageDescriptorLoader.getInstance(this.serverContext)
.loadBallerinaxPackagesFromCentral(this.serverContext);

if (lsClientCapabilities.getInitializationOptions().isEnableIndexPackages()) {
LSPackageLoader.getInstance(this.serverContext).loadModules(this.serverContext);
}
if (lsClientCapabilities.getInitializationOptions().isEnableMemoryUsageMonitor()) {
MemoryUsageMonitor.getInstance(this.serverContext).start(client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,21 @@

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import io.ballerina.projects.PackageName;
import io.ballerina.projects.PackageOrg;
import io.ballerina.projects.PackageVersion;
import io.ballerina.projects.Settings;
import io.ballerina.projects.util.ProjectUtils;
import org.ballerinalang.central.client.CentralAPIClient;
import org.ballerinalang.central.client.model.Package;
import org.ballerinalang.langserver.commons.LanguageServerContext;
import org.ballerinalang.langserver.commons.client.ExtendedLanguageClient;
import org.ballerinalang.langserver.extensions.ballerina.connector.CentralPackageListResult;
import org.eclipse.lsp4j.ProgressParams;
import org.eclipse.lsp4j.WorkDoneProgressBegin;
import org.eclipse.lsp4j.WorkDoneProgressCreateParams;
import org.eclipse.lsp4j.WorkDoneProgressEnd;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.wso2.ballerinalang.util.RepoUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
Expand All @@ -46,9 +42,11 @@
public class CentralPackageDescriptorLoader {
public static final LanguageServerContext.Key<CentralPackageDescriptorLoader> CENTRAL_PACKAGE_HOLDER_KEY =
new LanguageServerContext.Key<>();
private final List<Package> centralPackages = new ArrayList<>();
private final List<LSPackageLoader.ModuleInfo> centralPackages = new ArrayList<>();
private boolean isLoaded = false;

private final LSClientLogger clientLogger;

public static CentralPackageDescriptorLoader getInstance(LanguageServerContext context) {
CentralPackageDescriptorLoader centralPackageDescriptorLoader = context.get(CENTRAL_PACKAGE_HOLDER_KEY);
if (centralPackageDescriptorLoader == null) {
Expand All @@ -59,38 +57,25 @@ public static CentralPackageDescriptorLoader getInstance(LanguageServerContext c

private CentralPackageDescriptorLoader(LanguageServerContext context) {
context.put(CENTRAL_PACKAGE_HOLDER_KEY, this);
this.clientLogger = LSClientLogger.getInstance(context);
}

public void loadBallerinaxPackagesFromCentral(LanguageServerContext lsContext) {
String taskId = UUID.randomUUID().toString();
ExtendedLanguageClient languageClient = lsContext.get(ExtendedLanguageClient.class);
CompletableFuture.runAsync(() -> {
WorkDoneProgressCreateParams workDoneProgressCreateParams = new WorkDoneProgressCreateParams();
workDoneProgressCreateParams.setToken(taskId);
languageClient.createProgress(workDoneProgressCreateParams);

WorkDoneProgressBegin beginNotification = new WorkDoneProgressBegin();
beginNotification.setTitle("Ballerina Central Packages");
beginNotification.setCancellable(false);
beginNotification.setMessage("Loading...");
languageClient.notifyProgress(new ProgressParams(Either.forLeft(taskId),
Either.forLeft(beginNotification)));
}).thenRunAsync(() -> {
centralPackages.addAll(CentralPackageDescriptorLoader.getInstance(lsContext).getPackagesFromCentral());
}).thenRunAsync(() -> {
WorkDoneProgressEnd endNotification = new WorkDoneProgressEnd();
endNotification.setMessage("Loaded Successfully!");
languageClient.notifyProgress(new ProgressParams(Either.forLeft(taskId),
Either.forLeft(endNotification)));
public CompletableFuture<List<LSPackageLoader.ModuleInfo>> getCentralPackages() {
return CompletableFuture.supplyAsync(() -> {
if (!isLoaded) {
//Load packages from central
clientLogger.logTrace("Loading packages from Ballerina Central");
this.getPackagesFromCentral().forEach(packageInfo -> {
PackageOrg packageOrg = PackageOrg.from(packageInfo.getOrganization());
PackageName packageName = PackageName.from(packageInfo.getName());
PackageVersion packageVersion = PackageVersion.from(packageInfo.getVersion());
centralPackages.add(new LSPackageLoader.ModuleInfo(packageOrg, packageName, packageVersion, null));
});
clientLogger.logTrace("Successfully loaded packages from Ballerina Central");
}
isLoaded = true;
return this.centralPackages;
});
isLoaded = true;
}

public List<Package> getCentralPackages(LanguageServerContext context) {
if (!isLoaded) {
loadBallerinaxPackagesFromCentral(context);
}
return centralPackages;
}

private List<Package> getPackagesFromCentral() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ private InitializationOptions parseInitializationOptions(Map<String, Object> ini
Boolean.parseBoolean(String.valueOf(inlayHintsSupport));
initializationOptions.setEnableInlayHints(enableInlayHintsSupport);

//Enable indexing user home by default
Object indexPackages = initOptions.get(InitializationOptions.KEY_ENABLE_INDEX_PACKAGES);
boolean enableIndexPackages = indexPackages == null || Boolean.parseBoolean(String.valueOf(indexPackages));
initializationOptions.setEnableIndexPackages(enableIndexPackages);

Object memoryUsageMonitor = initOptions.get(InitializationOptions.KEY_ENABLE_MEMORY_USAGE_MONITOR);
boolean enableMemoryUsageMonitor = memoryUsageMonitor != null &&
Boolean.parseBoolean(String.valueOf(memoryUsageMonitor));
Expand Down Expand Up @@ -219,8 +224,9 @@ public static class InitializationOptionsImpl implements InitializationOptions {
private boolean enableLSLightWeightMode = false;
private boolean supportPositionalRenamePopup = false;
private boolean enableInlayHints = false;
private boolean enableIndexPackages = false;
private boolean enableMemoryUsageMonitor = false;

@Override
public boolean isBalaSchemeSupported() {
return supportBalaScheme;
Expand Down Expand Up @@ -274,6 +280,14 @@ public void setEnableInlayHints(boolean enableInlayHints) {
this.enableInlayHints = enableInlayHints;
}

@Override
public boolean isEnableIndexPackages() {
return enableIndexPackages;
}
public void setEnableIndexPackages(boolean enableIndexPackages) {
this.enableIndexPackages = enableIndexPackages;
}

@Override
public boolean isEnableMemoryUsageMonitor() {
return enableMemoryUsageMonitor;
Expand Down
Loading

0 comments on commit 60357b2

Please sign in to comment.