Search in sources :

Example 11 with Match

use of org.eclipse.search.ui.text.Match in project eclipse.platform.text by eclipse.

the class LineElement method getMatches.

public FileMatch[] getMatches(AbstractTextSearchResult result) {
    ArrayList<FileMatch> res = new ArrayList<>();
    Match[] matches = result.getMatches(fParent);
    for (Match match : matches) {
        FileMatch curr = (FileMatch) match;
        if (curr.getLineElement() == this) {
            res.add(curr);
        }
    }
    return res.toArray(new FileMatch[res.size()]);
}
Also used : ArrayList(java.util.ArrayList) Match(org.eclipse.search.ui.text.Match)

Example 12 with Match

use of org.eclipse.search.ui.text.Match in project eclipse.platform.text by eclipse.

the class SearchResultUpdater method handleDelta.

private void handleDelta(IResourceDelta d) {
    try {
        d.accept(delta -> {
            switch(delta.getKind()) {
                case IResourceDelta.ADDED:
                    return false;
                case IResourceDelta.REMOVED:
                    IResource res = delta.getResource();
                    if (res instanceof IFile) {
                        Match[] matches = fResult.getMatches(res);
                        fResult.removeMatches(matches);
                    }
                    break;
                case IResourceDelta.CHANGED:
                    // handle changed resource
                    break;
            }
            return true;
        });
    } catch (CoreException e) {
        SearchPlugin.log(e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IResource(org.eclipse.core.resources.IResource) Match(org.eclipse.search.ui.text.Match)

Example 13 with Match

use of org.eclipse.search.ui.text.Match 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 14 with Match

use of org.eclipse.search.ui.text.Match 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)

Example 15 with Match

use of org.eclipse.search.ui.text.Match in project eclipse.platform.text by eclipse.

the class PositionTrackerTest method checkInsertAtZero.

private void checkInsertAtZero(AbstractTextSearchResult result, IFile file) throws PartInitException, BadLocationException {
    Match[] matches = result.getMatches(file);
    int[] originalStarts = new int[matches.length];
    for (int i = 0; i < originalStarts.length; i++) {
        originalStarts[i] = matches[i].getOffset();
    }
    try {
        SearchTestPlugin.openTextEditor(SearchPlugin.getActivePage(), file);
        ITextFileBuffer fb = FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
        IDocument doc = fb.getDocument();
        doc.replace(0, 0, "Test");
        for (int i = 0; i < originalStarts.length; i++) {
            Position currentPosition = InternalSearchUI.getInstance().getPositionTracker().getCurrentPosition(matches[i]);
            assertNotNull(currentPosition);
            assertEquals(originalStarts[i] + "Test".length(), currentPosition.getOffset());
        }
    } finally {
        SearchPlugin.getActivePage().closeAllEditors(false);
    }
}
Also used : Position(org.eclipse.jface.text.Position) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) Match(org.eclipse.search.ui.text.Match)

Aggregations

Match (org.eclipse.search.ui.text.Match)45 AbstractTextSearchResult (org.eclipse.search.ui.text.AbstractTextSearchResult)19 Test (org.junit.Test)14 ISearchQuery (org.eclipse.search.ui.ISearchQuery)11 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)7 IFile (org.eclipse.core.resources.IFile)7 Position (org.eclipse.jface.text.Position)7 Annotation (org.eclipse.jface.text.source.Annotation)6 ArrayList (java.util.ArrayList)5 BadLocationException (org.eclipse.jface.text.BadLocationException)5 IDocument (org.eclipse.jface.text.IDocument)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)4 ISearchResult (org.eclipse.search.ui.ISearchResult)4 IResource (org.eclipse.core.resources.IResource)3 MatchEvent (org.eclipse.search.ui.text.MatchEvent)3 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2