From 707adc2ff53a7f6f036fa10fc1b142e5051953ed Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 8 May 2012 14:11:00 +0400 Subject: [PATCH] add multiline string support to editor find (works for plain text searches not regexps) --- lib/ace/search.js | 231 ++++++++++++++++++++++------------------------ 1 file changed, 112 insertions(+), 119 deletions(-) diff --git a/lib/ace/search.js b/lib/ace/search.js index e30f3aad..b3c3d3df 100644 --- a/lib/ace/search.js +++ b/lib/ace/search.js @@ -63,7 +63,7 @@ var Range = require("./range").Range; * * `wholeWord`: `false` * * `scope`: `ALL` * * `regExp`: `false` - * + * **/ var Search = function() { @@ -88,7 +88,7 @@ Search.SELECTION = 2; * - options (Object): An object containing all the new search properties * * Sets the search options via the `options` parameter. - * + * **/ this.set = function(options) { oop.mixin(this.$options, options); @@ -99,8 +99,8 @@ Search.SELECTION = 2; * Search.getOptions() -> Object * * [Returns an object containing all the search options.]{: #Search.getOptions} - * - **/ + * + **/ this.getOptions = function() { return lang.copyObject(this.$options); }; @@ -110,16 +110,14 @@ Search.SELECTION = 2; * - session (EditSession): The session to search with * * Searches for `options.needle`. If found, this method returns the [[Range `Range`]] where the text first occurs. If `options.backwards` is `true`, the search goes backwards in the session. - * - **/ + * + **/ this.find = function(session) { if (!this.$options.needle) return null; - var iterator = this.$options.backwards - ? this.$backwardMatchIterator(session) - : this.$forwardMatchIterator(session); - + var iterator = this.$matchIterator(session); + if (!iterator) return false; @@ -137,17 +135,15 @@ Search.SELECTION = 2; * - session (EditSession): The session to search with * * Searches for all occurances `options.needle`. If found, this method returns an array of [[Range `Range`s]] where the text first occurs. If `options.backwards` is `true`, the search goes backwards in the session. - * - **/ + * + **/ this.findAll = function(session) { var options = this.$options; if (!options.needle) return []; - var iterator = options.backwards - ? this.$backwardMatchIterator(session) - : this.$forwardMatchIterator(session); - + var iterator = this.$matchIterator(session); + if (!iterator) return false; @@ -173,16 +169,14 @@ Search.SELECTION = 2; * + (String): If `options.regExp` is `true`, this function returns `input` with the replacement already made. Otherwise, this function just returns `replacement`.
* If `options.needle` was not found, this function returns `null`. * - * Searches for `options.needle` in `input`, and, if found, replaces it with `replacement`. + * Searches for `options.needle` in `input`, and, if found, replaces it with `replacement`. * - * - * - **/ + **/ this.replace = function(input, replacement) { var re = this.$assembleRegExp(); if (!re) return; - + var match = re.exec(input); if (match && match[0].length == input.length) { if (this.$options.regExp) { @@ -196,89 +190,97 @@ Search.SELECTION = 2; }; /** internal, hide - * Search.$forwardMatchIterator(session) -> String | Boolean + * Search.$matchIterator(session) -> String | Boolean * - session (EditSession): The session to search with * - * - * - **/ - this.$forwardMatchIterator = function(session) { + * + * + **/ + this.$matchIterator = function(session) { var re = this.$assembleRegExp(); if (!re) return false; - - var self = this; - return { - forEach: function(callback) { - self.$forwardLineIterator(session).forEach(function(line, startIndex, row) { - if (startIndex) { - line = line.substring(startIndex); - } + var self = this, callback, backwards = this.$options.backwards; - var matches = []; + if (this.$options.$isMultiLine) { + var matchIterator = function(line, startIndex, row) { + var startLine = line; + if (startIndex) + line = line.substring(startIndex); - line.replace(re, function(str) { - var offset = arguments[arguments.length-2]; - matches.push({ - str: str, - offset: startIndex + offset - }); - return str; - }); + var len = re.length; + var part = re[0]; + if (line.slice(-part.length) != part) + return; - for (var i=0; i String - * - session (EditSession): The session to search with - * - * - * - **/ - this.$backwardMatchIterator = function(session) { - var re = this.$assembleRegExp(); - if (!re) - return false; - - var self = this; + var matches = []; - return { - forEach: function(callback) { - self.$backwardLineIterator(session).forEach(function(line, startIndex, row) { - if (startIndex) { - line = line.substring(startIndex); - } - - var matches = []; - - line.replace(re, function(str, offset) { - matches.push({ - str: str, - offset: startIndex + offset - }); - return str; + line.replace(re, function(str) { + var offset = arguments[arguments.length-2]; + matches.push({ + str: str, + offset: startIndex + offset }); - - for (var i=matches.length-1; i>= 0; i--) { - var match = matches[i]; - var range = self.$rangeFromMatch(row, match.offset, match.str.length); - if (callback(range)) - return true; - } + return str; }); + + for (var i=matches.length-1; i>= 0; i--) { + var match = matches[i]; + var range = self.$rangeFromMatch(row, match.offset, match.str.length); + if (callback(range)) + return true; + } } - }; + } else { + var matchIterator = function(line, startIndex, row) { + if (startIndex) + line = line.substring(startIndex); + + var matches = []; + + line.replace(re, function(str) { + var offset = arguments[arguments.length-2]; + matches.push({ + str: str, + offset: startIndex + offset + }); + return str; + }); + + for (var i=0; i