From 43c66a02a4f848cb7e5fb15d303c19377fbaf15c Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 23 Feb 2012 15:38:03 -0700 Subject: [PATCH] Merge Delta into Point MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- spec/atom/line-map-spec.coffee | 14 +++---- src/atom/delta.coffee | 39 ------------------- src/atom/line-map.coffee | 57 ++++++++++++++-------------- src/atom/line-wrapper.coffee | 3 +- src/atom/point.coffee | 33 ++++++++++++---- src/atom/range.coffee | 3 +- src/atom/screen-line-fragment.coffee | 6 +-- 7 files changed, 65 insertions(+), 90 deletions(-) delete mode 100644 src/atom/delta.coffee diff --git a/spec/atom/line-map-spec.coffee b/spec/atom/line-map-spec.coffee index 5868510ae..517e16628 100644 --- a/spec/atom/line-map-spec.coffee +++ b/spec/atom/line-map-spec.coffee @@ -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]) diff --git a/src/atom/delta.coffee b/src/atom/delta.coffee deleted file mode 100644 index 867614bbb..000000000 --- a/src/atom/delta.coffee +++ /dev/null @@ -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) diff --git a/src/atom/line-map.coffee b/src/atom/line-map.coffee index 2d0c340f8..4fa2dea28 100644 --- a/src/atom/line-map.coffee +++ b/src/atom/line-map.coffee @@ -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) diff --git a/src/atom/line-wrapper.coffee b/src/atom/line-wrapper.coffee index 0845fa319..16ee097a4 100644 --- a/src/atom/line-wrapper.coffee +++ b/src/atom/line-wrapper.coffee @@ -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)... diff --git a/src/atom/point.coffee b/src/atom/point.coffee index 85521baff..d18d3affe 100644 --- a/src/atom/point.coffee +++ b/src/atom/point.coffee @@ -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})" + diff --git a/src/atom/range.coffee b/src/atom/range.coffee index 0faa9992b..2a8254f9a 100644 --- a/src/atom/range.coffee +++ b/src/atom/range.coffee @@ -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) diff --git a/src/atom/screen-line-fragment.coffee b/src/atom/screen-line-fragment.coffee index 994ff3c7b..7ac5d9987 100644 --- a/src/atom/screen-line-fragment.coffee +++ b/src/atom/screen-line-fragment.coffee @@ -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) ->