From 785b800d5bd61619fa18f85bf8cec59fb3521112 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 9 Feb 2012 14:19:47 -0800 Subject: [PATCH] Add allowEOL param to screenPositionFromBufferPosition --- spec/atom/line-wrapper-spec.coffee | 13 +++++++++++-- src/atom/line-wrapper.coffee | 13 ++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spec/atom/line-wrapper-spec.coffee b/spec/atom/line-wrapper-spec.coffee index 1ac9317bd..b7dce318f 100644 --- a/spec/atom/line-wrapper-spec.coffee +++ b/spec/atom/line-wrapper-spec.coffee @@ -174,19 +174,28 @@ fdescribe "LineWrapper", -> expect(line2.endColumn).toBe 14 expect(line2.textLength).toBe 3 - describe ".screenPositionFromBufferPosition(point)", -> + describe ".screenPositionFromBufferPosition(point, allowEOL=false)", -> 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]) # on a wrapped line expect(wrapper.screenPositionFromBufferPosition([3, 5])).toEqual([3, 5]) expect(wrapper.screenPositionFromBufferPosition([3, 50])).toEqual([3, 50]) - expect(wrapper.screenPositionFromBufferPosition([3, 51])).toEqual([4, 0]) + expect(wrapper.screenPositionFromBufferPosition([3, 62])).toEqual([4, 11]) # following a wrapped line expect(wrapper.screenPositionFromBufferPosition([4, 5])).toEqual([5, 5]) + describe "when allowEOL is true", -> + it "preserves a position at the end of a wrapped screen line ", -> + expect(wrapper.screenPositionFromBufferPosition([3, 51], true)).toEqual([3, 51]) + + describe "when allowEOL is false", -> + 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]) + describe ".bufferPositionFromScreenPosition(point)", -> it "translates the given screen position to a buffer position, account for wrapped lines", -> # before any wrapped lines diff --git a/src/atom/line-wrapper.coffee b/src/atom/line-wrapper.coffee index 3f3611a2f..a56f9f540 100644 --- a/src/atom/line-wrapper.coffee +++ b/src/atom/line-wrapper.coffee @@ -91,15 +91,22 @@ class LineWrapper end = @screenPositionFromBufferPosition(bufferRange.end) new Range(start,end) - screenPositionFromBufferPosition: (bufferPosition) -> + screenPositionFromBufferPosition: (bufferPosition, allowEOL=false) -> bufferPosition = Point.fromObject(bufferPosition) row = 0 for wrappedLine in @wrappedLines[0...bufferPosition.row] row += wrappedLine.screenLines.length column = bufferPosition.column - for screenLine in @wrappedLines[bufferPosition.row].screenLines - break if screenLine.endColumn > bufferPosition.column + + screenLines = @wrappedLines[bufferPosition.row].screenLines + 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.textLength row++