Parameterize config on editor instead of using global atom.config

This commit is contained in:
Nathan Sobo 2015-10-02 21:38:02 -06:00
parent 3d118b8a33
commit 51abab930d
8 changed files with 39 additions and 30 deletions

View File

@ -15,7 +15,7 @@ describe "TextEditor", ->
it "properly renders soft-wrapped lines when randomly mutated", ->
times 10, (i) ->
buffer = new TextBuffer
editor = new TextEditor({buffer})
editor = atom.workspace.buildTextEditor({buffer})
editor.setEditorWidthInChars(80)
tokenizedBuffer = editor.displayBuffer.tokenizedBuffer
steps = []
@ -80,7 +80,7 @@ describe "TextEditor", ->
text
getReferenceScreenLines = ->
referenceEditor = new TextEditor({})
referenceEditor = atom.workspace.buildTextEditor()
referenceEditor.setEditorWidthInChars(80)
referenceEditor.setText(editor.getText())
referenceEditor.setSoftWrapped(editor.isSoftWrapped())

View File

@ -5,7 +5,7 @@ describe "Selection", ->
beforeEach ->
buffer = atom.project.bufferForPathSync('sample.js')
editor = new TextEditor(buffer: buffer, tabLength: 2)
editor = atom.workspace.buildTextEditor(buffer: buffer, tabLength: 2)
selection = editor.getLastSelection()
afterEach ->

View File

@ -33,7 +33,7 @@ describe "TextEditorElement", ->
describe "when the model is assigned", ->
it "adds the 'mini' attribute if .isMini() returns true on the model", ->
element = new TextEditorElement
model = new TextEditor(mini: true)
model = atom.workspace.buildTextEditor(mini: true)
element.setModel(model)
expect(element.hasAttribute('mini')).toBe true
@ -67,7 +67,7 @@ describe "TextEditorElement", ->
describe "when the editor is detached from the DOM and then reattached", ->
it "does not render duplicate line numbers", ->
editor = new TextEditor
editor = atom.workspace.buildTextEditor()
editor.setText('1\n2\n3')
element = atom.views.getView(editor)
@ -80,7 +80,7 @@ describe "TextEditorElement", ->
expect(element.shadowRoot.querySelectorAll('.line-number').length).toBe initialCount
it "does not render duplicate decorations in custom gutters", ->
editor = new TextEditor
editor = atom.workspace.buildTextEditor()
editor.setText('1\n2\n3')
editor.addGutter({name: 'test-gutter'})
marker = editor.markBufferRange([[0, 0], [2, 0]])

View File

@ -17,7 +17,7 @@ describe "TextEditorPresenter", ->
spyOn(window, "clearInterval").andCallFake window.fakeClearInterval
buffer = new TextBuffer(filePath: require.resolve('./fixtures/sample.js'))
editor = new TextEditor({buffer})
editor = atom.workspace.buildTextEditor({buffer})
waitsForPromise -> buffer.load()
afterEach ->
@ -2804,7 +2804,7 @@ describe "TextEditorPresenter", ->
performSetup = ->
buffer = new TextBuffer
editor = new TextEditor({buffer})
editor = atom.workspace.buildTextEditor({buffer})
editor.setEditorWidthInChars(80)
presenterParams =
model: editor

View File

@ -31,7 +31,7 @@ describe "TextEditor", ->
runs ->
fs.mkdirSync(pathToOpen)
expect(TextEditor.deserialize(editor1.serialize())).toBeUndefined()
expect(TextEditor.deserialize(editor1.serialize(), atom)).toBeUndefined()
it "restores selections and folds based on markers in the buffer", ->
editor.setSelectedBufferRange([[1, 2], [3, 4]])
@ -39,7 +39,7 @@ describe "TextEditor", ->
editor.foldBufferRow(4)
expect(editor.isFoldedAtBufferRow(4)).toBeTruthy()
editor2 = TextEditor.deserialize(editor.serialize())
editor2 = TextEditor.deserialize(editor.serialize(), atom)
expect(editor2.id).toBe editor.id
expect(editor2.getBuffer().getPath()).toBe editor.getBuffer().getPath()
@ -52,7 +52,7 @@ describe "TextEditor", ->
atom.config.set('editor.showInvisibles', true)
previousInvisibles = editor.tokenizedLineForScreenRow(0).invisibles
editor2 = TextEditor.deserialize(editor.serialize())
editor2 = TextEditor.deserialize(editor.serialize(), atom)
expect(previousInvisibles).toBeDefined()
expect(editor2.displayBuffer.tokenizedLineForScreenRow(0).invisibles).toEqual previousInvisibles
@ -62,7 +62,7 @@ describe "TextEditor", ->
state = editor.serialize()
atom.config.set('editor.invisibles', eol: '?')
editor2 = TextEditor.deserialize(state)
editor2 = TextEditor.deserialize(state, atom)
expect(editor.tokenizedLineForScreenRow(0).invisibles.eol).toBe '?'
@ -4411,11 +4411,10 @@ describe "TextEditor", ->
describe '.get/setPlaceholderText()', ->
it 'can be created with placeholderText', ->
TextBuffer = require 'text-buffer'
newEditor = new TextEditor
buffer: new TextBuffer
newEditor = atom.workspace.buildTextEditor(
mini: true
placeholderText: 'yep'
)
expect(newEditor.getPlaceholderText()).toBe 'yep'
it 'models placeholderText and emits an event when changed', ->
@ -4443,7 +4442,7 @@ describe "TextEditor", ->
describe "when there's no repository for the editor's file", ->
it "doesn't do anything", ->
editor = new TextEditor({})
editor = atom.workspace.buildTextEditor()
editor.setText("stuff")
editor.checkoutHeadRevision()

View File

@ -99,7 +99,7 @@ class TextEditorElement extends HTMLElement
@model ? @buildModel()
buildModel: ->
@setModel(new TextEditor(
@setModel(atom.workspace.buildTextEditor(
buffer: new TextBuffer(@textContent)
softWrapped: false
tabLength: 2

View File

@ -65,7 +65,7 @@ class TextEditor extends Model
selectionFlashDuration: 500
gutterContainer: null
@deserialize: (state) ->
@deserialize: (state, atomEnvironment) ->
try
displayBuffer = DisplayBuffer.deserialize(state.displayBuffer)
catch error
@ -75,12 +75,19 @@ class TextEditor extends Model
throw error
state.displayBuffer = displayBuffer
state.config = atomEnvironment.config
state.registerEditor = true
new this(state)
constructor: ({@softTabs, @scrollRow, @scrollColumn, initialLine, initialColumn, tabLength, softWrapped, @displayBuffer, buffer, registerEditor, suppressCursorCreation, @mini, @placeholderText, lineNumberGutterVisible, largeFileMode}={}) ->
constructor: (params={}) ->
super
{
@softTabs, @scrollRow, @scrollColumn, initialLine, initialColumn, tabLength,
softWrapped, @displayBuffer, buffer, registerEditor, suppressCursorCreation,
@mini, @placeholderText, lineNumberGutterVisible, largeFileMode, @config
} = params
@emitter = new Emitter
@disposables = new CompositeDisposable
@cursors = []
@ -105,7 +112,7 @@ class TextEditor extends Model
@languageMode = new LanguageMode(this)
@setEncoding(atom.config.get('core.fileEncoding', scope: @getRootScopeDescriptor()))
@setEncoding(@config.get('core.fileEncoding', scope: @getRootScopeDescriptor()))
@gutterContainer = new GutterContainer(this)
@lineNumberGutter = @gutterContainer.addGutter
@ -146,7 +153,7 @@ class TextEditor extends Model
subscribeToTabTypeConfig: ->
@tabTypeSubscription?.dispose()
@tabTypeSubscription = atom.config.observe 'editor.tabType', scope: @getRootScopeDescriptor(), =>
@tabTypeSubscription = @config.observe 'editor.tabType', scope: @getRootScopeDescriptor(), =>
@softTabs = @shouldUseSoftTabs(defaultValue: @softTabs)
destroyed: ->
@ -454,7 +461,10 @@ class TextEditor extends Model
copy: ->
displayBuffer = @displayBuffer.copy()
softTabs = @getSoftTabs()
newEditor = new TextEditor({@buffer, displayBuffer, @tabLength, softTabs, suppressCursorCreation: true, registerEditor: true})
newEditor = new TextEditor({
@buffer, displayBuffer, @tabLength, softTabs, suppressCursorCreation: true,
registerEditor: true, @config
})
for marker in @findMarkers(editorId: @id)
marker.copy(editorId: newEditor.id, preserveFolds: true)
newEditor
@ -592,14 +602,14 @@ class TextEditor extends Model
# Essential: Saves the editor's text buffer.
#
# See {TextBuffer::save} for more details.
save: -> @buffer.save(backup: atom.config.get('editor.backUpBeforeSaving'))
save: -> @buffer.save(backup: @config.get('editor.backUpBeforeSaving'))
# Essential: Saves the editor's text buffer as the given path.
#
# See {TextBuffer::saveAs} for more details.
#
# * `filePath` A {String} path.
saveAs: (filePath) -> @buffer.saveAs(filePath, backup: atom.config.get('editor.backUpBeforeSaving'))
saveAs: (filePath) -> @buffer.saveAs(filePath, backup: @config.get('editor.backUpBeforeSaving'))
# Determine whether the user should be prompted to save before closing
# this editor.
@ -746,7 +756,7 @@ class TextEditor extends Model
return false unless @emitWillInsertTextEvent(text)
groupingInterval = if options.groupUndo
atom.config.get('editor.undoGroupingInterval')
@config.get('editor.undoGroupingInterval')
else
0
@ -2380,10 +2390,10 @@ class TextEditor extends Model
#
# Returns a {Boolean}
shouldUseSoftTabs: ({defaultValue}) ->
tabType = atom.config.get('editor.tabType', scope: @getRootScopeDescriptor())
tabType = @config.get('editor.tabType', scope: @getRootScopeDescriptor())
switch tabType
when 'auto'
@usesSoftTabs() ? defaultValue ? atom.config.get('editor.softTabs') ? true
@usesSoftTabs() ? defaultValue ? @config.get('editor.softTabs') ? true
when 'hard'
false
when 'soft'
@ -2906,10 +2916,10 @@ class TextEditor extends Model
###
shouldAutoIndent: ->
atom.config.get("editor.autoIndent", scope: @getRootScopeDescriptor())
@config.get("editor.autoIndent", scope: @getRootScopeDescriptor())
shouldAutoIndentOnPaste: ->
atom.config.get("editor.autoIndentOnPaste", scope: @getRootScopeDescriptor())
@config.get("editor.autoIndentOnPaste", scope: @getRootScopeDescriptor())
###
Section: Event Handlers

View File

@ -458,7 +458,7 @@ class Workspace extends Model
@buildTextEditor(_.extend({buffer, largeFileMode}, options))
buildTextEditor: (params) ->
new TextEditor(params)
new TextEditor(_.extend({@config}, params))
# Public: Asynchronously reopens the last-closed item's URI if it hasn't already been
# reopened.