Search in sources :

Example 6 with IAnnotationModel

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

the class EditorAccessHighlighter method addHighlights.

@Override
public void addHighlights(Match[] matches) {
    Map<IAnnotationModel, HashMap<Annotation, Position>> mapsByAnnotationModel = new HashMap<>();
    for (Match match : matches) {
        int offset = match.getOffset();
        int length = match.getLength();
        if (offset >= 0 && length >= 0) {
            try {
                Position position = createPosition(match);
                if (position != null) {
                    Map<Annotation, Position> map = getMap(mapsByAnnotationModel, match);
                    if (map != null) {
                        Annotation annotation = match.isFiltered() ? new Annotation(SearchPlugin.FILTERED_SEARCH_ANNOTATION_TYPE, true, null) : new Annotation(SearchPlugin.SEARCH_ANNOTATION_TYPE, true, null);
                        fMatchesToAnnotations.put(match, annotation);
                        map.put(annotation, position);
                    }
                }
            } catch (BadLocationException e) {
                SearchPlugin.log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, SearchMessages.EditorAccessHighlighter_error_badLocation, e));
            }
        }
    }
    for (Entry<IAnnotationModel, HashMap<Annotation, Position>> entry : mapsByAnnotationModel.entrySet()) {
        addAnnotations(entry.getKey(), entry.getValue());
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException) Match(org.eclipse.search.ui.text.Match)

Example 7 with IAnnotationModel

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

the class EditorAccessHighlighter method getMap.

private Map<Annotation, Position> getMap(Map<IAnnotationModel, HashMap<Annotation, Position>> mapsByAnnotationModel, Match match) {
    IAnnotationModel model = fEditorAcess.getAnnotationModel(match);
    if (model == null)
        return null;
    HashMap<Annotation, Position> map = mapsByAnnotationModel.get(model);
    if (map == null) {
        map = new HashMap<>();
        mapsByAnnotationModel.put(model, map);
    }
    return map;
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 8 with IAnnotationModel

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

the class EditorAnnotationManager method getAnnotationModel.

private static IAnnotationModel getAnnotationModel(IWorkbenchPart part) {
    IAnnotationModel model = null;
    model = part.getAdapter(IAnnotationModel.class);
    if (model == null) {
        ITextEditor textEditor = null;
        if (part instanceof ITextEditor) {
            textEditor = (ITextEditor) part;
        }
        if (textEditor != null) {
            IDocumentProvider dp = textEditor.getDocumentProvider();
            if (dp != null)
                model = dp.getAnnotationModel(textEditor.getEditorInput());
        }
    }
    return model;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel)

Example 9 with IAnnotationModel

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

the class AnnotationManagerTest method testAddAnnotation.

@Test
public void testAddAnnotation() throws Exception {
    NewSearchUI.runQueryInForeground(null, fQuery1);
    AbstractTextSearchResult result = (AbstractTextSearchResult) fQuery1.getSearchResult();
    Object[] files = result.getElements();
    try {
        for (int i = 0; i < files.length; i++) {
            IFile file = (IFile) files[i];
            ITextEditor editor = (ITextEditor) SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
            IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
            annotationModel.getAnnotationIterator();
            HashSet<Position> positions = new HashSet<>();
            for (Iterator<Annotation> iter = annotationModel.getAnnotationIterator(); iter.hasNext(); ) {
                Annotation annotation = iter.next();
                if (annotation.getType().equals(fAnnotationTypeLookup.getAnnotationType(NewSearchUI.SEARCH_MARKER, IMarker.SEVERITY_INFO))) {
                    positions.add(annotationModel.getPosition(annotation));
                }
            }
            Match[] matches = result.getMatches(file);
            for (int j = 0; j < matches.length; j++) {
                Position position = new Position(matches[j].getOffset(), matches[j].getLength());
                // $NON-NLS-1$
                assertTrue("position not found at: " + j, positions.remove(position));
            }
            assertEquals(0, positions.size());
        }
    } finally {
        SearchPlugin.getActivePage().closeAllEditors(false);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) Annotation(org.eclipse.jface.text.source.Annotation) FileMatch(org.eclipse.search.internal.ui.text.FileMatch) Match(org.eclipse.search.ui.text.Match) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with IAnnotationModel

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

the class LineAnnotationManagerTest method testLineBasedQuery.

@Test
public void testLineBasedQuery() throws Exception {
    NewSearchUI.runQueryInForeground(null, fLineQuery);
    AbstractTextSearchResult result = (AbstractTextSearchResult) fLineQuery.getSearchResult();
    Object[] files = result.getElements();
    try {
        for (int i = 0; i < files.length; i++) {
            IFile file = (IFile) files[0];
            ITextEditor editor = (ITextEditor) SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
            IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
            IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
            annotationModel.getAnnotationIterator();
            ArrayList<Position> positions = new ArrayList<>();
            for (Iterator<Annotation> iter = annotationModel.getAnnotationIterator(); iter.hasNext(); ) {
                Annotation annotation = iter.next();
                if (annotation.getType().equals(fAnnotationTypeLookup.getAnnotationType(NewSearchUI.SEARCH_MARKER, IMarker.SEVERITY_INFO))) {
                    positions.add(annotationModel.getPosition(annotation));
                }
            }
            Match[] matches = result.getMatches(file);
            for (int j = 0; j < matches.length; j++) {
                Position position = computeDocumentPositionFromLineMatch(document, matches[j]);
                assertTrue("position not found at: " + j, positions.remove(position));
            }
            assertEquals(0, positions.size());
        }
    } finally {
        SearchPlugin.getActivePage().closeAllEditors(false);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) Annotation(org.eclipse.jface.text.source.Annotation) Match(org.eclipse.search.ui.text.Match) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

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