diff --git a/src/default-directory-searcher.coffee b/src/default-directory-searcher.coffee index 6b8ffe3e3..3955d38e3 100644 --- a/src/default-directory-searcher.coffee +++ b/src/default-directory-searcher.coffee @@ -11,13 +11,16 @@ class DirectorySearch excludeVcsIgnores: options.excludeVcsIgnores globalExclusions: options.exclusions follow: options.follow + searchOptions = + leadingContextLineCount: options.leadingContextLineCount + trailingContextLineCount: options.trailingContextLineCount @task = new Task(require.resolve('./scan-handler')) @task.on 'scan:result-found', options.didMatch @task.on 'scan:file-error', options.didError @task.on 'scan:paths-searched', options.didSearchPaths @promise = new Promise (resolve, reject) => @task.on('task:cancelled', reject) - @task.start rootPaths, regex.source, scanHandlerOptions, => + @task.start rootPaths, regex.source, scanHandlerOptions, searchOptions, => @task.terminate() resolve() diff --git a/src/scan-handler.coffee b/src/scan-handler.coffee index 8ee8f715e..db2e8299b 100644 --- a/src/scan-handler.coffee +++ b/src/scan-handler.coffee @@ -2,13 +2,13 @@ path = require "path" async = require "async" {PathSearcher, PathScanner, search} = require 'scandal' -module.exports = (rootPaths, regexSource, options) -> +module.exports = (rootPaths, regexSource, options, searchOptions={}) -> callback = @async() PATHS_COUNTER_SEARCHED_CHUNK = 50 pathsSearched = 0 - searcher = new PathSearcher() + searcher = new PathSearcher(searchOptions) searcher.on 'file-error', ({code, path, message}) -> emit('scan:file-error', {code, path, message}) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 8095632fd..73fb7f954 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -2826,6 +2826,11 @@ class TextEditor extends Model # {::backwardsScanInBufferRange} to avoid tripping over your own changes. # # * `regex` A {RegExp} to search for. + # * `options` (optional) {Object} + # * `leadingContextLineCount` {Number} default `0`; The number of lines + # before the matched line to include in the results object. + # * `trailingContextLineCount` {Number} default `0`; The number of lines + # after the matched line to include in the results object. # * `iterator` A {Function} that's called on each match # * `object` {Object} # * `match` The current regular expression match. @@ -2833,7 +2838,12 @@ class TextEditor extends Model # * `range` The {Range} of the match. # * `stop` Call this {Function} to terminate the scan. # * `replace` Call this {Function} with a {String} to replace the match. - scan: (regex, iterator) -> @buffer.scan(regex, iterator) + scan: (regex, options={}, iterator) -> + if _.isFunction(options) + iterator = options + options = {} + + @buffer.scan(regex, options, iterator) # Essential: Scan regular expression matches in a given range, calling the given # iterator function on each match. diff --git a/src/workspace.js b/src/workspace.js index a386b5ff7..6c5daba12 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -1202,6 +1202,10 @@ module.exports = class Workspace extends Model { // * `paths` An {Array} of glob patterns to search within. // * `onPathsSearched` (optional) {Function} to be periodically called // with number of paths searched. + // * `leadingContextLineCount` {Number} default `0`; The number of lines + // before the matched line to include in the results object. + // * `trailingContextLineCount` {Number} default `0`; The number of lines + // after the matched line to include in the results object. // * `iterator` {Function} callback on each file found. // // Returns a {Promise} with a `cancel()` method that will cancel all @@ -1261,6 +1265,8 @@ module.exports = class Workspace extends Model { excludeVcsIgnores: this.config.get('core.excludeVcsIgnoredPaths'), exclusions: this.config.get('core.ignoredNames'), follow: this.config.get('core.followSymlinks'), + leadingContextLineCount: options.leadingContextLineCount || 0, + trailingContextLineCount: options.trailingContextLineCount || 0, didMatch: result => { if (!this.project.isPathModified(result.filePath)) { return iterator(result)