Search in sources :

Example 6 with ISynchronizable

use of org.eclipse.jface.text.ISynchronizable 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 7 with ISynchronizable

use of org.eclipse.jface.text.ISynchronizable in project eclipse.jdt.ls by eclipse.

the class DocumentAdapter method setContents.

@Override
public void setContents(String contents) {
    synchronized (lock) {
        if (fDocument == null) {
            if (fTextFileBuffer != null) {
                fDocument = fTextFileBuffer.getDocument();
            } else {
                ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
                fDocument = manager.createEmptyDocument(fFile.getFullPath(), LocationKind.IFILE);
            }
            fDocument.addDocumentListener(this);
            ((ISynchronizable) fDocument).setLockObject(lock);
        }
    }
    if (!contents.equals(fDocument.get())) {
        fDocument.set(contents);
    }
}
Also used : ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ISynchronizable(org.eclipse.jface.text.ISynchronizable)

Aggregations

ISynchronizable (org.eclipse.jface.text.ISynchronizable)7 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IDocument (org.eclipse.jface.text.IDocument)2 Position (org.eclipse.jface.text.Position)2 Annotation (org.eclipse.jface.text.source.Annotation)2 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)2 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)2 ArrayList (java.util.ArrayList)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 IRangeComparator (org.eclipse.compare.rangedifferencer.IRangeComparator)1 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 Status (org.eclipse.core.runtime.Status)1 Job (org.eclipse.core.runtime.jobs.Job)1 EObject (org.eclipse.emf.ecore.EObject)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1