WIP: Wrap lines at token boundaries when soft wrap is on

This is still really rough... cursor positioning isn't working properly
yet. Nor does anything adjust when the window is resized.
This commit is contained in:
Nathan Sobo 2012-02-07 14:43:33 -07:00
parent 525116a80b
commit 62470f61ab
2 changed files with 44 additions and 11 deletions

View File

@ -50,6 +50,16 @@ describe "Editor", ->
buffer.insert([1,0], "/*")
expect(editor.lines.find('.line:eq(2) span:eq(0)')).toMatchSelector '.comment'
describe "when soft-wrap is enabled", ->
beforeEach ->
editor.attachToDom()
editor.width(editor.charWidth * 50)
editor.setSoftWrap(true)
it "wraps lines that are too long to fit within the editor's width", ->
expect(editor.lines.find('pre:eq(3)').text()).toBe " var pivot = items.shift(), current, left = [],"
expect(editor.lines.find('pre:eq(4)').text()).toBe " right = [];"
describe "cursor movement", ->
describe ".setCursorPosition({row, column})", ->
beforeEach ->

View File

@ -19,6 +19,7 @@ class Editor extends View
vScrollMargin: 2
hScrollMargin: 10
softWrap: false
cursor: null
buffer: null
undoManager: null
@ -112,24 +113,43 @@ class Editor extends View
buildLineElement: (row) ->
tokens = @highlighter.tokensForRow(row)
maxSegmentLength =
if @softWrap
Math.floor(@width() / @charWidth)
else
Infinity
currentSegmentLength = 0
currentSegment = []
segments = [currentSegment]
for token in @highlighter.tokensForRow(row)
if (currentSegmentLength + token.value.length) <= maxSegmentLength
currentSegmentLength += token.value.length
currentSegment.push(token)
else
currentSegment = [token]
currentSegmentLength = token.value.length
segments.push(currentSegment)
$$ ->
@pre class: 'line', =>
if tokens.length
for token in tokens
@span { class: token.type.replace('.', ' ') }, token.value
else
@raw '&nbsp;'
setBuffer: (@buffer) ->
@highlighter = new Highlighter(@buffer)
@undoManager = new UndoManager(@buffer)
for segment in segments
@pre class: 'line', =>
if segment.length
for token in segment
@span { class: token.type.replace('.', ' ') }, token.value
else
@raw '&nbsp;'
renderLines: ->
@lines.empty()
for row in [0..@buffer.lastRow()]
line = @buildLineElement(row)
@lines.append line
setBuffer: (@buffer) ->
@highlighter = new Highlighter(@buffer)
@undoManager = new UndoManager(@buffer)
@renderLines()
@setCursorPosition(row: 0, column: 0)
@buffer.on 'change', (e) =>
@ -165,6 +185,9 @@ class Editor extends View
getLineElement: (row) ->
@lines.find("pre.line:eq(#{row})")
setSoftWrap: (@softWrap) ->
@renderLines()
clipPosition: ({row, column}) ->
if row > @buffer.lastRow()
row = @buffer.lastRow()