diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index fc3ce224d..0704eeff2 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -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 -> diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 81ef25b35..272bf610a 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -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 ' ' - - 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 ' ' + 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()