From f2f3983054b5e2b5ec9f81627eb1cb5dad3b4eb9 Mon Sep 17 00:00:00 2001 From: Oleg Shpynov Date: Mon, 28 Mar 2011 17:14:07 +0400 Subject: [PATCH] Decouple Optimize imports and reformat code checkin handlers --- .../vcs/checkin/BeforeCheckinHandlerUtil.java | 59 +++++++++++++ .../OptimizeImportsBeforeCheckinHandler.java | 87 +++++++++++++++++++ .../OptimizeOptionsCheckinHandlerFactory.java | 30 +++++++ ...java => ReformatBeforeCheckinHandler.java} | 63 ++------------ ...ava => ReformatCheckinHandlerFactory.java} | 6 +- resources/src/META-INF/IdeaPlugin.xml | 3 +- 6 files changed, 188 insertions(+), 60 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/openapi/vcs/checkin/BeforeCheckinHandlerUtil.java create mode 100644 platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeImportsBeforeCheckinHandler.java create mode 100644 platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeOptionsCheckinHandlerFactory.java rename platform/lang-impl/src/com/intellij/openapi/vcs/checkin/{StandardBeforeCheckinHandler.java => ReformatBeforeCheckinHandler.java} (53%) rename platform/lang-impl/src/com/intellij/openapi/vcs/checkin/{StandardCheckinHandlerFactory.java => ReformatCheckinHandlerFactory.java} (87%) diff --git a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/BeforeCheckinHandlerUtil.java b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/BeforeCheckinHandlerUtil.java new file mode 100644 index 000000000000..5e15f0dab578 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/BeforeCheckinHandlerUtil.java @@ -0,0 +1,59 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.vcs.checkin; + +import com.intellij.openapi.components.StorageScheme; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ex.ProjectEx; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; +import com.intellij.psi.util.PsiUtilBase; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * @author oleg + */ +public class BeforeCheckinHandlerUtil { + + public static PsiFile[] getPsiFiles(final Project myProject, final Collection selectedFiles) { + ArrayList result = new ArrayList(); + PsiManager psiManager = PsiManager.getInstance(myProject); + + VirtualFile projectFileDir = null; + final StorageScheme storageScheme = ((ProjectEx) myProject).getStateStore().getStorageScheme(); + if (StorageScheme.DIRECTORY_BASED.equals(storageScheme)) { + VirtualFile baseDir = myProject.getBaseDir(); + if (baseDir != null) { + projectFileDir = baseDir.findChild(Project.DIRECTORY_STORE_FOLDER); + } + } + + for (VirtualFile file : selectedFiles) { + if (file.isValid()) { + if (projectFileDir != null && VfsUtil.isAncestor(projectFileDir, file, false)) { + continue; + } + PsiFile psiFile = psiManager.findFile(file); + if (psiFile != null) result.add(psiFile); + } + } + return PsiUtilBase.toPsiFileArray(result); + } +} diff --git a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeImportsBeforeCheckinHandler.java b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeImportsBeforeCheckinHandler.java new file mode 100644 index 000000000000..cb7390cd151d --- /dev/null +++ b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeImportsBeforeCheckinHandler.java @@ -0,0 +1,87 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.intellij.openapi.vcs.checkin; + +import com.intellij.codeInsight.actions.OptimizeImportsProcessor; +import com.intellij.openapi.fileEditor.FileDocumentManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.CheckinProjectPanel; +import com.intellij.openapi.vcs.VcsBundle; +import com.intellij.openapi.vcs.VcsConfiguration; +import com.intellij.openapi.vcs.ui.RefreshableOnComponent; +import com.intellij.openapi.vfs.VirtualFile; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.awt.*; +import java.util.Collection; + +public class OptimizeImportsBeforeCheckinHandler extends CheckinHandler implements CheckinMetaHandler { + protected final Project myProject; + private final CheckinProjectPanel myPanel; + + public OptimizeImportsBeforeCheckinHandler(final Project project, final CheckinProjectPanel panel) { + myProject = project; + myPanel = panel; + } + + @Nullable + public RefreshableOnComponent getBeforeCheckinConfigurationPanel() { + final JCheckBox optimizeBox = new JCheckBox(VcsBundle.message("checkbox.checkin.options.optimize.imports")); + + return new RefreshableOnComponent() { + public JComponent getComponent() { + final JPanel panel = new JPanel(new GridLayout(1, 0)); + panel.add(optimizeBox); + return panel; + } + + public void refresh() { + } + + public void saveState() { + getSettings().OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT = optimizeBox.isSelected(); + } + + public void restoreState() { + optimizeBox.setSelected(getSettings().OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT); + } + }; + + } + + protected VcsConfiguration getSettings() { + return VcsConfiguration.getInstance(myProject); + } + + public void runCheckinHandlers(final Runnable finishAction) { + final VcsConfiguration configuration = VcsConfiguration.getInstance(myProject); + final Collection files = myPanel.getVirtualFiles(); + + final Runnable performCheckoutAction = new Runnable() { + public void run() { + FileDocumentManager.getInstance().saveAllDocuments(); + finishAction.run(); + } + }; + + if (configuration.OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT) { + new OptimizeImportsProcessor(myProject, BeforeCheckinHandlerUtil.getPsiFiles(myProject, files), performCheckoutAction).run(); + } + + } +} diff --git a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeOptionsCheckinHandlerFactory.java b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeOptionsCheckinHandlerFactory.java new file mode 100644 index 000000000000..87f3bebcfc21 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/OptimizeOptionsCheckinHandlerFactory.java @@ -0,0 +1,30 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.intellij.openapi.vcs.checkin; + +import com.intellij.openapi.vcs.CheckinProjectPanel; +import org.jetbrains.annotations.NotNull; + +/** + * @author oleg + */ +public class OptimizeOptionsCheckinHandlerFactory extends CheckinHandlerFactory { + @NotNull + public CheckinHandler createHandler(final CheckinProjectPanel panel) { + return new OptimizeImportsBeforeCheckinHandler(panel.getProject(), panel); + } +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/StandardBeforeCheckinHandler.java b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/ReformatBeforeCheckinHandler.java similarity index 53% rename from platform/lang-impl/src/com/intellij/openapi/vcs/checkin/StandardBeforeCheckinHandler.java rename to platform/lang-impl/src/com/intellij/openapi/vcs/checkin/ReformatBeforeCheckinHandler.java index 21a4483b75f9..56f4dc29da38 100644 --- a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/StandardBeforeCheckinHandler.java +++ b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/ReformatBeforeCheckinHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,46 +16,36 @@ package com.intellij.openapi.vcs.checkin; -import com.intellij.codeInsight.actions.OptimizeImportsProcessor; import com.intellij.codeInsight.actions.ReformatCodeProcessor; -import com.intellij.openapi.components.StorageScheme; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ex.ProjectEx; import com.intellij.openapi.vcs.CheckinProjectPanel; import com.intellij.openapi.vcs.VcsBundle; import com.intellij.openapi.vcs.VcsConfiguration; import com.intellij.openapi.vcs.ui.RefreshableOnComponent; -import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiManager; -import com.intellij.psi.util.PsiUtilBase; import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; -import java.util.ArrayList; import java.util.Collection; -public class StandardBeforeCheckinHandler extends CheckinHandler implements CheckinMetaHandler { +public class ReformatBeforeCheckinHandler extends CheckinHandler implements CheckinMetaHandler { protected final Project myProject; private final CheckinProjectPanel myPanel; - public StandardBeforeCheckinHandler(final Project project, final CheckinProjectPanel panel) { + public ReformatBeforeCheckinHandler(final Project project, final CheckinProjectPanel panel) { myProject = project; myPanel = panel; } @Nullable public RefreshableOnComponent getBeforeCheckinConfigurationPanel() { - final JCheckBox optimizeBox = new JCheckBox(VcsBundle.message("checkbox.checkin.options.optimize.imports")); final JCheckBox reformatBox = new JCheckBox(VcsBundle.message("checkbox.checkin.options.reformat.code")); return new RefreshableOnComponent() { public JComponent getComponent() { - final JPanel panel = new JPanel(new GridLayout(2, 0)); - panel.add(optimizeBox); + final JPanel panel = new JPanel(new GridLayout(1, 0)); panel.add(reformatBox); return panel; } @@ -64,12 +54,10 @@ public class StandardBeforeCheckinHandler extends CheckinHandler implements Chec } public void saveState() { - getSettings().OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT = optimizeBox.isSelected(); getSettings().REFORMAT_BEFORE_PROJECT_COMMIT = reformatBox.isSelected(); } public void restoreState() { - optimizeBox.setSelected(getSettings().OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT); reformatBox.setSelected(getSettings().REFORMAT_BEFORE_PROJECT_COMMIT); } }; @@ -91,22 +79,11 @@ public class StandardBeforeCheckinHandler extends CheckinHandler implements Chec } }; - final Runnable reformatCodeAndPerformCheckout = new Runnable() { - public void run() { - if (reformat(configuration, true)) { - new ReformatCodeProcessor(myProject, getPsiFiles(files), performCheckoutAction).run(); - } - else { - performCheckoutAction.run(); - } - } - }; - - if (configuration.OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT) { - new OptimizeImportsProcessor(myProject, getPsiFiles(files), reformatCodeAndPerformCheckout).run(); + if (reformat(configuration, true)) { + new ReformatCodeProcessor(myProject, BeforeCheckinHandlerUtil.getPsiFiles(myProject, files), performCheckoutAction).run(); } else { - reformatCodeAndPerformCheckout.run(); + performCheckoutAction.run(); } } @@ -115,30 +92,4 @@ public class StandardBeforeCheckinHandler extends CheckinHandler implements Chec return checkinProject ? configuration.REFORMAT_BEFORE_PROJECT_COMMIT : configuration.REFORMAT_BEFORE_FILE_COMMIT; } - private PsiFile[] getPsiFiles(Collection selectedFiles) { - ArrayList result = new ArrayList(); - PsiManager psiManager = PsiManager.getInstance(myProject); - - VirtualFile projectFileDir = null; - final StorageScheme storageScheme = ((ProjectEx) myProject).getStateStore().getStorageScheme(); - if (StorageScheme.DIRECTORY_BASED.equals(storageScheme)) { - VirtualFile baseDir = myProject.getBaseDir(); - if (baseDir != null) { - projectFileDir = baseDir.findChild(Project.DIRECTORY_STORE_FOLDER); - } - } - - for (VirtualFile file : selectedFiles) { - if (file.isValid()) { - if (projectFileDir != null && VfsUtil.isAncestor(projectFileDir, file, false)) { - continue; - } - PsiFile psiFile = psiManager.findFile(file); - if (psiFile != null) result.add(psiFile); - } - } - return PsiUtilBase.toPsiFileArray(result); - } - - } diff --git a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/StandardCheckinHandlerFactory.java b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/ReformatCheckinHandlerFactory.java similarity index 87% rename from platform/lang-impl/src/com/intellij/openapi/vcs/checkin/StandardCheckinHandlerFactory.java rename to platform/lang-impl/src/com/intellij/openapi/vcs/checkin/ReformatCheckinHandlerFactory.java index 6a50b493c544..1694cf3335f9 100644 --- a/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/StandardCheckinHandlerFactory.java +++ b/platform/lang-impl/src/com/intellij/openapi/vcs/checkin/ReformatCheckinHandlerFactory.java @@ -16,15 +16,15 @@ package com.intellij.openapi.vcs.checkin; -import org.jetbrains.annotations.NotNull; import com.intellij.openapi.vcs.CheckinProjectPanel; +import org.jetbrains.annotations.NotNull; /** * @author yole */ -public class StandardCheckinHandlerFactory extends CheckinHandlerFactory { +public class ReformatCheckinHandlerFactory extends CheckinHandlerFactory { @NotNull public CheckinHandler createHandler(final CheckinProjectPanel panel) { - return new StandardBeforeCheckinHandler(panel.getProject(), panel); + return new ReformatBeforeCheckinHandler(panel.getProject(), panel); } } \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index df485bacd239..3716378bf3d2 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -746,7 +746,8 @@ implementationClass="com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor"/> - + +