Search in sources :

Example 1 with Suggestion

use of com.intellij.spellchecker.engine.Suggestion in project intellij-community by JetBrains.

the class MisspelledHeaderInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PsiElementVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof Header) {
                Header header = (Header) element;
                String headerName = header.getName();
                SortedSet<Suggestion> matches = new TreeSet<>();
                addMatches(headerName, CUSTOM_HEADERS, matches);
                addMatches(headerName, myRepository.getAllHeaderNames(), matches);
                Suggestion bestMatch = ContainerUtil.getFirstItem(matches);
                if (bestMatch != null && headerName.equals(bestMatch.getWord())) {
                    return;
                }
                List<LocalQuickFix> fixes = new ArrayList<>();
                for (Suggestion match : matches) {
                    fixes.add(new HeaderRenameQuickFix(header, match.getWord()));
                    if (fixes.size() == MAX_SUGGESTIONS)
                        break;
                }
                if (bestMatch == null || bestMatch.getMetrics() > TYPO_DISTANCE) {
                    fixes.add(new CustomHeaderQuickFix(header, CUSTOM_HEADERS));
                }
                holder.registerProblem(header.getNameElement(), ManifestBundle.message("inspection.header.message"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fixes.toArray(new LocalQuickFix[fixes.size()]));
            }
        }

        private void addMatches(String headerName, Collection<String> headers, SortedSet<Suggestion> matches) {
            for (String candidate : headers) {
                int distance = EditDistance.optimalAlignment(headerName, candidate, false);
                if (distance <= MAX_DISTANCE) {
                    matches.add(new Suggestion(candidate, distance));
                }
            }
        }
    };
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Suggestion(com.intellij.spellchecker.engine.Suggestion) Header(org.jetbrains.lang.manifest.psi.Header) AbstractCollection(com.intellij.util.xmlb.annotations.AbstractCollection) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 PsiElement (com.intellij.psi.PsiElement)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 Suggestion (com.intellij.spellchecker.engine.Suggestion)1 AbstractCollection (com.intellij.util.xmlb.annotations.AbstractCollection)1 NotNull (org.jetbrains.annotations.NotNull)1 Header (org.jetbrains.lang.manifest.psi.Header)1