Search in sources :

Example 16 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project webtools.sourceediting by eclipse.

the class ReplaceNameTemplateContext method evaluate.

/*
	 * @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
	 */
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    TemplateBuffer buffer = super.evaluate(template);
    String templateName = template.getName().toLowerCase();
    if (buffer != null) {
        if ((fInsertOffset > -1) && (fInsertOffset > getStart())) {
            String prefix = getDocument().get(getStart(), fInsertOffset - getStart());
            if (!templateName.startsWith(prefix.toLowerCase())) {
                // generate a new buffer that actually contains the
                // text that was going to be overwritten
                int prefixSize = prefix.length();
                TemplateVariable[] newTemplateVar = buffer.getVariables();
                for (int i = 0; i < newTemplateVar.length; i++) {
                    int[] offsets = newTemplateVar[i].getOffsets();
                    for (int j = 0; j < offsets.length; j++) {
                        offsets[j] += prefixSize;
                    }
                }
                buffer = new TemplateBuffer(prefix + buffer.getString(), newTemplateVar);
            }
        }
    }
    return buffer;
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer)

Example 17 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project dbeaver by serge-rider.

the class SQLAttributeResolver method resolveAll.

@Override
protected String[] resolveAll(final TemplateContext context) {
    final DBCExecutionContext executionContext = ((DBPContextProvider) context).getExecutionContext();
    if (executionContext == null) {
        return super.resolveAll(context);
    }
    TemplateVariable tableVariable = ((SQLContext) context).getTemplateVariable("table");
    final String tableName = tableVariable == null ? null : tableVariable.getDefaultValue();
    if (!CommonUtils.isEmpty(tableName)) {
        final List<DBSEntityAttribute> attributes = new ArrayList<>();
        DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    List<DBSEntity> entities = new ArrayList<>();
                    SQLEntityResolver.resolveTables(monitor, executionContext, context, entities);
                    if (!CommonUtils.isEmpty(entities)) {
                        DBSEntity table = DBUtils.findObject(entities, tableName);
                        if (table != null) {
                            attributes.addAll(CommonUtils.safeCollection(table.getAttributes(monitor)));
                        }
                    }
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        RuntimeUtils.runTask(runnable, "Resolve attributes", 1000);
        if (!CommonUtils.isEmpty(attributes)) {
            String[] result = new String[attributes.size()];
            for (int i = 0; i < attributes.size(); i++) {
                DBSEntityAttribute entity = attributes.get(i);
                result[i] = entity.getName();
            }
            return result;
        }
    }
    return super.resolveAll(context);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Example 18 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project flux by eclipse.

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * This method should work with all AST levels.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName, String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
    boolean needsTarget = targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (decl.isConstructor()) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (needsTarget) {
        if (delegate)
            templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
        else
            templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    }
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, decl.getName().getIdentifier());
    if (!decl.isConstructor()) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
    }
    if (needsTarget) {
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
    }
    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;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument textBuffer = new Document(str);
    List<TypeParameter> typeParams = shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
    String[] typeParamNames = new String[typeParams.size()];
    for (int i = 0; i < typeParamNames.length; i++) {
        TypeParameter elem = typeParams.get(i);
        typeParamNames[i] = elem.getName().getIdentifier();
    }
    List<SingleVariableDeclaration> params = decl.parameters();
    String[] paramNames = new String[params.size()];
    for (int i = 0; i < paramNames.length; i++) {
        SingleVariableDeclaration elem = params.get(i);
        paramNames[i] = elem.getName().getIdentifier();
    }
    String[] exceptionNames = getExceptionNames(decl);
    String returnType = null;
    if (!decl.isConstructor()) {
        returnType = ASTNodes.asString(getReturnType(decl));
    }
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return textBuffer.get();
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) TemplateException(org.eclipse.jface.text.templates.TemplateException) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template) CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Example 19 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project flux by eclipse.

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, String, String[], String[], String, String[], IMethod, String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String[] typeParameterNames, IMethod target, boolean delegate, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (retTypeSig == null) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (target != null) {
        if (delegate)
            templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
        else
            templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    }
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    if (retTypeSig != null) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, Signature.toString(retTypeSig));
    }
    if (target != null) {
        String targetTypeName = target.getDeclaringType().getFullyQualifiedName('.');
        String[] targetParamTypeNames = getParameterTypeNamesForSeeTag(target);
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
    }
    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;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument document = new Document(str);
    String[] exceptionNames = new String[excTypeSig.length];
    for (int i = 0; i < excTypeSig.length; i++) {
        exceptionNames[i] = Signature.toString(excTypeSig[i]);
    }
    String returnType = retTypeSig != null ? Signature.toString(retTypeSig) : null;
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(document, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParameterNames, false, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return document.get();
}
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) Template(org.eclipse.jface.text.templates.Template) CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Example 20 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project dbeaver by dbeaver.

the class SQLAttributeResolver method resolveAll.

@Override
protected String[] resolveAll(final TemplateContext context) {
    final DBCExecutionContext executionContext = ((DBPContextProvider) context).getExecutionContext();
    if (executionContext == null) {
        return super.resolveAll(context);
    }
    TemplateVariable tableVariable = ((SQLContext) context).getTemplateVariable("table");
    final String tableName = tableVariable == null ? null : tableVariable.getDefaultValue();
    if (!CommonUtils.isEmpty(tableName)) {
        final List<DBSEntityAttribute> attributes = new ArrayList<>();
        DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {

            @Override
            public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    List<DBSEntity> entities = new ArrayList<>();
                    SQLEntityResolver.resolveTables(monitor, executionContext, context, entities);
                    if (!CommonUtils.isEmpty(entities)) {
                        DBSEntity table = DBUtils.findObject(entities, tableName);
                        if (table != null) {
                            attributes.addAll(CommonUtils.safeCollection(table.getAttributes(monitor)));
                        }
                    }
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        RuntimeUtils.runTask(runnable, "Resolve attributes", 1000);
        if (!CommonUtils.isEmpty(attributes)) {
            String[] result = new String[attributes.size()];
            for (int i = 0; i < attributes.size(); i++) {
                DBSEntityAttribute entity = attributes.get(i);
                result[i] = entity.getName();
            }
            return result;
        }
    }
    return super.resolveAll(context);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)44 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)30 IDocument (org.eclipse.jface.text.IDocument)14 Test (org.junit.Test)14 Document (org.eclipse.jface.text.Document)13 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Template (org.eclipse.jface.text.templates.Template)10 TemplateException (org.eclipse.jface.text.templates.TemplateException)10 CoreException (org.eclipse.core.runtime.CoreException)9 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 ArrayList (java.util.ArrayList)5 IRegion (org.eclipse.jface.text.IRegion)4 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)3 MultiVariable (org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable)3 CodeTemplateContext (org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext)3 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2