Skip to content

Commit

Permalink
Merge pull request atom#13713 from dirk-thomas/match_with_context
Browse files Browse the repository at this point in the history
pass through line count options
  • Loading branch information
Max Brunsfeld authored Mar 10, 2017
2 parents a792fa3 + 3485530 commit ad3c4d9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"random-words": "0.0.1",
"resolve": "^1.1.6",
"runas": "^3.1",
"scandal": "^3.0.0",
"scandal": "^3.1.0",
"scoped-property-store": "^0.17.0",
"scrollbar-style": "^3.2",
"season": "^6.0.0",
Expand Down
12 changes: 8 additions & 4 deletions spec/workspace-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,9 @@ i = /test/; #FIXME\
it('calls the callback with all regex results in all files in the project', () => {
const results = []
waitsForPromise(() =>
atom.workspace.scan(/(a)+/, result => results.push(result))
atom.workspace.scan(
/(a)+/, {leadingContextLineCount: 1, trailingContextLineCount: 1},
result => results.push(result))
)

runs(() => {
Expand All @@ -1349,14 +1351,16 @@ i = /test/; #FIXME\
lineTextOffset: 0,
range: [[0, 0], [0, 3]],
leadingContextLines: [],
trailingContextLines: []
trailingContextLines: ['cc aa cc']
})
})
})

it('works with with escaped literals (like $ and ^)', () => {
const results = []
waitsForPromise(() => atom.workspace.scan(/\$\w+/, result => results.push(result)))
waitsForPromise(() => atom.workspace.scan(
/\$\w+/, {leadingContextLineCount: 1, trailingContextLineCount: 1},
result => results.push(result)))

runs(() => {
expect(results.length).toBe(1)
Expand All @@ -1368,7 +1372,7 @@ i = /test/; #FIXME\
lineText: 'dollar$bill',
lineTextOffset: 0,
range: [[2, 6], [2, 11]],
leadingContextLines: [],
leadingContextLines: ['cc aa cc'],
trailingContextLines: []
})
})
Expand Down
5 changes: 4 additions & 1 deletion src/default-directory-searcher.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions src/scan-handler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
12 changes: 11 additions & 1 deletion src/text-editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2826,14 +2826,24 @@ 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.
# * `matchText` A {String} with the text of the match.
# * `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.
Expand Down
6 changes: 6 additions & 0 deletions src/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ad3c4d9

Please sign in to comment.