Search in sources :

Example 66 with TemplateBuffer

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

the class XbaseTemplateContext method evaluate.

@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    IXtextDocument xDocument = xtextDocumentUtil.getXtextDocument(getDocument());
    // Ensure clean state before evaluation starts
    imports.clear();
    TemplateBuffer resolvedContent = super.evaluate(template);
    Position position = new Position(getCompletionOffset(), 0);
    List<ReplaceRegion> rewrite = createImports(imports, xDocument);
    if (rewrite.size() > 0 && !isReadOnly()) {
        // Remember the completion offset before performing doc changes
        // $NON-NLS-1$
        final String category = "__template_position_import_section" + System.currentTimeMillis();
        IPositionUpdater updater = new DefaultPositionUpdater(category);
        xDocument.addPositionCategory(category);
        xDocument.addPositionUpdater(updater);
        xDocument.addPosition(position);
        try {
            replaceConverter.convertToTextEdit(rewrite).apply(xDocument);
            // restore CompletionOffset
            setCompletionOffset(position.getOffset());
        } catch (BadLocationException e) {
            throw new TemplateException(e);
        } finally {
            xDocument.removePosition(position);
            xDocument.removePositionUpdater(updater);
            try {
                xDocument.removePositionCategory(category);
            } catch (BadPositionCategoryException e) {
                throw new TemplateException(e);
            }
        }
    }
    return resolvedContent;
}
Also used : Position(org.eclipse.jface.text.Position) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TemplateException(org.eclipse.jface.text.templates.TemplateException) DefaultPositionUpdater(org.eclipse.jface.text.DefaultPositionUpdater) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) IPositionUpdater(org.eclipse.jface.text.IPositionUpdater) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 67 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project KaiZen-OpenAPI-Editor by RepreZen.

the class SwaggerTemplateContext method evaluateForDisplay.

/**
 * @since 2.3
 */
public TemplateBuffer evaluateForDisplay(Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template))
        return null;
    TemplateTranslator translator = new TemplateTranslator();
    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 68 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project titan.EclipsePlug-ins by eclipse.

the class TITANTemplateContext method evaluate.

@Override
public TemplateBuffer evaluate(final Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template)) {
        return null;
    }
    TemplateTranslator translator = new TemplateTranslator();
    TemplateBuffer buffer = translator.translate(template);
    getContextType().resolve(buffer, this);
    if (isReadOnly()) {
        // if it is read only we should not modify it
        return buffer;
    }
    // calculate base indentation prefix
    IDocument document = getDocument();
    String prefixString = "";
    String delimeter = null;
    try {
        IRegion lineRegion = document.getLineInformationOfOffset(getCompletionOffset());
        int firstCharLocation = FirstCharAction.firstVisibleCharLocation(document, lineRegion);
        if (firstCharLocation != -1) {
            prefixString = document.get(lineRegion.getOffset(), firstCharLocation - lineRegion.getOffset());
        }
        delimeter = document.getLineDelimiter(document.getLineOfOffset(getCompletionOffset()));
    } catch (BadLocationException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
    TemplateVariable[] variables = buffer.getVariables();
    // apply the base indentation prefix to every line but the first
    IDocument temporalDocument = new Document(buffer.getString());
    MultiTextEdit edit = new MultiTextEdit(0, temporalDocument.getLength());
    List<RangeMarker> positions = variablesToPositions(variables);
    for (int i = temporalDocument.getNumberOfLines() - 1; i >= 0; i--) {
        edit.addChild(new InsertEdit(temporalDocument.getLineOffset(i), prefixString));
    }
    edit.addChildren(positions.toArray(new TextEdit[positions.size()]));
    // replace line delimeters with the ones at the insertion
    String delimeterZero = temporalDocument.getLineDelimiter(0);
    if (delimeter != null && delimeterZero != null && !delimeter.equals(delimeterZero)) {
        FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(temporalDocument);
        int startOffset = 0;
        IRegion region = adapter.find(startOffset, delimeterZero, true, false, false, false);
        while (region != null) {
            edit.addChild(new ReplaceEdit(region.getOffset(), region.getLength(), delimeter));
            startOffset = region.getOffset() + region.getLength();
            region = adapter.find(startOffset, delimeterZero, true, false, false, false);
        }
    }
    edit.apply(temporalDocument, TextEdit.UPDATE_REGIONS);
    positionsToVariables(positions, variables);
    buffer.setContent(temporalDocument.get(), variables);
    return buffer;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) RangeMarker(org.eclipse.text.edits.RangeMarker) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter)

Example 69 with TemplateBuffer

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

the class SnippetCompletionProposal method getSnippetContent.

private static String getSnippetContent(SnippetCompletionContext scc, CodeGenerationTemplate templateSetting, boolean snippetStringSupport) throws CoreException {
    ICompilationUnit cu = scc.getCompilationUnit();
    Template template = templateSetting.createTemplate();
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), scc.getRecommendedLineSeprator());
    context.setVariable(PACKAGEHEADER, scc.getPackageHeader());
    String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
    List<IType> types = Arrays.asList(cu.getAllTypes());
    int postfix = 0;
    while (!types.isEmpty() && types.stream().filter(isTypeExists(typeName)).findFirst().isPresent()) {
        typeName = "Inner" + JavaCore.removeJavaLikeExtension(cu.getElementName()) + (postfix == 0 ? "" : "_" + postfix);
        postfix++;
    }
    if (postfix > 0 && snippetStringSupport) {
        context.setVariable(CodeTemplateContextType.TYPENAME, "${1:" + typeName + "}");
    } else {
        context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
    }
    context.setVariable(CURSOR, snippetStringSupport ? "${0}" : "");
    // TODO Consider making evaluateTemplate public in StubUtility
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null) {
        return null;
    }
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    return str;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CodeTemplateContext(org.eclipse.jdt.internal.core.manipulation.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) 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) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) IType(org.eclipse.jdt.core.IType)

Example 70 with TemplateBuffer

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

the class ErlTemplateProposal method getAdditionalProposalInfo.

@Override
public String getAdditionalProposalInfo() {
    try {
        final TemplateContext context = getContext();
        context.setReadOnly(true);
        TemplateBuffer templateBuffer;
        try {
            final Template template = getTemplate();
            if (context instanceof ErlangTemplateContext) {
                final ErlangTemplateContext etc = (ErlangTemplateContext) context;
                templateBuffer = etc.evaluate(template, true);
            } else {
                templateBuffer = context.evaluate(template);
            }
        } catch (final TemplateException e) {
            return null;
        }
        return templateBuffer.getString();
    } catch (final BadLocationException e) {
        return null;
    }
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) BadLocationException(org.eclipse.jface.text.BadLocationException) Template(org.eclipse.jface.text.templates.Template)

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