Search in sources :

Example 16 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project android-butterknife-zelezny by avast.

the class InjectWriter method run.

@Override
public void run() throws Throwable {
    final IButterKnife butterKnife = ButterKnifeFactory.findButterKnifeForPsiElement(mProject, mFile);
    if (butterKnife == null) {
        // Butterknife library is not available for project
        return;
    }
    if (mCreateHolder) {
        generateAdapter(butterKnife);
    } else {
        if (Utils.getInjectCount(mElements) > 0) {
            generateFields(butterKnife);
        }
        generateInjects(butterKnife);
        if (Utils.getClickCount(mElements) > 0) {
            generateClick();
        }
        Utils.showInfoNotification(mProject, String.valueOf(Utils.getInjectCount(mElements)) + " injections and " + String.valueOf(Utils.getClickCount(mElements)) + " onClick added to " + mFile.getName());
    }
    // reformat class
    JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(mProject);
    styleManager.optimizeImports(mFile);
    styleManager.shortenClassReferences(mClass);
    new ReformatCodeProcessor(mProject, mClass.getContainingFile(), null, false).runWithoutProgress();
}
Also used : JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IButterKnife(com.avast.android.butterknifezelezny.butterknife.IButterKnife) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor)

Example 17 with ReformatCodeProcessor

use of com.intellij.codeInsight.actions.ReformatCodeProcessor in project moe-ide-integration by multi-os-engine.

the class MOEModuleBuilder method setupRootModel.

@Override
public void setupRootModel(final ModifiableRootModel rootModel) throws ConfigurationException {
    Project project = rootModel.getProject();
    Module module = rootModel.getModule();
    myJdk = MOESdkType.getJDK();
    ProjectRootManager.getInstance(project).setProjectSdk(myJdk);
    rootModel.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(MOESdkType.REQUIRED_JAVA_LANGUAGE_LEVEL);
    super.setupRootModel(rootModel);
    ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
    String projectPath = rootModel.getProject().getBasePath();
    String contentEntryPath = getContentEntryPath();
    if (contentEntryPath == null || contentEntryPath.isEmpty()) {
        throw new RuntimeException("Can't get content entry path.");
    }
    VirtualFile contentRoot = LocalFileSystem.getInstance().findFileByIoFile(new File(contentEntryPath));
    try {
        createModule(contentRoot, project);
    } catch (MOEProjectComposer.MOEProjectComposerException e) {
        throw new ConfigurationException(e.getMessage());
    }
    VirtualFile[] contentFiles = new VirtualFile[] { contentRoot };
    VfsUtil.markDirtyAndRefresh(false, true, true, contentFiles);
    new ReformatCodeProcessor(project, module, false).run();
    String resourcePath = "src/main/" + MOESdkPlugin.getResourcesFolderName();
    String sourcePath = "src/main/java";
    for (ContentEntry entry : rootModel.getContentEntries()) {
        for (SourceFolder srcFolder : entry.getSourceFolders()) {
            entry.removeSourceFolder(srcFolder);
        }
        VirtualFile sourceFile = null;
        if (contentRoot != null) {
            sourceFile = contentRoot.findFileByRelativePath(sourcePath);
        }
        if (sourceFile != null) {
            entry.addSourceFolder(sourceFile, false);
        }
        VirtualFile resourceFolder = null;
        if (contentRoot != null) {
            resourceFolder = contentRoot.findFileByRelativePath(resourcePath);
        }
        if (resourceFolder != null && resourceFolder.exists()) {
            SourceFolder sourceFolder = entry.addSourceFolder(resourceFolder, JavaResourceRootType.RESOURCE);
            JavaSourceRootProperties properties = sourceFolder.getJpsElement().getProperties(JavaModuleSourceRootTypes.SOURCES);
            if (properties != null) {
                properties.setForGeneratedSources(true);
            }
        }
    }
    try {
        configureGradle(rootModel);
    } catch (IOException e) {
        MOEToolWindow.getInstance(project).error("Error occurred during gradle configuration: " + e.getMessage());
    }
    if (!isNewProject) {
        File settingsGradle = new File(projectPath, "settings.gradle");
        try {
            if (!settingsGradle.exists()) {
                if (!settingsGradle.createNewFile()) {
                    MOEToolWindow.getInstance(project).error("Error occurred during gradle settings file.");
                }
            }
            modifyGradleSettings(settingsGradle, new Module[] { rootModel.getModule() });
        } catch (IOException e) {
            MOEToolWindow.getInstance(project).error("Error occurred during gradle configuration: " + e.getMessage());
        }
    }
    if (contentRoot != null) {
        contentRoot.refresh(false, true);
    }
    Sdk sdk = MOESdkType.getMOESdk(rootModel.getModule());
    if (sdk != null) {
        rootModel.setSdk(sdk);
    } else {
        MOEToolWindow.getInstance(project).error("Error, unable set Sdk.");
    }
    rootModel.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(MOESdkType.REQUIRED_JAVA_LANGUAGE_LEVEL);
    StartupManager.getInstance(project).runWhenProjectIsInitialized(new Runnable() {

        @Override
        public void run() {
            configureRun(rootModel);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MOEProjectComposer(org.moe.generator.project.MOEProjectComposer) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) IOException(java.io.IOException) JavaSourceRootProperties(org.jetbrains.jps.model.java.JavaSourceRootProperties) Project(com.intellij.openapi.project.Project) SourceFolder(com.intellij.openapi.roots.SourceFolder) ConfigurationException(com.intellij.openapi.options.ConfigurationException) ContentEntry(com.intellij.openapi.roots.ContentEntry) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) LanguageLevelModuleExtension(com.intellij.openapi.roots.LanguageLevelModuleExtension) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 18 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)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