From 33ff32f9a46531f7ff233247c151bf93e3f36675 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 24 Feb 2012 15:35:28 -0700 Subject: [PATCH] Add clipScreenPosition to LineFolder & LineMap --- spec/atom/line-folder-spec.coffee | 21 ++++++++++++++++++++- src/atom/line-folder.coffee | 3 +++ src/atom/line-map.coffee | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/spec/atom/line-folder-spec.coffee b/spec/atom/line-folder-spec.coffee index 7a7f2703e..7c9d26f90 100644 --- a/spec/atom/line-folder-spec.coffee +++ b/spec/atom/line-folder-spec.coffee @@ -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] + + + + diff --git a/src/atom/line-folder.coffee b/src/atom/line-folder.coffee index bda1a3979..d098df11a 100644 --- a/src/atom/line-folder.coffee +++ b/src/atom/line-folder.coffee @@ -136,6 +136,9 @@ class LineFolder bufferPositionForScreenPosition: (screenPosition) -> @lineMap.bufferPositionForScreenPosition(screenPosition) + clipScreenPosition: (screenPosition) -> + @lineMap.clipScreenPosition(screenPosition) + screenRangeForBufferRange: (bufferRange) -> @lineMap.screenRangeForBufferRange(bufferRange) diff --git a/src/atom/line-map.coffee b/src/atom/line-map.coffee index 4fa2dea28..b63b14b7d 100644 --- a/src/atom/line-map.coffee +++ b/src/atom/line-map.coffee @@ -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 +