Search in sources :

Example 6 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 7 with ReformatCodeProcessor

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

the class AndroidXmlFormatterTest method testLayout8.

public void testLayout8() throws Exception {
    final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + "layout8.xml", "res/layout/layout.xml");
    myFixture.configureFromExistingVirtualFile(f);
    WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {

        @Override
        public void run() {
            RearrangeCodeProcessor processor = new RearrangeCodeProcessor(new ReformatCodeProcessor(getProject(), false));
            processor.run();
        }
    });
    myFixture.checkResultByFile(BASE_PATH + "layout8_after.xml");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RearrangeCodeProcessor(com.intellij.codeInsight.actions.RearrangeCodeProcessor) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Example 8 with ReformatCodeProcessor

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

the class AndroidXmlFormatterTest method testLayoutNonFirstNamespace.

// Regression test for http://b.android.com/196833
// "Android" style for XML formatting doesn't insert a newline character before the first attribute
// (even when that setting is enabled) is a namespace declaration. However, it does it only when
// namespace declaration is already first attribute in the list. Given that Android XML formatting
// by default rearranges attributes, it would lead to weird results: if namespace declaration is
// not on the first line, on first "Reformat Code" run it would but namespace first and keep the
// line break if it was already there, and on the second run it would remove line break.
public void testLayoutNonFirstNamespace() throws Exception {
    final VirtualFile f = myFixture.copyFileToProject(BASE_PATH + "layout_non_first_namespace.xml", "res/layout/layout.xml");
    myFixture.configureFromExistingVirtualFile(f);
    WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {

        @Override
        public void run() {
            RearrangeCodeProcessor processor = new RearrangeCodeProcessor(new ReformatCodeProcessor(getProject(), false));
            processor.run();
        }
    });
    myFixture.checkResultByFile(BASE_PATH + "layout_non_first_namespace_after.xml");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RearrangeCodeProcessor(com.intellij.codeInsight.actions.RearrangeCodeProcessor) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Example 9 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project intellij-plugins by StepicOrg.

the class ReformatUtils method reformatSelectedEditor.

public static void reformatSelectedEditor(@NotNull Project project, @NotNull Document document) {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
    if (file == null)
        return;
    new ReformatCodeProcessor(new OptimizeImportsProcessor(project, file), false).run();
}
Also used : OptimizeImportsProcessor(com.intellij.codeInsight.actions.OptimizeImportsProcessor) PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Aggregations

ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)9 PsiFile (com.intellij.psi.PsiFile)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 RearrangeCodeProcessor (com.intellij.codeInsight.actions.RearrangeCodeProcessor)2 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)2 Project (com.intellij.openapi.project.Project)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 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 Module (com.intellij.openapi.module.Module)1 ModuleListener (com.intellij.openapi.project.ModuleListener)1 VcsConfiguration (com.intellij.openapi.vcs.VcsConfiguration)1 BulkVirtualFileListenerAdapter (com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter)1 PsiElement (com.intellij.psi.PsiElement)1