Skip to content

Commit

Permalink
Issue 638 - Fresh commit on top of main to avoid what resulted in a m…
Browse files Browse the repository at this point in the history
…essage rebase/merge on the original branch.
  • Loading branch information
SCWells72 authored and angelozerr committed Dec 4, 2024
1 parent 3a8f390 commit fa59307
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
******************************************************************************/
package com.redhat.devtools.lsp4ij.features.completion;

import com.intellij.codeInsight.completion.CompletionContributor;
import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.*;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.UserDataHolder;
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;
Expand All @@ -35,9 +35,7 @@
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 +123,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,6 +136,51 @@ private void addCompletionItems(@NotNull CompletionParameters parameters,
LookupElement lookupItem = completionFeature.createLookupElement(item, context);
if (lookupItem != null) {
completionFeature.addLookupItem(context, 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, add completions from other all other contributors except for the word completion contributor.
// Note that the word completion contributor only really kicks in when LSP completions are added in the context
// of a TextMate file (see TextMateCompletionContributor), but that's going to be very common for LSP usage
// since it's often adding (pseudo-)language support when not already present in the host JetBrains IDE.
if ((size > 0) && (parameters.getInvocationCount() < 2)) {
// Don't allow automatic execution of the remaining completion contributors; we'll execute them ourselves below
result.stopHere();

// Try to disable word completion by temporarily setting FORBID_WORD_COMPLETION=true on the completion process.
// Note that this will not work in some older IDE versions where WordCompletionContributor.addWordCompletionVariants()
// did not yet check the value of FORBID_WORD_COMPLETION
CompletionProcess completionProcess = parameters.getProcess();
UserDataHolder userDataHolder = completionProcess instanceof UserDataHolder ? (UserDataHolder) completionProcess : null;
boolean originalForbidWordCompletion = false;
if (userDataHolder != null) {
originalForbidWordCompletion = userDataHolder.getUserData(BaseCompletionService.FORBID_WORD_COMPLETION) == Boolean.TRUE;
userDataHolder.putUserData(BaseCompletionService.FORBID_WORD_COMPLETION, true);
}
try {
if (userDataHolder != null) {
// If we disabled word completion, just run all remaining contributors
result.runRemainingContributors(parameters, true);
} else {
// If not, run all remaining contributors and only add results with distinct lookup strings
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);
}
}
});
}
} finally {
// If appropriate, re-enable word completion
if (userDataHolder != null) {
userDataHolder.putUserData(BaseCompletionService.FORBID_WORD_COMPLETION, originalForbidWordCompletion);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
id="LSPCompletionContributor"
language="any"
implementationClass="com.redhat.devtools.lsp4ij.features.completion.LSPCompletionContributor"
order="first"/>
order="first, before wordCompletion"/>

<!-- LSP textDocument/definition request support -->
<gotoDeclarationHandler
Expand Down

0 comments on commit fa59307

Please sign in to comment.