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);
}
}
}
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());
}
}
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);
}
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;
}
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();
}
});
}
}
Aggregations