Search in sources :

Example 1 with LineData

use of com.intellij.rt.coverage.data.LineData in project intellij-community by JetBrains.

the class JaCoCoCoverageRunner method loadExecutionData.

private static void loadExecutionData(@NotNull final File sessionDataFile, ProjectData data, @NotNull Project project) throws IOException {
    final ExecutionDataStore executionDataStore = new ExecutionDataStore();
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(sessionDataFile);
        final ExecutionDataReader executionDataReader = new ExecutionDataReader(fis);
        executionDataReader.setExecutionDataVisitor(executionDataStore);
        executionDataReader.setSessionInfoVisitor(new ISessionInfoVisitor() {

            public void visitSessionInfo(SessionInfo info) {
                System.out.println(info.toString());
            }
        });
        while (executionDataReader.read()) {
        }
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    final CoverageBuilder coverageBuilder = new CoverageBuilder();
    final Analyzer analyzer = new Analyzer(executionDataStore, coverageBuilder);
    final Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
        final CompilerModuleExtension compilerModuleExtension = CompilerModuleExtension.getInstance(module);
        if (compilerModuleExtension != null) {
            final VirtualFile[] roots = compilerModuleExtension.getOutputRoots(true);
            for (VirtualFile root : roots) {
                analyzer.analyzeAll(VfsUtil.virtualToIoFile(root));
            }
        }
    }
    for (IClassCoverage classCoverage : coverageBuilder.getClasses()) {
        String className = classCoverage.getName();
        className = className.replace('\\', '.').replace('/', '.');
        final ClassData classData = data.getOrCreateClassData(className);
        final Collection<IMethodCoverage> methods = classCoverage.getMethods();
        LineData[] lines = new LineData[classCoverage.getLastLine() + 1];
        for (IMethodCoverage method : methods) {
            final String desc = method.getName() + method.getDesc();
            // Line numbers are 1-based here.
            final int firstLine = method.getFirstLine();
            final int lastLine = method.getLastLine();
            for (int i = firstLine; i <= lastLine; i++) {
                final ILine methodLine = method.getLine(i);
                final int methodLineStatus = methodLine.getStatus();
                if (methodLineStatus == ICounter.EMPTY)
                    continue;
                final LineData lineData = new LineData(i, desc) {

                    @Override
                    public int getStatus() {
                        switch(methodLineStatus) {
                            case ICounter.FULLY_COVERED:
                                return LineCoverage.FULL;
                            case ICounter.PARTLY_COVERED:
                                return LineCoverage.PARTIAL;
                            default:
                                return LineCoverage.NONE;
                        }
                    }
                };
                lineData.setHits(methodLineStatus == ICounter.FULLY_COVERED || methodLineStatus == ICounter.PARTLY_COVERED ? 1 : 0);
                lines[i] = lineData;
            }
        }
        classData.setLines(lines);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ISessionInfoVisitor(org.jacoco.core.data.ISessionInfoVisitor) SessionInfo(org.jacoco.core.data.SessionInfo) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension) FileInputStream(java.io.FileInputStream) ExecutionDataReader(org.jacoco.core.data.ExecutionDataReader) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) ExecutionDataStore(org.jacoco.core.data.ExecutionDataStore) Module(com.intellij.openapi.module.Module)

Example 2 with LineData

use of com.intellij.rt.coverage.data.LineData in project intellij-community by JetBrains.

the class CoverageDataManagerImpl method selectSubCoverage.

public void selectSubCoverage(@NotNull final CoverageSuitesBundle suite, final List<String> testNames) {
    suite.restoreCoverageData();
    final ProjectData data = suite.getCoverageData();
    if (data == null)
        return;
    mySubCoverageIsActive = true;
    final Map<String, Set<Integer>> executionTrace = new HashMap<>();
    for (CoverageSuite coverageSuite : suite.getSuites()) {
        final String fileName = coverageSuite.getCoverageDataFileName();
        final File tracesDir = getTracesDirectory(fileName);
        for (String testName : testNames) {
            final File file = new File(tracesDir, FileUtil.sanitizeFileName(testName) + ".tr");
            if (file.exists()) {
                DataInputStream in = null;
                try {
                    in = new DataInputStream(new FileInputStream(file));
                    int traceSize = in.readInt();
                    for (int i = 0; i < traceSize; i++) {
                        final String className = in.readUTF();
                        final int linesSize = in.readInt();
                        Set<Integer> lines = executionTrace.get(className);
                        if (lines == null) {
                            lines = new HashSet<>();
                            executionTrace.put(className, lines);
                        }
                        for (int l = 0; l < linesSize; l++) {
                            lines.add(in.readInt());
                        }
                    }
                } catch (Exception e) {
                    LOG.error(e);
                } finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        LOG.error(e);
                    }
                }
            }
        }
    }
    final ProjectData projectData = new ProjectData();
    for (String className : executionTrace.keySet()) {
        ClassData loadedClassData = projectData.getClassData(className);
        if (loadedClassData == null) {
            loadedClassData = projectData.getOrCreateClassData(className);
        }
        final Set<Integer> lineNumbers = executionTrace.get(className);
        final ClassData oldData = data.getClassData(className);
        LOG.assertTrue(oldData != null, "missed className: \"" + className + "\"");
        final Object[] oldLines = oldData.getLines();
        LOG.assertTrue(oldLines != null);
        int maxNumber = oldLines.length;
        for (Integer lineNumber : lineNumbers) {
            if (lineNumber >= maxNumber) {
                maxNumber = lineNumber + 1;
            }
        }
        final LineData[] lines = new LineData[maxNumber];
        for (Integer line : lineNumbers) {
            final int lineIdx = line.intValue() - 1;
            String methodSig = null;
            if (lineIdx < oldData.getLines().length) {
                final LineData oldLineData = oldData.getLineData(lineIdx);
                if (oldLineData != null) {
                    methodSig = oldLineData.getMethodSignature();
                }
            }
            final LineData lineData = new LineData(lineIdx, methodSig);
            if (methodSig != null) {
                loadedClassData.registerMethodSignature(lineData);
            }
            lineData.setStatus(LineCoverage.FULL);
            lines[lineIdx] = lineData;
        }
        loadedClassData.setLines(lines);
    }
    suite.setCoverageData(projectData);
    renewCoverageData(suite);
}
Also used : HashSet(com.intellij.util.containers.HashSet) Set(java.util.Set) HashMap(com.intellij.util.containers.HashMap) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) WriteExternalException(com.intellij.openapi.util.WriteExternalException) InvalidDataException(com.intellij.openapi.util.InvalidDataException) IOException(java.io.IOException) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) ProjectData(com.intellij.rt.coverage.data.ProjectData)

Example 3 with LineData

use of com.intellij.rt.coverage.data.LineData in project intellij-community by JetBrains.

the class CoverageLineMarkerRenderer method paint.

public void paint(Editor editor, Graphics g, Rectangle r) {
    final TextAttributes color = editor.getColorsScheme().getAttributes(myKey);
    Color bgColor = color.getBackgroundColor();
    if (bgColor == null) {
        bgColor = color.getForegroundColor();
    }
    if (editor.getSettings().isLineNumbersShown() || ((EditorGutterComponentEx) editor.getGutter()).isAnnotationsShown()) {
        if (bgColor != null) {
            bgColor = ColorUtil.toAlpha(bgColor, 150);
        }
    }
    if (bgColor != null) {
        g.setColor(bgColor);
    }
    g.fillRect(r.x, r.y, r.width, r.height);
    final LineData lineData = getLineData(editor.xyToLogicalPosition(new Point(0, r.y)).line);
    if (lineData != null && lineData.isCoveredByOneTest()) {
        g.drawImage(ImageLoader.loadFromResource("/gutter/unique.png"), r.x, r.y, 8, 8, editor.getComponent());
    }
}
Also used : LineData(com.intellij.rt.coverage.data.LineData) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 4 with LineData

use of com.intellij.rt.coverage.data.LineData in project intellij-community by JetBrains.

the class CoverageLineMarkerRenderer method showHint.

private void showHint(final Editor editor, final Point point, final int lineNumber) {
    final JPanel panel = new JPanel(new BorderLayout());
    panel.add(createActionsToolbar(editor, lineNumber), BorderLayout.NORTH);
    final LineData lineData = getLineData(lineNumber);
    final EditorImpl uEditor;
    if (lineData != null && lineData.getStatus() != LineCoverage.NONE && !mySubCoverageActive) {
        final EditorFactory factory = EditorFactory.getInstance();
        final Document doc = factory.createDocument(getReport(editor, lineNumber));
        doc.setReadOnly(true);
        uEditor = (EditorImpl) factory.createEditor(doc, editor.getProject());
        panel.add(EditorFragmentComponent.createEditorFragmentComponent(uEditor, 0, doc.getLineCount(), false, false), BorderLayout.CENTER);
    } else {
        uEditor = null;
    }
    final LightweightHint hint = new LightweightHint(panel) {

        @Override
        public void hide() {
            if (uEditor != null)
                EditorFactory.getInstance().releaseEditor(uEditor);
            super.hide();
        }
    };
    HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, point, HintManagerImpl.HIDE_BY_ANY_KEY | HintManagerImpl.HIDE_BY_TEXT_CHANGE | HintManagerImpl.HIDE_BY_OTHER_HINT | HintManagerImpl.HIDE_BY_SCROLLING, -1, false, new HintHint(editor, point));
}
Also used : LineData(com.intellij.rt.coverage.data.LineData) EditorFactory(com.intellij.openapi.editor.EditorFactory) HintHint(com.intellij.ui.HintHint) EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) LightweightHint(com.intellij.ui.LightweightHint) Document(com.intellij.openapi.editor.Document)

Example 5 with LineData

use of com.intellij.rt.coverage.data.LineData in project intellij-community by JetBrains.

the class CoverageLineMarkerRenderer method getReport.

private String getReport(final Editor editor, final int lineNumber) {
    final LineData lineData = getLineData(lineNumber);
    final Document document = editor.getDocument();
    final Project project = editor.getProject();
    assert project != null;
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
    assert psiFile != null;
    final int lineStartOffset = document.getLineStartOffset(lineNumber);
    final int lineEndOffset = document.getLineEndOffset(lineNumber);
    return myCoverageSuite.getCoverageEngine().generateBriefReport(editor, psiFile, lineNumber, lineStartOffset, lineEndOffset, lineData);
}
Also used : Project(com.intellij.openapi.project.Project) LineData(com.intellij.rt.coverage.data.LineData) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) LightweightHint(com.intellij.ui.LightweightHint) HintHint(com.intellij.ui.HintHint)

Aggregations

LineData (com.intellij.rt.coverage.data.LineData)21 ClassData (com.intellij.rt.coverage.data.ClassData)11 ProjectData (com.intellij.rt.coverage.data.ProjectData)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Nullable (org.jetbrains.annotations.Nullable)5 Document (com.intellij.openapi.editor.Document)4 PsiFile (com.intellij.psi.PsiFile)4 Map (java.util.Map)4 NotNull (org.jetbrains.annotations.NotNull)4 Module (com.intellij.openapi.module.Module)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 CoverageReport (com.google.jstestdriver.idea.rt.coverage.CoverageReport)2 Editor (com.intellij.openapi.editor.Editor)2 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)2 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)2 DocumentListener (com.intellij.openapi.editor.event.DocumentListener)2 DocumentMarkupModel (com.intellij.openapi.editor.impl.DocumentMarkupModel)2 FileEditor (com.intellij.openapi.fileEditor.FileEditor)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2