|
| 1 | +package org.moreunit.intellij.plugin.actions; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.navigation.GotoTargetHandler; |
| 4 | +import com.intellij.openapi.editor.Editor; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.openapi.vfs.VirtualFile; |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import com.intellij.psi.PsiFile; |
| 9 | +import com.intellij.psi.PsiManager; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | +import org.moreunit.intellij.plugin.files.SubjectFile; |
| 13 | +import org.moreunit.intellij.plugin.navigation.FileEditorHistory; |
| 14 | +import org.moreunit.intellij.plugin.navigation.ProjectFileEditorHistory; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | + |
| 18 | +public class JumpToLastOpenedTestOrCodeHandler extends GotoTargetHandler { |
| 19 | + |
| 20 | + private final JumpToTestOrCodeHandler jumpToTestOrCodeHandler = new JumpToTestOrCodeHandler(); |
| 21 | + |
| 22 | + @Override |
| 23 | + protected String getFeatureUsedKey() { |
| 24 | + return "org.moreunit.actions.jumpToLastOpenedTest"; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + protected boolean shouldSortTargets() { |
| 29 | + return jumpToTestOrCodeHandler.shouldSortTargets(); |
| 30 | + } |
| 31 | + |
| 32 | + @Nullable |
| 33 | + @Override |
| 34 | + protected GotoData getSourceAndTargetElements(Editor editor, PsiFile srcFile) { |
| 35 | + Project project = editor.getProject(); |
| 36 | + FileEditorHistory fileEditorHistory = project.getComponent(ProjectFileEditorHistory.class).getHistory(); |
| 37 | + |
| 38 | + SubjectFile subject = new SubjectFile(srcFile.getVirtualFile()); |
| 39 | + |
| 40 | + final VirtualFile destFile; |
| 41 | + if (subject.isTestFile()) { |
| 42 | + destFile = fileEditorHistory.getLastFocusedProdFile(); |
| 43 | + } else { |
| 44 | + destFile = fileEditorHistory.getLastFocusedTestFile(); |
| 45 | + } |
| 46 | + |
| 47 | + if (destFile != null) { |
| 48 | + PsiFile psiFile = PsiManager.getInstance(project).findFile(destFile); |
| 49 | + return new GotoData(srcFile, new PsiElement[]{ psiFile }, Collections.<AdditionalAction>emptyList()); |
| 50 | + } |
| 51 | + |
| 52 | + return jumpToTestOrCodeHandler.getSourceAndTargetElements(editor, srcFile); |
| 53 | + } |
| 54 | + |
| 55 | + @NotNull |
| 56 | + @Override |
| 57 | + protected String getChooserTitle(PsiElement sourceElement, String name, int length) { |
| 58 | + return jumpToTestOrCodeHandler.getChooserTitle(sourceElement, name, length); |
| 59 | + } |
| 60 | + |
| 61 | + @NotNull |
| 62 | + @Override |
| 63 | + protected String getNotFoundMessage(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { |
| 64 | + return jumpToTestOrCodeHandler.getNotFoundMessage(project, editor, file); |
| 65 | + } |
| 66 | +} |
0 commit comments