How can I open up a new view into an already-open file?


#1

I’m trying my hand at developing a package. My functionality requires a new text editor for an already-open file. How can I achieve this?

Initially, I hoped I could do something like this:

  • Get a handle editor to the current TextEditor.
  • Use editor to get a handle buffer to its TextBuffer.
  • Create a new TextEditor for buffer.

However, I can’t find the methods to do this. I expected TextBuffer to have an addTextEditor() method, or TextEditor to have a setBuffer() method, or something along those lines.

I figure I can probably get a new TextEditor into my file if I’m willing to do it in a separate pane (using the URI method of opening a file); however, I’d much prefer to keep it in the same pane if possible.


#2

I believe you’re looking for workspace.buildTextEditor(). It should drop the new TextEditor into the active pane.


#3

As far as I can see, buildTextEditor just makes a new editor that is not assigned to the same file as the active editor.

Having made an editor with buildTextEditor, how do I assign it to the existing file/buffer?


#4

Hang on a sec, I want some clarification. Based on your initial post, I assumed that atom.workspace.getActivePane().splitRight({copyActiveItem: true}) was not what you were looking for because that’s the obvious answer, but that may have been a bad assumption. What does splitRight() not do that you need to do, if anything?


How to create new file/textEditor/buffer in package?
#5

I assumed that atom.workspace.getActivePane().splitRight({copyActiveItem: true}) was not what you were looking for because that’s the obvious answer

I did say “I’m trying my hand”, which means this is my first time. That in turn implies that obvious answers can and will escape me.

What does splitRight() not do that you need to do, if anything?

It does not keep it in the same pane.

To reiterate my needs from the original post:

  • Make a new text editor
  • Which points at an already-open file
  • In the same pane (preferably)

#6

The pane:split-right-and-copy-active-item family of commands is built-in and I assume fairly common usage. I thought that if you wanted it to behave like a TextEditor opened with “Split Right” and then dragging the tab back into the original pane, you would have said so.

  • Make a new text editor
  • Which points at an already-open file
  • In the same pane (preferably)

I tested and this works without even a flicker:

firstPane = atom.workspace.getActivePane()
itemIndex = firstPane.getActiveItemIndex()
secondPane = firstPane.splitRight({copyActiveItem: true})
secondPane.moveItemToPane(secondPane.getActiveItem(), firstPane, itemIndex + 1)

#7

Brilliant, thank you.

I thought that if you wanted it to behave like a TextEditor opened with “Split Right” and then dragging the tab back into the original pane, you would have said so.

I was trying to think in terms of the API, not in terms of user commands which would presumably be more brittle.


#8

User commands are generally more brittle, yes, but I can’t think of any that aren’t replicable via the API. That’s one of the great things about Atom: I essentially just rebuilt the split command but with a destination of “the tab after the current active item”, and it would be really easy to overwrite pretty much all of the user commands like that. I don’t actually want to, but I perceive value in having the power to do so.


#9

Wow that worked!
Thanks!