Search in sources :

Example 21 with ValidationProblem

use of org.eclipse.mylyn.wikitext.validation.ValidationProblem in project mylyn.docs by eclipse.

the class ListWhitespaceValidationRuleTest method testNegativeMatchNumeric.

public void testNegativeMatchNumeric() {
    String markup = "some text\n\n   1. a valid list item\n      1. another valid list item\n\nmore text";
    ValidationProblem problem = rule.findProblem(markup, 0, markup.length());
    assertNull(problem);
}
Also used : ValidationProblem(org.eclipse.mylyn.wikitext.validation.ValidationProblem)

Example 22 with ValidationProblem

use of org.eclipse.mylyn.wikitext.validation.ValidationProblem in project mylyn.docs by eclipse.

the class ListWhitespaceValidationRuleTest method testPositiveMatchSecondItemNotMultipleOf3.

public void testPositiveMatchSecondItemNotMultipleOf3() {
    String markup = "some text\n\n   * a valid list item\n     * not a list item\n\nmore text";
    ValidationProblem problem = rule.findProblem(markup, 0, markup.length());
    assertNotNull(problem);
    assertEquals(34, problem.getOffset());
}
Also used : ValidationProblem(org.eclipse.mylyn.wikitext.validation.ValidationProblem)

Example 23 with ValidationProblem

use of org.eclipse.mylyn.wikitext.validation.ValidationProblem in project mylyn.docs by eclipse.

the class AnnotationMarkupValidator method createProblems.

@SuppressWarnings("unchecked")
@Override
protected void createProblems(IProgressMonitor monitor, IDocument document, IRegion region, List<ValidationProblem> problems) throws CoreException {
    Object lockObject;
    if (annotationModel instanceof ISynchronizable) {
        lockObject = ((ISynchronizable) annotationModel).getLockObject();
    } else {
        lockObject = annotationModel;
    }
    synchronized (lockObject) {
        List<Annotation> toRemove = null;
        Iterator<Annotation> annotationIterator = annotationModel.getAnnotationIterator();
        while (annotationIterator.hasNext()) {
            Annotation annotation = annotationIterator.next();
            if (ValidationProblemAnnotation.isValidationAnnotation(annotation)) {
                Position position = annotationModel.getPosition(annotation);
                int offset = position.getOffset();
                if (overlaps(region, offset, position.getLength()) || offset >= document.getLength()) {
                    if (toRemove == null) {
                        toRemove = new ArrayList<>();
                    }
                    toRemove.add(annotation);
                }
            }
        }
        Map<Annotation, Position> annotationsToAdd = new HashMap<>();
        for (ValidationProblem problem : problems) {
            annotationsToAdd.put(new ValidationProblemAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
        }
        if (toRemove != null && annotationModel instanceof IAnnotationModelExtension) {
            Annotation[] annotationsToRemove = toRemove.toArray(new Annotation[toRemove.size()]);
            ((IAnnotationModelExtension) annotationModel).replaceAnnotations(annotationsToRemove, annotationsToAdd);
        } else {
            if (toRemove != null) {
                for (Annotation annotation : toRemove) {
                    annotationModel.removeAnnotation(annotation);
                }
            }
            for (Map.Entry<Annotation, Position> entry : annotationsToAdd.entrySet()) {
                annotationModel.addAnnotation(entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) ISynchronizable(org.eclipse.jface.text.ISynchronizable) ValidationProblem(org.eclipse.mylyn.wikitext.validation.ValidationProblem) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) Annotation(org.eclipse.jface.text.source.Annotation) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with ValidationProblem

use of org.eclipse.mylyn.wikitext.validation.ValidationProblem in project mylyn.docs by eclipse.

the class ResourceMarkerMarkupValidator method createProblems.

@Override
protected void createProblems(IProgressMonitor monitor, IDocument document, IRegion region, List<ValidationProblem> problems) throws CoreException {
    final int findMarkersWorkSize = 100;
    final int zeroProblemsStep = 10;
    monitor.beginTask(Messages.ResourceMarkerMarkupValidator_creatingMarkers, problems.size() + findMarkersWorkSize + zeroProblemsStep);
    // find and remove any existing validation errors in the given region.
    List<IMarker> markersInRegion = new ArrayList<>(5);
    // we also track markers by offset, however we don't track multiple markers at the same offset
    Map<Integer, IMarker> markerByOffset = new HashMap<>();
    {
        IMarker[] findMarkers = // $NON-NLS-1$
        resource.findMarkers(// $NON-NLS-1$
        "org.eclipse.mylyn.wikitext.ui.validation.problem", // $NON-NLS-1$
        true, IResource.DEPTH_ZERO);
        for (IMarker marker : findMarkers) {
            int offset = marker.getAttribute(IMarker.CHAR_START, 0);
            int end = marker.getAttribute(IMarker.CHAR_END, offset);
            if (overlaps(region, offset, end - offset) || offset >= document.getLength()) {
                markersInRegion.add(marker);
                markerByOffset.put(offset, marker);
            }
        }
        monitor.worked(findMarkersWorkSize);
    }
    if (problems.isEmpty()) {
        for (IMarker marker : markersInRegion) {
            marker.delete();
        }
        monitor.worked(zeroProblemsStep);
        monitor.done();
        return;
    }
    monitor.worked(zeroProblemsStep);
    // bug 261747: compute a delta so that we can avoid flicker
    if (!markersInRegion.isEmpty()) {
        // find all problems for which there is a marker that matches, and remove the problem from our
        // collection of problems to create
        Iterator<ValidationProblem> problemIt = problems.iterator();
        while (problemIt.hasNext()) {
            ValidationProblem problem = problemIt.next();
            IMarker marker = markerByOffset.get(problem.getOffset());
            if (marker != null) {
                int charEnd = marker.getAttribute(IMarker.CHAR_END, -1);
                if ((problem.getOffset() + problem.getLength()) == charEnd) {
                    if (toMarkerSeverity(problem.getSeverity()) == marker.getAttribute(IMarker.SEVERITY, -1)) {
                        if (problem.getMessage().equals(marker.getAttribute(IMarker.MESSAGE, ""))) {
                            // $NON-NLS-1$
                            problemIt.remove();
                            markerByOffset.remove(problem.getOffset());
                            markersInRegion.remove(marker);
                            monitor.worked(1);
                        }
                    }
                }
            }
        }
        // remove all markers that had no matching problem
        for (IMarker marker : markersInRegion) {
            marker.delete();
        }
    }
    for (ValidationProblem problem : problems) {
        IMarker marker = resource.createMarker(problem.getMarkerId());
        marker.setAttribute(IMarker.TRANSIENT, true);
        marker.setAttribute(IMarker.SEVERITY, toMarkerSeverity(problem.getSeverity()));
        marker.setAttribute(IMarker.MESSAGE, problem.getMessage());
        marker.setAttribute(IMarker.CHAR_START, problem.getOffset());
        marker.setAttribute(IMarker.CHAR_END, problem.getOffset() + problem.getLength());
        try {
            int line = document.getLineOfOffset(problem.getOffset());
            marker.setAttribute(IMarker.LINE_NUMBER, line + 1);
        } catch (BadLocationException e) {
        // ignore
        }
        monitor.worked(1);
    }
    monitor.done();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ValidationProblem(org.eclipse.mylyn.wikitext.validation.ValidationProblem) IMarker(org.eclipse.core.resources.IMarker) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 25 with ValidationProblem

use of org.eclipse.mylyn.wikitext.validation.ValidationProblem in project mylyn.docs by eclipse.

the class WikiToDocTask method performValidation.

protected void performValidation(MarkupLanguage markupLanguage, Path path, String markupContent) {
    // $NON-NLS-1$
    getProject().log(MessageFormat.format("Validating {0}", path.name), Project.MSG_VERBOSE);
    StandaloneMarkupValidator markupValidator = StandaloneMarkupValidator.getValidator(markupLanguage.getName());
    List<ValidationProblem> problems = markupValidator.validate(markupContent);
    int errorCount = 0;
    int warningCount = 0;
    for (ValidationProblem problem : problems) {
        int messageLevel = Project.MSG_ERR;
        if (problem.getSeverity() == Severity.ERROR) {
            ++errorCount;
        } else if (problem.getSeverity() == Severity.WARNING) {
            ++warningCount;
            messageLevel = Project.MSG_WARN;
        }
        // $NON-NLS-1$
        log(String.format("%s:%s %s", path.name, problem.getOffset(), problem.getMessage()), messageLevel);
    }
    if ((errorCount > 0 && isFailOnValidationError()) || (warningCount > 0 && isFailOnValidationWarning())) {
        throw new BuildException(// $NON-NLS-1$
        MessageFormat.format(// $NON-NLS-1$
        "Validation failed with {0} errors and {1} warnings: {0}", // $NON-NLS-1$
        errorCount, warningCount, path.name));
    }
}
Also used : StandaloneMarkupValidator(org.eclipse.mylyn.wikitext.validation.StandaloneMarkupValidator) ValidationProblem(org.eclipse.mylyn.wikitext.validation.ValidationProblem) BuildException(org.apache.tools.ant.BuildException)

Aggregations

ValidationProblem (org.eclipse.mylyn.wikitext.validation.ValidationProblem)27 Matcher (java.util.regex.Matcher)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BuildException (org.apache.tools.ant.BuildException)2 StandaloneMarkupValidator (org.eclipse.mylyn.wikitext.validation.StandaloneMarkupValidator)2 Map (java.util.Map)1 IMarker (org.eclipse.core.resources.IMarker)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ISynchronizable (org.eclipse.jface.text.ISynchronizable)1 Position (org.eclipse.jface.text.Position)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)1 MarkdownLanguage (org.eclipse.mylyn.wikitext.markdown.MarkdownLanguage)1 LinkDefinitionUsageTracker (org.eclipse.mylyn.wikitext.markdown.internal.LinkDefinitionUsageTracker)1 Position (org.eclipse.mylyn.wikitext.markdown.internal.LinkDefinitionUsageTracker.Position)1 MarkdownContentState (org.eclipse.mylyn.wikitext.markdown.internal.MarkdownContentState)1 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)1 Severity (org.eclipse.mylyn.wikitext.validation.ValidationProblem.Severity)1