From e24b5fbc2460a3b78e2480f96bd29407f153e28b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 8 Feb 2012 12:30:46 -0700 Subject: [PATCH] Add LineWrapper.displayPositionFromBufferPosition --- spec/atom/editor-spec.coffee | 2 +- spec/atom/line-wrapper-spec.coffee | 26 ++++++++++++++++++++++---- src/atom/line-wrapper.coffee | 12 ++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 6ddeb567b..111926368 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -50,7 +50,7 @@ describe "Editor", -> buffer.insert([1,0], "/*") expect(editor.lines.find('.line:eq(2) span:eq(0)')).toMatchSelector '.comment' - fdescribe "when soft-wrap is enabled", -> + describe "when soft-wrap is enabled", -> beforeEach -> editor.attachToDom() editor.width(editor.charWidth * 50) diff --git a/spec/atom/line-wrapper-spec.coffee b/spec/atom/line-wrapper-spec.coffee index fd24b9d3f..8b1d5623f 100644 --- a/spec/atom/line-wrapper-spec.coffee +++ b/spec/atom/line-wrapper-spec.coffee @@ -47,17 +47,35 @@ fdescribe "LineWrapper", -> expect(segments[2].lastIndex).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]) + describe ".displayPositionFromBufferPosition(point)", -> - it "returns the position of the indicated row and column on screen, accounting for wrapped lines", -> - # no wrap + it "translates the given buffer position to a display position, accounting for wrapped lines", -> + # before any wrapped lines expect(wrapper.displayPositionFromBufferPosition(row: 0, column: 5)).toEqual(row: 0, column: 5) - # wrap once + # 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) - # following a wrap + # following a wrapped line expect(wrapper.displayPositionFromBufferPosition(row: 4, column: 5)).toEqual(row: 5, column: 5) + describe ".bufferPositionFromDisplayPosition(point)", -> + it "translates the given display 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) + + # 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) + + # following a wrapped line + expect(wrapper.bufferPositionFromDisplayPosition(row: 5, column: 5)).toEqual(row: 4, column: 5) diff --git a/src/atom/line-wrapper.coffee b/src/atom/line-wrapper.coffee index 508c734a7..4bd43396e 100644 --- a/src/atom/line-wrapper.coffee +++ b/src/atom/line-wrapper.coffee @@ -34,6 +34,7 @@ class LineWrapper lastBreakIndex = startIndex currentSegment = [] + currentSegment.startColumn = 0 currentSegment.lastIndex = 0 currentSegment.textLength = 0 segments = [currentSegment] @@ -42,6 +43,7 @@ class LineWrapper if currentSegment.lastIndex >= nextBreak nextBreak = breakIndices.shift() newSegment = [] + newSegment.startColumn = currentSegment.lastIndex newSegment.lastIndex = currentSegment.lastIndex newSegment.textLength = 0 segments.push(newSegment) @@ -65,3 +67,13 @@ class LineWrapper { row, column } + + bufferPositionFromDisplayPosition: (displayPosition) -> + bufferRow = 0 + currentScreenRow = 0 + for screenLines in @lines + for screenLine in screenLines + if currentScreenRow == displayPosition.row + return { row: bufferRow, column: screenLine.startColumn + displayPosition.column } + currentScreenRow++ + bufferRow++