From 7beafa2da6a854cbf82c0d4fb7afb5286d0dda12 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 30 Jun 2016 12:42:15 +0200 Subject: [PATCH] Introduce TextEditor.prototype.update This allows TextEditor objects to be used within an etch component easily. --- spec/text-editor-spec.coffee | 17 +++++++++++ src/text-editor.coffee | 59 +++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index c3de3dbc6..da9ad5431 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -94,6 +94,23 @@ describe "TextEditor", -> editor2.unfoldBufferRow(4) expect(editor2.isFoldedAtBufferRow(4)).not.toBe editor.isFoldedAtBufferRow(4) + describe ".update()", -> + it "updates the editor with the supplied config parameters", -> + atom.config.set('editor.showInvisibles', true) + editor.onDidChange(changeSpy = jasmine.createSpy('onDidChange')) + editor.update({ + tabLength: 6, softTabs: false, softWrapped: true, editorWidthInChars: 40, + ignoreInvisibles: true, mini: false, lineNumberGutterVisible: false + }) + expect(changeSpy.callCount).toBe(1) + expect(editor.getTabLength()).toBe(6) + expect(editor.getSoftTabs()).toBe(false) + expect(editor.isSoftWrapped()).toBe(true) + expect(editor.getEditorWidthInChars()).toBe(40) + expect(editor.getInvisibles()).toEqual({}) + expect(editor.isMini()).toBe(false) + expect(editor.isLineNumberGutterVisible()).toBe(false) + describe "config defaults", -> it "uses the `editor.tabLength`, `editor.softWrap`, and `editor.softTabs`, and `core.fileEncoding` config values", -> editor1 = null diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 1a3f83232..c7aa93606 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -187,6 +187,49 @@ class TextEditor extends Model if grammar? @setGrammar(grammar) + update: (params) -> + { + softTabs, tabLength, softWrapped, mini, placeholderText, lineNumberGutterVisible, + showInvisibles, ignoreInvisibles, editorWidthInChars + } = params + + resetDisplayLayer = false + + if softTabs? and softTabs isnt @softTabs + @setSoftTabs(softTabs) + + if tabLength? and tabLength isnt @tabLength + @setTabLength(tabLength, false) + resetDisplayLayer = true + + if softWrapped? and softWrapped isnt @softWrapped + @setSoftWrapped(softWrapped, false) + resetDisplayLayer = true + + if mini? and mini isnt @mini + @setMini(mini) + + if placeholderText? and placeholderText isnt @placeholderText + @setPlaceholderText(placeholderText) + + if lineNumberGutterVisible? and lineNumberGutterVisible isnt @lineNumberGutterVisible + @setLineNumberGutterVisible(lineNumberGutterVisible) + + if showInvisibles? and showInvisibles isnt @showInvisibles + @showInvisibles = showInvisibles + resetDisplayLayer = true + + if ignoreInvisibles? and ignoreInvisibles isnt @ignoreInvisibles + @setIgnoreInvisibles(ignoreInvisibles, false) + resetDisplayLayer = true + + if editorWidthInChars? and editorWidthInChars isnt @editorWidthInChars + @setEditorWidthInChars(editorWidthInChars, false) + resetDisplayLayer = true + + if resetDisplayLayer + @resetDisplayLayer() + serialize: -> tokenizedBufferState = @tokenizedBuffer.serialize() @@ -652,12 +695,12 @@ class TextEditor extends Model # # * `editorWidthInChars` A {Number} representing the width of the # {TextEditorElement} in characters. - setEditorWidthInChars: (editorWidthInChars) -> + setEditorWidthInChars: (editorWidthInChars, resetDisplayLayer=true) -> if editorWidthInChars > 0 previousWidthInChars = @editorWidthInChars @editorWidthInChars = editorWidthInChars if editorWidthInChars isnt previousWidthInChars and @isSoftWrapped() - @resetDisplayLayer() + @resetDisplayLayer() if resetDisplayLayer # Returns the editor width in characters. getEditorWidthInChars: -> @@ -2721,18 +2764,18 @@ class TextEditor extends Model # # * `tabLength` {Number} length of a single tab. Setting to `null` will # fallback to using the `editor.tabLength` config setting - setTabLength: (tabLength) -> + setTabLength: (tabLength, resetDisplayLayer=true) -> return if tabLength is @tabLength @tabLength = tabLength @tokenizedBuffer.setTabLength(@tabLength) - @resetDisplayLayer() + @resetDisplayLayer() if resetDisplayLayer - setIgnoreInvisibles: (ignoreInvisibles) -> + setIgnoreInvisibles: (ignoreInvisibles, resetDisplayLayer=true) -> return if ignoreInvisibles is @ignoreInvisibles @ignoreInvisibles = ignoreInvisibles - @resetDisplayLayer() + @resetDisplayLayer() if resetDisplayLayer getInvisibles: -> scopeDescriptor = @getRootScopeDescriptor() @@ -2805,10 +2848,10 @@ class TextEditor extends Model # * `softWrapped` A {Boolean} # # Returns a {Boolean}. - setSoftWrapped: (softWrapped) -> + setSoftWrapped: (softWrapped, resetDisplayLayer=true) -> if softWrapped isnt @softWrapped @softWrapped = softWrapped - @resetDisplayLayer() + @resetDisplayLayer() if resetDisplayLayer softWrapped = @isSoftWrapped() @emitter.emit 'did-change-soft-wrapped', softWrapped softWrapped