Find/replace now works

This commit is contained in:
Daniel Perrett 2016-10-25 08:45:52 +01:00
parent a0423994bc
commit 0c1672c5cb

View File

@ -7,8 +7,10 @@ var BaseWidget = require('base-widget');
var Slap = require('./Slap');
var BaseFindForm = require('./BaseFindForm');
var Label = require('./Label');
var Field = require('editor-widget').Field;
var Button = require('./Button');
FindForm._label = " find (/.*/ for regex): ";
FindForm._label = " Find (/.*/ for regex): ";
FindForm._regExpRegExp = /^\/(.+)\/(\w*)$/i;
FindForm._invalidRegExpMessageRegExp = /^(Invalid regular expression:|Invalid flags supplied to RegExp constructor)/;
function FindForm (opts) {
@ -17,9 +19,33 @@ function FindForm (opts) {
if (!(self instanceof FindForm)) return new FindForm(opts);
BaseFindForm.call(self, _.merge({
height: 2,
findField: {left: FindForm._label.length}
}, Slap.global.options.form.find, opts));
self.replaceLabel = new Label(_.merge({
parent: self,
tags: true,
content: ' Replace with: ',
top: 1,
height: 1,
left: 0
}, self.options.findLabel));
self.replaceField = new Field(_.merge({
parent: self,
top: 1,
left: 15,
right: 9
}, Slap.global.options.editor, Slap.global.options.field, self.options.replaceField));
self.replaceButton = new Button(_.merge({
parent: self,
content: "Replace",
top: 1,
right: 0
}, Slap.global.options.button, self.options.replaceButton));
self.findLabel = new Label(_.merge({
parent: self,
tags: true,
@ -50,7 +76,8 @@ FindForm.prototype._initHandlers = function () {
editor.destroyMarkers({type: 'findMatch'});
editor._updateContent();
});
self.on('find', lodash.throttle(function (pattern, direction) {
var findOrReplace = function (pattern, direction, replacement) {
direction = direction || 0;
editor.destroyMarkers({type: 'findMatch'});
try {
@ -84,6 +111,9 @@ FindForm.prototype._initHandlers = function () {
var cmp = matchRange.start.compare(selectionRange.start);
if (cmp === direction) {
self.selectRange(matchRange);
if (replacement) {
editor.textBuf.setTextInRange(editor.selection.getRange(), replacement);
}
return true;
} else if (!cmp && matches.length === 1) {
header.message("this is the only occurrence", 'info');
@ -92,8 +122,17 @@ FindForm.prototype._initHandlers = function () {
})) {
header.message("search wrapped", 'info');
self.selectRange(matches[0].range);
if (replacement) {
editor.textBuf.setTextInRange(editor.selection.getRange(), replacement);
}
}
editor._updateContent();
};
self.on('find', lodash.throttle(findOrReplace, self.options.perf.findThrottle));
self.replaceButton.on('press', lodash.throttle( function () {
findOrReplace(self.findField.value(), 0, self.replaceField.value());
}, self.options.perf.findThrottle));
self.findField.on('keypress', function (ch, key) {