Use LineMap in LineWrapper.screenPositionForBufferPosition

Also rename from screenPositionFrom… to screenPositionFor… and rename allowEOL parameter (which defaults to false) to eagerWrap (which defaults to true).
This commit is contained in:
Nathan Sobo
2012-02-20 22:14:59 -07:00
parent a05c0e077f
commit 8c810bbbcf
3 changed files with 18 additions and 33 deletions

View File

@@ -100,27 +100,27 @@ describe "LineWrapper", ->
expect(event.oldRange).toEqual([[0, 0], [15, 2]])
expect(event.newRange).toEqual([[0, 0], [18, 2]])
describe ".screenPositionFromBufferPosition(point, allowEOL=false)", ->
describe ".screenPositionForBufferPosition(point, eagerWrap=true)", ->
it "translates the given buffer position to a screen position, accounting for wrapped lines", ->
# before any wrapped lines
expect(wrapper.screenPositionFromBufferPosition([0, 5])).toEqual([0, 5])
expect(wrapper.screenPositionFromBufferPosition([0, 29])).toEqual([0, 29])
expect(wrapper.screenPositionForBufferPosition([0, 5])).toEqual([0, 5])
expect(wrapper.screenPositionForBufferPosition([0, 29])).toEqual([0, 29])
# on a wrapped line
expect(wrapper.screenPositionFromBufferPosition([3, 5])).toEqual([3, 5])
expect(wrapper.screenPositionFromBufferPosition([3, 50])).toEqual([3, 50])
expect(wrapper.screenPositionFromBufferPosition([3, 62])).toEqual([4, 11])
expect(wrapper.screenPositionForBufferPosition([3, 5])).toEqual([3, 5])
expect(wrapper.screenPositionForBufferPosition([3, 50])).toEqual([3, 50])
expect(wrapper.screenPositionForBufferPosition([3, 62])).toEqual([4, 11])
# following a wrapped line
expect(wrapper.screenPositionFromBufferPosition([4, 5])).toEqual([5, 5])
expect(wrapper.screenPositionForBufferPosition([4, 5])).toEqual([5, 5])
describe "when allowEOL is true", ->
describe "when eagerWrap is false", ->
it "preserves a position at the end of a wrapped screen line ", ->
expect(wrapper.screenPositionFromBufferPosition([3, 51], true)).toEqual([3, 51])
expect(wrapper.screenPositionForBufferPosition([3, 51], false)).toEqual([3, 51])
describe "when allowEOL is false", ->
describe "when eagerWrap is true", ->
it "translates a position at the end of a wrapped screen line to the begining of the next screen line", ->
expect(wrapper.screenPositionFromBufferPosition([3, 51])).toEqual([4, 0])
expect(wrapper.screenPositionForBufferPosition([3, 51], true)).toEqual([4, 0])
describe ".bufferPositionFromScreenPosition(point)", ->
it "translates the given screen position to a buffer position, account for wrapped lines", ->

View File

@@ -209,7 +209,7 @@ class Editor extends View
new Point(row, column)
pixelPositionFromPoint: (position) ->
{ row, column } = @lineWrapper.screenPositionFromBufferPosition(position)
{ row, column } = @lineWrapper.screenPositionForBufferPosition(position)
{ top: row * @lineHeight, left: column * @charWidth }
pointFromPixelPosition: ({top, left}) ->

View File

@@ -56,10 +56,10 @@ class LineWrapper
@trigger 'change', { oldRange, newRange }
firstScreenRowForBufferRow: (bufferRow) ->
@screenPositionFromBufferPosition([bufferRow, 0]).row
@screenPositionForBufferPosition([bufferRow, 0]).row
lastScreenRowForBufferRow: (bufferRow) ->
startRow = @screenPositionFromBufferPosition([bufferRow, 0]).row
startRow = @screenPositionForBufferPosition([bufferRow, 0]).row
startRow + (@index.at(bufferRow).screenLines.length - 1)
buildWrappedLinesForBufferRows: (start, end) ->
@@ -101,27 +101,12 @@ class LineWrapper
return @maxLength
screenRangeFromBufferRange: (bufferRange) ->
start = @screenPositionFromBufferPosition(bufferRange.start, true)
end = @screenPositionFromBufferPosition(bufferRange.end, true)
start = @screenPositionForBufferPosition(bufferRange.start, false)
end = @screenPositionForBufferPosition(bufferRange.end, false)
new Range(start,end)
screenPositionFromBufferPosition: (bufferPosition, allowEOL=false) ->
bufferPosition = Point.fromObject(bufferPosition)
screenLines = @index.at(bufferPosition.row).screenLines
row = @index.spanForIndex(bufferPosition.row) - screenLines.length
column = bufferPosition.column
for screenLine, index in screenLines
break if index == screenLines.length - 1
if allowEOL
break if screenLine.endColumn >= bufferPosition.column
else
break if screenLine.endColumn > bufferPosition.column
column -= screenLine.text.length
row++
new Point(row, column)
screenPositionForBufferPosition: (bufferPosition, eagerWrap=true) ->
return @lineMap.screenPositionForBufferPosition(bufferPosition, eagerWrap)
bufferPositionFromScreenPosition: (screenPosition) ->
screenPosition = Point.fromObject(screenPosition)