From 0bc510ab58baa1962f4bc2f9304a033add0fc4af Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 24 Feb 2012 21:56:18 -0700 Subject: [PATCH] Properly translate positions when wrapper and folder are composed The key was in LineMap.linesForScreenRows. For each screen line, it concatenates all line fragments (if there are indeed more than 1) that traverse that line to return a single line fragment representing the line. The key was to update the buffer delta for that fragment to always be 1,0. Because the wrapper is treating the folder as if it's the buffer, the lines it stores in its map need to traverse only a single "buffer" line (that's a single line after folds are taken into account). We may need better language than "screen" and "buffer" because the wrapper treats the folder as the "buffer" but that's confusing because it isn't. --- spec/atom/editor-spec.coffee | 2 ++ spec/atom/line-wrapper-spec.coffee | 2 ++ src/atom/line-folder.coffee | 2 +- src/atom/line-map.coffee | 8 +++----- src/atom/line-wrapper.coffee | 5 ++++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index 71201f16e..1a75184bb 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -743,3 +743,5 @@ describe "Editor", -> expect(editor.selection.isEmpty()).toBeTruthy() expect(editor.getCursorScreenPosition()).toEqual [4, 32] + editor.setCursorScreenPosition([9, 2]) + expect(editor.getCursorScreenPosition()).toEqual [9, 2] diff --git a/spec/atom/line-wrapper-spec.coffee b/spec/atom/line-wrapper-spec.coffee index e2ba4ee9c..ba0756ae2 100644 --- a/spec/atom/line-wrapper-spec.coffee +++ b/spec/atom/line-wrapper-spec.coffee @@ -129,6 +129,7 @@ describe "LineWrapper", -> it "adjusts the position to account for the fold", -> fold = folder.createFold(new Range([4, 29], [7, 4])) expect(wrapper.screenPositionForBufferPosition([7, 4])).toEqual [5, 32] + expect(wrapper.screenPositionForBufferPosition([8, 12])).toEqual [6, 12] describe ".bufferPositionForScreenPosition(point)", -> it "translates the given screen position to a buffer position, account for wrapped lines", -> @@ -147,6 +148,7 @@ describe "LineWrapper", -> it "adjusts the position to account for the fold", -> fold = folder.createFold(new Range([4, 29], [7, 4])) expect(wrapper.bufferPositionForScreenPosition([5, 32])).toEqual [7, 4] + expect(wrapper.bufferPositionForScreenPosition([6, 12])).toEqual [8, 12] describe ".wrapScreenLine(screenLine)", -> makeTokens = (tokenValues...) -> diff --git a/src/atom/line-folder.coffee b/src/atom/line-folder.coffee index f708611eb..cc2474865 100644 --- a/src/atom/line-folder.coffee +++ b/src/atom/line-folder.coffee @@ -114,7 +114,7 @@ class LineFolder @lineMap.lineForScreenRow(screenRow) getLines: -> - @lineMap.getScreenLines() + @lineMap.screenLinesForRows(0, @lastRow()) lineCount: -> @lineMap.screenLineCount() diff --git a/src/atom/line-map.coffee b/src/atom/line-map.coffee index 6c3ae24c9..dd933d2b9 100644 --- a/src/atom/line-map.coffee +++ b/src/atom/line-map.coffee @@ -50,14 +50,10 @@ class LineMap replaceScreenRows: (start, end, screenLines) -> @spliceAtScreenRow(start, end - start + 1, screenLines) - getScreenLines: -> - return @screenLines - lineForScreenRow: (row) -> @linesForScreenRows(row, row)[0] linesForScreenRows: (startRow, endRow) -> - lastLine = null lines = [] delta = new Point @@ -67,11 +63,13 @@ class LineMap if pendingFragment pendingFragment = pendingFragment.concat(fragment) else - pendingFragment = fragment + pendingFragment = _.clone(fragment) if pendingFragment.screenDelta.row > 0 + pendingFragment.bufferDelta = new Point(1, 0) lines.push pendingFragment pendingFragment = null delta = delta.add(fragment.screenDelta) + lines lineForBufferRow: (row) -> diff --git a/src/atom/line-wrapper.coffee b/src/atom/line-wrapper.coffee index 19dd2e6c3..b0b34bd6b 100644 --- a/src/atom/line-wrapper.coffee +++ b/src/atom/line-wrapper.coffee @@ -106,11 +106,14 @@ class LineWrapper @lineMap.linesForScreenRows(startRow, endRow) getLines: -> - @linesForScreenRows(0, @lineCount() - 1) + @linesForScreenRows(0, @lastRow()) lineCount: -> @lineMap.screenLineCount() + lastRow: -> + @lineCount() - 1 + logLines: (start=0, end=@lineCount() - 1)-> @lineMap.logLines(start, end)