Skip to content

Commit

Permalink
Made context-aware code completion sorting an opt-in client-side conf…
Browse files Browse the repository at this point in the history
…iguration option that defaults to disabled. Note that even when disabled, this still includes the changes to use the configured language case-sensitivity when comparing completions as that should likely have been included in the original changes for code completions using language case-sensitivity.
  • Loading branch information
SCWells72 committed Dec 22, 2024
1 parent 3316257 commit 4edcca2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public boolean isCompletionSupported(@NotNull PsiFile file) {
* @param file the file.
* @return true the file associated with a language server can support resolve completion and false otherwise.
*/
public boolean isResolveCompletionSupported(@Nullable PsiFile file) {
public boolean isResolveCompletionSupported(@NotNull PsiFile file) {
return getCompletionCapabilityRegistry().isResolveCompletionSupported(file);
}

Expand Down Expand Up @@ -182,7 +182,7 @@ public Icon getIcon(@NotNull CompletionItem item) {
/**
* Returns true if the IntelliJ lookup is strike out and false otherwise.
*
* @param item
* @param item the completion item
* @return true if the IntelliJ lookup is strike out and false otherwise.
*/
public boolean isStrikeout(@NotNull CompletionItem item) {
Expand Down Expand Up @@ -215,12 +215,12 @@ public boolean isItemTextBold(@NotNull CompletionItem item) {
/**
* Don't override this method, we need to revisit the API and the prefix computation (to customize it).
*
* @param context
* @param completionPrefix
* @param result
* @param lookupItem
* @param priority
* @param item
* @param context the completion context
* @param completionPrefix the completion prefix
* @param result the completion result set
* @param lookupItem the lookup item
* @param priority the completion priority
* @param item the completion item
*/
@ApiStatus.Internal
public void addLookupItem(@NotNull LSPCompletionContext context,
Expand Down Expand Up @@ -260,11 +260,11 @@ public void addLookupItem(@NotNull LSPCompletionContext context,
}

/**
* Returns true if completion item must be resolved and false otherwise when completion item is applied.
* Returns true if completion item must be resolved and false otherwise when completion item is used.
*
* @param item the completion item which is applied.
* @param item the completion item which is used.
* @param file the file.
* @return true if completion item must be resolved and false otherwise when completion item is applied.
* @return true if completion item must be resolved and false otherwise when completion item is used.
*/
public boolean shouldResolveOnApply(@NotNull CompletionItem item,
@NotNull PsiFile file) {
Expand Down Expand Up @@ -293,4 +293,15 @@ public void setServerCapabilities(@Nullable ServerCapabilities serverCapabilitie
completionCapabilityRegistry.setServerCapabilities(serverCapabilities);
}
}

/**
* Determines whether or not client-side context-aware completion sorting should be used for the specified file.
*
* @param file the file
* @return true if client-side context-aware completion sorting should be used; otherwise false
*/
public boolean useContextAwareSorting(@NotNull PsiFile file) {
// Default to disabled
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.features.LSPClientFeatures;
import com.redhat.devtools.lsp4ij.client.features.LSPCompletionFeature;
import com.redhat.devtools.lsp4ij.client.features.LSPCompletionProposal;
import com.redhat.devtools.lsp4ij.client.indexing.ProjectIndexingManager;
Expand Down Expand Up @@ -118,15 +119,19 @@ private void addCompletionItems(@NotNull CompletionParameters parameters,
items.addAll(completionList.getItems());
}

// Sort the completions
PrefixMatcher prefixMatcher = result.getPrefixMatcher();
String currentWord = getCurrentWord(parameters);
boolean caseSensitive = languageServer.getClientFeatures().isCaseSensitive(parameters.getOriginalFile());
PsiFile originalFile = parameters.getOriginalFile();
LSPClientFeatures clientFeatures = languageServer.getClientFeatures();

// Sort the completions as appropriate based on client configuration
boolean useContextAwareSorting = clientFeatures.getCompletionFeature().useContextAwareSorting(originalFile);
PrefixMatcher prefixMatcher = useContextAwareSorting ? result.getPrefixMatcher() : null;
String currentWord = useContextAwareSorting ? getCurrentWord(parameters) : null;
boolean caseSensitive = clientFeatures.isCaseSensitive(originalFile);
items.sort(new CompletionItemComparator(prefixMatcher, currentWord, caseSensitive));
int size = items.size();

Set<String> addedLookupStrings = new HashSet<>();
var completionFeature = languageServer.getClientFeatures().getCompletionFeature();
var completionFeature = clientFeatures.getCompletionFeature();
LSPCompletionFeature.LSPCompletionContext context = new LSPCompletionFeature.LSPCompletionContext(parameters, languageServer);
// Items now sorted by priority, low index == high priority
for (int i = 0; i < size; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@
*/
public class ClientConfigurationSettings {
/**
* Client-side code workspace symbol settings.
* Client-side code completion settings.
*/
public static class ClientConfigurationCompletionSettings {
/**
* Whether or not client-side context-aware completion sorting should be used. Defaults to false.
*/
public boolean useContextAwareSorting = false;
}

/**
* Client-side workspace symbol settings.
*/
public static class ClientConfigurationWorkspaceSymbolSettings {
/**
Expand All @@ -35,7 +45,12 @@ public static class ClientConfigurationWorkspaceSymbolSettings {
public boolean caseSensitive = false;

/**
* Client-side code workspace symbol settings
* Client-side code completion settings
*/
public @NotNull ClientConfigurationCompletionSettings completion = new ClientConfigurationCompletionSettings();

/**
* Client-side workspace symbol settings
*/
public @NotNull ClientConfigurationWorkspaceSymbolSettings workspaceSymbol = new ClientConfigurationWorkspaceSymbolSettings();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public UserDefinedClientFeatures() {
super();

// Use the extended feature implementations
setCompletionFeature(new UserDefinedCompletionFeature());
setWorkspaceSymbolFeature(new UserDefinedWorkspaceSymbolFeature());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.devtools.lsp4ij.server.definition.launching;

import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.client.features.LSPCompletionFeature;
import org.jetbrains.annotations.NotNull;

/**
* Adds client-side code completion configuration features.
*/
public class UserDefinedCompletionFeature extends LSPCompletionFeature {

@Override
public boolean useContextAwareSorting(@NotNull PsiFile file) {
UserDefinedLanguageServerDefinition serverDefinition = (UserDefinedLanguageServerDefinition) getClientFeatures().getServerDefinition();
ClientConfigurationSettings clientConfiguration = serverDefinition.getLanguageServerClientConfiguration();
return clientConfiguration != null ? clientConfiguration.completion.useContextAwareSorting : super.useContextAwareSorting(file);
}
}
13 changes: 13 additions & 0 deletions src/main/resources/jsonSchema/clientSettings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
"description": "Whether or not the language grammar is case-sensitive.",
"default": false
},
"completion": {
"type": "object",
"title": "Client-side completion configuration",
"additionalProperties": false,
"properties": {
"useContextAwareSorting": {
"type": "boolean",
"title": "Use context-aware completion sorting",
"description": "Whether or not client-side context-aware completion sorting should be used.",
"default": false
}
}
},
"workspaceSymbol": {
"type": "object",
"title": "Client-side workspace symbol configuration",
Expand Down

0 comments on commit 4edcca2

Please sign in to comment.