diff --git a/spec/text-editor-registry-spec.js b/spec/text-editor-registry-spec.js index 05c9548b7..86bb71a6f 100644 --- a/spec/text-editor-registry-spec.js +++ b/spec/text-editor-registry-spec.js @@ -15,15 +15,11 @@ describe('TextEditorRegistry', function () { registry = new TextEditorRegistry({ assert: atom.assert, config: atom.config, - clipboard: atom.clipboard, grammarRegistry: atom.grammars, packageManager: {deferredActivationHooks: null} }) - editor = new TextEditor({ - config: atom.config, - clipboard: atom.clipboard, - }) + editor = new TextEditor() }) afterEach(function () { @@ -194,10 +190,7 @@ describe('TextEditorRegistry', function () { it('does not update the editor when config settings change for unrelated scope selectors', async function () { await atom.packages.activatePackage('language-javascript') - const editor2 = new TextEditor({ - config: atom.config, - clipboard: atom.clipboard, - }) + const editor2 = new TextEditor() editor2.setGrammar(atom.grammars.selectGrammar('test.js')) @@ -224,7 +217,6 @@ describe('TextEditorRegistry', function () { registry = new TextEditorRegistry({ assert: atom.assert, config: atom.config, - clipboard: atom.clipboard, grammarRegistry: atom.grammars, packageManager: { deferredActivationHooks: [], @@ -669,10 +661,7 @@ describe('TextEditorRegistry', function () { describe('serialization', function () { it('persists editors\' grammar overrides', async function () { - const editor2 = new TextEditor({ - config: atom.config, - clipboard: atom.clipboard, - }) + const editor2 = new TextEditor() await atom.packages.activatePackage('language-c') await atom.packages.activatePackage('language-html') @@ -691,7 +680,6 @@ describe('TextEditorRegistry', function () { const registryCopy = new TextEditorRegistry({ assert: atom.assert, config: atom.config, - clipboard: atom.clipboard, grammarRegistry: atom.grammars, packageManager: {deferredActivationHooks: null} }) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 2c4ba084f..4192c5d5a 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -54,7 +54,6 @@ describe "TextEditor", -> # reusing the same buffer instance editor2 = TextEditor.deserialize(editor.serialize(), { assert: atom.assert, - clipboard: atom.clipboard, textEditors: atom.textEditors, project: { bufferForIdSync: (id) -> TextBuffer.deserialize(editor.buffer.serialize()) @@ -5831,11 +5830,7 @@ describe "TextEditor", -> atom.packages.activatePackage('language-coffee-script') it "sets the grammar", -> - editor = new TextEditor({ - grammar: atom.grammars.grammarForScopeName('source.coffee') - clipboard: atom.clipboard - }) - + editor = new TextEditor({grammar: atom.grammars.grammarForScopeName('source.coffee')}) expect(editor.getGrammar().name).toBe 'CoffeeScript' describe "softWrapAtPreferredLineLength", -> diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index fe4c661c6..06a650b87 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -29,7 +29,7 @@ describe "Workspace", -> atom.workspace = new Workspace({ config: atom.config, project: atom.project, packageManager: atom.packages, grammarRegistry: atom.grammars, deserializerManager: atom.deserializers, - notificationManager: atom.notifications, clipboard: atom.clipboard, + notificationManager: atom.notifications, applicationDelegate: atom.applicationDelegate, viewRegistry: atom.views, assert: atom.assert.bind(atom), textEditorRegistry: atom.textEditors @@ -871,7 +871,7 @@ describe "Workspace", -> workspace2 = new Workspace({ config: atom.config, project: atom.project, packageManager: atom.packages, notificationManager: atom.notifications, deserializerManager: atom.deserializers, - clipboard: atom.clipboard, viewRegistry: atom.views, grammarRegistry: atom.grammars, + viewRegistry: atom.views, grammarRegistry: atom.grammars, applicationDelegate: atom.applicationDelegate, assert: atom.assert.bind(atom), textEditorRegistry: atom.textEditors }) @@ -934,7 +934,7 @@ describe "Workspace", -> workspace2 = new Workspace({ config: atom.config, project: atom.project, packageManager: atom.packages, notificationManager: atom.notifications, deserializerManager: atom.deserializers, - clipboard: atom.clipboard, viewRegistry: atom.views, grammarRegistry: atom.grammars, + viewRegistry: atom.views, grammarRegistry: atom.grammars, applicationDelegate: atom.applicationDelegate, assert: atom.assert.bind(atom), textEditorRegistry: atom.textEditors }) diff --git a/src/atom-environment.coffee b/src/atom-environment.coffee index 13361c576..92a8d1ad3 100644 --- a/src/atom-environment.coffee +++ b/src/atom-environment.coffee @@ -28,7 +28,6 @@ ThemeManager = require './theme-manager' MenuManager = require './menu-manager' ContextMenuManager = require './context-menu-manager' CommandInstaller = require './command-installer' -Clipboard = require './clipboard' Project = require './project' TitleBar = require './title-bar' Workspace = require './workspace' @@ -127,7 +126,7 @@ class AtomEnvironment extends Model # Call .loadOrCreate instead constructor: (params={}) -> - {@blobStore, @applicationDelegate, @window, @document, @configDirPath, @enablePersistence, onlyLoadBaseStyleSheets} = params + {@blobStore, @applicationDelegate, @window, @document, @clipboard, @configDirPath, @enablePersistence, onlyLoadBaseStyleSheets} = params @unloaded = false @loadTime = null @@ -182,20 +181,18 @@ class AtomEnvironment extends Model @packages.setContextMenuManager(@contextMenu) @packages.setThemeManager(@themes) - @clipboard = new Clipboard() - @project = new Project({notificationManager: @notifications, packageManager: @packages, @config, @applicationDelegate}) @commandInstaller = new CommandInstaller(@getVersion(), @applicationDelegate) @textEditors = new TextEditorRegistry({ - @config, grammarRegistry: @grammars, assert: @assert.bind(this), @clipboard, + @config, grammarRegistry: @grammars, assert: @assert.bind(this), packageManager: @packages }) @workspace = new Workspace({ @config, @project, packageManager: @packages, grammarRegistry: @grammars, deserializerManager: @deserializers, - notificationManager: @notifications, @applicationDelegate, @clipboard, viewRegistry: @views, assert: @assert.bind(this), + notificationManager: @notifications, @applicationDelegate, viewRegistry: @views, assert: @assert.bind(this), textEditorRegistry: @textEditors, }) diff --git a/src/initialize-application-window.coffee b/src/initialize-application-window.coffee index 63cfa1c2e..585ec93f7 100644 --- a/src/initialize-application-window.coffee +++ b/src/initialize-application-window.coffee @@ -20,16 +20,21 @@ module.exports = ({blobStore}) -> AtomEnvironment = require './atom-environment' ApplicationDelegate = require './application-delegate' + Clipboard = require './clipboard' + TextEditor = require './text-editor' + + clipboard = new Clipboard + TextEditor.setClipboard(clipboard) + window.atom = new AtomEnvironment({ - window, document, blobStore, + window, document, clipboard, blobStore, applicationDelegate: new ApplicationDelegate, - configDirPath: process.env.ATOM_HOME - enablePersistence: true + configDirPath: process.env.ATOM_HOME, + enablePersistence: true, env: process.env }) atom.startEditorWindow().then -> - # Workaround for focus getting cleared upon window creation windowFocused = -> window.removeEventListener('focus', windowFocused) diff --git a/src/initialize-test-window.coffee b/src/initialize-test-window.coffee index 6c812b4d3..e4706fe1d 100644 --- a/src/initialize-test-window.coffee +++ b/src/initialize-test-window.coffee @@ -21,6 +21,8 @@ module.exports = ({blobStore}) -> {getWindowLoadSettings} = require './window-load-settings-helpers' AtomEnvironment = require '../src/atom-environment' ApplicationDelegate = require '../src/application-delegate' + Clipboard = require '../src/clipboard' + TextEditor = require '../src/text-editor' require '../src/electron-shims' {testRunnerPath, legacyTestRunnerPath, headless, logFile, testPaths} = getWindowLoadSettings() @@ -58,11 +60,15 @@ module.exports = ({blobStore}) -> document.title = "Spec Suite" + clipboard = new Clipboard + TextEditor.setClipboard(clipboard) + testRunner = require(testRunnerPath) legacyTestRunner = require(legacyTestRunnerPath) buildDefaultApplicationDelegate = -> new ApplicationDelegate() buildAtomEnvironment = (params) -> params = cloneObject(params) + params.clipboard = clipboard unless params.hasOwnProperty("clipboard") params.blobStore = blobStore unless params.hasOwnProperty("blobStore") params.onlyLoadBaseStyleSheets = true unless params.hasOwnProperty("onlyLoadBaseStyleSheets") new AtomEnvironment(params) diff --git a/src/selection.coffee b/src/selection.coffee index d63605a86..5eaa9c8dd 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -14,7 +14,7 @@ class Selection extends Model initialScreenRange: null wordwise: false - constructor: ({@cursor, @marker, @editor, id, @clipboard}) -> + constructor: ({@cursor, @marker, @editor, id}) -> @emitter = new Emitter @assignId(id) @@ -605,7 +605,7 @@ class Selection extends Model startLevel = @editor.indentLevelForLine(precedingText) if maintainClipboard - {text: clipboardText, metadata} = @clipboard.readWithMetadata() + {text: clipboardText, metadata} = @editor.constructor.clipboard.readWithMetadata() metadata ?= {} unless metadata.selections? metadata.selections = [{ @@ -618,9 +618,9 @@ class Selection extends Model indentBasis: startLevel, fullLine: fullLine }) - @clipboard.write([clipboardText, selectionText].join("\n"), metadata) + @editor.constructor.clipboard.write([clipboardText, selectionText].join("\n"), metadata) else - @clipboard.write(selectionText, { + @editor.constructor.clipboard.write(selectionText, { indentBasis: startLevel, fullLine: fullLine }) diff --git a/src/text-editor-registry.js b/src/text-editor-registry.js index 9868df2d5..b29a3887c 100644 --- a/src/text-editor-registry.js +++ b/src/text-editor-registry.js @@ -39,9 +39,8 @@ const GRAMMAR_SELECTION_RANGE = Range(Point.ZERO, Point(10, 0)).freeze() // done using your editor, be sure to call `dispose` on the returned disposable // to avoid leaking editors. export default class TextEditorRegistry { - constructor ({config, grammarRegistry, clipboard, assert, packageManager}) { + constructor ({config, grammarRegistry, assert, packageManager}) { this.assert = assert - this.clipboard = clipboard this.config = config this.grammarRegistry = grammarRegistry this.scopedSettingsDelegate = new ScopedSettingsDelegate(config) @@ -109,10 +108,7 @@ export default class TextEditorRegistry { } build (params) { - params = Object.assign({ - clipboard: this.clipboard, - assert: this.assert - }, params) + params = Object.assign({assert: this.assert}, params) let scope = null if (params.buffer) { diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 034b9e8f0..052d5e1b4 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -59,6 +59,9 @@ ZERO_WIDTH_NBSP = '\ufeff' # soft wraps and folds to ensure your code interacts with them correctly. module.exports = class TextEditor extends Model + @setClipboard: (clipboard) -> + @clipboard = clipboard + serializationVersion: 1 buffer: null @@ -114,7 +117,6 @@ class TextEditor extends Model if state.displayLayer = state.buffer.getDisplayLayer(state.displayLayerId) state.selectionsMarkerLayer = state.displayLayer.getMarkerLayer(state.selectionsMarkerLayerId) - state.clipboard = atomEnvironment.clipboard state.assert = atomEnvironment.assert.bind(atomEnvironment) editor = new this(state) if state.registered @@ -123,19 +125,20 @@ class TextEditor extends Model editor constructor: (params={}) -> + unless @constructor.clipboard? + throw new Error("Must call TextEditor.setClipboard at least once before creating TextEditor instances") + super { @softTabs, @firstVisibleScreenRow, @firstVisibleScreenColumn, initialLine, initialColumn, tabLength, @softWrapped, @decorationManager, @selectionsMarkerLayer, @buffer, suppressCursorCreation, - @mini, @placeholderText, lineNumberGutterVisible, @largeFileMode, @clipboard, + @mini, @placeholderText, lineNumberGutterVisible, @largeFileMode, @assert, grammar, @showInvisibles, @autoHeight, @autoWidth, @scrollPastEnd, @editorWidthInChars, @tokenizedBuffer, @displayLayer, @invisibles, @showIndentGuide, @softWrapped, @softWrapAtPreferredLineLength, @preferredLineLength } = params - throw new Error("Must pass a clipboard parameter when constructing TextEditors") unless @clipboard? - @assert ?= (condition) -> condition @firstVisibleScreenRow ?= 0 @firstVisibleScreenColumn ?= 0 @@ -705,7 +708,7 @@ class TextEditor extends Model suppressCursorCreation: true, tabLength: @tokenizedBuffer.getTabLength(), @firstVisibleScreenRow, @firstVisibleScreenColumn, - @clipboard, @assert, displayLayer, grammar: @getGrammar(), + @assert, displayLayer, grammar: @getGrammar(), @autoWidth, @autoHeight }) @@ -2718,7 +2721,7 @@ class TextEditor extends Model # Returns the new {Selection}. addSelection: (marker, options={}) -> cursor = @addCursor(marker) - selection = new Selection(Object.assign({editor: this, marker, cursor, @clipboard}, options)) + selection = new Selection(Object.assign({editor: this, marker, cursor}, options)) @selections.push(selection) selectionBufferRange = selection.getBufferRange() @mergeIntersectingSelections(preserveFolds: options.preserveFolds) @@ -3127,7 +3130,7 @@ class TextEditor extends Model # # * `options` (optional) See {Selection::insertText}. pasteText: (options={}) -> - {text: clipboardText, metadata} = @clipboard.readWithMetadata() + {text: clipboardText, metadata} = @constructor.clipboard.readWithMetadata() return false unless @emitWillInsertTextEvent(clipboardText) metadata ?= {} diff --git a/src/workspace.coffee b/src/workspace.coffee index c279e8062..89c53b678 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -28,7 +28,7 @@ class Workspace extends Model { @packageManager, @config, @project, @grammarRegistry, @notificationManager, - @clipboard, @viewRegistry, @grammarRegistry, @applicationDelegate, @assert, + @viewRegistry, @grammarRegistry, @applicationDelegate, @assert, @deserializerManager, @textEditorRegistry } = params