Skip to content

Get rid of the duplicated COMPLETION_EXECUTION_TIME in completion items #2635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public List<CompletionItem> getCompletionItems(IProgressMonitor monitor) {
response.setProposals(proposals);
}
response.setItems(completionItems);
response.setUri(this.uri);
response.setCommonData(CompletionResolveHandler.DATA_FIELD_URI, uri);
CompletionResponses.store(response);

return completionItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private static List<CompletionItem> getGenericSnippets(SnippetCompletionContext

response.setProposals(proposals);
response.setItems(res);
response.setUri(uri);
response.setCommonData(CompletionResolveHandler.DATA_FIELD_URI, uri);
CompletionResponses.store(response);
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public List<CompletionItem> complete(IDocument document, int offset, ICompilatio

response.setProposals(proposals);
response.setItems(res);
response.setUri(JDTUtils.toURI(compilationUnit));
response.setCommonData(CompletionResolveHandler.DATA_FIELD_URI, JDTUtils.toURI(compilationUnit));
CompletionResponses.store(response);
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -121,19 +122,35 @@ public Either<List<CompletionItem>, CompletionList> completion(CompletionParams
JavaLanguageServerPlugin.logInfo("Completion request completed");
}
long executionTime = System.currentTimeMillis() - startTime;
String lastRequestId = null;
for (CompletionItem item : $.getItems()) {
String requestId = "";
String proposalId = "";
Map<String, String> data = (Map<String, String>) item.getData();
if (data != null) {
requestId = data.getOrDefault(CompletionResolveHandler.DATA_FIELD_REQUEST_ID, "");
proposalId = data.getOrDefault(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID, "");
data.put(CompletionRanking.COMPLETION_EXECUTION_TIME, String.valueOf(executionTime));
}
if (requestId.isEmpty() || proposalId.isEmpty()) {
continue;
}
item.setCommand(new Command("", "java.completion.onDidSelect", Arrays.asList(
requestId,
proposalId
)));

if (Objects.equals(requestId, lastRequestId)) {
continue;
}
lastRequestId = requestId;
int pId = Integer.parseInt(proposalId);
long rId = Long.parseLong(requestId);
CompletionResponse completionResponse = CompletionResponses.get(rId);
if (completionResponse == null || completionResponse.getProposals().size() <= pId) {
JavaLanguageServerPlugin.logError("Failed to save common data for completion items.");
continue;
}
completionResponse.setCommonData(CompletionRanking.COMPLETION_EXECUTION_TIME, String.valueOf(executionTime));
}
return Either.forRight($);
}
Expand All @@ -155,6 +172,12 @@ public void onDidCompletionItemSelect(String requestId, String proposalId) throw
throw ExceptionFactory.newException("Cannot get the completion item.");
}

// get the cached completion execution time and set it to the selected item in case that providers need it.
String executionTime = completionResponse.getCommonData(CompletionRanking.COMPLETION_EXECUTION_TIME);
if (executionTime != null) {
((Map<String, String>)item.getData()).put(CompletionRanking.COMPLETION_EXECUTION_TIME, executionTime);
}

List<ICompletionRankingProvider> providers =
((CompletionContributionService) JavaLanguageServerPlugin.getCompletionContributionService()).getRankingProviders();
for (ICompletionRankingProvider provider : providers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public CompletionResolveHandler(PreferenceManager manager) {
this.manager = manager;
}

public static final String DATA_FIELD_URI = "uri";
public static final String DATA_FIELD_DECLARATION_SIGNATURE = "decl_signature";
public static final String DATA_FIELD_SIGNATURE= "signature";
public static final String DATA_FIELD_NAME = "name";
Expand All @@ -111,7 +112,7 @@ public CompletionItem resolve(CompletionItem param, IProgressMonitor monitor) {
throw new IllegalStateException("Invalid completion proposal");
}

String uri = completionResponse.getUri();
String uri = completionResponse.getCommonData(DATA_FIELD_URI);
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
if (unit == null) {
throw new IllegalStateException(NLS.bind("Unable to match Compilation Unit from {0} ", uri));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.handlers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.jdt.core.CompletionContext;
Expand All @@ -30,7 +32,10 @@ public class CompletionResponse {
private Long id;
private int offset;
private CompletionContext context;
private String uri;
/**
* Stores the data that are common among the completion items.
*/
private Map<String, String> commonData = new HashMap<>();
private List<CompletionProposal> proposals;
private List<CompletionItem> items;

Expand All @@ -57,18 +62,15 @@ public CompletionContext getContext() {
public void setContext(CompletionContext context) {
this.context = context;
}
/**
* the uri of the document.
*/
public String getUri() {
return uri;

public String getCommonData(String key) {
return this.commonData.get(key);
}
/**
* @param uri the document uri to set.
*/
public void setUri(String uri) {
this.uri = uri;

public void setCommonData(String key, String value) {
this.commonData.put(key, value);
}

/**
* @return the proposals
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.internal.codeassist.impl.AssistOptions;
import org.eclipse.jdt.ls.core.contentassist.CompletionRanking;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JSONUtility;
import org.eclipse.jdt.ls.core.internal.JavaClientConnection;
Expand Down Expand Up @@ -287,11 +288,32 @@ public void testCompletion_dataFieldURI() throws Exception {
long requestId = Long.parseLong(data.get(CompletionResolveHandler.DATA_FIELD_REQUEST_ID));
CompletionResponse completionResponse = CompletionResponses.get(requestId);
assertNotNull(completionResponse);
String uri = completionResponse.getUri();
String uri = completionResponse.getCommonData(CompletionResolveHandler.DATA_FIELD_URI);
assertNotNull(uri);
assertTrue("unexpected URI prefix: " + uri, uri.matches("file://.*/src/java/Foo\\.java"));
}

@Test
public void testCompletion_dataFieldExecutionTime() throws Exception {
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"public class Foo {\n"+
" void foo() {\n"+
" Objec\n"+
" }\n"+
"}\n");
CompletionList list = requestCompletions(unit, "Objec");
assertNotNull(list);
assertFalse("No proposals were found",list.getItems().isEmpty());

Map<String,String> data = (Map<String, String>) list.getItems().get(0).getData();
long requestId = Long.parseLong(data.get(CompletionResolveHandler.DATA_FIELD_REQUEST_ID));
CompletionResponse completionResponse = CompletionResponses.get(requestId);
assertNotNull(completionResponse);
String time = completionResponse.getCommonData(CompletionRanking.COMPLETION_EXECUTION_TIME);
assertNotNull(time);
}


@Test
public void testCompletion_constructor() throws Exception{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ public void testRank() throws Exception {
assertTrue(recommended.getLabel().startsWith("★"));
assertTrue(((Map)recommended.getData()).containsKey("foo"));
assertEquals(recommended.getFilterText(), recommended.getInsertText());
assertTrue(((Map)recommended.getData()).containsKey(CompletionRanking.COMPLETION_EXECUTION_TIME));
}

@Test
public void testOnDidCompletionItemSelect() throws Exception {
CompletionHandler handler = new CompletionHandler(JavaLanguageServerPlugin.getPreferencesManager());
CompletionResponse response = new CompletionResponse();
response.setItems(Arrays.asList(new CompletionItem()));
CompletionItem completionItem = new CompletionItem();
completionItem.setData(new HashMap<>());
response.setItems(Arrays.asList(completionItem));
CompletionResponses.store(response);
handler.onDidCompletionItemSelect(String.valueOf(response.getId()), "0");

Expand Down