package org.jacoco.coverage.actions;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.TextAnnotationGutterProvider;
import com.intellij.openapi.editor.colors.CodeInsightColors;
import com.intellij.openapi.editor.colors.ColorKey;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.FrameWrapper;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import org.jacoco.coverage.util.CoverageUtils;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.io.File;
import java.net.URI;
import java.util.List;

public class AnnotateFileWithCoverageAction extends AnAction {


    @Override
    public void actionPerformed(AnActionEvent e) {
        DataContext dataContext = e.getDataContext();
        Project project = PlatformDataKeys.PROJECT.getData(dataContext);
        if (project == null) {
            return;
        }

        showInBrowser(e, project);
    }

    private void showInBrowser(AnActionEvent e, Project project) {
        PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);

        if (psiFile instanceof PsiJavaFile) {
            PsiJavaFile javaFile = (PsiJavaFile) psiFile;

            File coverageRootPath = CoverageUtils.getCoverageReportRoot(project);
            if (coverageRootPath.exists()) {
                String packageName = javaFile.getPackageName();
                File url = new File(coverageRootPath, packageName);
                VirtualFile virtualFile = psiFile.getVirtualFile();
                if (virtualFile != null) {
                    url = new File(url, virtualFile.getName() + ".html");
                    BrowserUtil.launchBrowser(url.toURI().getPath());
                }
            }
        }

    }
}

@@ -144,709 +144,23 @@
 api.
-editor.Document;%0Aimport com.intellij.openapi.editor.Editor;%0Aimport com.intellij.openapi.editor.EditorFactory;%0Aimport com.intellij.openapi.editor.TextAnnotationGutterProvider;%0Aimport com.intellij.openapi.editor.colors.CodeInsightColors;%0Aimport com.intellij.openapi.editor.colors.ColorKey;%0Aimport com.intellij.openapi.editor.colors.EditorColors;%0Aimport com.intellij.openapi.editor.colors.EditorFontType;%0Aimport com.intellij.openapi.editor.markup.TextAttributes;%0Aimport com.intellij.openapi.fileEditor.FileDocumentManager;%0Aimport com.intellij.openapi.fileTypes.StdFileTypes;%0Aimport com.intellij.openapi.project.Project;%0Aimport com.intellij.openapi.ui.FrameWrapper;%0Aimport com.intellij.openapi.vfs.VfsUtil
+project.Project
 ;%0Aim
@@ -233,13 +233,15 @@
 .Psi
-Class
+Element
 ;%0Aim
@@ -319,151 +319,122 @@
 ort 
-org.jacoco.coverage.util.CoverageUtils;%0Aimport org.jetbrains.annotations.Nullable;%0A%0Aimport java.awt.*;%0Aimport java.io.File;%0Aimport java.net.URI
+com.intellij.testIntegration.TestFinderHelper;%0Aimport org.jacoco.coverage.util.CoverageUtils;%0A%0Aimport java.io.File
 ;%0Aim
@@ -1047,16 +1047,487 @@
 psiFile;
+%0A            if (TestFinderHelper.isTest(javaFile)) %7B%0A%0A                List%3CPsiElement%3E classesForTest = TestFinderHelper.findClassesForTest(javaFile);%0A                for (PsiElement element : classesForTest) %7B%0A                    if (element != null && element.getContainingFile() instanceof PsiJavaFile) %7B%0A                        javaFile = (PsiJavaFile) element.getContainingFile();%0A                        break;%0A                    %7D%0A                %7D%0A            %7D
 %0A%0A      
@@ -1821,19 +1821,20 @@
 lFile = 
-psi
+java
 File.get
@@ -1965,16 +1965,59 @@
 html%22);%0A
+                    if (url.exists())%7B%0A    
         
@@ -2078,16 +2078,38 @@
 ath());%0A
+                    %7D%0A
         
@@ -2138,21 +2138,20 @@
 %7D%0A        %7D%0A
-%0A
     %7D%0A%7D%0A

Use alternate file when requesting coverage for tests