Search in sources :

Example 11 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project intellij-community by JetBrains.

the class MavenUtil method runOrApplyFileTemplate.

private static void runOrApplyFileTemplate(Project project, VirtualFile file, String templateName, Properties properties, Properties conditions, boolean interactive) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance(project);
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties();
    if (!interactive) {
        allProperties.putAll(properties);
    }
    allProperties.putAll(conditions);
    String text = fileTemplate.getText(allProperties);
    Pattern pattern = Pattern.compile("\\$\\{(.*)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer builder = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(builder, "\\$" + matcher.group(1).toUpperCase() + "\\$");
    }
    matcher.appendTail(builder);
    text = builder.toString();
    TemplateImpl template = (TemplateImpl) TemplateManager.getInstance(project).createTemplate("", "", text);
    for (int i = 0; i < template.getSegmentsCount(); i++) {
        if (i == template.getEndSegmentNumber())
            continue;
        String name = template.getSegmentName(i);
        String value = "\"" + properties.getProperty(name, "") + "\"";
        template.addVariable(name, value, value, true);
    }
    if (interactive) {
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file);
        Editor editor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
        editor.getDocument().setText("");
        TemplateManager.getInstance(project).startTemplate(editor, template);
    } else {
        VfsUtil.saveText(file, template.getTemplateText());
        PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
        if (psiFile != null) {
            new ReformatCodeProcessor(project, psiFile, null, false).run();
        }
    }
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) SystemProperties(com.intellij.util.SystemProperties) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 12 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project intellij-community by JetBrains.

the class MavenPomXmlCompletionTagListenerContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    if (TemplateManager.getInstance(parameters.getOriginalFile().getProject()).getActiveTemplate(parameters.getEditor()) != null) {
        // Don't brake the template.
        return;
    }
    PsiFile psiFile = parameters.getOriginalFile();
    if (!(psiFile instanceof XmlFile))
        return;
    if (!MavenDomUtil.isProjectFile(psiFile))
        return;
    DomFileDescription<?> description = DomManager.getDomManager(psiFile.getProject()).getDomFileDescription((XmlFile) psiFile);
    if (!(description instanceof MavenDomProjectModelDescription))
        return;
    result.runRemainingContributors(parameters, r -> {
        final LookupElement lookupElement = r.getLookupElement();
        if (myHandledTags.contains(lookupElement.getLookupString())) {
            LookupElement decorator = LookupElementDecorator.withInsertHandler(lookupElement, new InsertHandler<LookupElementDecorator<LookupElement>>() {

                @Override
                public void handleInsert(final InsertionContext context, LookupElementDecorator<LookupElement> item) {
                    lookupElement.handleInsert(context);
                    Object object = lookupElement.getObject();
                    if ("dependency".equals(lookupElement.getLookupString()) && object instanceof XmlTag && "maven-4.0.0.xsd".equals(((XmlTag) object).getContainingFile().getName())) {
                        context.commitDocument();
                        CaretModel caretModel = context.getEditor().getCaretModel();
                        PsiElement psiElement = context.getFile().findElementAt(caretModel.getOffset());
                        XmlTag xmlTag = PsiTreeUtil.getParentOfType(psiElement, XmlTag.class);
                        if (xmlTag != null) {
                            DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement(xmlTag);
                            if (domElement instanceof MavenDomDependency) {
                                String s = "\n<groupId></groupId>\n<artifactId></artifactId>\n";
                                context.getDocument().insertString(caretModel.getOffset(), s);
                                caretModel.moveToOffset(caretModel.getOffset() + s.length() - "</artifactId>\n".length());
                                context.commitDocument();
                                new ReformatCodeProcessor(context.getProject(), context.getFile(), xmlTag.getTextRange(), false).run();
                                MavenDependencyCompletionUtil.invokeCompletion(context, CompletionType.BASIC);
                            }
                        }
                    }
                }
            });
            r = r.withLookupElement(decorator);
        }
        result.passResult(r);
    });
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) XmlFile(com.intellij.psi.xml.XmlFile) LookupElementDecorator(com.intellij.codeInsight.lookup.LookupElementDecorator) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency) DomElement(com.intellij.util.xml.DomElement) MavenDomProjectModelDescription(org.jetbrains.idea.maven.dom.MavenDomProjectModelDescription) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 13 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project Aspose.Imaging-for-Java by aspose-imaging.

the class AsposeMavenModuleBuilderHelper method updateFileContents.

private static void updateFileContents(Project project, final VirtualFile vf, final File f) throws Throwable {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    InputStream in = null;
    try {
        in = new FileInputStream(f);
        write(in, bytes);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    VfsUtil.saveText(vf, bytes.toString());
    PsiFile psiFile = PsiManager.getInstance(project).findFile(vf);
    if (psiFile != null) {
        new ReformatCodeProcessor(project, psiFile, null, false).run();
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Example 14 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project Aspose.BarCode-for-Java by aspose-barcode.

the class AsposeMavenModuleBuilderHelper method updateFileContents.

private static void updateFileContents(Project project, final VirtualFile vf, final File f) throws Throwable {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    InputStream in = null;
    try {
        in = new FileInputStream(f);
        write(in, bytes);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    VfsUtil.saveText(vf, bytes.toString());
    PsiFile psiFile = PsiManager.getInstance(project).findFile(vf);
    if (psiFile != null) {
        new ReformatCodeProcessor(project, psiFile, null, false).run();
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Example 15 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project Aspose.OCR-for-Java by aspose-ocr.

the class AsposeMavenModuleBuilderHelper method updateFileContents.

private static void updateFileContents(Project project, final VirtualFile vf, final File f) throws Throwable {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    InputStream in = null;
    try {
        in = new FileInputStream(f);
        write(in, bytes);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    VfsUtil.saveText(vf, bytes.toString());
    PsiFile psiFile = PsiManager.getInstance(project).findFile(vf);
    if (psiFile != null) {
        new ReformatCodeProcessor(project, psiFile, null, false).run();
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Aggregations

ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)18 PsiFile (com.intellij.psi.PsiFile)13 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)6 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)5 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)5 Properties (java.util.Properties)5 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Project (com.intellij.openapi.project.Project)3 RearrangeCodeProcessor (com.intellij.codeInsight.actions.RearrangeCodeProcessor)2 Module (com.intellij.openapi.module.Module)2 IButterKnife (com.avast.android.butterknifezelezny.butterknife.IButterKnife)1 OptimizeImportsProcessor (com.intellij.codeInsight.actions.OptimizeImportsProcessor)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupElementDecorator (com.intellij.codeInsight.lookup.LookupElementDecorator)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1