From 173fbba02b8504f473c27fbcd7cc4d593ebace24 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 11 Dec 2015 14:26:51 +0100 Subject: [PATCH] Wrap at the first CJK character before the boundary --- spec/display-buffer-spec.coffee | 17 ++++++++++++++--- src/tokenized-line.coffee | 10 +++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index 10df357e3..8c4adca44 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -130,14 +130,25 @@ describe "DisplayBuffer", -> expect(displayBuffer.tokenizedLineForScreenRow(10).text).toBe ' return ' expect(displayBuffer.tokenizedLineForScreenRow(11).text).toBe ' sort(left).concat(pivot).concat(sort(right));' - it "wraps the line at the boundary if it includes a CJK character", -> - buffer.setTextInRange([[0, 0], [1, 0]], 'abcd efg유私フ业余爱\n') + it "wraps the line at the first CJK character before the boundary", -> displayBuffer.setEditorWidthInChars(10) + + buffer.setTextInRange([[0, 0], [1, 0]], 'abcd efg유私フ业余爱\n') expect(displayBuffer.tokenizedLineForScreenRow(0).text).toBe 'abcd efg유私' expect(displayBuffer.tokenizedLineForScreenRow(1).text).toBe 'フ业余爱' + buffer.setTextInRange([[0, 0], [1, 0]], 'abcd ef유gef业余爱\n') + expect(displayBuffer.tokenizedLineForScreenRow(0).text).toBe 'abcd ef유' + expect(displayBuffer.tokenizedLineForScreenRow(1).text).toBe 'gef业余爱' + describe "when there is no whitespace before the boundary", -> - it "wraps the line exactly at the boundary since there's no more graceful place to wrap it", -> + it "wraps the line at the first CJK character before the boundary", -> + buffer.setTextInRange([[0, 0], [1, 0]], '私たちのabcdefghij\n') + displayBuffer.setEditorWidthInChars(10) + expect(displayBuffer.tokenizedLineForScreenRow(0).text).toBe '私たちの' + expect(displayBuffer.tokenizedLineForScreenRow(1).text).toBe 'abcdefghij' + + it "wraps the line exactly at the boundary when no CJK character is found, since there's no more graceful place to wrap it", -> buffer.setTextInRange([[0, 0], [1, 0]], 'abcdefghijklmnopqrstuvwxyz\n') displayBuffer.setEditorWidthInChars(10) expect(displayBuffer.tokenizedLineForScreenRow(0).text).toBe 'abcdefghij' diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index 2ff4c3b17..36802c2f7 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -321,19 +321,19 @@ class TokenizedLine return unless maxColumn? return unless @text.length > maxColumn - if isCjkCharacter(@text[maxColumn]) - # always wrap when a CJK character is at the wrap boundary - return maxColumn - else if /\s/.test(@text[maxColumn]) + if /\s/.test(@text[maxColumn]) # search forward for the start of a word past the boundary for column in [maxColumn..@text.length] return column if /\S/.test(@text[column]) return @text.length + else if isCjkCharacter(@text[maxColumn]) + maxColumn else # search backward for the start of the word on the boundary for column in [maxColumn..@firstNonWhitespaceIndex] - return column + 1 if /\s/.test(@text[column]) + if /\s/.test(@text[column]) or isCjkCharacter(@text[column]) + return column + 1 return maxColumn