Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

add context lines to matches #195

Merged
merged 1 commit into from
Mar 8, 2017
Merged

add context lines to matches #195

merged 1 commit into from
Mar 8, 2017

Conversation

dirk-thomas
Copy link
Contributor

This is in support of atom/find-and-replace#847.

The PR provide similar functionality as atom/scandal#41. It adds an optional options argument to the scan functions. The options can contain lineCountBefore and lineCountAfter. The return results now contain additionally linesBefore and linesAfter.

The previous signatures should continue to work without any need to change existing code.

@dirk-thomas
Copy link
Contributor Author

dirk-thomas commented Feb 11, 2017

Since the beta of 1.15 was just released it would be great if this extended API could be merged to become available in the next beta (1.16).

# * `lineCountBefore` {Number} default `0`; The number of lines before the
# matched line to include in the results object.
# * `lineCountAfter` {Number} default `0`; The number of lines after the
# matched line to include in the results object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After talking with @nathansobo about this new API, we decided that the following names would tell the story better:

  • lineCountBefore -> leadingContextLineCount
  • lineCountAfter -> trailingContextLineCount
  • linesBefore -> leadingContextLines
  • linesAfter -> trailingContextLines

Could you make those changes to the field names? I'll update scandal to match those names.

break if row >= @getLineCount()
argument.linesAfter.push(@lineForRow(row))

callback(argument)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since scanInRange is called a lot, I'd like to avoid creating this wrapper closure. Could you move this functionality to the SingleLineSearchCallbackArgument and MultiLineSearchCallbackArgument classes?

I think that iterator.iterate could take leadingContextLineCount and trailingContextLineCount as two new parameters, and could pass them to the callback arguments' constructors.

for i in [0...(options.lineCountBefore or 0)]
row = argument.range.start.row - i - 1
break if row < 0
argument.linesBefore.unshift(@lineForRow(row))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since unshift has to move every element in the array, I think it'd be slightly more direct and fast to iterate through the buffer's lines in the forwards direction. Maybe something like:

row = Math.max(0, range.start.row - lineCountBefore)
while row < range.start.row
  argument.linesBefore.push(@lineForRow(row))

@dirk-thomas
Copy link
Contributor Author

I updated the code based on your feedback. I moved the logic to populate the context lines here so that both CallbackArgument classes can use it.

One thing really confusing: Travis shows up "green" even if some tests are failing. That is probably something which should be addressed outside of this PR.

@winstliu
Copy link
Contributor

winstliu commented Mar 8, 2017

All tests seem to be passing though?

365 specs, 0 failures
Finished in 15.361 seconds

@dirk-thomas
Copy link
Contributor Author

Yeah, now they are. Before I had some commits which failed some tests but Travis was still green.

I made an example PR to demonstrate the problem. Please see #222.

constructor: (@buffer, @regex, range, @options={}, @chunkSize) ->
if _.isNumber(@options)
@chunkSize = @options
@options = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a conditional needed here? Don't we always pass the chunkSize argument? I'd think that we could make options the last argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three other classes don't have a chunkSize argument. I tried to keep the order of the common arguments the same (therefore options after range). The conditional is there to not break compatibility if any other code is calling this API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're saying; thanks for being careful about compatibility. In this case though, these classes are private and their constructors don't have to have the same signature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, no problem. I updated the patch accordingly.

@maxbrunsfeld maxbrunsfeld merged commit a594c2b into atom:master Mar 8, 2017
@maxbrunsfeld
Copy link
Contributor

Thanks @dirk-thomas; Great work!

@dirk-thomas dirk-thomas deleted the match_with_context branch March 8, 2017 15:33
@nathansobo
Copy link
Contributor

⚡️ ⚡️ ⚡️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants