Search in sources :

Example 1 with FindResult

use of com.intellij.find.FindResult in project intellij-community by JetBrains.

the class SearchResults method prevOccurrence.

public void prevOccurrence(boolean findSelected) {
    if (findSelected) {
        if (mySelectionManager.removeCurrentSelection()) {
            myCursor = firstOccurrenceAtOrAfterCaret();
        } else {
            myCursor = null;
        }
        notifyCursorMoved();
    } else {
        FindResult next = null;
        if (myFindModel == null)
            return;
        boolean processFromTheBeginning = false;
        if (myNotFoundState) {
            myNotFoundState = false;
            processFromTheBeginning = true;
        }
        if (!myFindModel.isGlobal()) {
            if (myCursor != null) {
                next = prevOccurrence(myCursor);
            }
        } else {
            next = firstOccurrenceBeforeCaret();
        }
        if (next == null) {
            if (processFromTheBeginning) {
                if (hasMatches()) {
                    next = getOccurrences().get(getOccurrences().size() - 1);
                }
            } else {
                setNotFoundState(false);
            }
        }
        moveCursorTo(next, false);
    }
    push();
}
Also used : FindResult(com.intellij.find.FindResult)

Example 2 with FindResult

use of com.intellij.find.FindResult in project intellij-community by JetBrains.

the class SearchResults method firstOccurrenceAtOrAfterCaret.

@Nullable
private FindResult firstOccurrenceAtOrAfterCaret() {
    int offset = getEditor().getCaretModel().getOffset();
    for (FindResult occurrence : myOccurrences) {
        if (offset <= occurrence.getEndOffset() && offset >= occurrence.getStartOffset()) {
            return occurrence;
        }
    }
    int selectionStartOffset = getEditor().getSelectionModel().getSelectionStart();
    int selectionEndOffset = getEditor().getSelectionModel().getSelectionEnd();
    for (FindResult occurrence : myOccurrences) {
        if (selectionEndOffset >= occurrence.getEndOffset() && selectionStartOffset <= occurrence.getStartOffset()) {
            return occurrence;
        }
    }
    return firstOccurrenceAfterCaret();
}
Also used : FindResult(com.intellij.find.FindResult) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with FindResult

use of com.intellij.find.FindResult in project intellij-community by JetBrains.

the class SearchResults method findInRange.

private void findInRange(TextRange r, Editor editor, FindModel findModel, ArrayList<FindResult> results) {
    VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(editor.getDocument());
    CharSequence charSequence = editor.getDocument().getCharsSequence();
    int offset = r.getStartOffset();
    int maxOffset = Math.min(r.getEndOffset(), charSequence.length());
    FindManager findManager = FindManager.getInstance(getProject());
    while (true) {
        FindResult result;
        try {
            CharSequence bombedCharSequence = StringUtil.newBombedCharSequence(charSequence, 3000);
            result = findManager.findString(bombedCharSequence, offset, findModel, virtualFile);
        } catch (PatternSyntaxException | ProcessCanceledException e) {
            result = null;
        }
        if (result == null || !result.isStringFound())
            break;
        int newOffset = result.getEndOffset();
        if (result.getEndOffset() > maxOffset)
            break;
        if (offset == newOffset) {
            if (offset < maxOffset - 1) {
                offset++;
            } else {
                results.add(result);
                break;
            }
        } else {
            offset = newOffset;
        }
        results.add(result);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FindManager(com.intellij.find.FindManager) FindResult(com.intellij.find.FindResult) PatternSyntaxException(java.util.regex.PatternSyntaxException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 4 with FindResult

use of com.intellij.find.FindResult in project intellij-community by JetBrains.

the class SelectionManager method updateSelection.

public void updateSelection(boolean removePreviousSelection, boolean removeAllPreviousSelections) {
    Editor editor = mySearchResults.getEditor();
    if (removeAllPreviousSelections) {
        editor.getCaretModel().removeSecondaryCarets();
    }
    final FindResult cursor = mySearchResults.getCursor();
    if (cursor == null) {
        if (removePreviousSelection && !myHadSelectionInitially)
            editor.getSelectionModel().removeSelection();
        return;
    }
    if (mySearchResults.getFindModel().isGlobal()) {
        if (removePreviousSelection || removeAllPreviousSelections) {
            FoldingModel foldingModel = editor.getFoldingModel();
            final FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
            foldingModel.runBatchFoldingOperation(() -> {
                for (FoldRegion region : myRegionsToRestore) {
                    if (region.isValid())
                        region.setExpanded(false);
                }
                myRegionsToRestore.clear();
                for (FoldRegion region : allRegions) {
                    if (!region.isValid())
                        continue;
                    if (cursor.intersects(TextRange.create(region))) {
                        if (!region.isExpanded()) {
                            region.setExpanded(true);
                            myRegionsToRestore.add(region);
                        }
                    }
                }
            });
            editor.getSelectionModel().setSelection(cursor.getStartOffset(), cursor.getEndOffset());
            editor.getCaretModel().moveToOffset(cursor.getEndOffset());
        } else {
            FindUtil.selectSearchResultInEditor(editor, cursor, -1);
        }
        editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
    } else {
        if (!SearchResults.insideVisibleArea(editor, cursor)) {
            LogicalPosition pos = editor.offsetToLogicalPosition(cursor.getStartOffset());
            editor.getScrollingModel().scrollTo(pos, ScrollType.CENTER);
        }
    }
}
Also used : FindResult(com.intellij.find.FindResult)

Example 5 with FindResult

use of com.intellij.find.FindResult in project intellij-community by JetBrains.

the class LivePreview method highlightUsages.

private void highlightUsages() {
    if (mySearchResults.getEditor() == null)
        return;
    if (mySearchResults.getMatchesCount() >= mySearchResults.getMatchesLimit())
        return;
    for (FindResult range : mySearchResults.getOccurrences()) {
        if (range.getEndOffset() > mySearchResults.getEditor().getDocument().getTextLength())
            continue;
        TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES);
        if (range.getLength() == 0) {
            attributes = attributes.clone();
            attributes.setEffectType(EffectType.BOXED);
            attributes.setEffectColor(attributes.getBackgroundColor());
        }
        if (mySearchResults.isExcluded(range)) {
            highlightRange(range, strikeout(), myHighlighters);
        } else {
            highlightRange(range, attributes, myHighlighters);
        }
    }
    updateInSelectionHighlighters();
    if (!myListeningSelection) {
        mySearchResults.getEditor().getSelectionModel().addSelectionListener(this);
        myListeningSelection = true;
    }
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) FindResult(com.intellij.find.FindResult)

Aggregations

FindResult (com.intellij.find.FindResult)13 FindModel (com.intellij.find.FindModel)3 Editor (com.intellij.openapi.editor.Editor)3 FindManager (com.intellij.find.FindManager)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Document (com.intellij.openapi.editor.Document)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 Project (com.intellij.openapi.project.Project)1 Segment (com.intellij.openapi.util.Segment)1 TextRange (com.intellij.openapi.util.TextRange)1 FutureResult (com.intellij.util.concurrency.FutureResult)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 Nullable (org.jetbrains.annotations.Nullable)1