Search in sources :

Example 1 with ReformatCodeProcessor

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

the class MvcModuleStructureSynchronizer method initComponent.

@Override
public void initComponent() {
    final MessageBusConnection connection = myProject.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

        @Override
        public void rootsChanged(ModuleRootEvent event) {
            myModificationTracker.incModificationCount();
            queue(SyncAction.SyncLibrariesInPluginsModule, myProject);
            queue(SyncAction.UpgradeFramework, myProject);
            queue(SyncAction.CreateAppStructureIfNeeded, myProject);
            queue(SyncAction.UpdateProjectStructure, myProject);
            queue(SyncAction.EnsureRunConfigurationExists, myProject);
            updateProjectViewVisibility();
        }
    });
    connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleAdded(@NotNull Project project, @NotNull Module module) {
            queue(SyncAction.UpdateProjectStructure, module);
            queue(SyncAction.CreateAppStructureIfNeeded, module);
        }
    });
    connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(new VirtualFileAdapter() {

        @Override
        public void fileCreated(@NotNull final VirtualFileEvent event) {
            myModificationTracker.incModificationCount();
            final VirtualFile file = event.getFile();
            final String fileName = event.getFileName();
            if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName) || isApplicationDirectoryName(fileName)) {
                queue(SyncAction.UpdateProjectStructure, file);
                queue(SyncAction.EnsureRunConfigurationExists, file);
            } else if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
                queue(SyncAction.UpdateProjectStructure, file);
            } else {
                if (!myProject.isInitialized())
                    return;
                final Module module = ProjectRootManager.getInstance(myProject).getFileIndex().getModuleForFile(file);
                if (module == null) {
                    // Maybe it is creation of a plugin in plugin directory.
                    if (file.isDirectory()) {
                        if (myPluginRoots.contains(file.getParent())) {
                            queue(SyncAction.UpdateProjectStructure, myProject);
                            return;
                        }
                        if (!myOutOfModuleDirectoryCreatedActionAdded) {
                            queue(SyncAction.OutOfModuleDirectoryCreated, myProject);
                            myOutOfModuleDirectoryCreatedActionAdded = true;
                        }
                    }
                    return;
                }
                if (!MvcConsole.isUpdatingVfsByConsoleProcess(module))
                    return;
                final MvcFramework framework = MvcFramework.getInstance(module);
                if (framework == null)
                    return;
                if (framework.isToReformatOnCreation(file) || file.isDirectory()) {
                    ApplicationManager.getApplication().invokeLater(() -> {
                        if (!file.isValid())
                            return;
                        if (!framework.hasSupport(module))
                            return;
                        final List<VirtualFile> files = new ArrayList<>();
                        if (file.isDirectory()) {
                            ModuleRootManager.getInstance(module).getFileIndex().iterateContentUnderDirectory(file, new ContentIterator() {

                                @Override
                                public boolean processFile(VirtualFile fileOrDir) {
                                    if (!fileOrDir.isDirectory() && framework.isToReformatOnCreation(fileOrDir)) {
                                        files.add(file);
                                    }
                                    return true;
                                }
                            });
                        } else {
                            files.add(file);
                        }
                        PsiManager manager = PsiManager.getInstance(myProject);
                        for (VirtualFile virtualFile : files) {
                            PsiFile psiFile = manager.findFile(virtualFile);
                            if (psiFile != null) {
                                new ReformatCodeProcessor(myProject, psiFile, null, false).run();
                            }
                        }
                    }, module.getDisposed());
                }
            }
        }

        @Override
        public void fileDeleted(@NotNull VirtualFileEvent event) {
            myModificationTracker.incModificationCount();
            final VirtualFile file = event.getFile();
            if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
                queue(SyncAction.UpdateProjectStructure, file);
            }
        }

        @Override
        public void contentsChanged(@NotNull VirtualFileEvent event) {
            final String fileName = event.getFileName();
            if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName)) {
                queue(SyncAction.UpdateProjectStructure, event.getFile());
            }
        }

        @Override
        public void fileMoved(@NotNull VirtualFileMoveEvent event) {
            myModificationTracker.incModificationCount();
        }

        @Override
        public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
            if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) {
                myModificationTracker.incModificationCount();
            }
        }
    }));
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) PsiManager(com.intellij.psi.PsiManager) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) BulkVirtualFileListenerAdapter(com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 2 with ReformatCodeProcessor

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

the class AsposeMavenUtil method runOrApplyFileTemplate.

private static void runOrApplyFileTemplate(Project project, VirtualFile file, String templateName, Properties properties) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance();
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties(project);
    allProperties.putAll(properties);
    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);
    }
    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) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) Properties(java.util.Properties)

Example 3 with ReformatCodeProcessor

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

the class AsposeMavenUtil method runOrApplyFileTemplate.

private static void runOrApplyFileTemplate(Project project, VirtualFile file, String templateName, Properties properties) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance();
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties(project);
    allProperties.putAll(properties);
    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);
    }
    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) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) Properties(java.util.Properties)

Example 4 with ReformatCodeProcessor

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

the class AsposeMavenUtil method runOrApplyFileTemplate.

private static void runOrApplyFileTemplate(Project project, VirtualFile file, String templateName, Properties properties) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance();
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties(project);
    allProperties.putAll(properties);
    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);
    }
    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) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) Properties(java.util.Properties)

Example 5 with ReformatCodeProcessor

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

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