From 75d7950ef59a57d66032dc1b1055dd4cff657e4a Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 10 Oct 2012 14:42:10 -0700 Subject: [PATCH] Fix linemap translation when an atomic token is at the end of a line. Previously, if you entered an atomic token (like a tab) on an empty line it would not position the cursor after the token. This commit fixes that. If the last screen line token is atomic, and screenLine.translateColumn's sourceColumn parameter is greater than the start position of the atomic token, then the translated column should be the end position of the atomic token. --- spec/app/screen-line-spec.coffee | 14 ++++++++------ src/app/screen-line.coffee | 7 +++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/spec/app/screen-line-spec.coffee b/spec/app/screen-line-spec.coffee index a3275866f..4d0d9d181 100644 --- a/spec/app/screen-line-spec.coffee +++ b/spec/app/screen-line-spec.coffee @@ -6,8 +6,8 @@ describe "ScreenLine", -> [editSession, buffer, tabText, screenLine, tokenizedBuffer] = [] beforeEach -> - tabText = ' ' - editSession = fixturesProject.buildEditSessionForPath('sample.js') + tabText = '••' + editSession = fixturesProject.buildEditSessionForPath('sample.js', { tabText } ) { buffer, tokenizedBuffer } = editSession screenLine = tokenizedBuffer.lineForScreenRow(3) @@ -81,8 +81,7 @@ describe "ScreenLine", -> describe ".translateColumn(sourceDeltaType, targetDeltaType, sourceColumn, skipAtomicTokens: false)", -> beforeEach -> - buffer.insert([0, 13], '\t') - buffer.insert([0, 0], '\t\t') + buffer.setText("\t\t-") screenLine = tokenizedBuffer.lineForScreenRow(0) describe "when translating from buffer to screen coordinates", -> @@ -91,8 +90,11 @@ describe "ScreenLine", -> expect(screenLine.translateColumn('bufferDelta', 'screenDelta', 1)).toBe 2 expect(screenLine.translateColumn('bufferDelta', 'screenDelta', 2)).toBe 4 expect(screenLine.translateColumn('bufferDelta', 'screenDelta', 3)).toBe 5 - expect(screenLine.translateColumn('bufferDelta', 'screenDelta', 15)).toBe 17 - expect(screenLine.translateColumn('bufferDelta', 'screenDelta', 16)).toBe 19 + + describe "when an atomic token is at the end of the line (regression)", -> + it "translates a buffer position correctly", -> + buffer.setText("\t") + expect(tokenizedBuffer.lineForScreenRow(0).translateColumn('bufferDelta', 'screenDelta', 1)).toBe 2 describe "when translating from screen coordinates to buffer coordinates", -> describe "when skipAtomicTokens is false (the default)", -> diff --git a/src/app/screen-line.coffee b/src/app/screen-line.coffee index c864015f9..ae6fd5a96 100644 --- a/src/app/screen-line.coffee +++ b/src/app/screen-line.coffee @@ -61,16 +61,19 @@ class ScreenLine currentSourceColumn = 0 currentTargetColumn = 0 + isSourceColumnBeforeLastToken = false for token in @tokens tokenStartTargetColumn = currentTargetColumn tokenStartSourceColumn = currentSourceColumn tokenEndSourceColumn = currentSourceColumn + token[sourceDeltaType] tokenEndTargetColumn = currentTargetColumn + token[targetDeltaType] - break if tokenEndSourceColumn > sourceColumn + if tokenEndSourceColumn > sourceColumn + isSourceColumnBeforeLastToken = true + break currentSourceColumn = tokenEndSourceColumn currentTargetColumn = tokenEndTargetColumn - if token?.isAtomic + if isSourceColumnBeforeLastToken and token?.isAtomic if skipAtomicTokens and sourceColumn > tokenStartSourceColumn tokenEndTargetColumn else