use of org.eclipse.jdt.ls.core.internal.handlers.CompletionResponse in project eclipse.jdt.ls by eclipse.
the class SnippetCompletionProposal method getGenericSnippets.
private static List<CompletionItem> getGenericSnippets(SnippetCompletionContext scc) throws JavaModelException {
CompletionResponse response = new CompletionResponse();
response.setContext(scc.getCompletionContext());
response.setOffset(scc.getCompletionContext().getOffset());
List<CompletionItem> res = new ArrayList<>();
CompletionContext completionContext = scc.getCompletionContext();
char[] completionToken = completionContext.getToken();
if (completionToken == null) {
return Collections.emptyList();
}
int tokenLocation = completionContext.getTokenLocation();
JavaContextType contextType = (JavaContextType) JavaLanguageServerPlugin.getInstance().getTemplateContextRegistry().getContextType(JavaContextType.ID_STATEMENTS);
if (contextType == null) {
return Collections.emptyList();
}
ICompilationUnit cu = scc.getCompilationUnit();
IDocument document = new Document(cu.getSource());
DocumentTemplateContext javaContext = contextType.createContext(document, completionContext.getOffset(), completionToken.length, cu);
Template[] templates = null;
if ((tokenLocation & CompletionContext.TL_STATEMENT_START) != 0) {
templates = JavaLanguageServerPlugin.getInstance().getTemplateStore().getTemplates(JavaContextType.ID_STATEMENTS);
} else {
// We only support statement templates for now.
}
if (templates == null || templates.length == 0) {
return Collections.emptyList();
}
String uri = JDTUtils.toURI(cu);
Template[] availableTemplates = Arrays.stream(templates).filter(t -> javaContext.canEvaluate(t)).toArray(Template[]::new);
List<CompletionProposal> proposals = new ArrayList<>();
for (int i = 0; i < availableTemplates.length; i++) {
Template template = availableTemplates[i];
final CompletionItem item = new CompletionItem();
item.setLabel(template.getName());
item.setKind(CompletionItemKind.Snippet);
item.setInsertTextFormat(InsertTextFormat.Snippet);
item.setInsertText(SnippetUtils.templateToSnippet(template.getPattern()));
item.setDetail(template.getDescription());
Map<String, String> data = new HashMap<>(3);
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(i));
item.setData(data);
proposals.add(i, new SnippetCompletionProposal(template));
res.add(item);
}
response.setProposals(proposals);
CompletionResponses.store(response);
return res;
}
Aggregations