Search in sources :

Example 31 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer 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 32 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project xtext-eclipse by eclipse.

the class XtextTemplateContext method evaluate.

@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template))
        return null;
    TemplateTranslator translator = createTemplateTranslator();
    TemplateBuffer buffer = translator.translate(template);
    getContextType().resolve(buffer, this);
    return buffer;
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 33 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project xtext-eclipse by eclipse.

the class XtextTemplateContextTest method assertTemplateBuffer.

protected void assertTemplateBuffer(String expectation, String document, int offset, String pattern) {
    Template template = new Template("", "", "", pattern, false);
    this.document.set(document);
    this.position.setOffset(offset);
    try {
        TemplateBuffer buffer = testMe.evaluate(template);
        assertEquals(expectation, buffer.getString());
    } catch (BadLocationException e) {
        e.printStackTrace();
        fail("Unexpected bad location expection: " + e);
    } catch (TemplateException e) {
        e.printStackTrace();
        fail("Unexpected template expection: " + e);
    }
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) BadLocationException(org.eclipse.jface.text.BadLocationException) Template(org.eclipse.jface.text.templates.Template)

Example 34 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project che by eclipse.

the class TemplateProposal method getAdditionalProposalInfo.

/*
	 * @see ICompletionProposal#getAdditionalProposalInfo()
	 */
public String getAdditionalProposalInfo() {
    try {
        fContext.setReadOnly(true);
        TemplateBuffer templateBuffer;
        try {
            templateBuffer = fContext.evaluate(fTemplate);
        } catch (TemplateException e) {
            return null;
        }
        IDocument document = new Document(templateBuffer.getString());
        IndentUtil.indentLines(document, new LineRange(0, document.getNumberOfLines()), null, null);
        StringBuffer buffer = new StringBuffer();
        HTMLPrinter.insertPageProlog(buffer, 0, JavadocFinder.getStyleSheet());
        HTMLPrinter.addParagraph(buffer, document.get());
        HTMLPrinter.addPageEpilog(buffer);
        return buffer.toString();
    } catch (BadLocationException e) {
        //			handleException(
        //					JavaPlugin.getActiveWorkbenchShell(), new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "", e))); //$NON-NLS-1$
        JavaPlugin.log(e);
        return null;
    }
}
Also used : 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) LineRange(org.eclipse.che.jface.text.source.LineRange) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 35 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project che by eclipse.

the class CodeTemplateContext method evaluate.

/*
	 * @see org.eclipse.jdt.internal.corext.template.TemplateContext#evaluate(org.eclipse.jdt.internal.corext.template.Template)
	 */
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    // test that all variables are defined
    Iterator<TemplateVariableResolver> iterator = getContextType().resolvers();
    while (iterator.hasNext()) {
        TemplateVariableResolver var = iterator.next();
        if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
            //$NON-NLS-1$ //$NON-NLS-2$
            Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined");
        }
    }
    if (!canEvaluate(template))
        return null;
    String pattern = changeLineDelimiter(template.getPattern(), fLineDelimiter);
    TemplateTranslator translator = new TemplateTranslator();
    TemplateBuffer buffer = translator.translate(pattern);
    getContextType().resolve(buffer, this);
    return buffer;
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Aggregations

TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)71 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)30 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TemplateException (org.eclipse.jface.text.templates.TemplateException)26 Document (org.eclipse.jface.text.Document)23 Template (org.eclipse.jface.text.templates.Template)23 Test (org.junit.Test)22 IDocument (org.eclipse.jface.text.IDocument)21 CoreException (org.eclipse.core.runtime.CoreException)17 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)11 TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)11 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)9 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)8 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)4 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)4 ArrayList (java.util.ArrayList)3 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)3