Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add backwardsScan function #4

Merged
merged 5 commits into from

2 participants

@watsonian

This adds an equivalent function to scan that uses backwardsScanInRange to make reverse scans possible in the buffer.

Required for atom/whitespace#12.

@nathansobo nathansobo merged commit c843813 into master
@nathansobo nathansobo deleted the scan-backwards branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 57 additions and 25 deletions.
  1. +47 −25 spec/text-buffer-spec.coffee
  2. +10 −0 src/text-buffer.coffee
View
72 spec/text-buffer-spec.coffee
@@ -992,7 +992,7 @@ describe "TextBuffer", ->
runs ->
expect(buffer.isModified()).toBeFalsy()
- describe ".getLines()", ->
+ describe "::getLines()", ->
it "returns an array of lines in the text contents", ->
filePath = require.resolve('./fixtures/sample.js')
fileContents = readFileSync(filePath, 'utf8')
@@ -1005,7 +1005,7 @@ describe "TextBuffer", ->
expect(buffer.getLines().length).toBe fileContents.split("\n").length
expect(buffer.getLines().join('\n')).toBe fileContents
- describe ".change(range, string)", ->
+ describe "::change(range, string)", ->
changeHandler = null
beforeEach ->
@@ -1122,7 +1122,7 @@ describe "TextBuffer", ->
buffer.change([0, 0], "hello")
expect(buffer.lineForRow(0)).toBe "var quicksort = function () {"
- describe ".setText(text)", ->
+ describe "::setText(text)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1147,7 +1147,7 @@ describe "TextBuffer", ->
expect(event.oldRange).toEqual expectedPreRange
expect(event.newRange).toEqual [[0, 0], [1, 14]]
- describe ".setTextViaDiff(text)", ->
+ describe "::setTextViaDiff(text)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1216,7 +1216,7 @@ describe "TextBuffer", ->
buffer.setTextViaDiff(newText)
expect(buffer.getText()).toBe newText
- describe ".save()", ->
+ describe "::save()", ->
saveBuffer = null
afterEach ->
@@ -1311,7 +1311,7 @@ describe "TextBuffer", ->
expect(buffer.isInConflict()).toBeFalsy()
expect(buffer.getText()).toBe(fileContents)
- describe ".saveAs(path)", ->
+ describe "::saveAs(path)", ->
[filePath, saveAsBuffer] = []
afterEach ->
@@ -1359,7 +1359,7 @@ describe "TextBuffer", ->
waitsFor ->
changeHandler.callCount > 0
- describe ".getTextInRange(range)", ->
+ describe "::getTextInRange(range)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1399,25 +1399,47 @@ describe "TextBuffer", ->
it "clips the range to the end of the buffer", ->
expect(buffer.getTextInRange([[12], [13, Infinity]])).toBe buffer.lineForRow(12)
- describe ".scan(regex, fn)", ->
+ describe "::scan(regex, fn)", ->
beforeEach ->
- filePath = require.resolve('./fixtures/sample.js')
- buffer = new TextBuffer({filePath, load: true})
+ buffer = new TextBuffer(filePath: require.resolve('./fixtures/sample.js'))
+ buffer.loadSync()
- waitsFor ->
- buffer.loaded
+ it "calls the given function with the information about each match", ->
+ matches = []
+ buffer.scan /current/g, (match) -> matches.push(match)
+ expect(matches.length).toBe 5
- it "retunrns lineText and lineTextOffset", ->
+ expect(matches[0].matchText).toBe 'current'
+ expect(matches[0].range).toEqual [[3, 31], [3, 38]]
+ expect(matches[0].lineText).toBe ' var pivot = items.shift(), current, left = [], right = [];'
+ expect(matches[0].lineTextOffset).toBe 0
+
+ expect(matches[1].matchText).toBe 'current'
+ expect(matches[1].range).toEqual [[5, 6], [5, 13]]
+ expect(matches[1].lineText).toBe ' current = items.shift();'
+ expect(matches[1].lineTextOffset).toBe 0
+
+ describe "::backwardsScan(regex, fn)", ->
+ beforeEach ->
+ buffer = new TextBuffer(filePath: require.resolve('./fixtures/sample.js'))
+ buffer.loadSync()
+
+ it "calls the given function with the information about each match in backwards order", ->
matches = []
- buffer.scan /current/, (match) ->
- matches.push(match)
- expect(matches.length).toBe 1
+ buffer.backwardsScan /current/g, (match) -> matches.push(match)
+ expect(matches.length).toBe 5
- expect(matches[0].matchText).toEqual 'current'
- expect(matches[0].lineText).toEqual ' var pivot = items.shift(), current, left = [], right = [];'
+ expect(matches[0].matchText).toBe 'current'
+ expect(matches[0].range).toEqual [[6, 56], [6, 63]]
+ expect(matches[0].lineText).toBe ' current < pivot ? left.push(current) : right.push(current);'
expect(matches[0].lineTextOffset).toBe 0
- describe ".scanInRange(range, regex, fn)", ->
+ expect(matches[1].matchText).toBe 'current'
+ expect(matches[1].range).toEqual [[6, 34], [6, 41]]
+ expect(matches[1].lineText).toBe ' current < pivot ? left.push(current) : right.push(current);'
+ expect(matches[1].lineTextOffset).toBe 0
+
+ describe "::scanInRange(range, regex, fn)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1535,7 +1557,7 @@ describe "TextBuffer", ->
expect(ranges.length).toBe 2
- describe ".backwardsScanInRange(range, regex, fn)", ->
+ describe "::backwardsScanInRange(range, regex, fn)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1606,7 +1628,7 @@ describe "TextBuffer", ->
expect(ranges[0]).toEqual [[6,34], [6,41]]
expect(ranges[1]).toEqual [[6,6], [6,13]]
- describe ".characterIndexForPosition(position)", ->
+ describe "::characterIndexForPosition(position)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1630,7 +1652,7 @@ describe "TextBuffer", ->
expect(buffer.characterIndexForPosition([2])).toBe 13
expect(buffer.characterIndexForPosition([3])).toBe 20
- describe ".positionForCharacterIndex(position)", ->
+ describe "::positionForCharacterIndex(position)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1653,7 +1675,7 @@ describe "TextBuffer", ->
expect(buffer.positionForCharacterIndex(13)).toEqual [2, 0]
expect(buffer.positionForCharacterIndex(20)).toEqual [3, 0]
- describe ".usesSoftTabs()", ->
+ describe "::usesSoftTabs()", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1669,7 +1691,7 @@ describe "TextBuffer", ->
buffer.setText("")
expect(buffer.usesSoftTabs()).toBeUndefined()
- describe ".isEmpty()", ->
+ describe "::isEmpty()", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
@@ -1730,7 +1752,7 @@ describe "TextBuffer", ->
runs ->
expect(contentsModifiedHandler).toHaveBeenCalledWith(false)
- describe ".append(text)", ->
+ describe "::append(text)", ->
beforeEach ->
filePath = require.resolve('./fixtures/sample.js')
buffer = new TextBuffer({filePath, load: true})
View
10 src/text-buffer.coffee
@@ -654,6 +654,16 @@ class TextBuffer
result.lineTextOffset = 0
iterator(result)
+ # Scans for text in the buffer _backwards_, calling a function on each match.
+ #
+ # regex - A {RegExp} representing the text to find.
+ # iterator - A {Function} that's called on each match.
+ backwardsScan: (regex, iterator) ->
+ @backwardsScanInRange regex, @getRange(), (result) =>
+ result.lineText = @lineForRow(result.range.start.row)
+ result.lineTextOffset = 0
+ iterator(result)
+
# Replace all matches of regex with replacementText
#
# regex - A {RegExp} representing the text to find.
Something went wrong with that request. Please try again.