use of org.rstudio.studio.client.workbench.views.source.editors.text.ace.Search in project rstudio by rstudio.
the class FindReplace method find.
private boolean find(FindType findType, boolean incremental) {
String searchString = display_.getFindValue().getValue();
if (searchString.length() == 0) {
// out the searching string then return to the original position
if (incremental && (incrementalSearchPosition_ != null))
editor_.setSelectionRange(Range.fromPoints(incrementalSearchPosition_, incrementalSearchPosition_));
return false;
}
boolean ignoreCase = !display_.getCaseSensitive().getValue();
boolean regex = display_.getRegex().getValue();
boolean wholeWord = display_.getWholeWord().getValue();
boolean wrap = display_.getWrapSearch().getValue();
// if we are searching in a selection then create a custom position
// (based on the current selection) and range (based on the originally
// saved selection range)
Position position = null;
Range range = null;
if (display_.getInSelection().getValue() && (targetSelection_ != null)) {
range = targetSelection_.getRange();
if (findType == FindType.Forward) {
Position selectionEnd = editor_.getSelectionEnd();
if (selectionEnd.isBefore(range.getEnd()))
position = selectionEnd;
} else {
Position selectionStart = editor_.getSelectionStart();
if (selectionStart.isAfter(range.getStart()))
position = selectionStart;
}
}
// incremental start position then set it, otherwise clear it
if (incremental) {
if (incrementalSearchPosition_ == null) {
if (position != null)
incrementalSearchPosition_ = position;
else
incrementalSearchPosition_ = defaultForward_ ? editor_.getSelectionStart() : editor_.getSelectionEnd();
}
// incremental searches always continue searching from the
// original search position
position = incrementalSearchPosition_;
} else {
incrementalSearchPosition_ = null;
}
// do the search
Search search = Search.create(searchString, findType != FindType.Forward, wrap, !ignoreCase, wholeWord, position, range, regex);
try {
Range resultRange = search.find(editor_.getSession());
if (resultRange == null) {
if (!incremental) {
globalDisplay_.showMessage(GlobalDisplay.MSG_INFO, errorCaption_, "No more occurrences.");
} else {
editor_.collapseSelection(true);
}
return false;
} else {
editor_.revealRange(resultRange, false);
return true;
}
} catch (Throwable e) {
globalDisplay_.showMessage(GlobalDisplay.MSG_ERROR, errorCaption_, "Invalid search term.");
return false;
}
}