Decouple Optimize imports and reformat code checkin handlers

This commit is contained in:
Oleg Shpynov
2011-03-28 17:15:13 +04:00
parent 9dfda9c0a0
commit f2f3983054
6 changed files with 188 additions and 60 deletions
@@ -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<VirtualFile> selectedFiles) {
ArrayList<PsiFile> result = new ArrayList<PsiFile>();
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);
}
}
@@ -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<VirtualFile> 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();
}
}
}
@@ -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);
}
}
@@ -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<VirtualFile> selectedFiles) {
ArrayList<PsiFile> result = new ArrayList<PsiFile>();
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);
}
}
@@ -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);
}
}
+2 -1
View File
@@ -746,7 +746,8 @@
implementationClass="com.intellij.codeInsight.editorActions.smartEnter.JavaSmartEnterProcessor"/>
<lang.smartEnterProcessor language="JSPX" implementationClass="com.intellij.codeInsight.completion.XmlSmartEnterProcessor"/>
<checkinHandlerFactory implementation="com.intellij.openapi.vcs.checkin.StandardCheckinHandlerFactory"/>
<checkinHandlerFactory implementation="com.intellij.openapi.vcs.checkin.ReformatCheckinHandlerFactory"/>
<checkinHandlerFactory implementation="com.intellij.openapi.vcs.checkin.OptimizeOptionsCheckinHandlerFactory"/>
<checkinHandlerFactory implementation="com.intellij.openapi.vcs.checkin.CodeAnalysisCheckinHandlerFactory"/>
<checkinHandlerFactory implementation="com.intellij.openapi.vcs.checkin.TodoCheckinHandlerFactory"/>