Skip to content

Commit

Permalink
Issue 638 - Now when we stop automatically collection completions fro…
Browse files Browse the repository at this point in the history
…m other contributors, we still execute all remaining contributors but don't add completions that are duplicates (in terms of the lookup string) of what has already been added in a first-class manner from LSP. This addresses both the unit test failure which was due to a missing active lookup from having stopped completions early and the concerns about not collecting valid completions from other contributors.
  • Loading branch information
SCWells72 committed Nov 30, 2024
1 parent 980a471 commit 269417c
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.util.containers.ContainerUtil;
import com.redhat.devtools.lsp4ij.LSPFileSupport;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServerItem;
import com.redhat.devtools.lsp4ij.client.ExecuteLSPFeatureStatus;
import com.redhat.devtools.lsp4ij.client.indexing.ProjectIndexingManager;
import com.redhat.devtools.lsp4ij.client.features.LSPCompletionFeature;
import com.redhat.devtools.lsp4ij.client.features.LSPCompletionProposal;
import com.redhat.devtools.lsp4ij.client.indexing.ProjectIndexingManager;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -125,6 +124,7 @@ private void addCompletionItems(@NotNull CompletionParameters parameters,
items.sort(completionProposalComparator);
int size = items.size();

Set<String> addedLookupStrings = new HashSet<>();
var completionFeature = languageServer.getClientFeatures().getCompletionFeature();
LSPCompletionFeature.LSPCompletionContext context = new LSPCompletionFeature.LSPCompletionContext(parameters, languageServer);
// Items now sorted by priority, low index == high priority
Expand All @@ -137,13 +137,25 @@ private void addCompletionItems(@NotNull CompletionParameters parameters,
LookupElement lookupItem = completionFeature.createLookupElement(item, context);
if (lookupItem != null) {
completionFeature.addLookupItem(completionPrefix, result, lookupItem, size- i, item);
ContainerUtil.addIfNotNull(addedLookupStrings, lookupItem.getLookupString());
}
}

// If completions were added from LSP and this is auto-completion or the explicit completion trigger was typed
// only once, don't show completions from other contributors, specifically the word completion contributor
// only once, add completions from other contributors carefully to avoid duplicates, specifically from the word
// completion contributor
if ((size > 0) && (parameters.getInvocationCount() < 2)) {
result.stopHere();
result.runRemainingContributors(parameters, completionResult -> {
LookupElement lookupElement = completionResult.getLookupElement();
if (lookupElement != null) {
String lookupString = lookupElement.getLookupString();
if (!addedLookupStrings.contains(lookupString)) {
result.consume(lookupElement);
addedLookupStrings.add(lookupString);
}
}
});
}
}

Expand Down

0 comments on commit 269417c

Please sign in to comment.