When splitting lines, keep leading whitespace on current line.

If we detect leading whitespace, we replace the next token with the
result of splitting the next token into two tokens, one containing any
leading whitespace and one with the rest. Then the leading whitespace
is added to the current line. When the second half of the split is
processed, it no longer has leading whitespace and is split to the next
line.
This commit is contained in:
Nathan Sobo 2012-02-08 19:15:00 -07:00
parent 21e05f7218
commit 34922a18fe
2 changed files with 31 additions and 5 deletions

View File

@ -148,7 +148,20 @@ describe "LineWrapper", ->
expect(screenLines[1]).toEqual [{value: 'abcde'}]
describe "when token has leading whitespace", ->
describe "when the exceeding token is whitespace", ->
it "splits the token in half and places the non-whitespace portion on the next line", ->
screenLines = wrapper.splitTokens([{value: '12345'}, {value: '12345'}, {value: ' abcde', type: 'foo'}, {value: 'ghi'}])
expect(screenLines.length).toBe 2
expect(screenLines[0]).toEqual [{value: '12345'}, {value: '12345'}, {value: ' ', type: 'foo'}]
expect(screenLines[1]).toEqual [{value: 'abcde', type: 'foo'}, {value: 'ghi'}]
describe "when the exceeding token is only whitespace", ->
it "keeps the token on the first line and places the following token on the next line", ->
screenLines = wrapper.splitTokens([{value: '12345'}, {value: '12345'}, {value: ' '}, {value: 'ghi'}])
expect(screenLines.length).toBe 2
expect(screenLines[0]).toEqual [{value: '12345'}, {value: '12345'}, {value: ' '}]
expect(screenLines[1]).toEqual [{value: 'ghi'}]
describe "when the exceeding token straddles the max line length", ->
describe "when token contains no whitespace", ->
describe "when token contains whitespace", ->

View File

@ -40,13 +40,26 @@ class LineWrapper
length = 0
screenLine = []
while tokens.length
break if length + tokens[0].value.length > @maxLength
token = tokens.shift()
length += token.value.length
screenLine.push token
nextToken = tokens[0]
if length + nextToken.value.length > @maxLength
# keep any leading whitespace on current line
if match = /\b/.exec(nextToken.value)
if match.index > 0
tokens[0..0] = @splitTokenAt(nextToken, match.index)
else
break
nextToken = tokens.shift()
length += nextToken.value.length
screenLine.push nextToken
[screenLine].concat @splitTokens(tokens)
splitTokenAt: (token, index) ->
{ type, value} = token
value1 = value.substring(0, index)
value2 = value.substring(index)
[{value: value1, type }, {value: value2, type}]
buildWrappedLineForBufferRow: (bufferRow) ->
wordRegex = getWordRegex()
line = @buffer.getLine(bufferRow)