Take double width chars into account when soft wrapping

This commit is contained in:
Antonio Scandurra
2015-10-15 16:24:08 +02:00
parent b2a7f4a28e
commit c2ee942df1
3 changed files with 26 additions and 1 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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()