Add clipScreenPosition to LineFolder & LineMap

This commit is contained in:
Nathan Sobo
2012-02-24 15:35:28 -07:00
parent b8ef7685de
commit 33ff32f9a4
3 changed files with 38 additions and 1 deletions

View File

@@ -32,7 +32,6 @@ describe "LineFolder", ->
[[range]] = unfoldHandler.argsForCall
expect(range).toEqual foldRange
describe "when there is a single fold spanning multiple lines", ->
it "replaces folded lines with a single line containing a placeholder and emits a change event", ->
[line4, line5] = folder.linesForScreenRows(4, 5)
@@ -313,3 +312,23 @@ describe "LineFolder", ->
expect(folder.bufferPositionForScreenPosition([4, 5])).toEqual [4, 5]
expect(folder.bufferPositionForScreenPosition([4, 13])).toEqual [4, 15]
expect(folder.bufferPositionForScreenPosition([4, 18])).toEqual [4, 20]
describe ".clipScreenPosition(screenPosition)", ->
beforeEach ->
folder.createFold(new Range([4, 29], [7, 4]))
it "returns the nearest valid position based on the current screen lines", ->
expect(folder.clipScreenPosition([-1, -1])).toEqual [0, 0]
expect(folder.clipScreenPosition([0, -1])).toEqual [0, 0]
expect(folder.clipScreenPosition([1, 10000])).toEqual [1, 30]
expect(folder.clipScreenPosition([2, 15])).toEqual [2, 15]
expect(folder.clipScreenPosition([4, 32])).toEqual [4, 32]
expect(folder.clipScreenPosition([4, 1000])).toEqual [4, 33]
expect(folder.clipScreenPosition([1000, 1000])).toEqual [10, 2]
it "clips positions inside a placeholder to the beginning of the placeholder", ->
expect(folder.clipScreenPosition([4, 30])).toEqual [4, 29]
expect(folder.clipScreenPosition([4, 31])).toEqual [4, 29]

View File

@@ -136,6 +136,9 @@ class LineFolder
bufferPositionForScreenPosition: (screenPosition) ->
@lineMap.bufferPositionForScreenPosition(screenPosition)
clipScreenPosition: (screenPosition) ->
@lineMap.clipScreenPosition(screenPosition)
screenRangeForBufferRange: (bufferRange) ->
@lineMap.screenRangeForBufferRange(bufferRange)

View File

@@ -135,3 +135,18 @@ class LineMap
end = @screenPositionForBufferPosition(bufferRange.end)
new Range(start, end)
clipScreenPosition: (screenPosition) ->
screenPosition = Point.fromObject(screenPosition)
screenPosition = new Point(Math.max(0, screenPosition.row), Math.max(0, screenPosition.column))
screenDelta = new Point
for screenLine in @screenLines
nextDelta = screenDelta.add(screenLine.screenDelta)
break if nextDelta.isGreaterThan(screenPosition)
screenDelta = nextDelta
maxColumn = screenDelta.column + screenLine.lengthForClipping()
screenDelta.column = Math.min(maxColumn, screenPosition.column)
screenDelta