Atom.workspace.open(...) without a new tab


#1

So from the API, workspace.open(uri) opens the file specified by the uri, and a returns a Promise that resolves to a TextEditor instance. But what I’m seeing is that in addition to returning the Promise, it automatically opens up a new pane with the opened file. So I’ve tried the following:

that = @
atom.workspace.open(@findFileIWantToOpen().getPath())
.then (editor) ->
    @MyViewWithEditor = new MyViewWithEditor(state.MMyViewWithEditorState, editor)
    that.panel = atom.workspace.addBottomPanel(item: @MyViewWithEditor.getElement(), visible: false)

Which copies the text editor opened in the new tab to my new panel. But it still leaves a new tab-looking thing open, which when closed, also deletes the editor in my new panel:

Which has lead me to the following fix:

that = @
atom.workspace.open(@findFileIWantToOpen().getPath())
.then (editor) ->
    new_editor = editor.copy() #copy the editor
    @MyViewWithEditor = new MyViewWithEditor(state.MMyViewWithEditorState, new_editor)
    that.panel = atom.workspace.addBottomPanel(item: @MyViewWithEditor.getElement(), visible: false)

Which leaves the editor in tact when closed, but gives me the following error when I try to type in the editor:

Uncaught TypeError: Cannot read property 'displayBuffer' of undefined

This lead me to believe that when closing the original editor, it deletes the reference to the display buffer, but sure enough in the text-editor code, it does indeed make a new copy.

copy: ->
    ....
    displayBuffer = @displayBuffer.copy()

Which I’ve also verified with the debugger.
So anyways, my question is: Does anyone know why this error would be showing up and how to fix it OR a simpler way to create a lone text buffer in a new panel?

[EDIT] It seems as though it might be coming from autocomplete:

TypeError: Cannot read property 'displayBuffer' of undefined
  at SymbolStore.module.exports.SymbolStore.getTokenizedLines (/Applications/Atom.app/Contents/Resources/app.asar/node_modules/autocomplete-plus/lib/symbol-store.js:336:20)
  at SymbolStore.module.exports.SymbolStore.operateOnTokensInBufferRange (/Applications/Atom.app/Contents/Resources/app.asar/node_modules/autocomplete-plus/lib/symbol-store.js:281:29)
  at SymbolStore.module.exports.SymbolStore.removeTokensInBufferRange (/Applications/Atom.app/Contents/Resources/app.asar/node_modules/autocomplete-plus/lib/symbol-store.js:276:19)
  at /Applications/Atom.app/Contents/Resources/app.asar/node_modules/autocomplete-plus/lib/symbol-provider.js:116:31
  at Emitter.module.exports.Emitter.emit (/Applications/Atom.app/Contents/Resources/app.asar/node_modules/event-kit/lib/emitter.js:82:11)
  at TextBuffer.module.exports.TextBuffer.applyChange (/Applications/Atom.app/Contents/Resources/app.asar/node_modules/text-buffer/lib/text-buffer.js:594:20)
  at TextBuffer.module.exports.TextBuffer.setTextInRange (/Applications/Atom.app/Contents/Resources/app.asar/node_modules/text-buffer/lib/text-buffer.js:534:12)
  at Selection.module.exports.Selection.insertText (/Applications/Atom.app/Contents/Resources/app.asar/src/selection.js:468:43)
  at /Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:736:29
  at /Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:778:29
  at TextBuffer.module.exports.TextBuffer.transact (/Applications/Atom.app/Contents/Resources/app.asar/node_modules/text-buffer/lib/text-buffer.js:740:18)
  at TextEditor.module.exports.TextEditor.transact (/Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:1151:26)
  at /Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:772:24
  at TextEditor.module.exports.TextEditor.mergeSelections (/Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:2013:43)
  at TextEditor.module.exports.TextEditor.mergeIntersectingSelections (/Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:1987:35)
  at TextEditor.module.exports.TextEditor.mutateSelectedText (/Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:770:19)
  at TextEditor.module.exports.TextEditor.insertText (/Applications/Atom.app/Contents/Resources/app.asar/src/text-editor.js:733:19)
  at TextEditorComponent.module.exports.TextEditorComponent.onTextInput (/Applications/Atom.app/Contents/Resources/app.asar/src/text-editor-component.js:444:35)
  at HTMLDivElement.__bind (/Applications/Atom.app/Contents/Resources/app.asar/src/text-editor-component.js:3:61)

#2

To create a new text editor, you can use

var textEditorView = document.createElement('atom-text-editor');
myPanel.appendChild(textEditorView);
atom.workspace.addBottomPanel({item: myPanel});

To set text, do

var textEditorModel = textEditorView.getModel();
textEditorModel.setText('...');

or maybe even

textEditorModel.getBuffer().setPath('path/to/some/file');

Though I don’t really know if that also loads a file etc. :wink:

PS you might find it useful to know about fat arrows in CoffeeScript. They allow you to replace

that = @
do ->
  that.someProperty

with

do =>
  @someProperty

Notice the => instead of ->


#3

Huge thanks for the PS!