diff --git a/lib/ace/incremental_search.js b/lib/ace/incremental_search.js index 48135519..87630f2e 100644 --- a/lib/ace/incremental_search.js +++ b/lib/ace/incremental_search.js @@ -68,8 +68,6 @@ oop.inherits(IncrementalSearch, Search); this.$editor = editor; var pos = editor.getCursorPosition(); this.$startPos = this.$currentPos = pos; - // this.$startRange = this.$currentRange = Range.fromPoints(pos, pos); - // this.$startRange = this.$currentRange = editor.selection.toOrientedRange(); this.installKeyboardHandler(editor); this.$options.needle = ''; this.$options.backwards = backwards; @@ -87,10 +85,10 @@ oop.inherits(IncrementalSearch, Search); this.$options.needle = ''; e.session.highlight(null); if (reset) { - e.moveCursorTo(this.$startPos); + e.moveCursorToPosition(this.$startPos); this.$currentPos = this.$startPos; } else { - e.selection.clearSelection(); + e.renderer.updateFull(true); // for highlight } return Range.fromPoints(this.$currentPos, this.$currentPos); } @@ -98,23 +96,28 @@ oop.inherits(IncrementalSearch, Search); iSearch.highlightAndFindWithNeedle = function(moveToNext, needleUpdateFunc) { if (!this.$editor) return null; var options = this.$options; + + // get search term if (needleUpdateFunc) options.needle = needleUpdateFunc(options.needle || '') || ''; - if (options.needle.length === 0) { - return this.cancelSearch(true); - } - - // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- - var session = this.$editor.session, pos = this.$currentPos; - options.start = pos; - var oldRange = this.$editor.selection.toOrientedRange(); - var found = this.find(session); - - // if (dir === "backward") range = Range.fromPoints(range.end, range.start); - this.$editor.selection.setRange(found || oldRange, options.backwards); - if (found && moveToNext) this.$currentPos = options.backwards ? found.end : found.start; + if (options.needle.length === 0) return this.cancelSearch(true); + // try to find the next occurence and enable highlighting marker + options.start = this.$currentPos; + var session = this.$editor.session, + found = this.find(session); session.highlight(options.re); - return found || oldRange; + this.$editor.renderer.updateFull(true); // force highlight layer redraw + if (found) { + if (options.backwards) found = Range.fromPoints(found.end, found.start); + this.$editor.moveCursorToPosition(found.end); + if (moveToNext) this.$currentPos = found.end; + } + console.log("searching %s from [%s/%s]: %s", + options.backwards ? 'backwards' : 'forwards', + options.start.row, options.start.column, + found ? found.toString() : 'found nothing'); + + return found; } this.addChar = function(c) { @@ -130,28 +133,19 @@ oop.inherits(IncrementalSearch, Search); } iSearch.next = function(backwards) { - var currentRange = this.$editor.selection.toOrientedRange(); - this.$currentPos = this.$options.backwards ? currentRange.start : currentRange.end; + // try to find the next occurence of whatever we have searched for + // earlier this.$options.backwards = backwards; + this.$currentPos = this.$editor.getCursorPosition(); return this.highlightAndFindWithNeedle(true); } iSearch.installKeyboardHandler = function(editor) { - // this.$origKeyboardHandlers = [].concat(editor.keyBinding.$handlers); - // this.$origKeyboardHandlers.reverse().forEach(function(handler) { - // editor.keyBinding.removeKeyboardHandler(handler); - // }); editor.keyBinding.addKeyboardHandler(this.$keyboardHandler); } iSearch.uninstallKeyboardHandler = function(editor) { editor.keyBinding.removeKeyboardHandler(this.$keyboardHandler); - // if (this.$origKeyboardHandlers) { - // this.$origKeyboardHandlers.forEach(function(handler) { - // editor.keyBinding.addKeyboardHandler(handler); - // }); - // delete this.$origKeyboardHandlers; - // } } iSearch.message = function(msg) { @@ -162,8 +156,7 @@ oop.inherits(IncrementalSearch, Search); iSearch.handleKeyboard = function(data, hashId, key, keyCode) { this.message("data: " + data + ", hashId: " + hashId + ", key: " + key + ", keyCode: " + keyCode); - var stop = {command: 'null'}, - result = undefined; + var stop = {command: 'null'}, result = undefined; console.log(this.$options.needle); if (hashId === 0) { diff --git a/lib/ace/incremental_search_test.js b/lib/ace/incremental_search_test.js index 0f48060d..230c3c28 100644 --- a/lib/ace/incremental_search_test.js +++ b/lib/ace/incremental_search_test.js @@ -108,11 +108,12 @@ module.exports = { var range = iSearch.next(); testRanges("Range: [1/3] -> [1/5]", [range], "range"); - range = iSearch.next(); - testRanges("Range: [1/3] -> [1/5]", [range], "range"); + range = iSearch.next(); // nothing to find + testRanges("", [range], "range"); + assert.ok(!editor.selection.isBackwards(), 'reoriented?'); range = iSearch.next(true); // backwards - testRanges("Range: [0/3] -> [0/5]", [range], "range"); + testRanges("Range: [1/5] -> [1/3]", [range], "range"); }, "test: cancelSearch" : function() { @@ -125,30 +126,32 @@ module.exports = { testRanges("Range: [0/3] -> [0/5]", [range], "range"); }, - "test: failing search keeps range" : function() { + "test: failing search keeps pos" : function() { iSearch.activate(editor); iSearch.addChar('1'); iSearch.addChar('2'); var range = iSearch.addChar('x'); - testRanges("Range: [0/3] -> [0/5]", [range], "range"); + testRanges("", [range], "range"); + assert.position(editor.getCursorPosition(), 0, 5); }, "test: backwards search" : function() { editor.moveCursorTo(1,0); iSearch.activate(editor, true); iSearch.addChar('1'); var range = iSearch.addChar('2');; - testRanges("Range: [0/3] -> [0/5]", [range], "range"); - } + testRanges("Range: [0/5] -> [0/3]", [range], "range"); + assert.position(editor.getCursorPosition(), 0, 3); + }, - // // "test: find simple text in document" : function() { - // var session = new EditSession(["juhu kinners 123", "456"]); - // var search = new Search().set({ - // needle: "kinners" - // }); - // session.getSelection().moveCursorTo(0, 13); - // var range = search.find(session); - // assert.position(range.start, 0, 5); - // assert.position(range.end, 0, 12); - // }, + "test: forwards then backwards, same result, reoriented range" : function() { + iSearch.activate(editor); + iSearch.addChar('1'); var range = iSearch.addChar('2');; + testRanges("Range: [0/3] -> [0/5]", [range], "range"); + assert.position(editor.getCursorPosition(), 0, 5); + + range = iSearch.next(true); + testRanges("Range: [0/5] -> [0/3]", [range], "range"); + assert.position(editor.getCursorPosition(), 0, 3); + } };