Search in sources :

Example 21 with Template

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

the class SQLSymbolInserter method verifyKey.

@Override
public void verifyKey(VerifyEvent event) {
    if (!event.doit) {
        return;
    }
    IDocument document = sourceViewer.getDocument();
    final Point selection = sourceViewer.getSelectedRange();
    final int offset = selection.x;
    final int length = selection.y;
    switch(event.character) {
        case '(':
        case '[':
            if (!closeBrackets) {
                return;
            }
            if (hasCharacterToTheRight(document, offset + length, event.character)) {
                return;
            }
        case '\'':
            if (event.character == '\'') {
                if (!closeSingleQuotes) {
                    return;
                }
                if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
                    return;
                }
            }
        case '"':
            if (event.character == '"') {
                if (!closeDoubleQuotes) {
                    return;
                }
                if (hasIdentifierToTheLeft(document, offset) || hasIdentifierToTheRight(document, offset + length)) {
                    return;
                }
            }
            try {
                ITypedRegion partition = TextUtilities.getPartition(document, SQLPartitionScanner.SQL_PARTITIONING, offset, true);
                if (!IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) && partition.getOffset() != offset) {
                    return;
                }
                if (!editor.validateEditorInputState()) {
                    return;
                }
                final char character = event.character;
                final char closingCharacter = getPeerCharacter(character);
                document.replace(offset, length, String.valueOf(character) + closingCharacter);
                SymbolLevel level = new SymbolLevel();
                bracketLevelStack.add(level);
                LinkedPositionGroup group = new LinkedPositionGroup();
                group.addPosition(new LinkedPosition(document, offset + 1, 0, LinkedPositionGroup.NO_STOP));
                LinkedModeModel model = new LinkedModeModel();
                model.addLinkingListener(this);
                model.addGroup(group);
                model.forceInstall();
                level.offset = offset;
                level.length = 2;
                // set up position tracking for our magic peers
                if (bracketLevelStack.size() == 1) {
                    document.addPositionCategory(CATEGORY);
                    document.addPositionUpdater(positionUpdater);
                }
                level.firstPosition = new Position(offset, 1);
                level.secondPosition = new Position(offset + 1, 1);
                document.addPosition(CATEGORY, level.firstPosition);
                document.addPosition(CATEGORY, level.secondPosition);
                level.uI = new EditorLinkedModeUI(model, sourceViewer);
                level.uI.setSimpleMode(true);
                level.uI.setExitPolicy(new ExitPolicy(closingCharacter, getEscapeCharacter(closingCharacter), bracketLevelStack));
                level.uI.setExitPosition(sourceViewer, offset + 2, 0, Integer.MAX_VALUE);
                level.uI.setCyclingMode(LinkedModeUI.CYCLE_NEVER);
                level.uI.enter();
                IRegion newSelection = level.uI.getSelectedRegion();
                sourceViewer.setSelectedRange(newSelection.getOffset(), newSelection.getLength());
                event.doit = false;
            } catch (BadLocationException | BadPositionCategoryException e) {
                log.debug(e);
            }
            break;
        case SWT.TAB:
            {
                try {
                    int curOffset = offset;
                    //                    }
                    while (curOffset > 0) {
                        if (!Character.isJavaIdentifierPart(document.getChar(curOffset - 1))) {
                            break;
                        }
                        curOffset--;
                    }
                    if (curOffset != offset) {
                        String templateName = document.get(curOffset, offset - curOffset);
                        SQLTemplatesPage templatesPage = editor.getTemplatesPage();
                        Template template = templatesPage.getTemplateStore().findTemplate(templateName);
                        if (template != null && template.isAutoInsertable()) {
                            sourceViewer.setSelectedRange(curOffset, offset - curOffset);
                            templatesPage.insertTemplate(template, document);
                            event.doit = false;
                        }
                    }
                } catch (BadLocationException e) {
                    log.debug(e);
                }
                break;
            }
    }
}
Also used : SQLTemplatesPage(org.jkiss.dbeaver.ui.editors.sql.templates.SQLTemplatesPage) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Template(org.eclipse.jface.text.templates.Template) IExitPolicy(org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy)

Example 22 with Template

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

the class UnimplementedCodeCleanUp method getOverridingMethodComment.

private String getOverridingMethodComment() {
    String templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    Template template = getCodeTemplate(templateName);
    if (template == null)
        //$NON-NLS-1$
        return "";
    //$NON-NLS-1$
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, "\n");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.FILENAME, "Face.java");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.PACKAGENAME, "test");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.PROJECTNAME, "TestProject");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, "Face");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, "method");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.RETURN_TYPE, "void");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, "test.IFace#foo()");
    return evaluateTemplate(template, context);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 23 with Template

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

the class StubUtility method setCodeTemplate.

/**
	 * Only to be used by tests
	 *
	 * @param templateId the template id
	 * @param pattern the new pattern
	 * @param project not used
	 */
public static void setCodeTemplate(String templateId, String pattern, IJavaProject project) {
    TemplateStore codeTemplateStore = JavaPlugin.getDefault().getCodeTemplateStore();
    TemplatePersistenceData data = codeTemplateStore.getTemplateData(templateId);
    Template orig = data.getTemplate();
    Template copy = new Template(orig.getName(), orig.getDescription(), orig.getContextTypeId(), pattern, true);
    data.setTemplate(copy);
}
Also used : TemplatePersistenceData(org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData) TemplateStore(org.eclipse.che.jface.text.templates.persistence.TemplateStore) Template(org.eclipse.jface.text.templates.Template)

Example 24 with Template

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

the class StubUtility method getTypeBody.

/**
	 * Don't use this method directly, use CodeGeneration.
	 * 
	 * @param templateID the template id of the type body to get. Valid id's are
	 *            {@link CodeTemplateContextType#CLASSBODY_ID},
	 *            {@link CodeTemplateContextType#INTERFACEBODY_ID},
	 *            {@link CodeTemplateContextType#ENUMBODY_ID},
	 *            {@link CodeTemplateContextType#ANNOTATIONBODY_ID},
	 * @param cu the compilation unit to which the template is added
	 * @param typeName the type name
	 * @param lineDelim the line delimiter to use
	 * @return return the type body template or <code>null</code>
	 * @throws CoreException thrown if the template could not be evaluated
	 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeBody(String, ICompilationUnit, String, String)
	 */
public static String getTypeBody(String templateID, ICompilationUnit cu, String typeName, String lineDelim) throws CoreException {
    if (!VALID_TYPE_BODY_TEMPLATES.contains(templateID)) {
        //$NON-NLS-1$
        throw new IllegalArgumentException("Invalid code template ID: " + templateID);
    }
    Template template = getCodeTemplate(templateID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 25 with Template

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

the class StubUtility method getSetterComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getSetterComment(ICompilationUnit, String, String, String, String, String, String, String)
	 */
public static String getSetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String paramName, String bareFieldName, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.SETTERCOMMENT_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);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
    context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);
    context.setVariable(CodeTemplateContextType.PARAM, paramName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Aggregations

Template (org.eclipse.jface.text.templates.Template)37 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)28 BadLocationException (org.eclipse.jface.text.BadLocationException)7 IDocument (org.eclipse.jface.text.IDocument)7 CoreException (org.eclipse.core.runtime.CoreException)6 Document (org.eclipse.jface.text.Document)6 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)6 TemplateException (org.eclipse.jface.text.templates.TemplateException)6 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IOException (java.io.IOException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TemplatePersistenceData (org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData)2 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)2 Position (org.eclipse.jface.text.Position)2 Region (org.eclipse.jface.text.Region)2