Search in sources :

Example 36 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class FileTemplateTabAsList method removeSelected.

@Override
public void removeSelected() {
    final FileTemplate selectedTemplate = getSelectedTemplate();
    if (selectedTemplate == null) {
        return;
    }
    final DefaultListModel model = (DefaultListModel) myList.getModel();
    final int selectedIndex = myList.getSelectedIndex();
    model.remove(selectedIndex);
    if (!model.isEmpty()) {
        myList.setSelectedIndex(Math.min(selectedIndex, model.size() - 1));
    }
    onTemplateSelected();
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 37 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class FileHeaderChecker method checkFileHeader.

static ProblemDescriptor checkFileHeader(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean onTheFly) {
    TIntObjectHashMap<String> offsetToProperty = new TIntObjectHashMap<>();
    FileTemplate defaultTemplate = FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
    Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
    Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
    if (!matcher.matches()) {
        return null;
    }
    PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class);
    if (element == null) {
        return null;
    }
    LocalQuickFix[] fixes = createQuickFix(matcher, offsetToProperty, file.getProject(), onTheFly);
    String description = InspectionsBundle.message("default.file.template.description");
    return manager.createProblemDescriptor(element, description, onTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
Also used : Pattern(java.util.regex.Pattern) PsiComment(com.intellij.psi.PsiComment) Matcher(java.util.regex.Matcher) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 38 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class FileHeaderChecker method createQuickFix.

private static LocalQuickFix[] createQuickFix(final Matcher matcher, final TIntObjectHashMap<String> offsetToProperty, Project project, boolean onTheFly) {
    final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(FileTemplateManager.FILE_HEADER_TEMPLATE_NAME);
    ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() {

        @Override
        public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
            PsiElement element = descriptor.getPsiElement();
            if (element == null)
                return;
            String newText;
            try {
                newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim();
            } catch (IOException e) {
                LOG.error(e);
                return;
            }
            if (!newText.isEmpty()) {
                PsiElement parent = element.getParent();
                PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText);
                for (PsiElement child : tempFile.getChildren()) {
                    if (child.getTextLength() > 0) {
                        parent.addBefore(child, element);
                    }
                }
            }
            element.delete();
        }
    };
    if (onTheFly) {
        LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix);
        return template.isDefault() ? new LocalQuickFix[] { editFileTemplateFix } : new LocalQuickFix[] { replaceTemplateFix, editFileTemplateFix };
    }
    return template.isDefault() ? null : new LocalQuickFix[] { replaceTemplateFix };
}
Also used : Project(com.intellij.openapi.project.Project) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement)

Example 39 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class CreateClassAction method buildDialog.

@Override
protected void buildDialog(final Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {
    builder.setTitle(IdeBundle.message("action.create.new.class")).addKind("Class", PlatformIcons.CLASS_ICON, JavaTemplateUtil.INTERNAL_CLASS_TEMPLATE_NAME).addKind("Interface", PlatformIcons.INTERFACE_ICON, JavaTemplateUtil.INTERNAL_INTERFACE_TEMPLATE_NAME);
    if (PsiUtil.getLanguageLevel(directory).isAtLeast(LanguageLevel.JDK_1_5)) {
        builder.addKind("Enum", PlatformIcons.ENUM_ICON, JavaTemplateUtil.INTERNAL_ENUM_TEMPLATE_NAME);
        builder.addKind("Annotation", PlatformIcons.ANNOTATION_TYPE_ICON, JavaTemplateUtil.INTERNAL_ANNOTATION_TYPE_TEMPLATE_NAME);
    }
    for (FileTemplate template : FileTemplateManager.getInstance(project).getAllTemplates()) {
        final JavaCreateFromTemplateHandler handler = new JavaCreateFromTemplateHandler();
        if (handler.handlesTemplate(template) && JavaCreateFromTemplateHandler.canCreate(directory)) {
            builder.addKind(template.getName(), JavaFileType.INSTANCE.getIcon(), template.getName());
        }
    }
    builder.setValidator(new InputValidatorEx() {

        @Override
        public String getErrorText(String inputString) {
            if (inputString.length() > 0 && !PsiNameHelper.getInstance(project).isQualifiedName(inputString)) {
                return "This is not a valid Java qualified name";
            }
            return null;
        }

        @Override
        public boolean checkInput(String inputString) {
            return true;
        }

        @Override
        public boolean canClose(String inputString) {
            return !StringUtil.isEmptyOrSpaces(inputString) && getErrorText(inputString) == null;
        }
    });
}
Also used : JavaCreateFromTemplateHandler(com.intellij.ide.fileTemplates.JavaCreateFromTemplateHandler) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) InputValidatorEx(com.intellij.openapi.ui.InputValidatorEx)

Example 40 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate 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)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)66 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)20 Project (com.intellij.openapi.project.Project)13 IOException (java.io.IOException)12 PsiFile (com.intellij.psi.PsiFile)10 Properties (java.util.Properties)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 PsiDirectory (com.intellij.psi.PsiDirectory)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiElement (com.intellij.psi.PsiElement)6 Module (com.intellij.openapi.module.Module)5 File (java.io.File)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 Nullable (org.jetbrains.annotations.Nullable)4 Editor (com.intellij.openapi.editor.Editor)3 ConfigurationException (com.intellij.openapi.options.ConfigurationException)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)2