Search in sources :

Example 1 with JavaContextType

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) JavaContextType(org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with JavaContextType

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;
}
Also used : JavaContextType(org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType) ContextTypeRegistry(org.eclipse.text.templates.ContextTypeRegistry) VarResolver(org.eclipse.jdt.internal.corext.template.java.VarResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 3 with JavaContextType

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Template(org.eclipse.jface.text.templates.Template) CoreException(org.eclipse.core.runtime.CoreException) CompletionContext(org.eclipse.jdt.core.CompletionContext) CompletionOnFieldType(org.eclipse.jdt.internal.codeassist.complete.CompletionOnFieldType) Map(java.util.Map) CodeTemplateContextType(org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) InsertTextFormat(org.eclipse.lsp4j.InsertTextFormat) TokenScanner(org.eclipse.jdt.internal.corext.dom.TokenScanner) IJavaProject(org.eclipse.jdt.core.IJavaProject) Predicate(java.util.function.Predicate) Status(org.eclipse.core.runtime.Status) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CompletionOnKeyword2(org.eclipse.jdt.internal.codeassist.complete.CompletionOnKeyword2) JavaModelUtil(org.eclipse.jdt.internal.corext.util.JavaModelUtil) IType(org.eclipse.jdt.core.IType) CompletionItem(org.eclipse.lsp4j.CompletionItem) List(java.util.List) CompletionResponse(org.eclipse.jdt.ls.core.internal.handlers.CompletionResponse) InternalCompletionContext(org.eclipse.jdt.internal.codeassist.InternalCompletionContext) JavaContextType(org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) JavaModelException(org.eclipse.jdt.core.JavaModelException) HashMap(java.util.HashMap) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) ArrayList(java.util.ArrayList) TemplateException(org.eclipse.jface.text.templates.TemplateException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) CompletionItemKind(org.eclipse.lsp4j.CompletionItemKind) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Strings(org.eclipse.jdt.internal.core.manipulation.util.Strings) BadLocationException(org.eclipse.jface.text.BadLocationException) CodeTemplateContext(org.eclipse.jdt.internal.core.manipulation.CodeTemplateContext) JavaCore(org.eclipse.jdt.core.JavaCore) ASTNodeSearchUtil(org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) CompletionResponses(org.eclipse.jdt.ls.core.internal.handlers.CompletionResponses) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) Initializer(org.eclipse.jdt.core.dom.Initializer) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IMethod(org.eclipse.jdt.core.IMethod) ITerminalSymbols(org.eclipse.jdt.core.compiler.ITerminalSymbols) CompletionOnSingleNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameReference) Collections(java.util.Collections) CompletionResolveHandler(org.eclipse.jdt.ls.core.internal.handlers.CompletionResolveHandler) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) CompletionContext(org.eclipse.jdt.core.CompletionContext) InternalCompletionContext(org.eclipse.jdt.internal.codeassist.InternalCompletionContext) CompletionResponse(org.eclipse.jdt.ls.core.internal.handlers.CompletionResponse) JavaContextType(org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType) CompletionItem(org.eclipse.lsp4j.CompletionItem) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

JavaContextType (org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Status (org.eclipse.core.runtime.Status)1 CompletionContext (org.eclipse.jdt.core.CompletionContext)1 CompletionProposal (org.eclipse.jdt.core.CompletionProposal)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IMethod (org.eclipse.jdt.core.IMethod)1 IPackageDeclaration (org.eclipse.jdt.core.IPackageDeclaration)1 IType (org.eclipse.jdt.core.IType)1