Search in sources :

Example 66 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.

the class TestReconcilerXML method testIllFormedNoCloseBracket.

/**
 * Tests reconciler by verifying error/warning found with ill-formed xml.
 * (missing close bracket)
 */
public void testIllFormedNoCloseBracket() {
    IDocument doc = fEditor.getAdapter(IDocument.class);
    doc.set("<html><body><h1>Title</h1></body></html>");
    ITextEditor textEditor = fEditor.getAdapter(ITextEditor.class);
    IAnnotationModel annoModel = textEditor.getDocumentProvider().getAnnotationModel(fEditor.getEditorInput());
    DefaultMarkerAnnotationAccess annotationAccess = new DefaultMarkerAnnotationAccess();
    // verify ill-formed xml
    try {
        doc.replace(6, 6, "<body ");
        Thread.sleep(5000);
        boolean errorFound = false;
        Iterator iter = annoModel.getAnnotationIterator();
        StringBuffer buffer = new StringBuffer();
        while (iter.hasNext()) {
            Annotation anno = (Annotation) iter.next();
            String annoType = anno.getType();
            buffer.append("\n");
            buffer.append(anno.getText());
            if ((annotationAccess.isSubtype(annoType, ANNOTATION_ERROR)) || (annotationAccess.isSubtype(annoType, ANNOTATION_WARNING))) {
                errorFound = true;
            }
        }
        assertTrue("testReconciler: Did not find expected errors in: " + doc.get() + buffer.toString(), errorFound);
    } catch (BadLocationException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) DefaultMarkerAnnotationAccess(org.eclipse.ui.texteditor.DefaultMarkerAnnotationAccess) Iterator(java.util.Iterator) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IDocument(org.eclipse.jface.text.IDocument) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 67 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project webtools.sourceediting by eclipse.

the class GoToMatchingTagAction method updateFor.

void updateFor(ISelection selection) {
    ITextEditor textEditor = getTextEditor();
    if (textEditor == null) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("no editor");
        }
        return;
    }
    IDocumentProvider documentProvider = textEditor.getDocumentProvider();
    if (documentProvider == null) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("no document provider");
        }
        return;
    }
    IAnnotationModel annotationModel = documentProvider.getAnnotationModel(textEditor.getEditorInput());
    if (annotationModel == null || !(annotationModel instanceof IAnnotationModelExtension)) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("no annotation model");
        }
        return;
    }
    List oldAnnotations = new ArrayList(2);
    Iterator annotationIterator = annotationModel.getAnnotationIterator();
    while (annotationIterator.hasNext()) {
        Annotation annotation = (Annotation) annotationIterator.next();
        if (ANNOTATION_TYPE.equals(annotation.getType())) {
            annotation.markDeleted(true);
            if (DEBUG) {
                // $NON-NLS-1$
                System.out.println("removing " + annotation);
            }
            oldAnnotations.add(annotation);
        }
    }
    Map newAnnotations = new HashMap();
    if (!selection.isEmpty() && selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
        Object o = ((IStructuredSelection) selection).getFirstElement();
        if (o instanceof IDOMNode) {
            int offset = ((ITextSelection) selection).getOffset();
            IStructuredDocumentRegion matchRegion = null;
            if (((Node) o).getNodeType() == Node.ATTRIBUTE_NODE) {
                o = ((Attr) o).getOwnerElement();
            }
            Position pStart = null;
            Position pEnd = null;
            // $NON-NLS-1$
            String tag = "";
            if (o instanceof IDOMNode) {
                IDOMNode node = (IDOMNode) o;
                IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
                if (startStructuredDocumentRegion != null && startStructuredDocumentRegion.containsOffset(offset)) {
                    if (startStructuredDocumentRegion.getNumberOfRegions() > 1) {
                        ITextRegion nameRegion = startStructuredDocumentRegion.getRegions().get(1);
                        pStart = new Position(startStructuredDocumentRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                        tag = startStructuredDocumentRegion.getText(nameRegion);
                    }
                    matchRegion = ((IDOMNode) o).getEndStructuredDocumentRegion();
                    if (matchRegion != null && matchRegion.getNumberOfRegions() > 1) {
                        ITextRegion nameRegion = matchRegion.getRegions().get(1);
                        pEnd = new Position(matchRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                    }
                } else {
                    IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
                    if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.containsOffset(offset)) {
                        if (endStructuredDocumentRegion.getNumberOfRegions() > 1) {
                            ITextRegion nameRegion = endStructuredDocumentRegion.getRegions().get(1);
                            pEnd = new Position(endStructuredDocumentRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                            tag = endStructuredDocumentRegion.getText(nameRegion);
                        }
                        matchRegion = ((IDOMNode) o).getStartStructuredDocumentRegion();
                        if (matchRegion != null && matchRegion.getNumberOfRegions() > 1) {
                            ITextRegion nameRegion = matchRegion.getRegions().get(1);
                            pStart = new Position(matchRegion.getStartOffset(nameRegion), nameRegion.getTextLength());
                        }
                    }
                }
            }
            if (pStart != null && pEnd != null) {
                Annotation annotation = new Annotation(ANNOTATION_TYPE, false, NLS.bind(XMLUIMessages.gotoMatchingTag_start, tag));
                newAnnotations.put(annotation, pStart);
                if (DEBUG) {
                    // $NON-NLS-1$
                    System.out.println("adding " + annotation);
                }
                annotation = new Annotation(ANNOTATION_TYPE, false, NLS.bind(XMLUIMessages.gotoMatchingTag_end, tag));
                newAnnotations.put(annotation, pEnd);
                if (DEBUG) {
                    // $NON-NLS-1$
                    System.out.println("adding " + annotation);
                }
            }
        }
    }
    ((IAnnotationModelExtension) annotationModel).replaceAnnotations((Annotation[]) oldAnnotations.toArray(new Annotation[oldAnnotations.size()]), newAnnotations);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Annotation(org.eclipse.jface.text.source.Annotation) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 68 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project bndtools by bndtools.

the class BndMarkerQuickAssistProcessor method computeQuickAssistProposals.

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext context) {
    List<ICompletionProposal> proposals = new LinkedList<ICompletionProposal>();
    ISourceViewer viewer = context.getSourceViewer();
    @SuppressWarnings("unused") IDocument document = viewer.getDocument();
    IAnnotationModel model = viewer.getAnnotationModel();
    @SuppressWarnings("rawtypes") Iterator iter = model.getAnnotationIterator();
    while (iter.hasNext()) {
        Annotation annotation = (Annotation) iter.next();
        if (annotation instanceof MarkerAnnotation && canFix(annotation)) {
            Position position = model.getPosition(annotation);
            if (isAtPosition(context.getOffset(), position)) {
                IMarker marker = ((MarkerAnnotation) annotation).getMarker();
                String errorType = marker.getAttribute("$bndType", null);
                if (errorType != null) {
                    BuildErrorDetailsHandler handler = BuildErrorDetailsHandlers.INSTANCE.findHandler(errorType);
                    if (handler != null) {
                        proposals.addAll(handler.getProposals(marker));
                    }
                }
            }
        }
    }
    if (proposals.isEmpty()) {
        proposals.add(new NoCompletionsProposal());
    }
    return proposals.toArray(new ICompletionProposal[0]);
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) LinkedList(java.util.LinkedList) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) BuildErrorDetailsHandler(org.bndtools.build.api.BuildErrorDetailsHandler) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) IMarker(org.eclipse.core.resources.IMarker) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IDocument(org.eclipse.jface.text.IDocument)

Example 69 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project dbeaver by serge-rider.

the class FileRefDocumentProvider method createElementInfo.

@Override
protected ElementInfo createElementInfo(Object element) throws CoreException {
    if (element instanceof IEditorInput) {
        IEditorInput input = (IEditorInput) element;
        IStorage storage = EditorUtils.getStorageFromInput(input);
        if (storage instanceof IFile) {
            IFile file = (IFile) storage;
            try {
                refreshFile(file);
            } catch (CoreException x) {
                log.warn("Can't refresh file", x);
            }
            IDocument d;
            IStatus s = null;
            try {
                d = createDocument(element);
            } catch (CoreException x) {
                log.warn("Can't create document", x);
                s = x.getStatus();
                d = createEmptyDocument();
            }
            // Set the initial line delimiter
            String initialLineDelimiter = GeneralUtils.getDefaultLineSeparator();
            if (initialLineDelimiter != null) {
                ((IDocumentExtension4) d).setInitialLineDelimiter(initialLineDelimiter);
            }
            IAnnotationModel m = createAnnotationModel(element);
            FileSynchronizer f = new FileSynchronizer(input);
            f.install();
            FileInfo info = new FileInfo(d, m, f);
            info.modificationStamp = computeModificationStamp(file);
            info.fStatus = s;
            return info;
        }
    }
    return super.createElementInfo(element);
}
Also used : IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 70 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonQuickAssistProcessor method getMarkersFor.

protected List<IMarker> getMarkersFor(ISourceViewer sourceViewer, int lineOffset, int lineLength) {
    List<IMarker> result = Lists.newArrayList();
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    Iterator annotationIter = annotationModel.getAnnotationIterator();
    while (annotationIter.hasNext()) {
        Object annotation = annotationIter.next();
        if (annotation instanceof MarkerAnnotation) {
            MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation;
            IMarker marker = markerAnnotation.getMarker();
            Position markerPosition = annotationModel.getPosition(markerAnnotation);
            if (markerPosition != null && markerPosition.overlapsWith(lineOffset, lineLength)) {
                result.add(marker);
            }
        }
    }
    return result;
}
Also used : MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) Position(org.eclipse.jface.text.Position) Iterator(java.util.Iterator) IMarker(org.eclipse.core.resources.IMarker) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel)

Aggregations

IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)148 Annotation (org.eclipse.jface.text.source.Annotation)71 Position (org.eclipse.jface.text.Position)58 IDocument (org.eclipse.jface.text.IDocument)41 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)26 Iterator (java.util.Iterator)23 ArrayList (java.util.ArrayList)20 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)20 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)19 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)19 BadLocationException (org.eclipse.jface.text.BadLocationException)18 IFile (org.eclipse.core.resources.IFile)17 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)16 IEditorInput (org.eclipse.ui.IEditorInput)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 List (java.util.List)12 CoreException (org.eclipse.core.runtime.CoreException)11 IMarker (org.eclipse.core.resources.IMarker)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10