[incremental search] cleaning up keyboard handling

This commit is contained in:
Robert Krahn 2013-03-10 19:32:20 -07:00
commit eab7df5a3d
2 changed files with 54 additions and 69 deletions

View file

@ -34,6 +34,7 @@ define(function(require, exports, module) {
var oop = require("./lib/oop");
var Range = require("./range").Range;
var Search = require("./search").Search;
var ISearchKbd = require("./commands/incremental_search_commands").IncrementalSearchKeyboardHandler;
/**
* @class IncrementalSearch
@ -46,40 +47,42 @@ var Search = require("./search").Search;
*
**/
/**
*
*
* Creates a new `IncrementalSearch` object. Options:
* Creates a new `IncrementalSearch` object.
*
* @constructor
**/
function IncrementalSearch() {
this.$options = {wrap: false, skipCurrent: false};
this.$keyboardHandler = this;
this.$keyboardHandler = new ISearchKbd(this);
}
oop.inherits(IncrementalSearch, Search);
;(function() {
var iSearch = this;
iSearch.activate = function(editor, backwards) {
this.activate = function(editor, backwards) {
this.$editor = editor;
var pos = editor.getCursorPosition();
this.$startPos = this.$currentPos = pos;
this.installKeyboardHandler(editor);
this.$startPos = this.$currentPos = editor.getCursorPosition();
this.$options.needle = '';
this.$options.backwards = backwards;
editor.keyBinding.addKeyboardHandler(this.$keyboardHandler);
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
var msg = this.$options.backwards ? 'reverse-' : '';
msg += 'isearch: ' + this.$options.needle;
this.message(msg);
}
iSearch.deactivate = function(reset) {
this.deactivate = function(reset) {
this.cancelSearch(reset);
this.uninstallKeyboardHandler(this.$editor);
delete this.$editor;
this.$editor.keyBinding.removeKeyboardHandler(this.$keyboardHandler);
this.message('');
}
iSearch.cancelSearch = function(reset) {
this.cancelSearch = function(reset) {
var e = this.$editor;
this.$prevNeedle = this.$options.needle;
this.$options.needle = '';
@ -88,16 +91,18 @@ oop.inherits(IncrementalSearch, Search);
this.$currentPos = this.$startPos;
}
e.session.highlight(null);
this.$editor.renderer.updateBackMarkers(); // force highlight layer redraw
e.renderer.updateBackMarkers(); // force highlight layer redraw
return Range.fromPoints(this.$currentPos, this.$currentPos);
}
iSearch.highlightAndFindWithNeedle = function(moveToNext, needleUpdateFunc) {
this.highlightAndFindWithNeedle = function(moveToNext, needleUpdateFunc) {
if (!this.$editor) return null;
var options = this.$options;
// get search term
if (needleUpdateFunc) options.needle = needleUpdateFunc(options.needle || '') || '';
if (needleUpdateFunc) {
options.needle = needleUpdateFunc.call(this, options.needle || '') || '';
}
if (options.needle.length === 0) return this.cancelSearch(true);
// try to find the next occurence and enable highlighting marker
@ -113,10 +118,11 @@ oop.inherits(IncrementalSearch, Search);
session.highlight(options.re);
this.$editor.renderer.updateBackMarkers();
}
console.log("searching %s from [%s/%s]: %s",
options.backwards ? 'backwards' : 'forwards',
options.start.row, options.start.column,
found ? found.toString() : 'found nothing');
var msg = options.backwards ? 'reverse-' : '';
msg += 'isearch: ' + options.needle;
if (!found) msg += ' (not found)';
this.message(msg);
return found;
}
@ -127,64 +133,32 @@ oop.inherits(IncrementalSearch, Search);
});
}
iSearch.removeChar = function(c) {
this.removeChar = function(c) {
return this.highlightAndFindWithNeedle(false, function(needle) {
return needle.length > 0 ? needle.substring(0, needle.length-1) : needle;
});
}
iSearch.next = function(backwards) {
this.next = function(options) {
// try to find the next occurence of whatever we have searched for
// earlier
this.$options.backwards = backwards;
// earlier.
// options = {[backwards: BOOL], [useCurrentOrPrevSearch: BOOL]}
options = options || {};
this.$options.backwards = !!options.backwards;
this.$currentPos = this.$editor.getCursorPosition();
return this.highlightAndFindWithNeedle(true);
return this.highlightAndFindWithNeedle(true, function(needle) {
return options.useCurrentOrPrevSearch && needle.length === 0 ?
this.$prevNeedle || '' : needle;
});
}
iSearch.installKeyboardHandler = function(editor) {
editor.keyBinding.addKeyboardHandler(this.$keyboardHandler);
}
iSearch.uninstallKeyboardHandler = function(editor) {
editor.keyBinding.removeKeyboardHandler(this.$keyboardHandler);
}
iSearch.message = function(msg) {
console.log(msg);
if (this.commandLine)
this.commandLine.setValue(msg, 1);
}
iSearch.handleKeyboard = function(data, hashId, key, keyCode) {
this.message("data: " + data + ", hashId: " + hashId + ", key: " + key + ", keyCode: " + keyCode);
var stop = {command: 'null'}, result = stop;
console.log(this.$options.needle);
if (hashId === 0) {
if (key === 'backspace') { this.removeChar(); result = stop; }
if (key === 'return') { this.deactivate(); this.message(''); return stop; }
if (key === 'esc') { this.deactivate(true); this.message(''); return stop; }
if (key === 'space') { this.addChar(" "); result = stop; }
if (key.length === 1) { this.addChar(key); result = stop; }
this.message = function(msg) {
var cmdLine = this.$editor && this.$editor.cmdLine;
if (cmdLine) {
cmdLine.setValue(msg, 1);
} else {
console.log(msg);
}
if (hashId === 1) {
if (key === 's' || key === 'r') {
if (this.$options.needle.length === 0)
this.$options.needle = this.$prevNeedle || '';
this.next(key === 'r');
result = stop;
}
if (key === 'g') { this.deactivate(true); this.message(''); return stop; }
// let others handle but don't deactivate iSearch
// stop.passEvent = true;
// return stop;
}
this.message((this.$options.backwards ? 'reverse-' : '') + 'isearch: ' + this.$options.needle);
// this.deactivate();
return result;
}

View file

@ -111,7 +111,7 @@ module.exports = {
range = iSearch.next(); // nothing to find
testRanges("", [range], "range");
range = iSearch.next(true); // backwards
range = iSearch.next({backwards: true}); // backwards
testRanges("Range: [1/5] -> [1/3]", [range], "range");
},
@ -147,9 +147,20 @@ module.exports = {
testRanges("Range: [0/3] -> [0/5]", [range], "range");
assert.position(editor.getCursorPosition(), 0, 5);
range = iSearch.next(true);
range = iSearch.next({backwards: true});
testRanges("Range: [0/5] -> [0/3]", [range], "range");
assert.position(editor.getCursorPosition(), 0, 3);
},
"test: reuse prev search via option" : function() {
iSearch.activate(editor);
iSearch.addChar('1'); iSearch.addChar('2');;
assert.position(editor.getCursorPosition(), 0, 5);
iSearch.deactivate();
iSearch.activate(editor);
iSearch.next({backwards: false, useCurrentOrPrevSearch: true});
assert.position(editor.getCursorPosition(), 1, 5);
}
};