Merge Delta into Point

Point and delta were really pretty much the same thing. Point might need to be renamed… I'm thinking now it should be called offset, and have rows and columns instead of a row and column. Then you could interpret it as an offset from the beginning of the buffer, or an offset from any other location.
This commit is contained in:
Nathan Sobo
2012-02-23 15:38:03 -07:00
parent e8ba72c3ec
commit 43c66a02a4
7 changed files with 65 additions and 90 deletions

View File

@@ -2,7 +2,7 @@ LineMap = require 'line-map'
ScreenLineFragment = require 'screen-line-fragment'
Buffer = require 'buffer'
Highlighter = require 'highlighter'
Delta = require 'delta'
Point = require 'point'
describe "LineMap", ->
[highlighter, map] = []
@@ -154,7 +154,7 @@ describe "LineMap", ->
it "returns the concatenated screen line fragments that comprise the given buffer row", ->
line1Text = line1.text
[line1a, line1b] = line1.splitAt(11)
line1a.screenDelta = new Delta(1, 0)
line1a.screenDelta = new Point(1, 0)
map.insertAtBufferRow(0, [line0, line1a, line1b, line2])
@@ -166,12 +166,12 @@ describe "LineMap", ->
# line1a-line3b describes a fold
[line1a, line1b] = line1.splitAt(10)
[line3a, line3b] = line3.splitAt(20)
line1a.bufferDelta.rows = 2
line1a.bufferDelta.columns = 20
line1a.bufferDelta.row = 2
line1a.bufferDelta.column = 20
# line4a-line4b describes a wrapped line
[line4a, line4b] = line4.splitAt(20)
line4a.screenDelta = new Delta(1, 0)
line4a.screenDelta = new Point(1, 0)
map.insertAtBufferRow(0, [line0, line1a, line3b, line4a, line4b])
@@ -196,8 +196,8 @@ describe "LineMap", ->
it "returns the total of all inserted screen row deltas", ->
[line1a, line1b] = line1.splitAt(10)
[line3a, line3b] = line3.splitAt(10)
line1a.screenDelta = new Delta(1, 0)
line3a.screenDelta = new Delta(1, 0)
line1a.screenDelta = new Point(1, 0)
line3a.screenDelta = new Point(1, 0)
map.insertAtBufferRow(0, [line0, line1a, line1b, line2])

View File

@@ -1,39 +0,0 @@
Point = require 'point'
module.exports =
class Delta
@fromObject: (object) ->
if object instanceof Delta
object
else
new Delta(object[0], object[1])
constructor: (@rows=0, @columns=0) ->
add: (other) ->
debugger unless other
rows = @rows + other.rows
if other.rows == 0
columns = @columns + other.columns
else
columns = other.columns
new Delta(rows, columns)
splitAt: (column) ->
if @rows == 0
rightColumns = @columns - column
else
rightColumns = @columns
[new Delta(0, column), new Delta(@rows, rightColumns)]
inspect: ->
"(#{@rows}, #{@columns})"
isEqual: (other) ->
other = Delta.fromObject(other)
@rows == other.rows and @columns == other.columns
toPoint: ->
new Point(@rows, @columns)

View File

@@ -1,5 +1,4 @@
_ = require 'underscore'
Delta = require 'delta'
Point = require 'point'
Range = require 'range'
@@ -10,12 +9,12 @@ class LineMap
insertAtBufferRow: (bufferRow, screenLines) ->
screenLines = [screenLines] unless _.isArray(screenLines)
delta = new Delta
delta = new Point
insertIndex = 0
for screenLine in @screenLines
nextDelta = delta.add(screenLine.bufferDelta)
break if nextDelta.rows > bufferRow
break if nextDelta.row > bufferRow
delta = nextDelta
insertIndex++
@@ -31,12 +30,12 @@ class LineMap
stopRow = startRow + rowCount
startIndex = undefined
stopIndex = 0
delta = new Delta
delta = new Point
for screenLine, i in @screenLines
startIndex = i if delta.rows == startRow and not startIndex
startIndex = i if delta.row == startRow and not startIndex
nextDelta = delta.add(screenLine[deltaType])
break if nextDelta.rows > stopRow
break if nextDelta.row > stopRow
delta = nextDelta
stopIndex++
@@ -60,16 +59,16 @@ class LineMap
linesForScreenRows: (startRow, endRow) ->
lastLine = null
lines = []
delta = new Delta
delta = new Point
for fragment in @screenLines
break if delta.rows > endRow
if delta.rows >= startRow
break if delta.row > endRow
if delta.row >= startRow
if pendingFragment
pendingFragment = pendingFragment.concat(fragment)
else
pendingFragment = fragment
if pendingFragment.screenDelta.rows > 0
if pendingFragment.screenDelta.row > 0
lines.push pendingFragment
pendingFragment = null
delta = delta.add(fragment.screenDelta)
@@ -77,10 +76,10 @@ class LineMap
lineForBufferRow: (row) ->
line = null
delta = new Delta
delta = new Point
for fragment in @screenLines
break if delta.rows > row
if delta.rows == row
break if delta.row > row
if delta.row == row
if line
line = line.concat(fragment)
else
@@ -89,47 +88,47 @@ class LineMap
line
bufferLineCount: ->
delta = new Delta
delta = new Point
for screenLine in @screenLines
delta = delta.add(screenLine.bufferDelta)
delta.rows
delta.row
screenLineCount: ->
delta = new Delta
delta = new Point
for screenLine in @screenLines
delta = delta.add(screenLine.screenDelta)
delta.rows
delta.row
screenPositionForBufferPosition: (bufferPosition, eagerWrap=true) ->
bufferPosition = Point.fromObject(bufferPosition)
bufferDelta = new Delta
screenDelta = new Delta
bufferDelta = new Point
screenDelta = new Point
for screenLine in @screenLines
nextDelta = bufferDelta.add(screenLine.bufferDelta)
break if nextDelta.toPoint().greaterThan(bufferPosition)
break if nextDelta.toPoint().isEqual(bufferPosition) and not eagerWrap
break if nextDelta.isGreaterThan(bufferPosition)
break if nextDelta.isEqual(bufferPosition) and not eagerWrap
bufferDelta = nextDelta
screenDelta = screenDelta.add(screenLine.screenDelta)
remainingBufferColumns = bufferPosition.column - bufferDelta.columns
additionalScreenColumns = Math.max(0, Math.min(remainingBufferColumns, screenLine.lengthForClipping()))
remainingBufferColumn = bufferPosition.column - bufferDelta.column
additionalScreenColumn = Math.max(0, Math.min(remainingBufferColumn, screenLine.lengthForClipping()))
new Point(screenDelta.rows, screenDelta.columns + additionalScreenColumns)
new Point(screenDelta.row, screenDelta.column + additionalScreenColumn)
bufferPositionForScreenPosition: (screenPosition) ->
screenPosition = Point.fromObject(screenPosition)
bufferDelta = new Delta
screenDelta = new Delta
bufferDelta = new Point
screenDelta = new Point
for screenLine in @screenLines
nextDelta = screenDelta.add(screenLine.screenDelta)
break if nextDelta.toPoint().greaterThan(screenPosition)
break if nextDelta.isGreaterThan(screenPosition)
screenDelta = nextDelta
bufferDelta = bufferDelta.add(screenLine.bufferDelta)
columns = bufferDelta.columns + (screenPosition.column - screenDelta.columns)
new Point(bufferDelta.rows, columns)
column = bufferDelta.column + (screenPosition.column - screenDelta.column)
new Point(bufferDelta.row, column)
screenRangeForBufferRange: (bufferRange) ->
start = @screenPositionForBufferPosition(bufferRange.start)

View File

@@ -3,7 +3,6 @@ EventEmitter = require 'event-emitter'
LineMap = require 'line-map'
Point = require 'point'
Range = require 'range'
Delta = require 'delta'
module.exports =
class LineWrapper
@@ -55,7 +54,7 @@ class LineWrapper
endColumn = startColumn + screenLine.text.length
else
[leftHalf, rightHalf] = screenLine.splitAt(splitColumn)
leftHalf.screenDelta = new Delta(1, 0)
leftHalf.screenDelta = new Point(1, 0)
screenLines.push leftHalf
endColumn = startColumn + leftHalf.text.length
screenLines.push @wrapScreenLine(rightHalf, endColumn)...

View File

@@ -11,16 +11,25 @@ class Point
new Point(row, column)
constructor: (@row, @column) ->
constructor: (@row=0, @column=0) ->
isEqual: (other) ->
if other instanceof Array
@row == other[0] and @column == other[1]
add: (other) ->
debugger unless other
row = @row + other.row
if other.row == 0
column = @column + other.column
else
@row == other.row and @column == other.column
column = other.column
inspect: ->
"(#{@row}, #{@column})"
new Point(row, column)
splitAt: (column) ->
if @row == 0
rightColumn = @column - column
else
rightColumn = @column
[new Point(0, column), new Point(@row, rightColumn)]
compare: (other) ->
if @row > other.row
@@ -35,5 +44,13 @@ class Point
else
0
greaterThan: (other) ->
isEqual: (other) ->
other = Point.fromObject(other)
@compare(other) == 0
isGreaterThan: (other) ->
@compare(other) > 0
inspect: ->
"(#{@row}, #{@column})"

View File

@@ -1,5 +1,4 @@
Point = require 'point'
Delta = require 'delta'
_ = require 'underscore'
@@ -37,5 +36,5 @@ class Range
columns = @end.column - @start.column
else
columns = @end.column
new Delta(rows, columns)
new Point(rows, columns)

View File

@@ -1,13 +1,13 @@
_ = require 'underscore'
Delta = require 'delta'
Point = require 'point'
module.exports =
class ScreenLineFragment
isAtomic: false
constructor: (@tokens, @text, screenDelta, bufferDelta, extraFields) ->
@screenDelta = Delta.fromObject(screenDelta)
@bufferDelta = Delta.fromObject(bufferDelta)
@screenDelta = Point.fromObject(screenDelta)
@bufferDelta = Point.fromObject(bufferDelta)
_.extend(this, extraFields)
splitAt: (column) ->