Search in sources :

Example 6 with ConstantNode

use of com.intellij.codeInsight.template.impl.ConstantNode in project intellij-community by JetBrains.

the class TestIntegrationUtils method createTestMethodTemplate.

public static Template createTestMethodTemplate(@NotNull MethodKind methodKind, TestFramework descriptor, @NotNull PsiClass targetClass, @Nullable PsiClass sourceClass, @Nullable String name, boolean automatic, Set<String> existingNames) {
    FileTemplateDescriptor templateDesc = methodKind.getFileTemplateDescriptor(descriptor);
    String templateName = templateDesc.getFileName();
    FileTemplate fileTemplate = FileTemplateManager.getInstance(targetClass.getProject()).getCodeTemplate(templateName);
    Template template = TemplateManager.getInstance(targetClass.getProject()).createTemplate("", "");
    String templateText;
    try {
        Properties properties = new Properties();
        if (sourceClass != null && sourceClass.isValid()) {
            properties.setProperty(FileTemplate.ATTRIBUTE_CLASS_NAME, sourceClass.getQualifiedName());
        }
        templateText = fileTemplate.getText(properties);
    } catch (IOException e) {
        LOG.warn(e);
        templateText = fileTemplate.getText();
    }
    if (name == null)
        name = methodKind.getDefaultName();
    if (existingNames != null && !existingNames.add(name)) {
        int idx = 1;
        while (existingNames.contains(name)) {
            final String newName = name + (idx++);
            if (existingNames.add(newName)) {
                name = newName;
                break;
            }
        }
    }
    templateText = StringUtil.replace(templateText, "${BODY}\n", "");
    int from = 0;
    while (true) {
        int index = templateText.indexOf("${NAME}", from);
        if (index == -1)
            break;
        template.addTextSegment(templateText.substring(from, index));
        if (index > 0 && !Character.isWhitespace(templateText.charAt(index - 1))) {
            name = StringUtil.capitalize(name);
        } else {
            name = StringUtil.decapitalize(name);
        }
        if (from == 0) {
            Expression nameExpr = new ConstantNode(name);
            template.addVariable("name", nameExpr, nameExpr, !automatic);
        } else {
            template.addVariableSegment("name");
        }
        from = index + "${NAME}".length();
    }
    template.addTextSegment(templateText.substring(from, templateText.length()));
    template.setToIndent(true);
    template.setToReformat(true);
    template.setToShortenLongNames(true);
    return template;
}
Also used : ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) FileTemplateDescriptor(com.intellij.ide.fileTemplates.FileTemplateDescriptor) Expression(com.intellij.codeInsight.template.Expression) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IOException(java.io.IOException) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Template(com.intellij.codeInsight.template.Template)

Example 7 with ConstantNode

use of com.intellij.codeInsight.template.impl.ConstantNode in project intellij-community by JetBrains.

the class TemplateBuilderImpl method replaceElement.

@Override
public void replaceElement(@NotNull PsiElement element, TextRange rangeWithinElement, String replacementText) {
    final RangeMarker key = myDocument.createRangeMarker(rangeWithinElement.shiftRight(element.getTextRange().getStartOffset()));
    ConstantNode value = new ConstantNode(replacementText);
    replaceElement(key, value);
}
Also used : ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Aggregations

ConstantNode (com.intellij.codeInsight.template.impl.ConstantNode)7 Template (com.intellij.codeInsight.template.Template)2 RangeMarker (com.intellij.openapi.editor.RangeMarker)2 GoType (com.goide.psi.GoType)1 GoTypeDeclaration (com.goide.psi.GoTypeDeclaration)1 GoTypeSpec (com.goide.psi.GoTypeSpec)1 Expression (com.intellij.codeInsight.template.Expression)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateDescriptor (com.intellij.ide.fileTemplates.FileTemplateDescriptor)1 PsiElement (com.intellij.psi.PsiElement)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1