Search in sources :

Example 46 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project titan.EclipsePlug-ins by eclipse.

the class AnnotationHover method getMarkerForLine.

/**
 * Helper function for {@link #getHoverInfo(ISourceViewer, int)}.
 *
 * @see #getHoverInfo(ISourceViewer, int)
 *
 * @param sourceViewer
 *                The viewer whose annotation we wish to find.
 * @param lineNumber
 *                The line number in which the annotation(s) are.
 * @return The list of markers residing in the given line in the given
 *         editor.
 */
protected List<IMarker> getMarkerForLine(final ISourceViewer sourceViewer, final int lineNumber) {
    final List<IMarker> markers = new ArrayList<IMarker>();
    final IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    if (annotationModel == null) {
        return markers;
    }
    final Iterator<?> iterator = annotationModel.getAnnotationIterator();
    while (iterator.hasNext()) {
        final Object o = iterator.next();
        if (o instanceof MarkerAnnotation) {
            final MarkerAnnotation actuaMarkerl = (MarkerAnnotation) o;
            try {
                final int actualLine = sourceViewer.getDocument().getLineOfOffset(annotationModel.getPosition(actuaMarkerl).getOffset());
                if (actualLine == lineNumber) {
                    markers.add(actuaMarkerl.getMarker());
                }
            } catch (BadLocationException e) {
                ErrorReporter.logExceptionStackTrace(e);
            }
        }
    }
    return markers;
}
Also used : MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) ArrayList(java.util.ArrayList) IMarker(org.eclipse.core.resources.IMarker) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 47 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project titan.EclipsePlug-ins by eclipse.

the class BaseTextHover method getHoverInfo.

@Override
public String getHoverInfo(final ITextViewer textViewer, final IRegion hoverRegion) {
    if (hoverRegion == null || textViewer == null) {
        return null;
    }
    IAnnotationModel annotationModel = getSourceViewer().getAnnotationModel();
    if (annotationModel != null) {
        Iterator<?> iterator = annotationModel.getAnnotationIterator();
        List<String> messages = new ArrayList<String>();
        while (iterator.hasNext()) {
            Object o = iterator.next();
            if (o instanceof MarkerAnnotation) {
                MarkerAnnotation actualMarker = (MarkerAnnotation) o;
                Position markerPosition = annotationModel.getPosition(actualMarker);
                if (markerPosition != null && markerPosition.getOffset() <= hoverRegion.getOffset() && markerPosition.getOffset() + markerPosition.getLength() >= hoverRegion.getOffset()) {
                    String message = actualMarker.getText();
                    if (message != null) {
                        // Marker error text hover (or tooltip in other words) handles error message
                        // in HTML format, and there can be situation, when the message contains
                        // < and > characters, which are handled as HTML control tags, so they
                        // are not visible. So these < and > characters are removed.
                        // Example: ANTLR sends the following error message during parsing:
                        // "mismatched input 'control' expecting <EOF>"
                        message = message.replaceAll("\\<([A-Z]+)\\>", "$1");
                    } else {
                        ErrorReporter.INTERNAL_ERROR("BaseTextHover.getHoverInfo(): message == null");
                    }
                    messages.add(message);
                }
            }
        }
        if (!messages.isEmpty()) {
            final StringBuilder builder = new StringBuilder();
            builder.append(messages.get(0));
            for (int i = 1; i < messages.size() && i <= 3; i++) {
                builder.append("<br></br>");
                builder.append(messages.get(i));
            }
            if (messages.size() > 3) {
                builder.append("<br></br>...");
            }
            return builder.toString();
        }
    }
    ErrorReporter.parallelDisplayInStatusLine(getTargetEditor(), null);
    DeclarationCollector declarationCollector = OpenDeclarationHelper.findVisibleDeclarations(getTargetEditor(), getReferenceParser(), textViewer.getDocument(), hoverRegion.getOffset(), false);
    if (declarationCollector == null) {
        return null;
    }
    List<DeclarationCollectionHelper> collected = declarationCollector.getCollected();
    // To handle reference problem in T3Doc
    if (T3Doc.isT3DocEnable()) {
        String string = T3Doc.getCommentStringBasedOnReference(declarationCollector, collected, getTargetEditor(), hoverRegion, getReferenceParser(), textViewer);
        if (string != null) {
            return string;
        }
    }
    if (collected.isEmpty()) {
        return null;
    }
    DeclarationCollectionHelper declaration = collected.get(0);
    // Check whether the T3Doc is enabled in the preferences window
    if (!T3Doc.isT3DocEnable()) {
        return declaration.description;
    }
    if (declaration.node != null) {
        if (declaration.node.getT3Doc(declaration.location) == null) {
            return "";
        }
        if (declaration.description != null) {
            return declaration.description + "<BR></BR>" + declaration.node.getT3Doc(declaration.location).toString();
        }
        return declaration.node.getT3Doc(declaration.location).toString();
    }
    if (declaration.scope != null) {
        if (declaration.description != null) {
            return declaration.description + declaration.scope.getT3Doc(declaration.location).toString();
        }
        return declaration.scope.getT3Doc(declaration.location).toString();
    }
    // return declaration.description;
    return "";
}
Also used : DeclarationCollector(org.eclipse.titan.designer.editors.actions.DeclarationCollector) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) DeclarationCollectionHelper(org.eclipse.titan.designer.editors.actions.DeclarationCollectionHelper) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation)

Example 48 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project titan.EclipsePlug-ins by eclipse.

the class MarkerHandler method deprecateMarkers.

/**
 * Mark the provided markers as deprecated.
 * <p>
 * Those that are in an open editor, can be manipulated directly,
 *  the others will receive their value later by the AnnotationImageProvider
 *
 *  @param editor the editor being open/edited triggering the change.
 *  @param markers the markers to mark deprecated.
 */
public static void deprecateMarkers(final ISemanticTITANEditor editor, final IMarker[] markers) {
    if (editor == null || markers == null) {
        return;
    }
    try {
        AbstractDecoratedTextEditor textEditor = null;
        IFile fileInput = null;
        if (editor instanceof AbstractDecoratedTextEditor) {
            textEditor = (AbstractDecoratedTextEditor) editor;
            if (textEditor.getEditorInput() instanceof FileEditorInput) {
                final FileEditorInput input = (FileEditorInput) textEditor.getEditorInput();
                fileInput = input.getFile();
            }
        }
        for (IMarker marker : markers) {
            if (fileInput != null && textEditor != null && fileInput.equals(marker.getResource())) {
                final IEditorInput editorInput = textEditor.getEditorInput();
                final IDocumentProvider provider = textEditor.getDocumentProvider();
                if (editorInput != null && provider != null) {
                    final IAnnotationModel model = provider.getAnnotationModel(editorInput);
                    final Iterator<?> e = model.getAnnotationIterator();
                    while (e.hasNext()) {
                        final Annotation annotation = (Annotation) e.next();
                        if (annotation instanceof MarkerAnnotation && marker.equals(((MarkerAnnotation) annotation).getMarker())) {
                            annotation.markDeleted(true);
                        }
                    }
                } else {
                    if (!marker.getAttribute(AnnotationImageProvider.DEPRECATED, false)) {
                        marker.setAttribute(AnnotationImageProvider.DEPRECATED, true);
                    }
                }
            } else {
                if (!marker.getAttribute(AnnotationImageProvider.DEPRECATED, false)) {
                    marker.setAttribute(AnnotationImageProvider.DEPRECATED, true);
                }
            }
        }
    } catch (CoreException e) {
        ErrorReporter.logExceptionStackTrace(MARKER_HANDLING_ERROR, e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) CoreException(org.eclipse.core.runtime.CoreException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IMarker(org.eclipse.core.resources.IMarker) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor)

Example 49 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project titan.EclipsePlug-ins by eclipse.

the class TTCNPPEditor method updateInactiveCodeAnnotations.

public void updateInactiveCodeAnnotations(final List<Location> inactiveCodeLocations) {
    if ((inactiveCodeLocations == null || inactiveCodeLocations.isEmpty()) && (inactiveCodeAnnotations == null || inactiveCodeAnnotations.length == 0)) {
        return;
    }
    IEditorInput editorInput = getEditorInput();
    if (editorInput == null) {
        return;
    }
    IDocumentProvider documentProvider = getDocumentProvider();
    if (documentProvider == null) {
        return;
    }
    IAnnotationModel annotationModel = documentProvider.getAnnotationModel(editorInput);
    if (annotationModel == null) {
        return;
    }
    Object lockObject = (annotationModel instanceof ISynchronizable) ? ((ISynchronizable) annotationModel).getLockObject() : annotationModel;
    synchronized (lockObject) {
        Map<Annotation, Position> annotationMap = new HashMap<Annotation, Position>();
        if (inactiveCodeLocations != null) {
            for (Location loc : inactiveCodeLocations) {
                Annotation annotationToAdd = new Annotation(INACTIVE_CODE_ANNOTATION_TYPE, false, "Inactive code");
                Position position = new Position(loc.getOffset(), loc.getEndOffset() - loc.getOffset());
                annotationMap.put(annotationToAdd, position);
            }
        }
        if (annotationModel instanceof IAnnotationModelExtension) {
            ((IAnnotationModelExtension) annotationModel).replaceAnnotations(inactiveCodeAnnotations, annotationMap);
        } else {
            if (inactiveCodeAnnotations != null) {
                for (Annotation annotationToRemove : inactiveCodeAnnotations) {
                    annotationModel.removeAnnotation(annotationToRemove);
                }
            }
            for (Map.Entry<Annotation, Position> entry : annotationMap.entrySet()) {
                annotationModel.addAnnotation(entry.getKey(), entry.getValue());
            }
        }
        inactiveCodeAnnotations = annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
    }
}
Also used : Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) ISynchronizable(org.eclipse.jface.text.ISynchronizable) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) Map(java.util.Map) HashMap(java.util.HashMap) IEditorInput(org.eclipse.ui.IEditorInput) Location(org.eclipse.titan.designer.AST.Location)

Example 50 with IAnnotationModel

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

the class DocumentRegionProcessor method endProcessing.

protected void endProcessing() {
    super.endProcessing();
    ValidatorStrategy validatorStrategy = getValidatorStrategy();
    if (validatorStrategy != null) {
        validatorStrategy.endProcessing();
    }
    /* single spell-check for everything to ensure that SpellingProblem offsets are correct */
    IReconcilingStrategy spellingStrategy = getSpellcheckStrategy();
    IDocument document = getDocument();
    if (spellingStrategy != null && document != null) {
        spellingStrategy.reconcile(new Region(0, document.getLength()));
    }
    IReconcilingStrategy semanticHighlightingStrategy = getSemanticHighlightingStrategy();
    if (semanticHighlightingStrategy != null && document != null) {
        semanticHighlightingStrategy.reconcile(new Region(0, document.getLength()));
    }
    if ((getTextViewer() instanceof ISourceViewer)) {
        ISourceViewer sourceViewer = (ISourceViewer) getTextViewer();
        IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
        for (int i = 0; i < fReconcileListeners.length; i++) {
            fReconcileListeners[i].reconciled(document, annotationModel, false, new NullProgressMonitor());
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IReconcilingStrategy(org.eclipse.jface.text.reconciler.IReconcilingStrategy) Region(org.eclipse.jface.text.Region) ITypedRegion(org.eclipse.jface.text.ITypedRegion) DirtyRegion(org.eclipse.jface.text.reconciler.DirtyRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) ValidatorStrategy(org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorStrategy) IDocument(org.eclipse.jface.text.IDocument)

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