Search in sources :

Example 21 with Position

use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.

the class AnnotationWithQuickFixesHover method getHoverInfoInternal.

// DIFF: this code is entirely different, as the hover subclasses from AbstractProblemHover and
// hooks different methods
@Override
protected Object getHoverInfoInternal(ITextViewer textViewer, final int lineNumber, final int offset) {
    AnnotationInfo result = recentAnnotationInfo;
    if (result != null)
        return result;
    List<Annotation> annotations = getAnnotations(lineNumber, offset);
    if (annotations != null) {
        for (Annotation annotation : annotations) {
            Position position = getAnnotationModel().getPosition(annotation);
            if (annotation.getText() != null && position != null) {
                final QuickAssistInvocationContext invocationContext = new QuickAssistInvocationContext(sourceViewer, position.getOffset(), position.getLength(), true);
                CompletionProposalRunnable runnable = new CompletionProposalRunnable(invocationContext);
                // Note: the resolutions have to be retrieved from the UI thread, otherwise
                // workbench.getActiveWorkbenchWindow() will return null in LanguageSpecificURIEditorOpener and
                // cause an exception
                Display.getDefault().syncExec(runnable);
                if (invocationContext.isMarkedCancelled()) {
                    return null;
                }
                result = new AnnotationInfo(annotation, position, sourceViewer, runnable.proposals);
                recentAnnotationInfo = result;
                return result;
            }
        }
    }
    return null;
}
Also used : Position(org.eclipse.jface.text.Position) IQuickAssistInvocationContext(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext) QuickAssistInvocationContext(org.eclipse.xtext.ui.editor.quickfix.QuickAssistInvocationContext) Annotation(org.eclipse.jface.text.source.Annotation)

Example 22 with Position

use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.

the class DefaultOccurrenceComputer method addOccurrenceAnnotation.

protected void addOccurrenceAnnotation(String type, IDocument document, ITextRegion textRegion, Map<Annotation, Position> annotationMap) {
    try {
        if (textRegion != null && textRegion.getLength() > 0) {
            Annotation annotation = new Annotation(type, false, document.get(textRegion.getOffset(), textRegion.getLength()));
            annotationMap.put(annotation, new Position(textRegion.getOffset(), textRegion.getLength()));
        }
    } catch (BadLocationException e) {
        LOG.error("Error creating occurrence annotation", e);
    }
}
Also used : Position(org.eclipse.jface.text.Position) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 23 with Position

use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.

the class AbstractTemplateProposalProvider method createPosition.

/**
 * Positions created for template contexts have to be added to the document so they are updated when the document
 * is modified.
 *
 * @since 2.8
 */
protected Position createPosition(ContentAssistContext context) {
    Position position = new Position(context.getReplaceRegion().getOffset(), context.getReplaceRegion().getLength());
    IDocument document = context.getDocument();
    if (document.containsPositionCategory(XTEXT_TEMPLATE_POS_CATEGORY)) {
        try {
            document.addPosition(XTEXT_TEMPLATE_POS_CATEGORY, position);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    return position;
}
Also used : Position(org.eclipse.jface.text.Position) IDocument(org.eclipse.jface.text.IDocument) TemplateException(org.eclipse.jface.text.templates.TemplateException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 24 with Position

use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.

the class AnnotationIssueProcessor method getAnnotationsToAdd.

protected Map<Annotation, Position> getAnnotationsToAdd(Multimap<Position, Annotation> positionToAnnotations, List<Issue> issues, IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
        return HashBiMap.create();
    }
    Map<Annotation, Position> annotationToPosition = Maps.newHashMapWithExpectedSize(issues.size());
    for (Issue issue : issues) {
        if (monitor.isCanceled()) {
            return annotationToPosition;
        }
        if (isSet(issue.getOffset()) && isSet(issue.getLength()) && issue.getMessage() != null) {
            String type = lookup.getAnnotationType(EValidator.MARKER, getMarkerSeverity(issue.getSeverity()));
            boolean isQuickfixable = false;
            if (issueResolutionProvider instanceof IssueResolutionProviderExtension) {
                isQuickfixable = ((IssueResolutionProviderExtension) issueResolutionProvider).hasResolutionFor(issue);
            } else {
                isQuickfixable = issueResolutionProvider.hasResolutionFor(issue.getCode());
            }
            Annotation annotation = new XtextAnnotation(type, false, xtextDocument, issue, isQuickfixable);
            if (issue.getOffset() < 0 || issue.getLength() < 0) {
                LOG.error("Invalid annotation position offset=" + issue.getOffset() + " length = " + issue.getLength());
            }
            Position position = new Position(Math.max(0, issue.getOffset()), Math.max(0, issue.getLength()));
            annotationToPosition.put(annotation, position);
            positionToAnnotations.put(position, annotation);
        }
    }
    return annotationToPosition;
}
Also used : IssueResolutionProviderExtension(org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProviderExtension) Issue(org.eclipse.xtext.validation.Issue) Position(org.eclipse.jface.text.Position) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation)

Example 25 with Position

use of org.eclipse.jface.text.Position in project xtext-eclipse by eclipse.

the class AnnotationIssueProcessor method announceAnnotationChanged.

protected void announceAnnotationChanged(Annotation annotation) {
    if (annotationModel instanceof XtextResourceMarkerAnnotationModel)
        ((XtextResourceMarkerAnnotationModel) annotationModel).fireAnnotationChangedEvent(annotation);
    else {
        Position position = annotationModel.getPosition(annotation);
        if (annotationModel instanceof IAnnotationModelExtension)
            ((IAnnotationModelExtension) annotationModel).modifyAnnotationPosition(annotation, position);
        else {
            annotationModel.removeAnnotation(annotation);
            annotationModel.addAnnotation(annotation, position);
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) XtextResourceMarkerAnnotationModel(org.eclipse.xtext.ui.editor.quickfix.XtextResourceMarkerAnnotationModel) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension)

Aggregations

Position (org.eclipse.jface.text.Position)421 BadLocationException (org.eclipse.jface.text.BadLocationException)145 Annotation (org.eclipse.jface.text.source.Annotation)110 IDocument (org.eclipse.jface.text.IDocument)80 ArrayList (java.util.ArrayList)75 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)57 IRegion (org.eclipse.jface.text.IRegion)55 Test (org.junit.Test)54 HashMap (java.util.HashMap)49 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)46 Point (org.eclipse.swt.graphics.Point)40 List (java.util.List)39 Iterator (java.util.Iterator)31 TypedPosition (org.eclipse.jface.text.TypedPosition)31 Region (org.eclipse.jface.text.Region)27 IFile (org.eclipse.core.resources.IFile)22 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)21 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)21 Map (java.util.Map)15 CoreException (org.eclipse.core.runtime.CoreException)15