Add allowEOL param to screenPositionFromBufferPosition

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-02-09 14:19:47 -08:00
parent 07d0bc400f
commit 785b800d5b
2 changed files with 21 additions and 5 deletions

View File

@@ -174,19 +174,28 @@ fdescribe "LineWrapper", ->
expect(line2.endColumn).toBe 14
expect(line2.textLength).toBe 3
describe ".screenPositionFromBufferPosition(point)", ->
describe ".screenPositionFromBufferPosition(point, allowEOL=false)", ->
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])
# on a wrapped line
expect(wrapper.screenPositionFromBufferPosition([3, 5])).toEqual([3, 5])
expect(wrapper.screenPositionFromBufferPosition([3, 50])).toEqual([3, 50])
expect(wrapper.screenPositionFromBufferPosition([3, 51])).toEqual([4, 0])
expect(wrapper.screenPositionFromBufferPosition([3, 62])).toEqual([4, 11])
# following a wrapped line
expect(wrapper.screenPositionFromBufferPosition([4, 5])).toEqual([5, 5])
describe "when allowEOL is true", ->
it "preserves a position at the end of a wrapped screen line ", ->
expect(wrapper.screenPositionFromBufferPosition([3, 51], true)).toEqual([3, 51])
describe "when allowEOL is false", ->
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])
describe ".bufferPositionFromScreenPosition(point)", ->
it "translates the given screen position to a buffer position, account for wrapped lines", ->
# before any wrapped lines

View File

@@ -91,15 +91,22 @@ class LineWrapper
end = @screenPositionFromBufferPosition(bufferRange.end)
new Range(start,end)
screenPositionFromBufferPosition: (bufferPosition) ->
screenPositionFromBufferPosition: (bufferPosition, allowEOL=false) ->
bufferPosition = Point.fromObject(bufferPosition)
row = 0
for wrappedLine in @wrappedLines[0...bufferPosition.row]
row += wrappedLine.screenLines.length
column = bufferPosition.column
for screenLine in @wrappedLines[bufferPosition.row].screenLines
break if screenLine.endColumn > bufferPosition.column
screenLines = @wrappedLines[bufferPosition.row].screenLines
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.textLength
row++