From 24c205b0f2edb4a85b98ed22a7e08b0b3dfa1999 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Wed, 30 Dec 2015 17:33:42 +0300 Subject: [PATCH] vcs: move methods to util class --- .../vcs/actions/AnnotateLocalFileAction.java | 16 ++----- .../actions/AnnotateRevisionActionBase.java | 13 ++--- .../vcs/actions/AnnotateToggleAction.java | 7 --- .../actions/AnnotateVcsVirtualFileAction.java | 27 +++-------- .../openapi/vcs/actions/VcsAnnotateUtil.java | 48 +++++++++++++++++++ 5 files changed, 62 insertions(+), 49 deletions(-) create mode 100644 platform/vcs-impl/src/com/intellij/openapi/vcs/actions/VcsAnnotateUtil.java diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateLocalFileAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateLocalFileAction.java index 378490e4992f..bccb3801296b 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateLocalFileAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateLocalFileAction.java @@ -37,7 +37,6 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -71,7 +70,7 @@ public class AnnotateLocalFileAction { private static boolean isSuspended(AnActionEvent e) { VirtualFile file = assertNotNull(VcsContextFactory.SERVICE.getInstance().createContextOn(e).getSelectedFile()); - return AnnotateToggleAction.getBackgroundableLock(e.getRequiredData(CommonDataKeys.PROJECT), file).isLocked(); + return VcsAnnotateUtil.getBackgroundableLock(e.getRequiredData(CommonDataKeys.PROJECT), file).isLocked(); } private static boolean isAnnotated(AnActionEvent e) { @@ -129,7 +128,7 @@ public class AnnotateLocalFileAction { final Ref fileAnnotationRef = new Ref(); final Ref exceptionRef = new Ref(); - AnnotateToggleAction.getBackgroundableLock(project, file).lock(); + VcsAnnotateUtil.getBackgroundableLock(project, file).lock(); final Task.Backgroundable annotateTask = new Task.Backgroundable(project, VcsBundle.message("retrieving.annotations"), true) { @Override @@ -155,7 +154,7 @@ public class AnnotateLocalFileAction { @Override public void onSuccess() { - AnnotateToggleAction.getBackgroundableLock(project, file).unlock(); + VcsAnnotateUtil.getBackgroundableLock(project, file).unlock(); if (!exceptionRef.isNull()) { LOG.warn(exceptionRef.get()); @@ -173,13 +172,8 @@ public class AnnotateLocalFileAction { @NotNull private static List getEditors(@NotNull VcsContext context) { Project project = assertNotNull(context.getProject()); - List list = new ArrayList(); - for (FileEditor fileEditor : FileEditorManager.getInstance(project).getEditors(context.getSelectedFile())) { - if (fileEditor instanceof TextEditor) { - list.add(((TextEditor)fileEditor).getEditor()); - } - } - return list; + VirtualFile file = assertNotNull(context.getSelectedFile()); + return VcsAnnotateUtil.getEditors(project, file); } public static class Provider implements AnnotateToggleAction.Provider { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateRevisionActionBase.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateRevisionActionBase.java index fc1cad471008..37b9ecd0717b 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateRevisionActionBase.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateRevisionActionBase.java @@ -7,7 +7,6 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.Task; -import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Ref; import com.intellij.openapi.vcs.AbstractVcs; import com.intellij.openapi.vcs.AbstractVcsHelper; @@ -16,7 +15,6 @@ import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.annotate.AnnotationProvider; import com.intellij.openapi.vcs.annotate.FileAnnotation; import com.intellij.openapi.vcs.history.VcsFileRevision; -import com.intellij.openapi.vcs.impl.BackgroundableActionLock; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.diff.Diff; import com.intellij.util.diff.FilesTooBigForDiffException; @@ -58,7 +56,7 @@ public abstract class AnnotateRevisionActionBase extends AnAction { AnnotationProvider provider = vcs.getCachingAnnotationProvider(); if (provider == null || !provider.isAnnotationValid(fileRevision)) return false; - if (getBackgroundableLock(vcs.getProject(), file).isLocked()) return false; + if (VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).isLocked()) return false; return true; } @@ -83,7 +81,7 @@ public abstract class AnnotateRevisionActionBase extends AnAction { final Ref newLineRef = new Ref(); final Ref exceptionRef = new Ref(); - getBackgroundableLock(vcs.getProject(), file).lock(); + VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).lock(); ProgressManager.getInstance().run(new Task.Backgroundable(vcs.getProject(), VcsBundle.message("retrieving.annotations"), true) { public void run(@NotNull ProgressIndicator indicator) { @@ -115,7 +113,7 @@ public abstract class AnnotateRevisionActionBase extends AnAction { @Override public void onSuccess() { - getBackgroundableLock(vcs.getProject(), file).unlock(); + VcsAnnotateUtil.getBackgroundableLock(vcs.getProject(), file).unlock(); if (!exceptionRef.isNull()) { AbstractVcsHelper.getInstance(myProject).showError(exceptionRef.get(), VcsBundle.message("operation.name.annotate")); @@ -126,9 +124,4 @@ public abstract class AnnotateRevisionActionBase extends AnAction { } }); } - - @NotNull - private static BackgroundableActionLock getBackgroundableLock(@NotNull Project project, @NotNull VirtualFile file) { - return AnnotateToggleAction.getBackgroundableLock(project, file); - } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java index cc726313b33a..761352ef6c2f 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateToggleAction.java @@ -34,9 +34,7 @@ import com.intellij.openapi.vcs.annotate.FileAnnotation; import com.intellij.openapi.vcs.annotate.LineAnnotationAspect; import com.intellij.openapi.vcs.history.VcsFileRevision; import com.intellij.openapi.vcs.history.VcsRevisionNumber; -import com.intellij.openapi.vcs.impl.BackgroundableActionLock; import com.intellij.openapi.vcs.impl.UpToDateLineNumberProviderImpl; -import com.intellij.openapi.vcs.impl.VcsBackgroundableActions; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; @@ -236,11 +234,6 @@ public class AnnotateToggleAction extends ToggleAction implements DumbAware, Ann commitAuthorColors.size() > 1 ? commitAuthorColors : null); } - @NotNull - public static BackgroundableActionLock getBackgroundableLock(@NotNull Project project, @NotNull VirtualFile file) { - return BackgroundableActionLock.getLock(project, VcsBackgroundableActions.ANNOTATE, file.getPath()); - } - @Nullable private static Provider getProvider(AnActionEvent e) { for (Provider provider : EP_NAME.getExtensions()) { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateVcsVirtualFileAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateVcsVirtualFileAction.java index 5fb5638cda70..3f774b777ce4 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateVcsVirtualFileAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/AnnotateVcsVirtualFileAction.java @@ -19,9 +19,6 @@ import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.fileEditor.FileEditor; -import com.intellij.openapi.fileEditor.FileEditorManager; -import com.intellij.openapi.fileEditor.TextEditor; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; @@ -43,7 +40,6 @@ import com.intellij.vcsUtil.VcsUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -60,7 +56,7 @@ public class AnnotateVcsVirtualFileAction { VirtualFile file = selectedFiles[0]; if (file.isDirectory() || file.getFileType().isBinary()) return false; - if (getEditors(project, file).isEmpty()) return false; + if (VcsAnnotateUtil.getEditors(project, file).isEmpty()) return false; AnnotationData data = extractData(project, file); if (data == null) return false; @@ -71,13 +67,13 @@ public class AnnotateVcsVirtualFileAction { private static boolean isSuspended(AnActionEvent e) { VirtualFile file = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE_ARRAY)[0]; - return AnnotateToggleAction.getBackgroundableLock(e.getRequiredData(CommonDataKeys.PROJECT), file).isLocked(); + return VcsAnnotateUtil.getBackgroundableLock(e.getRequiredData(CommonDataKeys.PROJECT), file).isLocked(); } private static boolean isAnnotated(AnActionEvent e) { Project project = e.getRequiredData(CommonDataKeys.PROJECT); VirtualFile file = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE_ARRAY)[0]; - List editors = getEditors(project, file); + List editors = VcsAnnotateUtil.getEditors(project, file); return ContainerUtil.exists(editors, new Condition() { @Override public boolean value(Editor editor) { @@ -89,7 +85,7 @@ public class AnnotateVcsVirtualFileAction { private static void perform(AnActionEvent e, boolean selected) { final Project project = e.getRequiredData(CommonDataKeys.PROJECT); final VirtualFile file = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE_ARRAY)[0]; - List editors = getEditors(project, file); + List editors = VcsAnnotateUtil.getEditors(project, file); if (!selected) { for (Editor editor : editors) { @@ -113,7 +109,7 @@ public class AnnotateVcsVirtualFileAction { final Ref fileAnnotationRef = new Ref(); final Ref exceptionRef = new Ref(); - AnnotateToggleAction.getBackgroundableLock(project, file).lock(); + VcsAnnotateUtil.getBackgroundableLock(project, file).lock(); final Task.Backgroundable annotateTask = new Task.Backgroundable(project, VcsBundle.message("retrieving.annotations"), true) { @Override @@ -139,7 +135,7 @@ public class AnnotateVcsVirtualFileAction { @Override public void onSuccess() { - AnnotateToggleAction.getBackgroundableLock(project, file).unlock(); + VcsAnnotateUtil.getBackgroundableLock(project, file).unlock(); if (!exceptionRef.isNull()) { LOG.warn(exceptionRef.get()); @@ -154,17 +150,6 @@ public class AnnotateVcsVirtualFileAction { ProgressManager.getInstance().run(annotateTask); } - @NotNull - private static List getEditors(@NotNull Project project, @NotNull VirtualFile file) { - List list = new ArrayList(); - for (FileEditor fileEditor : FileEditorManager.getInstance(project).getEditors(file)) { - if (fileEditor instanceof TextEditor) { - list.add(((TextEditor)fileEditor).getEditor()); - } - } - return list; - } - @Nullable private static AnnotationData extractData(@NotNull Project project, @NotNull VirtualFile file) { FilePath filePath = null; diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/VcsAnnotateUtil.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/VcsAnnotateUtil.java new file mode 100644 index 000000000000..dd3b686e90ea --- /dev/null +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/VcsAnnotateUtil.java @@ -0,0 +1,48 @@ +/* + * Copyright 2000-2015 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.actions; + +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.fileEditor.FileEditor; +import com.intellij.openapi.fileEditor.FileEditorManager; +import com.intellij.openapi.fileEditor.TextEditor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.impl.BackgroundableActionLock; +import com.intellij.openapi.vcs.impl.VcsBackgroundableActions; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +class VcsAnnotateUtil { + @NotNull + public static List getEditors(@NotNull Project project, @NotNull VirtualFile file) { + FileEditor[] editors = FileEditorManager.getInstance(project).getEditors(file); + return ContainerUtil.mapNotNull(editors, new Function() { + @Override + public Editor fun(FileEditor fileEditor) { + return fileEditor instanceof TextEditor ? ((TextEditor)fileEditor).getEditor() : null; + } + }); + } + + @NotNull + public static BackgroundableActionLock getBackgroundableLock(@NotNull Project project, @NotNull VirtualFile file) { + return BackgroundableActionLock.getLock(project, VcsBackgroundableActions.ANNOTATE, file.getPath()); + } +}