From 34922a18fec909bf94229f5c4ca8cb18f5c9ff52 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 8 Feb 2012 19:15:00 -0700 Subject: [PATCH] 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. --- spec/atom/line-wrapper-spec.coffee | 15 ++++++++++++++- src/atom/line-wrapper.coffee | 21 +++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/spec/atom/line-wrapper-spec.coffee b/spec/atom/line-wrapper-spec.coffee index b59ef3e7b..9c978c68e 100644 --- a/spec/atom/line-wrapper-spec.coffee +++ b/spec/atom/line-wrapper-spec.coffee @@ -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", -> diff --git a/src/atom/line-wrapper.coffee b/src/atom/line-wrapper.coffee index 3062df5a7..d7dc55585 100644 --- a/src/atom/line-wrapper.coffee +++ b/src/atom/line-wrapper.coffee @@ -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)