Search in sources :

Example 16 with IAnnotationModel

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

the class SelectMarkerRulerAction method gotoMarker.

private void gotoMarker(IMarker marker) {
    // Use the provided adapter if any
    IGotoMarker gotoMarkerAdapter = fTextEditor.getAdapter(IGotoMarker.class);
    if (gotoMarkerAdapter != null) {
        gotoMarkerAdapter.gotoMarker(marker);
        return;
    }
    int start = MarkerUtilities.getCharStart(marker);
    int end = MarkerUtilities.getCharEnd(marker);
    boolean selectLine = start < 0 || end < 0;
    IDocumentProvider documentProvider = fTextEditor.getDocumentProvider();
    IEditorInput editorInput = fTextEditor.getEditorInput();
    // look up the current range of the marker when the document has been edited
    IAnnotationModel model = documentProvider.getAnnotationModel(editorInput);
    if (model instanceof AbstractMarkerAnnotationModel) {
        AbstractMarkerAnnotationModel markerModel = (AbstractMarkerAnnotationModel) model;
        Position pos = markerModel.getMarkerPosition(marker);
        if (pos != null && !pos.isDeleted()) {
            // use position instead of marker values
            start = pos.getOffset();
            end = pos.getOffset() + pos.getLength();
        }
        if (pos != null && pos.isDeleted()) {
            // do nothing if position has been deleted
            return;
        }
    }
    IDocument document = documentProvider.getDocument(editorInput);
    if (selectLine) {
        int line;
        try {
            if (start >= 0)
                line = document.getLineOfOffset(start);
            else {
                line = MarkerUtilities.getLineNumber(marker);
                // Marker line numbers are 1-based
                --line;
            }
            end = start + document.getLineLength(line) - 1;
        } catch (BadLocationException e) {
            return;
        }
    }
    int length = document.getLength();
    if (end - 1 < length && start < length)
        fTextEditor.selectAndReveal(start, end - start);
}
Also used : Position(org.eclipse.jface.text.Position) IGotoMarker(org.eclipse.ui.ide.IGotoMarker) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 17 with IAnnotationModel

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

the class SpellingCorrectionProcessor method computeQuickAssistProposals.

/*
	 * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
	 */
@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) {
    ISourceViewer viewer = quickAssistContext.getSourceViewer();
    int documentOffset = quickAssistContext.getOffset();
    int length = viewer != null ? viewer.getSelectedRange().y : -1;
    TextInvocationContext context = new TextInvocationContext(viewer, documentOffset, length);
    IAnnotationModel model = viewer.getAnnotationModel();
    if (model == null)
        return fgNoSuggestionsProposal;
    List<ICompletionProposal> proposals = computeProposals(context, model);
    if (proposals.isEmpty())
        return fgNoSuggestionsProposal;
    return proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : TextInvocationContext(org.eclipse.jface.text.source.TextInvocationContext) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer)

Example 18 with IAnnotationModel

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

the class SpellingProblem method removeAllInActiveEditor.

/**
 * Removes all spelling problems that are reported
 * for the given <code>word</code> in the active editor.
 * <p>
 * <em>This a workaround to fix bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=134338
 * for 3.2 at the time where spelling still resides in JDT Text.
 * Once we move the spell check engine along with its quick fixes
 * down to Platform Text we need to provide the proposals with
 * a way to access the annotation model.</em>
 * </p>
 *
 * @param editor the text editor, if <code>null</code> this method does nothing
 * @param word the word for which to remove the problems or <code>null</code> to remove all
 * @since 3.3
 * @deprecated As of 3.4, replaced by {@link #removeAll(ISourceViewer, String)}
 */
@Deprecated
public static void removeAllInActiveEditor(ITextEditor editor, String word) {
    if (editor == null)
        return;
    IDocumentProvider documentProvider = editor.getDocumentProvider();
    if (documentProvider == null)
        return;
    IEditorInput editorInput = editor.getEditorInput();
    if (editorInput == null)
        return;
    IAnnotationModel model = documentProvider.getAnnotationModel(editorInput);
    if (model == null)
        return;
    IDocument document = documentProvider.getDocument(editorInput);
    if (document == null)
        return;
    boolean supportsBatchReplace = (model instanceof IAnnotationModelExtension);
    List<Annotation> toBeRemovedAnnotations = new ArrayList<>();
    Iterator<Annotation> iter = model.getAnnotationIterator();
    while (iter.hasNext()) {
        Annotation annotation = iter.next();
        if (SpellingAnnotation.TYPE.equals(annotation.getType())) {
            boolean doRemove = word == null;
            if (word == null)
                doRemove = true;
            else {
                String annotationWord = null;
                Position pos = model.getPosition(annotation);
                try {
                    annotationWord = document.get(pos.getOffset(), pos.getLength());
                } catch (BadLocationException e) {
                    continue;
                }
                doRemove = word.equals(annotationWord);
            }
            if (doRemove) {
                if (supportsBatchReplace)
                    toBeRemovedAnnotations.add(annotation);
                else
                    model.removeAnnotation(annotation);
            }
        }
    }
    if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) {
        Annotation[] annotationArray = toBeRemovedAnnotations.toArray(new Annotation[toBeRemovedAnnotations.size()]);
        ((IAnnotationModelExtension) model).replaceAnnotations(annotationArray, null);
    }
}
Also used : Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) 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) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 19 with IAnnotationModel

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

the class HighlightTest method checkHighlightForCaretOffset.

private void checkHighlightForCaretOffset(int pos, String expectedHighlight, int expectedHighlightCount) throws Exception {
    clearAnnotations();
    editor.selectAndReveal(pos, 0);
    waitForAnnotations(expectedHighlightCount);
    List<Annotation> annotations = getAnnotationsFromAnnotationModel();
    IAnnotationModel annotationModel = getAnnotationModel();
    IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    for (int i = 0; i < annotations.size(); i++) {
        Annotation annotation = annotations.get(i);
        Position position = annotationModel.getPosition(annotation);
        String highlight = document.get(position.offset, position.length);
        assertEquals("Wrong highlight " + i + " at position " + position.offset, expectedHighlight, highlight);
    }
    Assert.assertEquals("Wrong number of highlights", expectedHighlightCount, annotations.size());
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) IDocument(org.eclipse.jface.text.IDocument)

Example 20 with IAnnotationModel

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

the class DefaultWordHighlightStrategy method applyHighlights.

private void applyHighlights(int offset) {
    if (sourceViewer == null || !enabled) {
        removeOccurrenceAnnotations();
        return;
    }
    String text = document.get();
    offset = ((ITextViewerExtension5) sourceViewer).widgetOffset2ModelOffset(offset);
    String word = findCurrentWord(text, offset);
    if (word == null) {
        removeOccurrenceAnnotations();
        return;
    }
    Matcher m = WORD_PATTERN.matcher(text);
    Map<Annotation, Position> annotationMap = new HashMap<>();
    while (m.find()) {
        if (m.group().equals(word)) {
            annotationMap.put(new Annotation(ANNOTATION_TYPE, false, NLS.bind(Messages.DefaultWordHighlightStrategy_OccurrencesOf, word)), new Position(m.start(), m.end() - m.start()));
        }
    }
    if (annotationMap.size() < 2) {
        removeOccurrenceAnnotations();
        return;
    }
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    synchronized (getLockObject(annotationModel)) {
        if (annotationModel instanceof IAnnotationModelExtension) {
            ((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap);
        } else {
            removeOccurrenceAnnotations();
            Iterator<Entry<Annotation, Position>> iter = annotationMap.entrySet().iterator();
            while (iter.hasNext()) {
                Entry<Annotation, Position> mapEntry = iter.next();
                annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue());
            }
        }
        fOccurrenceAnnotations = annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
    }
}
Also used : Entry(java.util.Map.Entry) Matcher(java.util.regex.Matcher) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

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