@Override protected void apply(IDocument document, IStructuredModel model, Node node, int start, int end) { String message = mMarker.getAttribute(IMarker.MESSAGE, ""); String typo = TypoDetector.getTypo(message); if (typo == null) { return; } List<String> replacements = TypoDetector.getSuggestions(message); if (replacements.size() == 0) { return; } try { String current = document.get(start, end - start); if (current.equals(typo)) { document.replace(start, end - start, replacements.get(0)); } else { // The buffer has been edited; try to find the typo. FindReplaceDocumentAdapter finder = new FindReplaceDocumentAdapter(document); IRegion forward = finder.find(start, typo, true /*forward*/, true, true, false); IRegion backward = finder.find(start, typo, false /*forward*/, true, true, false); if (forward != null && backward != null) { // Pick the closest one int forwardDelta = forward.getOffset() - start; int backwardDelta = start - backward.getOffset(); if (forwardDelta < backwardDelta) { start = forward.getOffset(); } else { start = backward.getOffset(); } } else if (forward != null) { start = forward.getOffset(); } else if (backward != null) { start = backward.getOffset(); } else { return; } end = start + typo.length(); document.replace(start, end - start, replacements.get(0)); } } catch (BadLocationException e) { AdtPlugin.log(e, null); } }
@Override protected List<LintFix> getAllFixes() { String message = mMarker.getAttribute(IMarker.MESSAGE, ""); String typo = TypoDetector.getTypo(message); List<String> replacements = TypoDetector.getSuggestions(message); if (!replacements.isEmpty() && typo != null) { List<LintFix> allFixes = new ArrayList<LintFix>(replacements.size()); for (String replacement : replacements) { TypoFix fix = new TypoFix(mId, mMarker); fix.mTypo = typo; fix.mReplacement = replacement; allFixes.add(fix); } return allFixes; } return null; }