super 💄

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-02-08 12:45:02 -07:00
parent e24b5fbc24
commit 1ec29e29d7
2 changed files with 40 additions and 36 deletions

View File

@ -17,7 +17,7 @@ fdescribe "LineWrapper", ->
expect(line.length).toBeLessThan(50)
segments = wrapper.segmentsForRow(0)
expect(segments.length).toBe 1
expect(segments[0].lastIndex).toBe line.length
expect(segments[0].endColumn).toBe line.length
describe "when the line needs to wrap once", ->
it "breaks the line into 2 segments at the beginning of the first word that exceeds the max length", ->
@ -25,10 +25,10 @@ fdescribe "LineWrapper", ->
expect(line.length).toBeGreaterThan 50
segments = wrapper.segmentsForRow(6)
expect(segments.length).toBe 2
expect(segments[0].lastIndex).toBe 45
expect(segments[0].endColumn).toBe 45
expect(segments[0].map((t) -> t.value).join('')).toBe ' current < pivot ? left.push(current) : '
expect(segments[1].lastIndex).toBe 65
expect(segments[1].endColumn).toBe 65
expect(segments[1].map((t) -> t.value).join('')).toBe 'right.push(current);'
describe "when the line needs to wrap more than once", ->
@ -38,44 +38,44 @@ fdescribe "LineWrapper", ->
expect(segments.length).toBe 3
expect(segments[0].lastIndex).toBe 24
expect(segments[0].endColumn).toBe 24
expect(_.pluck(segments[0], 'value').join('')).toBe ' current < pivot ? '
expect(segments[1].lastIndex).toBe 45
expect(segments[1].endColumn).toBe 45
expect(_.pluck(segments[1], 'value').join('')).toBe 'left.push(current) : '
expect(segments[2].lastIndex).toBe 65
expect(segments[2].endColumn).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])
xdescribe ".tokensForScreenRow(row)", ->
it "returns tokens for the line fragment corresponding to the given screen row", ->
expect(buffer.tokensForScreenRow(3)).toEqual(wrapper.segmentsForRow(3)[0])
expect(buffer.tokensForScreenRow(4)).toEqual(wrapper.segmentsForRow(3)[1])
expect(buffer.tokensForScreenRow(5)).toEqual(wrapper.segmentsForRow(4)[0])
describe ".displayPositionFromBufferPosition(point)", ->
it "translates the given buffer position to a display position, accounting for wrapped lines", ->
describe ".screenPositionFromBufferPosition(point)", ->
it "translates the given buffer position to a screen position, accounting for wrapped lines", ->
# before any wrapped lines
expect(wrapper.displayPositionFromBufferPosition(row: 0, column: 5)).toEqual(row: 0, column: 5)
expect(wrapper.screenPositionFromBufferPosition([0, 5])).toEqual([0, 5])
# 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)
expect(wrapper.screenPositionFromBufferPosition([3, 5])).toEqual([3, 5])
expect(wrapper.screenPositionFromBufferPosition([3, 50])).toEqual([3, 50])
expect(wrapper.screenPositionFromBufferPosition([3, 51])).toEqual([4, 0])
# following a wrapped line
expect(wrapper.displayPositionFromBufferPosition(row: 4, column: 5)).toEqual(row: 5, column: 5)
expect(wrapper.screenPositionFromBufferPosition([4, 5])).toEqual([5, 5])
describe ".bufferPositionFromDisplayPosition(point)", ->
it "translates the given display position to a buffer position, account for wrapped lines", ->
describe ".bufferPositionFromScreenPosition(point)", ->
it "translates the given screen 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)
expect(wrapper.bufferPositionFromScreenPosition([0, 5])).toEqual([0, 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)
expect(wrapper.bufferPositionFromScreenPosition([3, 5])).toEqual([3, 5])
expect(wrapper.bufferPositionFromScreenPosition([4, 0])).toEqual([3, 51])
expect(wrapper.bufferPositionFromScreenPosition([4, 5])).toEqual([3, 56])
# following a wrapped line
expect(wrapper.bufferPositionFromDisplayPosition(row: 5, column: 5)).toEqual(row: 4, column: 5)
expect(wrapper.bufferPositionFromScreenPosition([5, 5])).toEqual([4, 5])

View File

@ -1,3 +1,4 @@
Point = require 'point'
getWordRegex = -> /\b[^\s]+/g
module.exports =
@ -35,45 +36,48 @@ class LineWrapper
currentSegment = []
currentSegment.startColumn = 0
currentSegment.lastIndex = 0
currentSegment.endColumn = 0
currentSegment.textLength = 0
segments = [currentSegment]
nextBreak = breakIndices.shift()
for token in @highlighter.tokensForRow(row)
if currentSegment.lastIndex >= nextBreak
if currentSegment.endColumn >= nextBreak
nextBreak = breakIndices.shift()
newSegment = []
newSegment.startColumn = currentSegment.lastIndex
newSegment.lastIndex = currentSegment.lastIndex
newSegment.startColumn = currentSegment.endColumn
newSegment.endColumn = currentSegment.endColumn
newSegment.textLength = 0
segments.push(newSegment)
currentSegment = newSegment
currentSegment.push token
currentSegment.lastIndex += token.value.length
currentSegment.endColumn += token.value.length
currentSegment.textLength += token.value.length
segments
displayPositionFromBufferPosition: (bufferPosition) ->
screenPositionFromBufferPosition: (bufferPosition) ->
bufferPosition = Point.fromObject(bufferPosition)
row = 0
for segments in @lines[0...bufferPosition.row]
row += segments.length
column = bufferPosition.column
for segment in @lines[bufferPosition.row]
break if segment.lastIndex > bufferPosition.column
break if segment.endColumn > bufferPosition.column
column -= segment.textLength
row++
{ row, column }
new Point(row, column)
bufferPositionFromDisplayPosition: (displayPosition) ->
bufferPositionFromScreenPosition: (screenPosition) ->
screenPosition = Point.fromObject(screenPosition)
bufferRow = 0
currentScreenRow = 0
for screenLines in @lines
for screenLine in screenLines
if currentScreenRow == displayPosition.row
return { row: bufferRow, column: screenLine.startColumn + displayPosition.column }
if currentScreenRow == screenPosition.row
return new Point(bufferRow, screenLine.startColumn + screenPosition.column)
currentScreenRow++
bufferRow++