Skip to content

Commit

Permalink
Take double width chars into account when soft wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
as-cii committed Oct 15, 2015
1 parent b2a7f4a commit c2ee942
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions spec/display-buffer-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion src/display-buffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) ->
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/token-iterator.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{SoftTab, HardTab, PairedCharacter, SoftWrapIndent} = require './special-token-symbols'
{isDoubleWidthCharacter} = require './text-utils'

module.exports =
class TokenIterator
Expand Down Expand Up @@ -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()

0 comments on commit c2ee942

Please sign in to comment.