[incremental search] cleaning up keyboard handling
This commit is contained in:
parent
35a0066965
commit
eab7df5a3d
2 changed files with 54 additions and 69 deletions
|
|
@ -34,6 +34,7 @@ define(function(require, exports, module) {
|
||||||
var oop = require("./lib/oop");
|
var oop = require("./lib/oop");
|
||||||
var Range = require("./range").Range;
|
var Range = require("./range").Range;
|
||||||
var Search = require("./search").Search;
|
var Search = require("./search").Search;
|
||||||
|
var ISearchKbd = require("./commands/incremental_search_commands").IncrementalSearchKeyboardHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class IncrementalSearch
|
* @class IncrementalSearch
|
||||||
|
|
@ -46,40 +47,42 @@ var Search = require("./search").Search;
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* Creates a new `IncrementalSearch` object. Options:
|
* Creates a new `IncrementalSearch` object.
|
||||||
*
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
**/
|
**/
|
||||||
function IncrementalSearch() {
|
function IncrementalSearch() {
|
||||||
this.$options = {wrap: false, skipCurrent: false};
|
this.$options = {wrap: false, skipCurrent: false};
|
||||||
this.$keyboardHandler = this;
|
this.$keyboardHandler = new ISearchKbd(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
oop.inherits(IncrementalSearch, Search);
|
oop.inherits(IncrementalSearch, Search);
|
||||||
|
|
||||||
;(function() {
|
;(function() {
|
||||||
|
|
||||||
var iSearch = this;
|
this.activate = function(editor, backwards) {
|
||||||
|
|
||||||
iSearch.activate = function(editor, backwards) {
|
|
||||||
this.$editor = editor;
|
this.$editor = editor;
|
||||||
var pos = editor.getCursorPosition();
|
this.$startPos = this.$currentPos = editor.getCursorPosition();
|
||||||
this.$startPos = this.$currentPos = pos;
|
|
||||||
this.installKeyboardHandler(editor);
|
|
||||||
this.$options.needle = '';
|
this.$options.needle = '';
|
||||||
this.$options.backwards = backwards;
|
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.cancelSearch(reset);
|
||||||
this.uninstallKeyboardHandler(this.$editor);
|
this.$editor.keyBinding.removeKeyboardHandler(this.$keyboardHandler);
|
||||||
delete this.$editor;
|
this.message('');
|
||||||
}
|
}
|
||||||
|
|
||||||
iSearch.cancelSearch = function(reset) {
|
this.cancelSearch = function(reset) {
|
||||||
var e = this.$editor;
|
var e = this.$editor;
|
||||||
this.$prevNeedle = this.$options.needle;
|
this.$prevNeedle = this.$options.needle;
|
||||||
this.$options.needle = '';
|
this.$options.needle = '';
|
||||||
|
|
@ -88,16 +91,18 @@ oop.inherits(IncrementalSearch, Search);
|
||||||
this.$currentPos = this.$startPos;
|
this.$currentPos = this.$startPos;
|
||||||
}
|
}
|
||||||
e.session.highlight(null);
|
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);
|
return Range.fromPoints(this.$currentPos, this.$currentPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
iSearch.highlightAndFindWithNeedle = function(moveToNext, needleUpdateFunc) {
|
this.highlightAndFindWithNeedle = function(moveToNext, needleUpdateFunc) {
|
||||||
if (!this.$editor) return null;
|
if (!this.$editor) return null;
|
||||||
var options = this.$options;
|
var options = this.$options;
|
||||||
|
|
||||||
// get search term
|
// 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);
|
if (options.needle.length === 0) return this.cancelSearch(true);
|
||||||
|
|
||||||
// try to find the next occurence and enable highlighting marker
|
// try to find the next occurence and enable highlighting marker
|
||||||
|
|
@ -113,10 +118,11 @@ oop.inherits(IncrementalSearch, Search);
|
||||||
session.highlight(options.re);
|
session.highlight(options.re);
|
||||||
this.$editor.renderer.updateBackMarkers();
|
this.$editor.renderer.updateBackMarkers();
|
||||||
}
|
}
|
||||||
console.log("searching %s from [%s/%s]: %s",
|
|
||||||
options.backwards ? 'backwards' : 'forwards',
|
var msg = options.backwards ? 'reverse-' : '';
|
||||||
options.start.row, options.start.column,
|
msg += 'isearch: ' + options.needle;
|
||||||
found ? found.toString() : 'found nothing');
|
if (!found) msg += ' (not found)';
|
||||||
|
this.message(msg);
|
||||||
|
|
||||||
return found;
|
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 this.highlightAndFindWithNeedle(false, function(needle) {
|
||||||
return needle.length > 0 ? needle.substring(0, needle.length-1) : 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
|
// try to find the next occurence of whatever we have searched for
|
||||||
// earlier
|
// earlier.
|
||||||
this.$options.backwards = backwards;
|
// options = {[backwards: BOOL], [useCurrentOrPrevSearch: BOOL]}
|
||||||
|
options = options || {};
|
||||||
|
this.$options.backwards = !!options.backwards;
|
||||||
this.$currentPos = this.$editor.getCursorPosition();
|
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) {
|
this.message = function(msg) {
|
||||||
editor.keyBinding.addKeyboardHandler(this.$keyboardHandler);
|
var cmdLine = this.$editor && this.$editor.cmdLine;
|
||||||
}
|
if (cmdLine) {
|
||||||
|
cmdLine.setValue(msg, 1);
|
||||||
iSearch.uninstallKeyboardHandler = function(editor) {
|
} else {
|
||||||
editor.keyBinding.removeKeyboardHandler(this.$keyboardHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
iSearch.message = function(msg) {
|
|
||||||
console.log(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; }
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,7 @@ module.exports = {
|
||||||
range = iSearch.next(); // nothing to find
|
range = iSearch.next(); // nothing to find
|
||||||
testRanges("", [range], "range");
|
testRanges("", [range], "range");
|
||||||
|
|
||||||
range = iSearch.next(true); // backwards
|
range = iSearch.next({backwards: true}); // backwards
|
||||||
testRanges("Range: [1/5] -> [1/3]", [range], "range");
|
testRanges("Range: [1/5] -> [1/3]", [range], "range");
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
@ -147,9 +147,20 @@ module.exports = {
|
||||||
testRanges("Range: [0/3] -> [0/5]", [range], "range");
|
testRanges("Range: [0/3] -> [0/5]", [range], "range");
|
||||||
assert.position(editor.getCursorPosition(), 0, 5);
|
assert.position(editor.getCursorPosition(), 0, 5);
|
||||||
|
|
||||||
range = iSearch.next(true);
|
range = iSearch.next({backwards: true});
|
||||||
testRanges("Range: [0/5] -> [0/3]", [range], "range");
|
testRanges("Range: [0/5] -> [0/3]", [range], "range");
|
||||||
assert.position(editor.getCursorPosition(), 0, 3);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue