use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class SearchGroup method findIt.
@Nullable
private TextRange findIt(@NotNull Editor editor, int startOffset, int count, int dir, boolean noSmartCase, boolean wrap, boolean showMessages, boolean wholeFile) {
TextRange res = null;
if (lastSearch == null || lastSearch.length() == 0) {
return res;
}
/*
int pflags = RE.REG_MULTILINE;
if (shouldIgnoreCase(lastSearch, noSmartCase))
{
pflags |= RE.REG_ICASE;
}
*/
//RE sp;
RegExp sp;
RegExp.regmmatch_T regmatch = new RegExp.regmmatch_T();
regmatch.rmm_ic = shouldIgnoreCase(lastSearch, noSmartCase);
sp = new RegExp();
regmatch.regprog = sp.vim_regcomp(lastSearch, 1);
if (regmatch == null) {
if (logger.isDebugEnabled())
logger.debug("bad pattern: " + lastSearch);
return res;
}
/*
int extra_col = 1;
int startcol = -1;
boolean found = false;
boolean match_ok = true;
LogicalPosition pos = editor.offsetToLogicalPosition(startOffset);
LogicalPosition endpos = null;
//REMatch match = null;
*/
CharacterPosition lpos = EditorHelper.offsetToCharacterPosition(editor, startOffset);
RegExp.lpos_T pos = new RegExp.lpos_T();
pos.lnum = lpos.line;
pos.col = lpos.column;
int found;
int lnum;
/* no init to shut up Apollo cc */
//RegExp.regmmatch_T regmatch;
CharPointer ptr;
int matchcol;
int startcol;
RegExp.lpos_T endpos = new RegExp.lpos_T();
int loop;
RegExp.lpos_T start_pos;
boolean at_first_line;
int extra_col = 1;
boolean match_ok;
long nmatched;
//int submatch = 0;
int first_lnum;
int lineCount = EditorHelper.getLineCount(editor);
int startLine = 0;
int endLine = lineCount;
do /* loop for count */
{
start_pos = new RegExp.lpos_T(pos);
/* remember start pos for detecting no match */
found = 0;
/* default: not found */
at_first_line = true;
/* default: start in first line */
if (pos.lnum == -1) /* correct lnum for when starting in line 0 */
{
pos.lnum = 0;
pos.col = 0;
at_first_line = false;
/* not in first line now */
}
/*
* Start searching in current line, unless searching backwards and
* we're in column 0.
*/
if (dir == -1 && start_pos.col == 0) {
lnum = pos.lnum - 1;
at_first_line = false;
} else {
lnum = pos.lnum;
}
int lcount = EditorHelper.getLineCount(editor);
for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
{
if (!wholeFile) {
startLine = lnum;
endLine = lnum + 1;
}
for (; lnum >= startLine && lnum < endLine; lnum += dir, at_first_line = false) {
/*
* Look for a match somewhere in the line.
*/
first_lnum = lnum;
nmatched = sp.vim_regexec_multi(regmatch, editor, lcount, lnum, 0);
if (nmatched > 0) {
/* match may actually be in another line when using \zs */
lnum += regmatch.startpos[0].lnum;
ptr = new CharPointer(EditorHelper.getLineBuffer(editor, lnum));
startcol = regmatch.startpos[0].col;
endpos = regmatch.endpos[0];
/*
* Forward search in the first line: match should be after
* the start position. If not, continue at the end of the
* match (this is vi compatible) or on the next char.
*/
if (dir == 1 && at_first_line) {
match_ok = true;
/*
* When match lands on a NUL the cursor will be put
* one back afterwards, compare with that position,
* otherwise "/$" will get stuck on end of line.
*/
while ((startcol - (startcol == ptr.strlen() ? 1 : 0)) < (start_pos.col + extra_col)) {
if (nmatched > 1) {
/* end is in next line, thus no match in
* this line */
match_ok = false;
break;
}
matchcol = endpos.col;
/* for empty match: advance one char */
if (matchcol == startcol && ptr.charAt(matchcol) != '