Java Code Examples for com.intellij.codeInspection.ProblemsHolder#getResultsArray()

The following examples show how to use com.intellij.codeInspection.ProblemsHolder#getResultsArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgnoreRelativeEntryInspection.java    From idea-gitignore with MIT License 6 votes vote down vote up
/**
 * Checks if entries are relative.
 *
 * @param file       current working file yo check
 * @param manager    {@link InspectionManager} to ask for {@link ProblemDescriptor}'s from
 * @param isOnTheFly true if called during on the fly editor highlighting. Called from Inspect Code action
 *                   otherwise
 * @return <code>null</code> if no problems found or not applicable at file level
 */
@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file,
                                     @NotNull InspectionManager manager, boolean isOnTheFly) {
    if (!(file instanceof IgnoreFile)) {
        return null;
    }

    final ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);

    file.acceptChildren(new IgnoreVisitor() {
        @Override
        public void visitEntry(@NotNull IgnoreEntry entry) {
            String path = entry.getText().replaceAll("\\\\(.)", "$1");
            if (path.contains("./")) {
                problemsHolder.registerProblem(entry, IgnoreBundle.message("codeInspection.relativeEntry.message"),
                        new IgnoreRelativeEntryFix(entry));
            }
            super.visitEntry(entry);
        }
    });

    return problemsHolder.getResultsArray();
}
 
Example 2
Source File: IgnoreCoverEntryInspection.java    From idea-gitignore with MIT License 4 votes vote down vote up
/**
 * Reports problems at file level. Checks if entries are covered by other entries.
 *
 * @param file       current working file to check
 * @param manager    {@link InspectionManager} to ask for {@link ProblemDescriptor}'s from
 * @param isOnTheFly true if called during on the fly editor highlighting. Called from Inspect Code action otherwise
 * @return <code>null</code> if no problems found or not applicable at file level
 */
@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager,
                                     boolean isOnTheFly) {
    final VirtualFile virtualFile = file.getVirtualFile();
    if (!(file instanceof IgnoreFile) || !Utils.isInProject(virtualFile, file.getProject())) {
        return null;
    }

    final VirtualFile contextDirectory = virtualFile.getParent();
    if (contextDirectory == null) {
        return null;
    }

    final Set<String> ignored = new HashSet<>();
    final Set<String> unignored = new HashSet<>();

    final ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly);
    final List<Pair<IgnoreEntry, IgnoreEntry>> result = new ArrayList<>();
    final Map<IgnoreEntry, Set<String>> map = new HashMap<>();

    final ArrayList<IgnoreEntry> entries = new ArrayList<>(Arrays.asList(
            ((IgnoreFile) file).findChildrenByClass(IgnoreEntry.class)
    ));
    final MatcherUtil matcher = IgnoreManager.getInstance(file.getProject()).getMatcher();
    final Map<IgnoreEntry, Set<String>> matchedMap = getPathsSet(contextDirectory, entries, matcher);

    for (IgnoreEntry entry : entries) {
        ProgressManager.checkCanceled();
        Set<String> matched = matchedMap.get(entry);
        Collection<String> intersection;
        boolean modified;

        if (!entry.isNegated()) {
            ignored.addAll(matched);
            intersection = ContainerUtil.intersection(unignored, matched);
            modified = unignored.removeAll(intersection);
        } else {
            unignored.addAll(matched);
            intersection = ContainerUtil.intersection(ignored, matched);
            modified = ignored.removeAll(intersection);
        }

        if (modified) {
            continue;
        }

        for (IgnoreEntry recent : map.keySet()) {
            ProgressManager.checkCanceled();
            Set<String> recentValues = map.get(recent);
            if (recentValues.isEmpty() || matched.isEmpty()) {
                continue;
            }

            if (entry.isNegated() == recent.isNegated()) {
                if (recentValues.containsAll(matched)) {
                    result.add(Pair.create(recent, entry));
                } else if (matched.containsAll(recentValues)) {
                    result.add(Pair.create(entry, recent));
                }
            } else {
                if (intersection.containsAll(recentValues)) {
                    result.add(Pair.create(entry, recent));
                }
            }
        }

        map.put(entry, matched);
    }

    for (Pair<IgnoreEntry, IgnoreEntry> pair : result) {
        problemsHolder.registerProblem(pair.second, message(pair.first, virtualFile, isOnTheFly),
                new IgnoreRemoveEntryFix(pair.second));
    }

    return problemsHolder.getResultsArray();
}