From 8c810bbbcf63be0e54b35fd20d9518d80ea57e8d Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 20 Feb 2012 22:14:59 -0700 Subject: [PATCH] Use LineMap in LineWrapper.screenPositionForBufferPosition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also rename from screenPositionFrom… to screenPositionFor… and rename allowEOL parameter (which defaults to false) to eagerWrap (which defaults to true). --- spec/atom/line-wrapper-spec.coffee | 22 +++++++++++----------- src/atom/editor.coffee | 2 +- src/atom/line-wrapper.coffee | 27 ++++++--------------------- 3 files changed, 18 insertions(+), 33 deletions(-) diff --git a/spec/atom/line-wrapper-spec.coffee b/spec/atom/line-wrapper-spec.coffee index b3f4df101..76467b249 100644 --- a/spec/atom/line-wrapper-spec.coffee +++ b/spec/atom/line-wrapper-spec.coffee @@ -100,27 +100,27 @@ describe "LineWrapper", -> expect(event.oldRange).toEqual([[0, 0], [15, 2]]) expect(event.newRange).toEqual([[0, 0], [18, 2]]) - describe ".screenPositionFromBufferPosition(point, allowEOL=false)", -> + describe ".screenPositionForBufferPosition(point, eagerWrap=true)", -> 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]) + expect(wrapper.screenPositionForBufferPosition([0, 5])).toEqual([0, 5]) + expect(wrapper.screenPositionForBufferPosition([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, 62])).toEqual([4, 11]) + expect(wrapper.screenPositionForBufferPosition([3, 5])).toEqual([3, 5]) + expect(wrapper.screenPositionForBufferPosition([3, 50])).toEqual([3, 50]) + expect(wrapper.screenPositionForBufferPosition([3, 62])).toEqual([4, 11]) # following a wrapped line - expect(wrapper.screenPositionFromBufferPosition([4, 5])).toEqual([5, 5]) + expect(wrapper.screenPositionForBufferPosition([4, 5])).toEqual([5, 5]) - describe "when allowEOL is true", -> + describe "when eagerWrap is false", -> it "preserves a position at the end of a wrapped screen line ", -> - expect(wrapper.screenPositionFromBufferPosition([3, 51], true)).toEqual([3, 51]) + expect(wrapper.screenPositionForBufferPosition([3, 51], false)).toEqual([3, 51]) - describe "when allowEOL is false", -> + describe "when eagerWrap is true", -> 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]) + expect(wrapper.screenPositionForBufferPosition([3, 51], true)).toEqual([4, 0]) describe ".bufferPositionFromScreenPosition(point)", -> it "translates the given screen position to a buffer position, account for wrapped lines", -> diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index dbc509554..9896c796b 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -209,7 +209,7 @@ class Editor extends View new Point(row, column) pixelPositionFromPoint: (position) -> - { row, column } = @lineWrapper.screenPositionFromBufferPosition(position) + { row, column } = @lineWrapper.screenPositionForBufferPosition(position) { top: row * @lineHeight, left: column * @charWidth } pointFromPixelPosition: ({top, left}) -> diff --git a/src/atom/line-wrapper.coffee b/src/atom/line-wrapper.coffee index 9c5700deb..b3216d3d0 100644 --- a/src/atom/line-wrapper.coffee +++ b/src/atom/line-wrapper.coffee @@ -56,10 +56,10 @@ class LineWrapper @trigger 'change', { oldRange, newRange } firstScreenRowForBufferRow: (bufferRow) -> - @screenPositionFromBufferPosition([bufferRow, 0]).row + @screenPositionForBufferPosition([bufferRow, 0]).row lastScreenRowForBufferRow: (bufferRow) -> - startRow = @screenPositionFromBufferPosition([bufferRow, 0]).row + startRow = @screenPositionForBufferPosition([bufferRow, 0]).row startRow + (@index.at(bufferRow).screenLines.length - 1) buildWrappedLinesForBufferRows: (start, end) -> @@ -101,27 +101,12 @@ class LineWrapper return @maxLength screenRangeFromBufferRange: (bufferRange) -> - start = @screenPositionFromBufferPosition(bufferRange.start, true) - end = @screenPositionFromBufferPosition(bufferRange.end, true) + start = @screenPositionForBufferPosition(bufferRange.start, false) + end = @screenPositionForBufferPosition(bufferRange.end, false) new Range(start,end) - screenPositionFromBufferPosition: (bufferPosition, allowEOL=false) -> - bufferPosition = Point.fromObject(bufferPosition) - screenLines = @index.at(bufferPosition.row).screenLines - row = @index.spanForIndex(bufferPosition.row) - screenLines.length - column = bufferPosition.column - - 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.text.length - row++ - - new Point(row, column) + screenPositionForBufferPosition: (bufferPosition, eagerWrap=true) -> + return @lineMap.screenPositionForBufferPosition(bufferPosition, eagerWrap) bufferPositionFromScreenPosition: (screenPosition) -> screenPosition = Point.fromObject(screenPosition)