Search in sources :

Example 16 with LineData

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

the class SrcFileAnnotator method createRangeHighlighter.

private RangeHighlighter createRangeHighlighter(final long date, final MarkupModel markupModel, final boolean coverageByTestApplicable, final TreeMap<Integer, LineData> executableLines, @Nullable final String className, final int line, final int lineNumberInCurrent, @NotNull final CoverageSuitesBundle coverageSuite, Object[] lines, @NotNull MyEditorBean editorBean) {
    EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
    final TextAttributes attributes = scheme.getAttributes(CoverageLineMarkerRenderer.getAttributesKey(line, executableLines));
    TextAttributes textAttributes = null;
    if (attributes.getBackgroundColor() != null) {
        textAttributes = attributes;
    }
    Document document = editorBean.getDocument();
    Editor editor = editorBean.getEditor();
    final int startOffset = document.getLineStartOffset(lineNumberInCurrent);
    final int endOffset = document.getLineEndOffset(lineNumberInCurrent);
    final RangeHighlighter highlighter = markupModel.addRangeHighlighter(startOffset, endOffset, HighlighterLayer.SELECTION - 1, textAttributes, HighlighterTargetArea.LINES_IN_RANGE);
    final Function<Integer, Integer> newToOldConverter = newLine -> {
        if (editor == null)
            return -1;
        final TIntIntHashMap oldLineMapping = getNewToOldLineMapping(date, editorBean);
        return oldLineMapping != null ? oldLineMapping.get(newLine.intValue()) : newLine.intValue();
    };
    final Function<Integer, Integer> oldToNewConverter = newLine -> {
        if (editor == null)
            return -1;
        final TIntIntHashMap newLineMapping = getOldToNewLineMapping(date, editorBean);
        return newLineMapping != null ? newLineMapping.get(newLine.intValue()) : newLine.intValue();
    };
    final LineMarkerRendererWithErrorStripe markerRenderer = coverageSuite.getLineMarkerRenderer(line, className, executableLines, coverageByTestApplicable, coverageSuite, newToOldConverter, oldToNewConverter, CoverageDataManager.getInstance(myProject).isSubCoverageActive());
    highlighter.setLineMarkerRenderer(markerRenderer);
    final LineData lineData = className != null ? (LineData) lines[line + 1] : null;
    if (lineData != null && lineData.getStatus() == LineCoverage.NONE) {
        highlighter.setErrorStripeMarkColor(markerRenderer.getErrorStripeColor(editor));
        highlighter.setThinErrorStripeMark(true);
        highlighter.setGreedyToLeft(true);
        highlighter.setGreedyToRight(true);
    }
    return highlighter;
}
Also used : AllIcons(com.intellij.icons.AllIcons) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ProjectData(com.intellij.rt.coverage.data.ProjectData) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) TextEditor(com.intellij.openapi.fileEditor.TextEditor) FilePath(com.intellij.openapi.vcs.FilePath) LineData(com.intellij.rt.coverage.data.LineData) LoadTextUtil(com.intellij.openapi.fileEditor.impl.LoadTextUtil) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) SoftReference(com.intellij.reference.SoftReference) com.intellij.openapi.editor.markup(com.intellij.openapi.editor.markup) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) LineTokenizer(com.intellij.openapi.util.text.LineTokenizer) AbstractVcs(com.intellij.openapi.vcs.AbstractVcs) TextRange(com.intellij.openapi.util.TextRange) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Nullable(org.jetbrains.annotations.Nullable) EditorNotificationPanel(com.intellij.ui.EditorNotificationPanel) Function(com.intellij.util.Function) ApplicationManager(com.intellij.openapi.application.ApplicationManager) LocalHistory(com.intellij.history.LocalHistory) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) DocumentListener(com.intellij.openapi.editor.event.DocumentListener) java.util(java.util) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) LineCoverage(com.intellij.rt.coverage.data.LineCoverage) Computable(com.intellij.openapi.util.Computable) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) VcsHistorySession(com.intellij.openapi.vcs.history.VcsHistorySession) ClassData(com.intellij.rt.coverage.data.ClassData) VcsContextFactory(com.intellij.openapi.vcs.actions.VcsContextFactory) FilesTooBigForDiffException(com.intellij.util.diff.FilesTooBigForDiffException) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) VcsUtil(com.intellij.vcsUtil.VcsUtil) TIntIntHashMap(gnu.trove.TIntIntHashMap) Key(com.intellij.openapi.util.Key) FileRevisionTimestampComparator(com.intellij.history.FileRevisionTimestampComparator) VcsHistoryProvider(com.intellij.openapi.vcs.history.VcsHistoryProvider) VcsFileRevision(com.intellij.openapi.vcs.history.VcsFileRevision) Editor(com.intellij.openapi.editor.Editor) Disposable(com.intellij.openapi.Disposable) File(java.io.File) Diff(com.intellij.util.diff.Diff) Alarm(com.intellij.util.Alarm) LineData(com.intellij.rt.coverage.data.LineData) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) Document(com.intellij.openapi.editor.Document) TextEditor(com.intellij.openapi.fileEditor.TextEditor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Editor(com.intellij.openapi.editor.Editor) TIntIntHashMap(gnu.trove.TIntIntHashMap)

Example 17 with LineData

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

the class SrcFileAnnotator method showCoverageInformation.

public void showCoverageInformation(final CoverageSuitesBundle suite) {
    // Store the values of myFile and myEditor in local variables to avoid an NPE after dispose() has been called in the EDT.
    final PsiFile psiFile = myFile;
    final Editor editor = myEditor;
    final Document document = myDocument;
    if (editor == null || psiFile == null || document == null)
        return;
    final VirtualFile file = getVirtualFile(psiFile);
    final MyEditorBean editorBean = new MyEditorBean(editor, file, document);
    final MarkupModel markupModel = DocumentMarkupModel.forDocument(document, myProject, true);
    final List<RangeHighlighter> highlighters = new ArrayList<>();
    final ProjectData data = suite.getCoverageData();
    if (data == null) {
        coverageDataNotFound(suite);
        return;
    }
    final CoverageEngine engine = suite.getCoverageEngine();
    final Set<String> qualifiedNames = engine.getQualifiedNames(psiFile);
    // let's find old content in local history and build mapping from old lines to new one
    // local history doesn't index libraries, so let's distinguish libraries content with other one
    final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    final long fileTimeStamp = file.getTimeStamp();
    final long coverageTimeStamp = suite.getLastCoverageTimeStamp();
    final TIntIntHashMap oldToNewLineMapping;
    //do not show coverage info over cls
    if (engine.isInLibraryClasses(myProject, file)) {
        return;
    }
    // if in libraries content
    if (projectFileIndex.isInLibrarySource(file)) {
        // compare file and coverage timestamps
        if (fileTimeStamp > coverageTimeStamp) {
            showEditorWarningMessage(CodeInsightBundle.message("coverage.data.outdated"));
            return;
        }
        oldToNewLineMapping = null;
    } else {
        // check local history
        oldToNewLineMapping = getOldToNewLineMapping(coverageTimeStamp, editorBean);
        if (oldToNewLineMapping == null) {
            // if history for file isn't available let's check timestamps
            if (fileTimeStamp > coverageTimeStamp && classesArePresentInCoverageData(data, qualifiedNames)) {
                showEditorWarningMessage(CodeInsightBundle.message("coverage.data.outdated"));
                return;
            }
        }
    }
    if (editor.getUserData(COVERAGE_HIGHLIGHTERS) != null) {
        //highlighters already collected - no need to do it twice
        return;
    }
    final Module module = ApplicationManager.getApplication().runReadAction(new Computable<Module>() {

        @Nullable
        @Override
        public Module compute() {
            return ModuleUtilCore.findModuleForPsiElement(psiFile);
        }
    });
    if (module != null) {
        if (engine.recompileProjectAndRerunAction(module, suite, () -> CoverageDataManager.getInstance(myProject).chooseSuitesBundle(suite))) {
            return;
        }
    }
    // now if oldToNewLineMapping is null we should use f(x)=id(x) mapping
    // E.g. all *.class files for java source file with several classes
    final Set<File> outputFiles = engine.getCorrespondingOutputFiles(psiFile, module, suite);
    final boolean subCoverageActive = CoverageDataManager.getInstance(myProject).isSubCoverageActive();
    final boolean coverageByTestApplicable = suite.isCoverageByTestApplicable() && !(subCoverageActive && suite.isCoverageByTestEnabled());
    final TreeMap<Integer, LineData> executableLines = new TreeMap<>();
    final TreeMap<Integer, Object[]> classLines = new TreeMap<>();
    final TreeMap<Integer, String> classNames = new TreeMap<>();
    class HighlightersCollector {

        private void collect(File outputFile, final String qualifiedName) {
            final ClassData fileData = data.getClassData(qualifiedName);
            if (fileData != null) {
                final Object[] lines = fileData.getLines();
                if (lines != null) {
                    final Object[] postProcessedLines = suite.getCoverageEngine().postProcessExecutableLines(lines, editor);
                    for (Object lineData : postProcessedLines) {
                        if (lineData instanceof LineData) {
                            final int line = ((LineData) lineData).getLineNumber() - 1;
                            final int lineNumberInCurrent;
                            if (oldToNewLineMapping != null) {
                                // use mapping based on local history
                                if (!oldToNewLineMapping.contains(line)) {
                                    continue;
                                }
                                lineNumberInCurrent = oldToNewLineMapping.get(line);
                            } else {
                                // use id mapping
                                lineNumberInCurrent = line;
                            }
                            executableLines.put(line, (LineData) lineData);
                            classLines.put(line, postProcessedLines);
                            classNames.put(line, qualifiedName);
                            ApplicationManager.getApplication().invokeLater(() -> {
                                if (lineNumberInCurrent >= document.getLineCount())
                                    return;
                                if (editorBean.isDisposed())
                                    return;
                                final RangeHighlighter highlighter = createRangeHighlighter(suite.getLastCoverageTimeStamp(), markupModel, coverageByTestApplicable, executableLines, qualifiedName, line, lineNumberInCurrent, suite, postProcessedLines, editorBean);
                                highlighters.add(highlighter);
                            });
                        }
                    }
                }
            } else if (outputFile != null && !subCoverageActive && engine.includeUntouchedFileInCoverage(qualifiedName, outputFile, psiFile, suite)) {
                collectNonCoveredFileInfo(outputFile, highlighters, markupModel, executableLines, coverageByTestApplicable, editorBean);
            }
        }
    }
    final HighlightersCollector collector = new HighlightersCollector();
    if (!outputFiles.isEmpty()) {
        for (File outputFile : outputFiles) {
            final String qualifiedName = engine.getQualifiedName(outputFile, psiFile);
            if (qualifiedName != null) {
                collector.collect(outputFile, qualifiedName);
            }
        }
    } else {
        //check non-compilable classes which present in ProjectData
        for (String qName : qualifiedNames) {
            collector.collect(null, qName);
        }
    }
    ApplicationManager.getApplication().invokeLater(() -> {
        if (!editorBean.isDisposed() && highlighters.size() > 0) {
            editor.putUserData(COVERAGE_HIGHLIGHTERS, highlighters);
        }
    });
    final DocumentListener documentListener = new DocumentAdapter() {

        @Override
        public void documentChanged(final DocumentEvent e) {
            myNewToOldLines = null;
            myOldToNewLines = null;
            List<RangeHighlighter> rangeHighlighters = editor.getUserData(COVERAGE_HIGHLIGHTERS);
            if (rangeHighlighters == null)
                rangeHighlighters = new ArrayList<>();
            int offset = e.getOffset();
            final int lineNumber = document.getLineNumber(offset);
            final int lastLineNumber = document.getLineNumber(offset + e.getNewLength());
            final TextRange changeRange = new TextRange(document.getLineStartOffset(lineNumber), document.getLineEndOffset(lastLineNumber));
            for (Iterator<RangeHighlighter> it = rangeHighlighters.iterator(); it.hasNext(); ) {
                final RangeHighlighter highlighter = it.next();
                if (!highlighter.isValid() || TextRange.create(highlighter).intersects(changeRange)) {
                    highlighter.dispose();
                    it.remove();
                }
            }
            final List<RangeHighlighter> highlighters = rangeHighlighters;
            myUpdateAlarm.cancelAllRequests();
            if (!myUpdateAlarm.isDisposed()) {
                myUpdateAlarm.addRequest(() -> {
                    final TIntIntHashMap newToOldLineMapping = getNewToOldLineMapping(suite.getLastCoverageTimeStamp(), editorBean);
                    if (newToOldLineMapping != null) {
                        ApplicationManager.getApplication().invokeLater(() -> {
                            if (editorBean.isDisposed())
                                return;
                            for (int line = lineNumber; line <= lastLineNumber; line++) {
                                final int oldLineNumber = newToOldLineMapping.get(line);
                                final LineData lineData = executableLines.get(oldLineNumber);
                                if (lineData != null) {
                                    RangeHighlighter rangeHighlighter = createRangeHighlighter(suite.getLastCoverageTimeStamp(), markupModel, coverageByTestApplicable, executableLines, classNames.get(oldLineNumber), oldLineNumber, line, suite, classLines.get(oldLineNumber), editorBean);
                                    highlighters.add(rangeHighlighter);
                                }
                            }
                            editor.putUserData(COVERAGE_HIGHLIGHTERS, highlighters.size() > 0 ? highlighters : null);
                        });
                    }
                }, 100);
            }
        }
    };
    document.addDocumentListener(documentListener);
    editor.putUserData(COVERAGE_DOCUMENT_LISTENER, documentListener);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentListener(com.intellij.openapi.editor.event.DocumentListener) Document(com.intellij.openapi.editor.Document) PsiFile(com.intellij.psi.PsiFile) TIntIntHashMap(gnu.trove.TIntIntHashMap) ProjectData(com.intellij.rt.coverage.data.ProjectData) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) TextRange(com.intellij.openapi.util.TextRange) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) TextEditor(com.intellij.openapi.fileEditor.TextEditor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Editor(com.intellij.openapi.editor.Editor) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with LineData

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

the class KarmaCoverageRunner method readProjectData.

@NotNull
private static ProjectData readProjectData(@NotNull File dataFile, @NotNull File basePath) throws IOException {
    ProjectData projectData = new ProjectData();
    LcovCoverageReport report = CoverageSerializationUtils.readLCOV(basePath, dataFile);
    for (Map.Entry<String, List<LcovCoverageReport.LineHits>> entry : report.getInfo().entrySet()) {
        String filePath = entry.getKey();
        ClassData classData = projectData.getOrCreateClassData(filePath);
        int max = 0;
        List<LcovCoverageReport.LineHits> lineHitsList = entry.getValue();
        if (lineHitsList.size() > 0) {
            LcovCoverageReport.LineHits lastLineHits = lineHitsList.get(lineHitsList.size() - 1);
            max = lastLineHits.getLineNumber();
        }
        LineData[] lines = new LineData[max + 1];
        for (LcovCoverageReport.LineHits lineHits : lineHitsList) {
            LineData lineData = new LineData(lineHits.getLineNumber(), null);
            lineData.setHits(lineHits.getHits());
            lines[lineHits.getLineNumber()] = lineData;
        }
        classData.setLines(lines);
    }
    return projectData;
}
Also used : LcovCoverageReport(com.intellij.javascript.testFramework.coverage.LcovCoverageReport) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) List(java.util.List) Map(java.util.Map) ProjectData(com.intellij.rt.coverage.data.ProjectData) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with LineData

use of com.intellij.rt.coverage.data.LineData in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoCoverageRunner method parseCoverage.

@Nullable
public static GoCoverageProjectData parseCoverage(@NotNull BufferedReader dataReader, @NotNull Project project, @Nullable Module module) throws IOException {
    GoCoverageProjectData result = new GoCoverageProjectData();
    String line;
    while ((line = dataReader.readLine()) != null) {
        if (line.isEmpty())
            continue;
        List<String> fileNameTail = StringUtil.split(line, ":");
        VirtualFile file = GoPackageUtil.findByImportPath(fileNameTail.get(0), project, module);
        if (file == null)
            continue;
        String filePath = file.getPath();
        List<String> tailParts = StringUtil.split(fileNameTail.get(1), " ");
        if (tailParts.size() != 3)
            continue;
        int statements = Integer.parseInt(tailParts.get(1));
        int hit = Integer.parseInt(tailParts.get(2));
        String offsets = tailParts.get(0);
        int firstDot = offsets.indexOf('.');
        int comma = offsets.indexOf(',', firstDot);
        int secondDot = offsets.indexOf('.', comma);
        if (firstDot == -1 || comma == -1 || secondDot == -1)
            continue;
        int lineStart = Integer.parseInt(offsets.substring(0, firstDot));
        int columnStart = Integer.parseInt(offsets.substring(firstDot + 1, comma));
        int lineEnd = Integer.parseInt(offsets.substring(comma + 1, secondDot));
        int columnEnd = Integer.parseInt(offsets.substring(secondDot + 1));
        result.addData(filePath, lineStart, columnStart, lineEnd, columnEnd, statements, hit);
    }
    result.processFiles(fileData -> {
        ClassData classData = result.getOrCreateClassData(fileData.myFilePath);
        int max = -1;
        TIntObjectHashMap<LineData> linesMap = new TIntObjectHashMap<>();
        for (GoCoverageProjectData.RangeData rangeData : fileData.myRangesData.values()) {
            for (int i = rangeData.startLine; i <= rangeData.endLine; i++) {
                LineData existingData = linesMap.get(i);
                if (existingData != null) {
                    existingData.setHits(existingData.getHits() + rangeData.hits);
                    // emulate partial
                    existingData.setFalseHits(0, 0);
                    existingData.setTrueHits(0, 0);
                } else {
                    LineData newData = new LineData(i, null);
                    newData.setHits(newData.getHits() + rangeData.hits);
                    linesMap.put(i, newData);
                }
            }
            max = Math.max(max, rangeData.endLine);
        }
        LineData[] linesArray = new LineData[max + 1];
        linesMap.forEachValue(data -> {
            data.fillArrays();
            linesArray[data.getLineNumber()] = data;
            return true;
        });
        classData.setLines(linesArray);
        return true;
    });
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with LineData

use of com.intellij.rt.coverage.data.LineData in project intellij by bazelbuild.

the class BlazeCoverageRunnerTest method lineData.

private static LineData lineData(int line, int hits) {
    LineData data = new LineData(line, null);
    data.setHits(hits);
    return data;
}
Also used : LineData(com.intellij.rt.coverage.data.LineData)

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