Skip to content

Commit db794e8

Browse files
committed
[searchcursor addon] Re-implement weird corner case behavior
Since the vim bindins are depending on that
1 parent 3091a34 commit db794e8

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

addon/search/searchcursor.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
for (var line = start.line, ch = start.ch, last = doc.lastLine(); line <= last; line++, ch = 0) {
3333
regexp.lastIndex = ch
3434
var string = doc.getLine(line), match = regexp.exec(string)
35-
if (match && match[0].length)
35+
if (match)
3636
return {from: Pos(line, match.index),
3737
to: Pos(line, match.index + match[0].length),
3838
match: match}
@@ -57,7 +57,7 @@
5757
chunk = chunk * 2
5858
regexp.lastIndex = start.ch
5959
var match = regexp.exec(string)
60-
if (match && match[0].length) {
60+
if (match) {
6161
var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
6262
var startLine = start.line + before.length - 1, startCh = before[before.length - 1].length
6363
return {from: Pos(startLine, startCh),
@@ -86,7 +86,7 @@
8686
var string = doc.getLine(line)
8787
if (ch > -1) string = string.slice(0, ch)
8888
var match = lastMatchIn(string, regexp)
89-
if (match && match[0].length)
89+
if (match)
9090
return {from: Pos(line, match.index),
9191
to: Pos(line, match.index + match[0].length),
9292
match: match}
@@ -104,7 +104,7 @@
104104
chunk *= 2
105105

106106
var match = lastMatchIn(string, regexp)
107-
if (match && match[0].length) {
107+
if (match) {
108108
var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
109109
var startLine = line + before.length, startCh = before[before.length - 1].length
110110
return {from: Pos(startLine, startCh),
@@ -223,6 +223,21 @@
223223

224224
find: function(reverse) {
225225
var result = this.matches(reverse, this.doc.clipPos(reverse ? this.pos.from : this.pos.to))
226+
227+
// Implements weird auto-growing behavior on null-matches for
228+
// backwards-compatiblity with the vim code (unfortunately)
229+
while (result && CodeMirror.cmpPos(result.from, result.to) == 0) {
230+
if (reverse) {
231+
if (result.from.ch) result.from = Pos(result.from.line, result.from.ch - 1)
232+
else if (result.from.line == this.doc.firstLine()) result = null
233+
else result = this.matches(reverse, this.doc.clipPos(Pos(result.from.line - 1)))
234+
} else {
235+
if (result.to.ch < this.doc.getLine(result.to.line).length) result.to = Pos(result.to.line, result.to.ch + 1)
236+
else if (result.to.line == this.doc.lastLine()) result = null
237+
else result = this.matches(reverse, Pos(result.to.line + 1, 0))
238+
}
239+
}
240+
226241
if (result) {
227242
this.pos = result
228243
this.atOccurrence = true

test/vim_test.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,12 +2369,8 @@ testVim('?_greedy_0_or_more', function(cm, vim, helpers) {
23692369
helpers.doKeys('?');
23702370
helpers.assertCursorAt(1, 1);
23712371
helpers.doKeys('n');
2372-
helpers.assertCursorAt(1, 0);
2373-
helpers.doKeys('n');
23742372
helpers.assertCursorAt(0, 5);
23752373
helpers.doKeys('n');
2376-
helpers.assertCursorAt(0, 4);
2377-
helpers.doKeys('n');
23782374
helpers.assertCursorAt(0, 3);
23792375
helpers.doKeys('n');
23802376
helpers.assertCursorAt(0, 0);

0 commit comments

Comments
 (0)