Correctly translate buffer positions to screen positions when the buffer has tab chars

This commit is contained in:
Nathan Sobo
2012-04-06 15:06:33 -06:00
parent 9da86982a8
commit 47685aab7b
5 changed files with 60 additions and 16 deletions

View File

@@ -136,7 +136,8 @@ class LineMap
targetDelta.column = 0
else
additionalColumns = sourcePosition.column - sourceDelta.column
targetDelta.column += lastLineFragment.clipColumn(additionalColumns, { skipAtomicTokens })
additionalColumns = lastLineFragment.translateColumn(sourceDeltaType, targetDeltaType, additionalColumns, { skipAtomicTokens })
targetDelta.column += additionalColumns
targetDelta

View File

@@ -44,24 +44,30 @@ class ScreenLineFragment
bufferDelta = @bufferDelta.add(other.bufferDelta)
new ScreenLineFragment(tokens, text, screenDelta, bufferDelta, {state: other.state})
clipColumn: (column, { skipAtomicTokens }) ->
translateColumn: (sourceDeltaType, targetDeltaType, sourceColumn, options={}) ->
{ skipAtomicTokens } = options
textLength = @text.length
column = Math.min(column, textLength)
sourceColumn = Math.min(sourceColumn, textLength)
currentColumn = 0
currentSourceColumn = 0
currentTargetColumn = 0
for token in @tokens
tokenStartColumn = currentColumn
tokenEndColumn = tokenStartColumn + token.value.length
break if tokenEndColumn > column
currentColumn = tokenEndColumn
tokenStartTargetColumn = currentTargetColumn
tokenStartSourceColumn = currentSourceColumn
tokenEndSourceColumn = currentSourceColumn + token[sourceDeltaType]
tokenEndTargetColumn = currentTargetColumn + token[targetDeltaType]
break if tokenEndSourceColumn > sourceColumn
currentSourceColumn = tokenEndSourceColumn
currentTargetColumn = tokenEndTargetColumn
if token?.isAtomic
if skipAtomicTokens and column > tokenStartColumn
tokenEndColumn
if skipAtomicTokens and sourceColumn > tokenStartSourceColumn
tokenEndTargetColumn
else
tokenStartColumn
tokenStartTargetColumn
else
column
remainingColumns = sourceColumn - currentSourceColumn
currentTargetColumn + remainingColumns
isSoftWrapped: ->
@screenDelta.row == 1 and @bufferDelta.row == 0

View File

@@ -4,7 +4,9 @@ class Token
type: null
isAtomic: null
constructor: ({@value, @type, @isAtomic}) ->
constructor: ({@value, @type, @isAtomic, @bufferDelta}) ->
@screenDelta = @value.length
@bufferDelta ?= @screenDelta
isEqual: (other) ->
@value == other.value and @type == other.type and !!@isAtomic == !!other.isAtomic
@@ -22,4 +24,4 @@ class Token
new Token(value: substring, type: @type)
buildTabToken: (tabText) ->
new Token(value: tabText, type: @type, isAtomic: true)
new Token(value: tabText, type: @type, bufferDelta: 1, isAtomic: true)