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: 1 addition & 1 deletion docs/LSPSupport.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Current state of [Language Features]( https://microsoft.github.io/language-serve
* ✅ [textDocument/hover](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover) (see [implementation details](#hover))
* ✅ [textDocument/codeLens](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeLens) (see [implementation details](#codeLens))
* ✅ [codeLens/resolve](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeLens_resolve)
* ❌ [workspace/codeLens/refresh](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeLens_refresh)
* ✅ [workspace/codeLens/refresh](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeLens_refresh)
* ✅ [textDocument/inlayHint](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint) (see [implementation details](#inlay-hint))
* ❌ [inlayHint/resolve](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#inlayHint_resolve)
* ✅ [workspace/inlayHint/refresh](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_inlayHint_refresh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,15 @@
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.client.CoalesceByKey;
import com.redhat.devtools.lsp4ij.internal.PromiseToCompletableFuture;
import com.redhat.devtools.lsp4ij.client.ProjectIndexingManager;
import com.redhat.devtools.lsp4ij.lifecycle.LanguageServerLifecycleManager;
import org.jetbrains.annotations.NotNull;

import java.text.MessageFormat;

/**
* Track file opened / closed to start language servers / disconnect file from language servers.
*/
public class ConnectDocumentToLanguageServerSetupParticipant implements ProjectManagerListener, FileEditorManagerListener {

private static class ConnectToLanguageServerCompletableFuture extends PromiseToCompletableFuture<Void> {

private static final String MESSAGE_KEY = "Connect ''{0}'' file to language servers.";

public ConnectToLanguageServerCompletableFuture(@NotNull VirtualFile file, @NotNull Project project) {
super(monitor -> {
connectToLanguageServer(file, project);
return null;
}, MessageFormat.format(MESSAGE_KEY, file.getUrl()), project, null, new CoalesceByKey(ConnectDocumentToLanguageServerSetupParticipant.class.getName(), file.getUrl()));
init();
}
}

@Override
public void projectOpened(@NotNull Project project) {
project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
Expand All @@ -59,7 +43,12 @@ public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile f
// to avoid starting language server in the EDT Thread which could freeze IJ.
// Wait for indexing is finished and read action is enabled
// --> force the start of all languages servers mapped with the given file when indexing is finished and read action is allowed
new ConnectToLanguageServerCompletableFuture(file, project);
ProjectIndexingManager
.waitForIndexingAll()
.thenApplyAsync(unused -> {
connectToLanguageServer(file, project);
return null;
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.client.features.LSPClientFeatures;
import com.redhat.devtools.lsp4ij.features.semanticTokens.SemanticTokensColorsProvider;
import com.redhat.devtools.lsp4ij.server.LanguageServerException;
import com.redhat.devtools.lsp4ij.server.Lease;
import com.redhat.devtools.lsp4ij.server.ServerWasStoppedException;
import com.redhat.devtools.lsp4ij.server.definition.LanguageServerDefinition;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.RenameOptions;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.lsp4j.services.TextDocumentService;
Expand All @@ -39,7 +41,7 @@ public class LanguageServerItem {
private final LanguageServer server;
private final ServerCapabilities serverCapabilities;

public LanguageServerItem(LanguageServer server, LanguageServerWrapper serverWrapper) {
public LanguageServerItem(@Nullable LanguageServer server, @NotNull LanguageServerWrapper serverWrapper) {
this.server = server;
this.serverWrapper = serverWrapper;
this.serverCapabilities = serverWrapper.getServerCapabilities();
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,19 +547,21 @@ private void shutdownAll(LanguageServer languageServerInstance, StreamConnection
// - exit

// shutdown the language server
try {
shutdownLanguageServerInstance(languageServerInstance);
} catch (Exception ex) {
getLanguageServerLifecycleManager().onError(this, ex);
}
if (provider.isAlive()) {
try {
shutdownLanguageServerInstance(languageServerInstance);
} catch (Exception ex) {
getLanguageServerLifecycleManager().onError(this, ex);
}

// exit the language server
// Consume language server exit() before cancelling launcher future (serverFuture.cancel())
// to avoid having error like "The pipe is being closed".
try {
exitLanguageServerInstance(languageServerInstance);
} catch (Exception ex) {
getLanguageServerLifecycleManager().onError(this, ex);
// exit the language server
// Consume language server exit() before cancelling launcher future (serverFuture.cancel())
// to avoid having error like "The pipe is being closed".
try {
exitLanguageServerInstance(languageServerInstance);
} catch (Exception ex) {
getLanguageServerLifecycleManager().onError(this, ex);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,22 @@ public boolean hasAny(@NotNull VirtualFile file,
.stream()
.filter(LanguageServerWrapper::isEnabled)
.filter(wrapper -> wrapper.getClientFeatures().isEnabled(file))
.map(wrapper ->
wrapper.getInitializedServer()
.thenComposeAsync(server -> {
if (server != null &&
(afterStartingServerFilter == null || afterStartingServerFilter.test(wrapper.getClientFeatures()))) {
return wrapper.connect(file, document);
}
return CompletableFuture.completedFuture(null);
}).thenAccept(server -> {
if (server != null) {
servers.add(new LanguageServerItem(server, wrapper));
}
})).toArray(CompletableFuture[]::new)))
.map(wrapper -> {
var clientFeatures = wrapper.getClientFeatures();
return wrapper.getInitializedServer()
.thenComposeAsync(server -> {
if (server != null &&
(afterStartingServerFilter == null || afterStartingServerFilter.test(clientFeatures))) {
return wrapper.connect(file, document);
}
return CompletableFuture.completedFuture(null);
}).thenAccept(server -> {
if (server != null) {
servers.add(new LanguageServerItem(server, wrapper));
}
});
}
).toArray(CompletableFuture[]::new)))
.thenApply(theVoid -> servers);
} catch (final ProcessCanceledException cancellation) {
throw cancellation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.lsp4ij.client;

/**
* Execute LSP feature status
*/
public enum ExecuteLSPFeatureStatus {

NOW, // The LSP feature can be executed now for a given file.
AFTER_INDEXING, // The LSP feature can be executed for a given file after the indexing project
NOT // The LSP feature cannot be executed for a given file.
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -51,7 +52,12 @@ protected <R> CompletableFuture<R> runAsBackground(String progressTitle, Functio
* @return the output of the function code
*/
protected <R> CompletableFuture<R> runAsBackground(String progressTitle, Function<ProgressIndicator, R> code, Object coalesceBy) {
return new LSPCompletableFuture<>(code, progressTitle, IndexAwareLanguageClient.this, coalesceBy);
var waitForIndexing = ProjectIndexingManager.waitForIndexingAll();
var future = new LSPCompletableFuture<>(code, progressTitle, IndexAwareLanguageClient.this, coalesceBy);
var root = waitForIndexing
.thenComposeAsync(unused -> future);
CancellationSupport.forwardCancellation(root,waitForIndexing, future);
return root;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.lsp4ij.client;

import com.intellij.util.indexing.diagnostic.ProjectDumbIndexingHistory;
import com.intellij.util.indexing.diagnostic.ProjectIndexingActivityHistoryListener;
import com.intellij.util.indexing.diagnostic.ProjectScanningHistory;
import com.redhat.devtools.lsp4ij.internal.editor.EditorFeatureManager;
import com.redhat.devtools.lsp4ij.internal.editor.EditorFeatureType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;

/**
* {@link ProjectIndexingActivityHistoryListener} implementation to track project indexing process (dum, scannings files, etc).
*/
@ApiStatus.Internal
public class ProjectIndexingListener implements ProjectIndexingActivityHistoryListener {

@Override
public void onStartedDumbIndexing(@NotNull ProjectDumbIndexingHistory history) {
ProjectIndexingManager.getInstance(history.getProject()).dumbIndexing = true;
}

@Override
public void onFinishedDumbIndexing(@NotNull ProjectDumbIndexingHistory history) {
ProjectIndexingManager manager = ProjectIndexingManager.getInstance(history.getProject());
manager.dumbIndexing = false;
refreshEditorsFeaturesNeeded(manager);
}

@Override
public void onStartedScanning(@NotNull ProjectScanningHistory history) {
ProjectIndexingManager.getInstance(history.getProject()).scanning = true;
}

@Override
public void onFinishedScanning(@NotNull ProjectScanningHistory history) {
ProjectIndexingManager manager = ProjectIndexingManager.getInstance(history.getProject());
manager.scanning = false;
refreshEditorsFeaturesNeeded(manager);
}

private void refreshEditorsFeaturesNeeded(ProjectIndexingManager manager) {
if (!manager.isIndexingAll()) {
// All opened project are indexed,
// refresh all editors which edit the files to refresh
while(!manager.filesToRefresh.isEmpty()) {
var files = new HashSet<>(manager.filesToRefresh);
for(var file : files) {
// Refresh all features (code vision, inlay hints, folding,etc)
// of editors which edit the current file.
EditorFeatureManager.getInstance(manager.project)
.refreshEditorFeature(file, EditorFeatureType.ALL, true);
}
manager.filesToRefresh.removeAll(files);
}
}
}
}
Loading