Skip to content

Commit

Permalink
WIP completiom refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Dec 4, 2019
1 parent 2b51f8c commit e156a5b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.jdt.ls.core.internal.handlers.CompletionResolveHandler;
import org.eclipse.jdt.ls.core.internal.handlers.CompletionResponse;
import org.eclipse.jdt.ls.core.internal.handlers.CompletionResponses;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jface.text.Region;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
Expand All @@ -55,6 +56,12 @@ public final class CompletionProposalRequestor extends CompletionRequestor {
private CompletionResponse response;
private boolean fIsTestCodeExcluded;
private CompletionContext context;
private boolean isComplete = true;
private PreferenceManager preferenceManager;

public boolean isComplete() {
return isComplete;
}

// Update SUPPORTED_KINDS when mapKind changes
// @formatter:off
Expand All @@ -73,8 +80,9 @@ public final class CompletionProposalRequestor extends CompletionRequestor {
CompletionItemKind.Text);
// @formatter:on

public CompletionProposalRequestor(ICompilationUnit aUnit, int offset) {
public CompletionProposalRequestor(ICompilationUnit aUnit, int offset, PreferenceManager preferenceManager) {
this.unit = aUnit;
this.preferenceManager = preferenceManager;
response = new CompletionResponse();
response.setOffset(offset);
fIsTestCodeExcluded = !isTestSource(unit.getJavaProject(), unit);
Expand Down Expand Up @@ -122,11 +130,31 @@ public void accept(CompletionProposal proposal) {
}

public List<CompletionItem> getCompletionItems() {
proposals.sort((p1, p2) -> {
int res = p2.getRelevance() - p1.getRelevance();
if (res == 0) {
res = p1.getCompletion().length - p2.getCompletion().length;
}
if (res == 0) {
res = String.valueOf(p2.getCompletion()).compareTo(String.valueOf(p1.getCompletion()));
}
return res;
});
response.setProposals(proposals);
CompletionResponses.store(response);
List<CompletionItem> completionItems = new ArrayList<>(proposals.size());
for (int i = 0; i < proposals.size(); i++) {
completionItems.add(toCompletionItem(proposals.get(i), i));
int maxCompletions = preferenceManager.getPreferences().getMaxCompletionResults();
if (proposals.size() > maxCompletions) {
//we keep receiving completions past our capacity so that makes the whole result incomplete
isComplete = false;
}

CompletionProposalReplacementProvider proposalProvider = new CompletionProposalReplacementProvider(unit, getContext(), response.getOffset(), preferenceManager.getClientPreferences());
for (int i = 0; i < Math.min(proposals.size(), maxCompletions); i++) {
CompletionProposal proposal = proposals.get(i);
CompletionItem item = toCompletionItem(proposal, i);
proposalProvider.updateReplacement(proposal, item, '\0');
completionItems.add(item);
}
return completionItems;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import java.util.List;
import java.util.Set;

import com.google.common.collect.Sets;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.ProgressMonitorWrapper;
Expand All @@ -37,17 +35,24 @@
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

import com.google.common.collect.Sets;

public class CompletionHandler{

public final static CompletionOptions DEFAULT_COMPLETION_OPTIONS = new CompletionOptions(Boolean.TRUE, Arrays.asList(".", "@", "#", "*"));
private static final Set<String> UNSUPPORTED_RESOURCES = Sets.newHashSet("module-info.java", "package-info.java");
private PreferenceManager manager;

public CompletionHandler(PreferenceManager manager) {
this.manager = manager;
}

Either<List<CompletionItem>, CompletionList> completion(CompletionParams position,
IProgressMonitor monitor) {
List<CompletionItem> completionItems = null;
CompletionList $ = null;
try {
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri());
completionItems = this.computeContentAssist(unit,
$ = this.computeContentAssist(unit,
position.getPosition().getLine(),
position.getPosition().getCharacter(), monitor);
} catch (OperationCanceledException ignorable) {
Expand All @@ -57,27 +62,31 @@ Either<List<CompletionItem>, CompletionList> completion(CompletionParams positio
JavaLanguageServerPlugin.logException("Problem with codeComplete for " + position.getTextDocument().getUri(), e);
monitor.setCanceled(true);
}
CompletionList $ = new CompletionList();
if ($ == null) {
$ = new CompletionList();
}
if ($.getItems() == null) {
$.setItems(Collections.emptyList());
}
if (monitor.isCanceled()) {
$.setIsIncomplete(true);
completionItems = null;
//completionItems = null;
JavaLanguageServerPlugin.logInfo("Completion request cancelled");
} else {
JavaLanguageServerPlugin.logInfo("Completion request completed");
}
$.setItems(completionItems == null ? Collections.emptyList() : completionItems);
return Either.forRight($);
}

private List<CompletionItem> computeContentAssist(ICompilationUnit unit, int line, int column, IProgressMonitor monitor) throws JavaModelException {
private CompletionList computeContentAssist(ICompilationUnit unit, int line, int column, IProgressMonitor monitor) throws JavaModelException {
CompletionResponses.clear();
if (unit == null) {
return Collections.emptyList();
return null;
}
List<CompletionItem> proposals = new ArrayList<>();

final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset);
CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset, manager);
// Allow completions for unresolved types - since 3.3
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
Expand All @@ -93,7 +102,6 @@ private List<CompletionItem> computeContentAssist(ICompilationUnit unit, int lin
collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);

collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);

collector.setFavoriteReferences(getFavoriteStaticMembers());

if (offset >-1 && !monitor.isCanceled()) {
Expand Down Expand Up @@ -126,7 +134,9 @@ public boolean isCanceled() {
}
}
}
return proposals;
CompletionList list = new CompletionList(proposals);
list.setIsIncomplete(!collector.isComplete());
return list;
}

private String[] getFavoriteStaticMembers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JSONUtility;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalReplacementProvider;
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalRequestor;
import org.eclipse.jdt.ls.core.internal.javadoc.JavadocContentAccess;
import org.eclipse.jdt.ls.core.internal.javadoc.JavadocContentAccess2;
Expand Down Expand Up @@ -92,11 +91,11 @@ public CompletionItem resolve(CompletionItem param, IProgressMonitor monitor) {
if (unit == null) {
throw new IllegalStateException(NLS.bind("Unable to match Compilation Unit from {0} ", uri));
}
CompletionProposalReplacementProvider proposalProvider = new CompletionProposalReplacementProvider(unit,
completionResponse.getContext(),
completionResponse.getOffset(),
this.manager.getClientPreferences());
proposalProvider.updateReplacement(completionResponse.getProposals().get(proposalId), param, '\0');
// CompletionProposalReplacementProvider proposalProvider = new CompletionProposalReplacementProvider(unit,
// completionResponse.getContext(),
// completionResponse.getOffset(),
// this.manager.getClientPreferences());
// proposalProvider.updateReplacement(completionResponse.getProposals().get(proposalId), param, '\0');
if (monitor.isCanceled()) {
param.setData(null);
return param;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
@Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(CompletionParams position) {
logInfo(">> document/completion");
CompletionHandler handler = new CompletionHandler();
CompletionHandler handler = new CompletionHandler(preferenceManager);
final IProgressMonitor[] monitors = new IProgressMonitor[1];
CompletableFuture<Either<List<CompletionItem>, CompletionList>> result = computeAsync((monitor) -> {
monitors[0] = monitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ public class Preferences {
public static final String JAVA_COMPLETION_FAVORITE_MEMBERS_KEY = "java.completion.favoriteStaticMembers";
public static final List<String> JAVA_COMPLETION_FAVORITE_MEMBERS_DEFAULT;

/**
* Preference key for maximum number of completion results to be returned.
* Defaults to 30.
*/
public static final String JAVA_COMPLETION_MAX_RESULTS_KEY = "java.completion.maxResults";
public static final int JAVA_COMPLETION_MAX_RESULTS_DEFAULT = 30;

/**
* A named preference that controls if the Java code assist only inserts
* completions. When set to true, code completion overwrites the current text.
Expand Down Expand Up @@ -359,8 +366,8 @@ public class Preferences {
private String formatterProfileName;
private Collection<IPath> rootPaths;
private Collection<IPath> triggerFiles;

private int parallelBuildsCount;
private int maxCompletionResults;

static {
JAVA_IMPORT_EXCLUSIONS_DEFAULT = new LinkedList<>();
Expand Down Expand Up @@ -471,6 +478,7 @@ public Preferences() {
importOrder = JAVA_IMPORT_ORDER_DEFAULT;
filteredTypes = JAVA_COMPLETION_FILTERED_TYPES_DEFAULT;
parallelBuildsCount = PreferenceInitializer.PREF_MAX_CONCURRENT_BUILDS_DEFAULT;
maxCompletionResults = JAVA_COMPLETION_MAX_RESULTS_DEFAULT;
}

/**
Expand Down Expand Up @@ -610,6 +618,9 @@ public static Preferences createFrom(Map<String, Object> configuration) {
maxConcurrentBuilds = maxConcurrentBuilds >= 1 ? maxConcurrentBuilds : 1;
prefs.setMaxBuildCount(maxConcurrentBuilds);

int maxCompletions = getInt(configuration, JAVA_COMPLETION_MAX_RESULTS_KEY, JAVA_COMPLETION_MAX_RESULTS_DEFAULT);
prefs.setMaxCompletionResults(maxCompletions);

return prefs;
}

Expand Down Expand Up @@ -1055,7 +1066,17 @@ public boolean isJavaFormatOnTypeEnabled() {
return javaFormatOnTypeEnabled;
}

public void setJavaFormatOnTypeEnabled(boolean javaFormatOnTypeEnabled) {
public Preferences setJavaFormatOnTypeEnabled(boolean javaFormatOnTypeEnabled) {
this.javaFormatOnTypeEnabled = javaFormatOnTypeEnabled;
return this;
}

public int getMaxCompletionResults() {
return maxCompletionResults;
}

public Preferences setMaxCompletionResults(int maxCompletions) {
this.maxCompletionResults = maxCompletions;
return this;
}
}

0 comments on commit e156a5b

Please sign in to comment.