From c2ee942df14b4863cb49c6f28f34bef49d39d127 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 15 Oct 2015 16:24:08 +0200 Subject: [PATCH] Take double width chars into account when soft wrapping --- spec/display-buffer-spec.coffee | 10 ++++++++++ src/display-buffer.coffee | 13 ++++++++++++- src/token-iterator.coffee | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index a6b6242d4..69d78bf70 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -60,6 +60,16 @@ describe "DisplayBuffer", -> changeHandler.reset() describe "rendering of soft-wrapped lines", -> + describe "when there are double width characters", -> + it "takes them into account when finding the soft wrap column", -> + displayBuffer.setDoubleWidthCharWidth(5) + buffer.setText("私たちのフ是一个地方,数千名学生12345业余爱们的板作为우리포럼hello world this is a pretty long latin line") + + expect(displayBuffer.tokenizedLineForScreenRow(0).text).toBe("私たちのフ是一个地方") + expect(displayBuffer.tokenizedLineForScreenRow(1).text).toBe(",数千名学生12345业余爱") + expect(displayBuffer.tokenizedLineForScreenRow(2).text).toBe("们的板作为우리포럼hello ") + expect(displayBuffer.tokenizedLineForScreenRow(3).text).toBe("world this is a pretty long latin line") + describe "when editor.softWrapAtPreferredLineLength is set", -> it "uses the preferred line length as the soft wrap column when it is less than the configured soft wrap column", -> atom.config.set('editor.preferredLineLength', 100) diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index a545235c2..948101385 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -195,6 +195,12 @@ class DisplayBuffer extends Model @defaultCharWidth = defaultCharWidth defaultCharWidth + getDoubleWidthCharWidth: -> @doubleWidthCharWidth + setDoubleWidthCharWidth: (doubleWidthCharWidth) -> + if doubleWidthCharWidth isnt @doubleWidthCharWidth + @doubleWidthCharWidth = doubleWidthCharWidth + doubleWidthCharWidth + getCursorWidth: -> 1 scrollToScreenRange: (screenRange, options = {}) -> @@ -282,8 +288,13 @@ class DisplayBuffer extends Model else charLength = 1 - charWidth = @getDefaultCharWidth() + if iterator.hasDoubleWidthCharacterAt(textIndex) + charWidth = @getDoubleWidthCharWidth() + else + charWidth = @getDefaultCharWidth() + return column if currentWidth + charWidth > lineMaxWidth + currentWidth += charWidth column += charLength textIndex += charLength diff --git a/src/token-iterator.coffee b/src/token-iterator.coffee index de874d499..4c4686635 100644 --- a/src/token-iterator.coffee +++ b/src/token-iterator.coffee @@ -1,4 +1,5 @@ {SoftTab, HardTab, PairedCharacter, SoftWrapIndent} = require './special-token-symbols' +{isDoubleWidthCharacter} = require './text-utils' module.exports = class TokenIterator @@ -82,5 +83,8 @@ class TokenIterator isPairedCharacter: -> @line.specialTokens[@index] is PairedCharacter + hasDoubleWidthCharacterAt: (charIndex) -> + isDoubleWidthCharacter(@getText()[charIndex]) + isAtomic: -> @isSoftTab() or @isHardTab() or @isSoftWrapIndentation() or @isPairedCharacter()