Make hard tabs align to columns.

This commit is contained in:
Andrew Stubbs
2014-05-14 13:30:18 +01:00
parent 4fa0b6e783
commit 1fe6c498ac
2 changed files with 25 additions and 13 deletions

View File

@@ -42,16 +42,19 @@ class Token
whitespaceRegexForTabLength: (tabLength) ->
WhitespaceRegexesByTabLength[tabLength] ?= new RegExp("([ ]{#{tabLength}})|(\t)|([^\t]+)", "g")
breakOutAtomicTokens: (tabLength, breakOutLeadingSoftTabs) ->
breakOutAtomicTokens: (tabLength, breakOutLeadingSoftTabs, startColumn) ->
if @hasSurrogatePair
outputTokens = []
column = startColumn
for token in @breakOutSurrogatePairs()
nextColumn += token.value.length
if token.isAtomic
outputTokens.push(token)
else
outputTokens.push(token.breakOutAtomicTokens(tabLength, breakOutLeadingSoftTabs)...)
outputTokens.push(token.breakOutAtomicTokens(tabLength, breakOutLeadingSoftTabs, column)...)
breakOutLeadingSoftTabs = token.isOnlyWhitespace() if breakOutLeadingSoftTabs
column = nextColumn
outputTokens
else
@@ -64,17 +67,21 @@ class Token
outputTokens = []
regex = @whitespaceRegexForTabLength(tabLength)
column = startColumn
while match = regex.exec(@value)
[fullMatch, softTab, hardTab] = match
token = null
if softTab and breakOutLeadingSoftTabs
outputTokens.push(@buildSoftTabToken(tabLength))
token = @buildSoftTabToken(tabLength)
else if hardTab
breakOutLeadingSoftTabs = false
outputTokens.push(@buildHardTabToken(tabLength))
token = @buildHardTabToken(tabLength, column)
else
breakOutLeadingSoftTabs = false
value = match[0]
outputTokens.push(new Token({value, @scopes}))
token = new Token({value, @scopes})
column += token.value.length
outputTokens.push(token)
outputTokens
@@ -105,17 +112,18 @@ class Token
isAtomic: true
)
buildHardTabToken: (tabLength) ->
@buildTabToken(tabLength, true)
buildHardTabToken: (tabLength, column) ->
@buildTabToken(tabLength, true, column)
buildSoftTabToken: (tabLength) ->
@buildTabToken(tabLength, false)
@buildTabToken(tabLength, false, 0)
buildTabToken: (tabLength, isHardTab) ->
buildTabToken: (tabLength, isHardTab, column) ->
tabStop = tabLength - column % tabLength
new Token(
value: _.multiplyString(" ", tabLength)
value: _.multiplyString(" ", tabStop)
scopes: @scopes
bufferDelta: if isHardTab then 1 else tabLength
bufferDelta: if isHardTab then 1 else tabStop
isAtomic: true
isHardTab: isHardTab
)

View File

@@ -5,8 +5,8 @@ idCounter = 1
module.exports =
class TokenizedLine
constructor: ({tokens, @lineEnding, @ruleStack, @startBufferColumn, @fold, @tabLength, @indentLevel}) ->
@tokens = @breakOutAtomicTokens(tokens)
@startBufferColumn ?= 0
@tokens = @breakOutAtomicTokens(tokens)
@text = _.pluck(@tokens, 'value').join('')
@bufferDelta = _.sum(_.pluck(@tokens, 'bufferDelta'))
@id = idCounter++
@@ -113,8 +113,12 @@ class TokenizedLine
breakOutAtomicTokens: (inputTokens) ->
outputTokens = []
breakOutLeadingSoftTabs = true
column = @startBufferColumn
for token in inputTokens
outputTokens.push(token.breakOutAtomicTokens(@tabLength, breakOutLeadingSoftTabs)...)
newTokens = token.breakOutAtomicTokens(@tabLength, breakOutLeadingSoftTabs, column)
for newToken in newTokens
column += newToken.value.length
outputTokens.push(newTokens...)
breakOutLeadingSoftTabs = token.isOnlyWhitespace() if breakOutLeadingSoftTabs
outputTokens