Move findWrapColumn into TokenizedLine

* 🐎 Check `isSoftWrapped` only once when updating screen lines
This commit is contained in:
Antonio Scandurra
2015-02-18 10:05:43 +01:00
parent b0c670b80a
commit 62434d9ad5
2 changed files with 28 additions and 29 deletions

View File

@@ -854,30 +854,6 @@ class DisplayBuffer extends Model
column = screenLine.clipScreenColumn(column, options)
new Point(row, column)
# Given a line, finds the point where it would wrap.
#
# line - The {String} to check
# softWrapColumn - The {Number} where you want soft wrapping to occur
#
# Returns a {Number} representing the `line` position where the wrap would take place.
# Returns `null` if a wrap wouldn't occur.
findWrapColumn: (line, softWrapColumn=@getSoftWrapColumn()) ->
return unless @isSoftWrapped()
return unless line.text.length > softWrapColumn
if /\s/.test(line.text[softWrapColumn])
# search forward for the start of a word past the boundary
for column in [softWrapColumn..line.text.length]
return column if /\S/.test(line.text[column])
return line.text.length
else
# search backward for the start of the word on the boundary
for column in [softWrapColumn..0] when line.isColumnOutsidePhantomToken(column)
return column + 1 if /\s/.test(line.text[column])
return softWrapColumn
# Calculates a {Range} representing the start of the {TextBuffer} until the end.
#
# Returns a {Range}.
@@ -1162,11 +1138,12 @@ class DisplayBuffer extends Model
bufferRow += foldedRowCount
else
softWraps = 0
while wrapScreenColumn = @findWrapColumn(tokenizedLine)
[wrappedLine, tokenizedLine] = tokenizedLine.softWrapAt(wrapScreenColumn)
break if wrappedLine.hasOnlyPhantomTokens()
screenLines.push(wrappedLine)
softWraps++
if @isSoftWrapped()
while wrapScreenColumn = tokenizedLine.findWrapColumn(@getSoftWrapColumn())
[wrappedLine, tokenizedLine] = tokenizedLine.softWrapAt(wrapScreenColumn)
break if wrappedLine.hasOnlyPhantomTokens()
screenLines.push(wrappedLine)
softWraps++
screenLines.push(tokenizedLine)
if softWraps > 0

View File

@@ -86,6 +86,28 @@ class TokenizedLine
getMaxBufferColumn: ->
@startBufferColumn + @bufferDelta
# Given a boundary column, finds the point where this line would wrap.
#
# maxColumn - The {Number} where you want soft wrapping to occur
#
# Returns a {Number} representing the `line` position where the wrap would take place.
# Returns `null` if a wrap wouldn't occur.
findWrapColumn: (maxColumn) ->
return unless @text.length > 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
# search backward for the start of the word on the boundary
for column in [maxColumn..0] when @isColumnOutsidePhantomToken(column)
return column + 1 if /\s/.test(@text[column])
return maxColumn
softWrapAt: (column) ->
return [new TokenizedLine([], '', [0, 0], [0, 0]), this] if column == 0