Search in sources :

Example 6 with FindResult

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

the class LivePreview method showReplacementPreview.

private void showReplacementPreview() {
    hideBalloon();
    if (!mySearchResults.isUpToDate())
        return;
    final FindResult cursor = mySearchResults.getCursor();
    final Editor editor = mySearchResults.getEditor();
    if (myDelegate != null && cursor != null) {
        String replacementPreviewText = myDelegate.getStringToReplace(editor, cursor);
        if (StringUtil.isEmpty(replacementPreviewText)) {
            replacementPreviewText = EMPTY_STRING_DISPLAY_TEXT;
        }
        final FindModel findModel = mySearchResults.getFindModel();
        if (findModel.isRegularExpressions() && findModel.isReplaceState()) {
            showBalloon(editor, replacementPreviewText);
        }
    }
}
Also used : FindModel(com.intellij.find.FindModel) Editor(com.intellij.openapi.editor.Editor) FindResult(com.intellij.find.FindResult)

Example 7 with FindResult

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

the class LivePreview method updateCursorHighlighting.

private void updateCursorHighlighting() {
    hideBalloon();
    if (myCursorHighlighter != null) {
        HighlightManager.getInstance(mySearchResults.getProject()).removeSegmentHighlighter(mySearchResults.getEditor(), myCursorHighlighter);
        myCursorHighlighter = null;
    }
    final FindResult cursor = mySearchResults.getCursor();
    Editor editor = mySearchResults.getEditor();
    if (cursor != null && cursor.getEndOffset() <= editor.getDocument().getTextLength()) {
        Set<RangeHighlighter> dummy = new HashSet<>();
        Color color = editor.getColorsScheme().getColor(EditorColors.CARET_COLOR);
        highlightRange(cursor, new TextAttributes(null, null, color, EffectType.ROUNDED_BOX, Font.PLAIN), dummy);
        if (!dummy.isEmpty()) {
            myCursorHighlighter = dummy.iterator().next();
        }
        editor.getScrollingModel().runActionOnScrollingFinished(() -> showReplacementPreview());
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) Editor(com.intellij.openapi.editor.Editor) FindResult(com.intellij.find.FindResult) HashSet(java.util.HashSet)

Example 8 with FindResult

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

the class ExpressionOccurrenceManager method findExpressionOccurrences.

public PsiExpression[] findExpressionOccurrences() {
    if (myMainOccurence instanceof PsiLiteralExpression && !myMainOccurence.isPhysical()) {
        final FindManager findManager = FindManager.getInstance(getScope().getProject());
        final FindModel findModel = (FindModel) findManager.getFindInFileModel().clone();
        findModel.setCaseSensitive(true);
        findModel.setRegularExpressions(false);
        String value = StringUtil.stripQuotesAroundValue(myMainOccurence.getText());
        if (value.length() > 0) {
            findModel.setStringToFind(value);
            final List<PsiExpression> results = new ArrayList<>();
            final PsiFile file = getScope().getContainingFile();
            final String text = getScope().getText();
            final int offset = getScope().getTextRange().getStartOffset();
            FindResult result = findManager.findString(text, 0, findModel);
            final Set<PsiLiteralExpression> literals = new HashSet<>();
            while (result.isStringFound()) {
                final int startOffset = offset + result.getStartOffset();
                final int endOffset = result.getEndOffset();
                final PsiLiteralExpression literalExpression = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), PsiLiteralExpression.class);
                if (literalExpression != null && !literals.contains(literalExpression)) {
                    //enum. occurrences inside string literals
                    final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(file.getProject(), file, startOffset, offset + endOffset);
                    if (expression != null && IntroduceVariableBase.getErrorMessage(expression) == null) {
                        results.add(expression);
                        literals.add(literalExpression);
                    }
                }
                result = findManager.findString(text, endOffset, findModel);
            }
            return results.toArray(new PsiExpression[results.size()]);
        }
    }
    return CodeInsightUtil.findExpressionOccurrences(myScope, myMainOccurence);
}
Also used : FindManager(com.intellij.find.FindManager) FindModel(com.intellij.find.FindModel) FindResult(com.intellij.find.FindResult)

Example 9 with FindResult

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

the class FindResultUsageInfo method isValid.

@Override
public boolean isValid() {
    if (!super.isValid())
        return false;
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(getPsiFile());
    if (document == null) {
        myCachedResult = null;
        return false;
    }
    Boolean cachedResult = myCachedResult;
    if (document.getModificationStamp() == myTimestamp && cachedResult != null) {
        return cachedResult;
    }
    myTimestamp = document.getModificationStamp();
    Segment segment = getSegment();
    if (segment == null) {
        myCachedResult = false;
        return false;
    }
    VirtualFile file = getPsiFile().getVirtualFile();
    Segment searchOffset;
    if (myAnchor != null) {
        searchOffset = myAnchor.getRange();
        if (searchOffset == null) {
            myCachedResult = false;
            return false;
        }
    } else {
        searchOffset = segment;
    }
    int offset = searchOffset.getStartOffset();
    Long data = myFindModel.getUserData(DOCUMENT_TIMESTAMP_KEY);
    if (data == null || data != myTimestamp) {
        data = myTimestamp;
        FindManagerImpl.clearPreviousFindData(myFindModel);
    }
    myFindModel.putUserData(DOCUMENT_TIMESTAMP_KEY, data);
    FindResult result;
    do {
        result = myFindManager.findString(document.getCharsSequence(), offset, myFindModel, file);
        offset = result.getEndOffset() == offset ? offset + 1 : result.getEndOffset();
        if (!result.isStringFound()) {
            myCachedResult = false;
            return false;
        }
    } while (result.getStartOffset() < segment.getStartOffset());
    boolean ret = segment.getStartOffset() == result.getStartOffset() && segment.getEndOffset() == result.getEndOffset();
    myCachedResult = ret;
    return ret;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) Segment(com.intellij.openapi.util.Segment) FindResult(com.intellij.find.FindResult)

Example 10 with FindResult

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

the class SearchResults method updateThreadSafe.

public void updateThreadSafe(final FindModel findModel, final boolean toChangeSelection, @Nullable final TextRange next, final int stamp) {
    if (myDisposed)
        return;
    final Editor editor = getEditor();
    final ArrayList<FindResult> results = new ArrayList<>();
    if (findModel != null) {
        updatePreviousFindModel(findModel);
        final FutureResult<int[]> startsRef = new FutureResult<>();
        final FutureResult<int[]> endsRef = new FutureResult<>();
        getSelection(editor, startsRef, endsRef);
        ApplicationManager.getApplication().runReadAction(() -> {
            Project project = getProject();
            if (myDisposed || project != null && project.isDisposed())
                return;
            int[] starts = new int[0];
            int[] ends = new int[0];
            try {
                starts = startsRef.get();
                ends = endsRef.get();
            } catch (InterruptedException | ExecutionException ignore) {
            }
            if (starts.length == 0 || findModel.isGlobal()) {
                findInRange(new TextRange(0, Integer.MAX_VALUE), editor, findModel, results);
            } else {
                for (int i = 0; i < starts.length; ++i) {
                    findInRange(new TextRange(starts[i], ends[i]), editor, findModel, results);
                }
            }
            final Runnable searchCompletedRunnable = () -> searchCompleted(results, editor, findModel, toChangeSelection, next, stamp);
            if (!ApplicationManager.getApplication().isUnitTestMode()) {
                UIUtil.invokeLaterIfNeeded(searchCompletedRunnable);
            } else {
                searchCompletedRunnable.run();
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) Project(com.intellij.openapi.project.Project) FutureResult(com.intellij.util.concurrency.FutureResult) Editor(com.intellij.openapi.editor.Editor) ExecutionException(java.util.concurrent.ExecutionException) 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