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)