Skip to content

Commit

Permalink
Store the uri of document into CompletionResponse (#2614)
Browse files Browse the repository at this point in the history
- instead of storing the uri into the data field of each completion item,
  now we store the uri into the CompletionResponse, and get it during
  resolving.
- Triggering completion via 'S' in Spring Petclinic project, the data
  size can be reduced from 3.05MB to 2.63MB. (Directly copy the trace
  to a text file)

Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored Apr 24, 2023
1 parent 96e1185 commit 1259f6c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public List<CompletionItem> getCompletionItems(IProgressMonitor monitor) {
response.setProposals(proposals);
}
response.setItems(completionItems);
response.setUri(this.uri);
CompletionResponses.store(response);

return completionItems;
Expand Down Expand Up @@ -339,7 +340,6 @@ public CompletionItem toCompletionItem(CompletionProposal proposal, int index) {
}
Map<String, String> data = new HashMap<>();
// append data field so that resolve request can use it.
data.put(CompletionResolveHandler.DATA_FIELD_URI, uri);
data.put(CompletionResolveHandler.DATA_FIELD_REQUEST_ID, String.valueOf(response.getId()));
data.put(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID, String.valueOf(index));
$.setData(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public List<CompletionItem> getProposals(ICompilationUnit cu, int offset, Comple
}
ci.setDocumentation(documentation);
Map<String, String> data = new HashMap<>(3);
data.put(CompletionResolveHandler.DATA_FIELD_URI, JDTUtils.toURI(cu));
data.put(CompletionResolveHandler.DATA_FIELD_REQUEST_ID, "0");
data.put(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID, "0");
ci.setData(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ private static List<CompletionItem> getGenericSnippets(SnippetCompletionContext
item.setLabelDetails(itemLabelDetails);
}

Map<String, String> data = new HashMap<>(3);
data.put(CompletionResolveHandler.DATA_FIELD_URI, uri);
Map<String, String> data = new HashMap<>(2);
data.put(CompletionResolveHandler.DATA_FIELD_REQUEST_ID, String.valueOf(response.getId()));
data.put(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID, String.valueOf(i));
item.setData(data);
Expand All @@ -358,6 +357,7 @@ private static List<CompletionItem> getGenericSnippets(SnippetCompletionContext

response.setProposals(proposals);
response.setItems(res);
response.setUri(uri);
CompletionResponses.store(response);
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ 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 @@ -99,7 +98,7 @@ public CompletionItem resolve(CompletionItem param, IProgressMonitor monitor) {
Map<String, String> data = JSONUtility.toModel(param.getData(),Map.class);
// clean resolve data
param.setData(null);
if (!CompletionProposalRequestor.SUPPORTED_KINDS.contains(param.getKind()) || data == null || !data.containsKey(DATA_FIELD_URI) || !data.containsKey(DATA_FIELD_REQUEST_ID) || !data.containsKey(DATA_FIELD_PROPOSAL_ID)) {
if (!CompletionProposalRequestor.SUPPORTED_KINDS.contains(param.getKind()) || data == null || !data.containsKey(DATA_FIELD_REQUEST_ID) || !data.containsKey(DATA_FIELD_PROPOSAL_ID)) {
return param;
}
int proposalId = Integer.parseInt(data.get(DATA_FIELD_PROPOSAL_ID));
Expand All @@ -108,7 +107,8 @@ public CompletionItem resolve(CompletionItem param, IProgressMonitor monitor) {
if (completionResponse == null || completionResponse.getProposals().size() <= proposalId) {
throw new IllegalStateException("Invalid completion proposal");
}
String uri = data.get(DATA_FIELD_URI);

String uri = completionResponse.getUri();
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 @@ -30,6 +30,7 @@ public class CompletionResponse {
private Long id;
private int offset;
private CompletionContext context;
private String uri;
private List<CompletionProposal> proposals;
private List<CompletionItem> items;

Expand All @@ -56,6 +57,18 @@ public CompletionContext getContext() {
public void setContext(CompletionContext context) {
this.context = context;
}
/**
* the uri of the document.
*/
public String getUri() {
return uri;
}
/**
* @param uri the document uri to set.
*/
public void setUri(String uri) {
this.uri = uri;
}
/**
* @return the proposals
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ public void testCompletionOnSingleName() throws Exception{
@SuppressWarnings("unchecked")
Map<String,String> data = (Map<String, String>) item.getData();
assertNotNull(data);
assertTrue(StringUtils.isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_URI)));
assertTrue(StringUtils.isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID)));
assertTrue(StringUtils.isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_REQUEST_ID)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ public void testCompletion_object() throws Exception{
@SuppressWarnings("unchecked")
Map<String,String> data = (Map<String, String>) item.getData();
assertNotNull(data);
assertTrue(isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_URI)));
assertTrue(isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID)));
assertTrue(isNotBlank(data.get(CompletionResolveHandler.DATA_FIELD_REQUEST_ID)));
}
Expand All @@ -283,15 +282,13 @@ public void testCompletion_dataFieldURI() throws Exception {
assertNotNull(list);
assertFalse("No proposals were found",list.getItems().isEmpty());

List<CompletionItem> items = new ArrayList<>(list.getItems());
for ( CompletionItem item : items) {
@SuppressWarnings("unchecked")
Map<String,String> data = (Map<String, String>) item.getData();
assertNotNull(data);
String uri = data.get(CompletionResolveHandler.DATA_FIELD_URI);
assertTrue(isNotBlank(uri));
assertTrue("unexpected URI prefix: " + uri, uri.matches("file://.*/src/java/Foo\\.java"));
}
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 uri = completionResponse.getUri();
assertNotNull(uri);
assertTrue("unexpected URI prefix: " + uri, uri.matches("file://.*/src/java/Foo\\.java"));
}


Expand Down

0 comments on commit 1259f6c

Please sign in to comment.