Add LineWrapper.displayPositionFromBufferPosition

This commit is contained in:
Nathan Sobo 2012-02-08 12:30:46 -07:00
parent bb72c839da
commit e24b5fbc24
3 changed files with 35 additions and 5 deletions

View File

@ -50,7 +50,7 @@ describe "Editor", ->
buffer.insert([1,0], "/*")
expect(editor.lines.find('.line:eq(2) span:eq(0)')).toMatchSelector '.comment'
fdescribe "when soft-wrap is enabled", ->
describe "when soft-wrap is enabled", ->
beforeEach ->
editor.attachToDom()
editor.width(editor.charWidth * 50)

View File

@ -47,17 +47,35 @@ fdescribe "LineWrapper", ->
expect(segments[2].lastIndex).toBe 65
expect(_.pluck(segments[2], 'value').join('')).toBe 'right.push(current);'
xdescribe ".tokensForDisplayRow(row)", ->
it "returns tokens for the line fragment corresponding to the given display row", ->
expect(buffer.tokensForDisplayRow(3)).toEqual(wrapper.segmentsForRow(3)[0])
expect(buffer.tokensForDisplayRow(4)).toEqual(wrapper.segmentsForRow(3)[1])
expect(buffer.tokensForDisplayRow(5)).toEqual(wrapper.segmentsForRow(4)[0])
describe ".displayPositionFromBufferPosition(point)", ->
it "returns the position of the indicated row and column on screen, accounting for wrapped lines", ->
# no wrap
it "translates the given buffer position to a display position, accounting for wrapped lines", ->
# before any wrapped lines
expect(wrapper.displayPositionFromBufferPosition(row: 0, column: 5)).toEqual(row: 0, column: 5)
# wrap once
# on a wrapped line
expect(wrapper.displayPositionFromBufferPosition(row: 3, column: 5)).toEqual(row: 3, column: 5)
expect(wrapper.displayPositionFromBufferPosition(row: 3, column: 50)).toEqual(row: 3, column: 50)
expect(wrapper.displayPositionFromBufferPosition(row: 3, column: 51)).toEqual(row: 4, column: 0)
# following a wrap
# following a wrapped line
expect(wrapper.displayPositionFromBufferPosition(row: 4, column: 5)).toEqual(row: 5, column: 5)
describe ".bufferPositionFromDisplayPosition(point)", ->
it "translates the given display position to a buffer position, account for wrapped lines", ->
# before any wrapped lines
expect(wrapper.bufferPositionFromDisplayPosition(row: 0, column: 5)).toEqual(row: 0, column: 5)
# on a wrapped line
expect(wrapper.bufferPositionFromDisplayPosition(row: 3, column: 5)).toEqual(row: 3, column: 5)
expect(wrapper.bufferPositionFromDisplayPosition(row: 4, column: 0)).toEqual(row: 3, column: 51)
expect(wrapper.bufferPositionFromDisplayPosition(row: 4, column: 5)).toEqual(row: 3, column: 56)
# following a wrapped line
expect(wrapper.bufferPositionFromDisplayPosition(row: 5, column: 5)).toEqual(row: 4, column: 5)

View File

@ -34,6 +34,7 @@ class LineWrapper
lastBreakIndex = startIndex
currentSegment = []
currentSegment.startColumn = 0
currentSegment.lastIndex = 0
currentSegment.textLength = 0
segments = [currentSegment]
@ -42,6 +43,7 @@ class LineWrapper
if currentSegment.lastIndex >= nextBreak
nextBreak = breakIndices.shift()
newSegment = []
newSegment.startColumn = currentSegment.lastIndex
newSegment.lastIndex = currentSegment.lastIndex
newSegment.textLength = 0
segments.push(newSegment)
@ -65,3 +67,13 @@ class LineWrapper
{ row, column }
bufferPositionFromDisplayPosition: (displayPosition) ->
bufferRow = 0
currentScreenRow = 0
for screenLines in @lines
for screenLine in screenLines
if currentScreenRow == displayPosition.row
return { row: bufferRow, column: screenLine.startColumn + displayPosition.column }
currentScreenRow++
bufferRow++