From cbd4fab8f24feac429c11cd579ec48149e266343 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 22 Aug 2013 18:45:04 -0700 Subject: [PATCH] Emit event from display buffer when soft wrap changes This allows the editor to be reactive to the edit session and always respect the edit session's current value when initially opened. --- spec/editor-spec.coffee | 14 +++++++------- src/display-buffer.coffee | 21 +++++++++++++++------ src/edit-session.coffee | 1 + src/editor.coffee | 12 +++++++----- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 4a8a9d667..236d7c561 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -1013,7 +1013,7 @@ describe "Editor", -> describe "when soft-wrap is enabled", -> beforeEach -> - editor.setSoftWrap(true) + editSession.setSoftWrap(true) it "does not scroll the buffer horizontally", -> editor.width(charWidth * 30) @@ -1480,7 +1480,7 @@ describe "Editor", -> describe "when wrapping is on", -> beforeEach -> - editor.setSoftWrap(true) + editSession.setSoftWrap(true) it "doesn't show the end of line invisible at the end of lines broken due to wrapping", -> editor.setText "a line that wraps" @@ -1631,10 +1631,10 @@ describe "Editor", -> describe "when soft-wrap is enabled", -> beforeEach -> + editSession.setSoftWrap(true) editor.attachToDom() setEditorHeightInLines(editor, 20) setEditorWidthInChars(editor, 50) - editor.setSoftWrap(true) expect(editor.activeEditSession.getSoftWrapColumn()).toBe 50 it "wraps lines that are too long to fit within the editor's width, adjusting cursor positioning accordingly", -> @@ -1703,7 +1703,7 @@ describe "Editor", -> otherEditor = new Editor(editSession: project.open('sample.js')) spyOn(otherEditor, 'setSoftWrapColumn') - otherEditor.setSoftWrap(true) + otherEditor.activeEditSession.setSoftWrap(true) expect(otherEditor.setSoftWrapColumn).not.toHaveBeenCalled() otherEditor.simulateDomAttachment() @@ -1743,7 +1743,7 @@ describe "Editor", -> describe "when wrapping is on", -> it "renders a • instead of line number for wrapped portions of lines", -> - editor.setSoftWrap(true) + editSession.setSoftWrap(true) editor.setSoftWrapColumn(50) expect(editor.gutter.find('.line-number').length).toEqual(8) expect(editor.gutter.find('.line-number:eq(3)').intValue()).toBe 4 @@ -1883,7 +1883,7 @@ describe "Editor", -> describe "when there is wrapping", -> beforeEach -> editor.attachToDom(30) - editor.setSoftWrap(true) + editSession.setSoftWrap(true) setEditorWidthInChars(editor, 20) it "highlights the line where the initial cursor position is", -> @@ -1946,7 +1946,7 @@ describe "Editor", -> describe "when there is wrapping", -> beforeEach -> - editor.setSoftWrap(true) + editSession.setSoftWrap(true) setEditorWidthInChars(editor, 20) it "highlights the line where the initial cursor position is", -> diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index 733693954..18436f06a 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -45,6 +45,12 @@ class DisplayBuffer @subscribe @buffer, 'markers-updated', @handleBufferMarkersUpdated @subscribe @buffer, 'marker-created', @handleBufferMarkerCreated + @state.on 'changed', ({key, newValue}) => + switch key + when 'softWrap' + @trigger 'soft-wrap-changed', newValue + @updateWrappedScreenLines() + serialize: -> @state.clone() getState: -> @state @@ -67,6 +73,14 @@ class DisplayBuffer @trigger 'changed', eventProperties @resumeMarkerObservers() + updateWrappedScreenLines: -> + start = 0 + end = @getLastRow() + @updateAllScreenLines() + screenDelta = @getLastRow() - end + bufferDelta = 0 + @triggerChanged({ start, end, screenDelta, bufferDelta }) + ### Public ### # Sets the visibility of the tokenized buffer. @@ -83,12 +97,7 @@ class DisplayBuffer # softWrapColumn - A {Number} defining the soft wrap limit. setSoftWrapColumn: (softWrapColumn) -> @state.set('softWrapColumn', softWrapColumn) - start = 0 - end = @getLastRow() - @updateAllScreenLines() - screenDelta = @getLastRow() - end - bufferDelta = 0 - @triggerChanged({ start, end, screenDelta, bufferDelta }) + @updateWrappedScreenLines() if @getSoftWrap() getSoftWrapColumn: -> @state.get('softWrapColumn') diff --git a/src/edit-session.coffee b/src/edit-session.coffee index e7c2a4e45..29d4cf3ff 100644 --- a/src/edit-session.coffee +++ b/src/edit-session.coffee @@ -97,6 +97,7 @@ class EditSession @subscribe @displayBuffer, "changed", (e) => @trigger 'screen-lines-changed', e @subscribe @displayBuffer, "markers-updated", => @mergeIntersectingSelections() @subscribe @displayBuffer, 'grammar-changed', => @handleGrammarChange() + @subscribe @displayBuffer, 'soft-wrap-changed', (args...) => @trigger 'soft-wrap-changed', args... getViewClass: -> require 'editor' diff --git a/src/editor.coffee b/src/editor.coffee index cda5be922..19475f514 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -773,6 +773,9 @@ class Editor extends View @activeEditSession.on 'scroll-left-changed.editor', (scrollLeft) => @scrollLeft(scrollLeft) + @activeEditSession.on 'soft-wrap-changed.editor', (softWrap) => + @setSoftWrap(softWrap) + @trigger 'editor:path-changed' @resetDisplay() @@ -902,7 +905,7 @@ class Editor extends View # Activates soft wraps in the editor. toggleSoftWrap: -> - @setSoftWrap(not @activeEditSession.getSoftWrap()) + @activeEditSession.setSoftWrap(not @activeEditSession.getSoftWrap()) calcSoftWrapColumn: -> Math.floor(@scrollView.width() / @charWidth) @@ -912,10 +915,8 @@ class Editor extends View # softWrap - A {Boolean} which, if `true`, sets soft wraps # softWrapColumn - A {Number} indicating the length of a line in the editor when soft # wrapping turns on - setSoftWrap: (softWrap, softWrapColumn=undefined) -> - @activeEditSession.setSoftWrap(softWrap) - @setSoftWrapColumn(softWrapColumn) if @attached - if @activeEditSession.getSoftWrap() + setSoftWrap: (softWrap) -> + if softWrap @addClass 'soft-wrap' @scrollLeft(0) else @@ -1133,6 +1134,7 @@ class Editor extends View @updateLayerDimensions() @scrollTop(editSessionScrollTop) @scrollLeft(editSessionScrollLeft) + @setSoftWrap(@activeEditSession.getSoftWrap()) @newCursors = @activeEditSession.getAllCursors() @newSelections = @activeEditSession.getAllSelections() @updateDisplay(suppressAutoScroll: true)