diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 8e3551ed3..1982ae1e1 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 eac57df53..fd24b9d3f 100644 --- a/spec/atom/line-wrapper-spec.coffee +++ b/spec/atom/line-wrapper-spec.coffee @@ -47,5 +47,17 @@ fdescribe "LineWrapper", -> expect(segments[2].lastIndex).toBe 65 expect(_.pluck(segments[2], 'value').join('')).toBe 'right.push(current);' - describe "when the buffer changes", -> - + describe ".displayPositionFromBufferPosition(point)", -> + it "returns the position of the indicated row and column on screen, accounting for wrapped lines", -> + # no wrap + expect(wrapper.displayPositionFromBufferPosition(row: 0, column: 5)).toEqual(row: 0, column: 5) + + # wrap once + 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 + expect(wrapper.displayPositionFromBufferPosition(row: 4, column: 5)).toEqual(row: 5, column: 5) + + diff --git a/src/atom/line-wrapper.coffee b/src/atom/line-wrapper.coffee index d6e44325e..508c734a7 100644 --- a/src/atom/line-wrapper.coffee +++ b/src/atom/line-wrapper.coffee @@ -35,6 +35,7 @@ class LineWrapper currentSegment = [] currentSegment.lastIndex = 0 + currentSegment.textLength = 0 segments = [currentSegment] nextBreak = breakIndices.shift() for token in @highlighter.tokensForRow(row) @@ -50,3 +51,17 @@ class LineWrapper currentSegment.textLength += token.value.length segments + + displayPositionFromBufferPosition: (bufferPosition) -> + row = 0 + for segments in @lines[0...bufferPosition.row] + row += segments.length + + column = bufferPosition.column + for segment in @lines[bufferPosition.row] + break if segment.lastIndex > bufferPosition.column + column -= segment.textLength + row++ + + { row, column } +