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

Add backwardsScan function #4

Merged
merged 5 commits into from Mar 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 47 additions & 25 deletions spec/text-buffer-spec.coffee
Expand Up @@ -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')
Expand All @@ -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 ->
Expand Down Expand Up @@ -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})
Expand All @@ -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})
Expand Down Expand Up @@ -1216,7 +1216,7 @@ describe "TextBuffer", ->
buffer.setTextViaDiff(newText)
expect(buffer.getText()).toBe newText

describe ".save()", ->
describe "::save()", ->
saveBuffer = null

afterEach ->
Expand Down Expand Up @@ -1311,7 +1311,7 @@ describe "TextBuffer", ->
expect(buffer.isInConflict()).toBeFalsy()
expect(buffer.getText()).toBe(fileContents)

describe ".saveAs(path)", ->
describe "::saveAs(path)", ->
[filePath, saveAsBuffer] = []

afterEach ->
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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})
Expand All @@ -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})
Expand All @@ -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})
Expand All @@ -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})
Expand Down Expand Up @@ -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})
Expand Down
10 changes: 10 additions & 0 deletions src/text-buffer.coffee
Expand Up @@ -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.
Expand Down