Search in sources :

Example 21 with TemplateVariable

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

the class SQLEntityResolver method resolveTables.

static void resolveTables(DBRProgressMonitor monitor, DBCExecutionContext executionContext, TemplateContext context, List<DBSEntity> entities) throws DBException {
    TemplateVariable schemaVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_SCHEMA);
    TemplateVariable catalogVariable = ((SQLContext) context).getTemplateVariable(SQLContainerResolver.VAR_NAME_CATALOG);
    String catalogName = catalogVariable == null ? null : catalogVariable.getDefaultValue();
    String schemaName = schemaVariable == null ? null : schemaVariable.getDefaultValue();
    DBSObjectContainer objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, executionContext.getDataSource());
    if (objectContainer == null) {
        return;
    }
    if (!CommonUtils.isEmpty(catalogName) || !CommonUtils.isEmpty(schemaName)) {
        // Find container for specified schema/catalog
        objectContainer = (DBSObjectContainer) DBUtils.getObjectByPath(monitor, executionContext, objectContainer, catalogName, schemaName, null);
    } else {
        objectContainer = DBUtils.getSelectedObject(executionContext, DBSObjectContainer.class);
    }
    if (objectContainer != null) {
        makeProposalsFromChildren(monitor, objectContainer, entities);
    }
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable)

Example 22 with TemplateVariable

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

the class TypeVariableResolver method resolve.

/*
	 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
	 * @since 3.3
	 */
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
    if (!(variable instanceof MultiVariable)) {
        super.resolve(variable, context);
        return;
    }
    MultiVariable mv = (MultiVariable) variable;
    List<String> params = variable.getVariableType().getParams();
    if (params.isEmpty()) {
        super.resolve(variable, context);
        return;
    }
    JavaContext jc = (JavaContext) context;
    String reference = params.get(0);
    int index = 0;
    if (params.size() > 1) {
        String indexParam = params.get(1);
        try {
            index = Integer.parseInt(indexParam);
        } catch (NumberFormatException x) {
        }
    }
    TemplateVariable refVar = jc.getTemplateVariable(reference);
    if (refVar instanceof JavaVariable) {
        JavaVariable jvar = (JavaVariable) refVar;
        resolve(mv, jvar, index, jc);
        return;
    }
    super.resolve(variable, context);
}
Also used : MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable)

Example 23 with TemplateVariable

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

the class ElementTypeResolver method resolve.

/*
	 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
	 * @since 3.3
	 */
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
    if (!(variable instanceof MultiVariable)) {
        super.resolve(variable, context);
        return;
    }
    MultiVariable mv = (MultiVariable) variable;
    List<String> params = variable.getVariableType().getParams();
    if (params.isEmpty()) {
        super.resolve(variable, context);
        return;
    }
    JavaContext jc = (JavaContext) context;
    String reference = params.get(0);
    TemplateVariable refVar = jc.getTemplateVariable(reference);
    if (refVar instanceof JavaVariable) {
        JavaVariable jvar = (JavaVariable) refVar;
        resolve(mv, jvar, jc);
        return;
    }
    super.resolve(variable, context);
}
Also used : MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable)

Example 24 with TemplateVariable

use of org.eclipse.jface.text.templates.TemplateVariable in project che 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 25 with TemplateVariable

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

the class GlobalTemplateVariablesDateTest method assertBufferStringAndVariables.

/**
 * Ensures that {@link TemplateBuffer#getString()} equals the expected String and that all
 * {@link TemplateBuffer#getVariables()} are resolved and unambiguous.
 *
 * @param expected expected result
 * @param buffer the template buffer
 */
private static void assertBufferStringAndVariables(String expected, TemplateBuffer buffer) {
    assertEquals(expected, buffer.getString());
    for (TemplateVariable v : buffer.getVariables()) {
        assertTrue(v.isResolved());
        assertTrue(v.isUnambiguous());
    }
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable)

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