mirror of
https://github.com/atom/atom.git
synced 2026-01-24 14:28:14 -05:00
Remove SpanIndex. Fully replaced by LineMap
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
SpanIndex = require 'span-index'
|
||||
|
||||
describe "SpanIndex", ->
|
||||
index = null
|
||||
beforeEach ->
|
||||
index = new SpanIndex
|
||||
|
||||
describe ".insert(index, span(s), values)", ->
|
||||
describe "when called with an array of spans", ->
|
||||
it "assigns each span in the array to the corresponding entry", ->
|
||||
index.insert(0, [2, 1], ['a', 'b'])
|
||||
expect(index.indexForSpan(1).index).toBe 0
|
||||
expect(index.indexForSpan(2).index).toBe 1
|
||||
|
||||
describe "when called with a single number as the span", ->
|
||||
it "assigns that span to all entries", ->
|
||||
index.insert(0, 1, ['a', 'b'])
|
||||
expect(index.indexForSpan(0).index).toBe 0
|
||||
expect(index.indexForSpan(1).index).toBe 1
|
||||
|
||||
describe ".updateSpans(start, end, spans)", ->
|
||||
it "updates the spans of a range of entries indicated by the given index to the given value", ->
|
||||
index.insert(0, [3, 2, 3, 1, 2], ['a', 'b', 'c', 'd', 'e'])
|
||||
index.updateSpans(1, 3, 1)
|
||||
expect(index.spanForIndex(0)).toBe 3
|
||||
expect(index.spanForIndex(1)).toBe 4
|
||||
expect(index.spanForIndex(2)).toBe 5
|
||||
expect(index.spanForIndex(3)).toBe 6
|
||||
expect(index.spanForIndex(4)).toBe 8
|
||||
|
||||
describe ".sliceBySpan(start, end)", ->
|
||||
describe "when the index contains values that start and end evenly on the given start/end span indices", ->
|
||||
it "returns the spanning values with a start and end offset of 0", ->
|
||||
index.insert(0, [1, 2, 3, 1, 2], ['a', 'b', 'c', 'd', 'e'])
|
||||
{ values, startOffset, endOffset } = index.sliceBySpan(1, 6)
|
||||
|
||||
expect(values).toEqual ['b', 'c', 'd']
|
||||
expect(startOffset).toBe 0
|
||||
expect(endOffset).toBe 0
|
||||
|
||||
describe "when the index contains values that overlap the given start/end span indices", ->
|
||||
it "includes the overlapping values, assigning the start and end offsets to indicate where they overlap the desired span indices", ->
|
||||
index.insert(0, [3, 1, 1, 3, 1], ['a', 'b', 'c', 'd', 'e'])
|
||||
{ values, startOffset, endOffset } = index.sliceBySpan(1, 7)
|
||||
|
||||
expect(values).toEqual ['a', 'b', 'c', 'd']
|
||||
expect(startOffset).toBe 1
|
||||
expect(endOffset).toBe 2
|
||||
|
||||
index.clear()
|
||||
index.insert(0, [1, 4, 1], ['a', 'b', 'c'])
|
||||
{ values, startOffset, endOffset } = index.sliceBySpan(3, 4)
|
||||
|
||||
expect(values).toEqual ['b']
|
||||
expect(startOffset).toBe 2
|
||||
expect(endOffset).toBe 3
|
||||
|
||||
describe "when the index contains values with a span of 0", ->
|
||||
it "treats 0-spanning values as having no width", ->
|
||||
index.insert(0, [0, 0, 3, 2, 3, 1], ['a', 'b', 'c', 'd', 'e', 'f'])
|
||||
{ values, startOffset, endOffset } = index.sliceBySpan(1, 7)
|
||||
expect(values).toEqual ['c', 'd', 'e']
|
||||
expect(startOffset).toBe 1
|
||||
expect(endOffset).toBe 2
|
||||
|
||||
it "does not include 0-spanning values in the returned slice", ->
|
||||
index.insert(0, [3, 0, 2, 0, 3, 1], ['a', 'b', 'c', 'd', 'e', 'f'])
|
||||
{ values, startOffset, endOffset } = index.sliceBySpan(1, 7)
|
||||
expect(values).toEqual ['a', 'c', 'e']
|
||||
expect(startOffset).toBe 1
|
||||
expect(endOffset).toBe 2
|
||||
|
||||
describe ".lengthBySpan()", ->
|
||||
it "returns the sum the spans of all entries in the index", ->
|
||||
index.insert(0, [3, 0, 2, 0, 3, 1], ['a', 'b', 'c', 'd', 'e', 'f'])
|
||||
expect(index.lengthBySpan()).toBe 9
|
||||
|
||||
describe ".indexForSpan(span)", ->
|
||||
it "returns the index of the entry whose aggregated span meets or exceeds the given span, plus an offset", ->
|
||||
index.insert(0, [3, 0, 2, 1], ['a', 'b', 'c', 'd'])
|
||||
expect(index.indexForSpan(0)).toEqual(index: 0, offset: 0)
|
||||
expect(index.indexForSpan(2)).toEqual(index: 0, offset: 2)
|
||||
expect(index.indexForSpan(3)).toEqual(index: 2, offset: 0)
|
||||
expect(index.indexForSpan(4)).toEqual(index: 2, offset: 1)
|
||||
expect(index.indexForSpan(5)).toEqual(index: 3, offset: 0)
|
||||
|
||||
describe ".spanForIndex(index)", ->
|
||||
it "returns the aggregate of spans for all elements up to and including the given index", ->
|
||||
index.insert(0, [3, 0, 2, 0, 3, 1], ['a', 'b', 'c', 'd', 'e', 'f'])
|
||||
expect(index.spanForIndex(0)).toBe 3
|
||||
expect(index.spanForIndex(1)).toBe 3
|
||||
expect(index.spanForIndex(2)).toBe 5
|
||||
expect(index.spanForIndex(3)).toBe 5
|
||||
expect(index.spanForIndex(4)).toBe 8
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
_ = require 'underscore'
|
||||
|
||||
module.exports =
|
||||
class SpanIndex
|
||||
constructor: ->
|
||||
@entries = []
|
||||
|
||||
insert: (index, spans, values) ->
|
||||
@entries[index..index] = @buildIndexEntries(spans, values)
|
||||
|
||||
replace: (index, span, value) ->
|
||||
@splice(index, index, span, [value])
|
||||
|
||||
splice: (start, end, spans, values) ->
|
||||
@entries[start..end] = @buildIndexEntries(spans, values)
|
||||
|
||||
updateSpans: (start, end, span) ->
|
||||
for i in [start..end]
|
||||
@entries[i].span = span
|
||||
|
||||
at: (index) ->
|
||||
@entries[index].value
|
||||
|
||||
last: ->
|
||||
_.last(@entries).value
|
||||
|
||||
clear: ->
|
||||
@entries = []
|
||||
|
||||
lengthBySpan: ->
|
||||
length = 0
|
||||
for entry in @entries
|
||||
length += entry.span
|
||||
length
|
||||
|
||||
sliceBySpan: (start, end) ->
|
||||
currentSpan = 0
|
||||
values = []
|
||||
|
||||
for entry in @entries
|
||||
continue if entry.span is 0
|
||||
nextSpan = currentSpan + entry.span
|
||||
if nextSpan > start
|
||||
startOffset = start - currentSpan if currentSpan <= start
|
||||
if currentSpan <= end
|
||||
values.push entry.value
|
||||
endOffset = end - currentSpan if nextSpan >= end
|
||||
else
|
||||
break
|
||||
currentSpan = nextSpan
|
||||
|
||||
{ values, startOffset, endOffset }
|
||||
|
||||
|
||||
indexForSpan: (targetSpan) ->
|
||||
currentSpan = 0
|
||||
index = 0
|
||||
offset = 0
|
||||
for entry in @entries
|
||||
nextSpan = currentSpan + entry.span
|
||||
if nextSpan > targetSpan
|
||||
offset = targetSpan - currentSpan
|
||||
return { index, offset}
|
||||
currentSpan = nextSpan
|
||||
index++
|
||||
|
||||
spanForIndex: (index) ->
|
||||
span = 0
|
||||
for i in [0..index]
|
||||
span += @entries[i].span
|
||||
span
|
||||
|
||||
buildIndexEntries: (spans, values) ->
|
||||
if _.isArray(spans)
|
||||
_.zip(spans, values).map ([span, value]) -> new SpanIndexEntry(span, value)
|
||||
else
|
||||
values.map (value) -> new SpanIndexEntry(spans, value)
|
||||
|
||||
class SpanIndexEntry
|
||||
constructor: (@span, @value) ->
|
||||
|
||||
Reference in New Issue
Block a user