use of org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType in project eclipse.jdt.ls by eclipse.
the class SnippetCompletionProposal method evaluateGenericTemplate.
public static String evaluateGenericTemplate(ICompilationUnit cu, CompletionContext completionContext, Template template) {
JavaContextType contextType = (JavaContextType) JavaLanguageServerPlugin.getInstance().getTemplateContextRegistry().getContextType(JavaContextType.ID_STATEMENTS);
char[] completionToken = completionContext.getToken();
if (contextType == null || completionToken == null) {
return null;
}
TemplateBuffer buffer = null;
try {
IDocument document = new Document(cu.getSource());
DocumentTemplateContext javaContext = contextType.createContext(document, completionContext.getOffset(), completionToken.length, cu);
buffer = javaContext.evaluate(template);
} catch (JavaModelException | BadLocationException | TemplateException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
return null;
}
if (buffer == null) {
return null;
}
String content = buffer.getString();
if (Strings.containsOnlyWhitespaces(content)) {
return null;
}
return content;
}
use of org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType in project eclipse.jdt.ls by eclipse.
the class JavaLanguageServerPlugin method getTemplateContextRegistry.
/**
* Returns the template context type registry for the java plug-in.
*
* @return the template context type registry for the java plug-in
*/
public synchronized ContextTypeRegistry getTemplateContextRegistry() {
if (fContextTypeRegistry == null) {
ContextTypeRegistry registry = new ContextTypeRegistry();
JavaContextType statementContextType = new JavaContextType();
statementContextType.setId(JavaContextType.ID_STATEMENTS);
statementContextType.setName(JavaContextType.ID_STATEMENTS);
statementContextType.initializeContextTypeResolvers();
// Todo: Some of the resolvers is defined in the XML of the jdt.ui, now we have to add them manually.
// See: https://github.com/eclipse/eclipse.jdt.ui/blob/cf6c42522ee5a5ea21a34fcfdecf3504d4750a04/org.eclipse.jdt.ui/plugin.xml#L5619-L5625
TemplateVariableResolver resolver = new VarResolver();
resolver.setType("var");
statementContextType.addResolver(resolver);
registry.addContextType(statementContextType);
fContextTypeRegistry = registry;
}
return fContextTypeRegistry;
}
use of org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType 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