Replace/close current tab?


#1

I’m looking for an output as similar as possible to open a new buffer on vim.

I can’t figure out how to accomplish this, even in the most naive alternative that is close the current file and open a new one. I can open a new one with atom.workspaceView.open, and I can get the current tab by atom.workspace.getActiveEditor(), but I can’t figure out how to close it.

So, how can I close the current tab, or even better, somehow replace the file open on it by another one?


#2

To close the active editor, you can do atom.workspace.getActiveEditor().destroy().

From what I’ve seen of the data model, an Editor has a TextBuffer. The TextBuffer is used to hold the text and only the Editor knows where that text is stored on disk (if it is at all yet). So I suppose you could allocate a new TextBuffer, point the Editor to it … and destroy or update the path the Editor points to. Or you could just close the tab and open a new one :smiley:

I understand the model of text buffers being separate from the files that they represent that editors like vim and Emacs use, but I don’t understand what benefit it provides to the user. To me closing and opening the tab versus opening a new file in the same tab is six of one and a half-dozen of the other. I’d be interested to hear what difference it makes (other than maybe saving the trouble of recycling some objects and such).


#3

Thank you yet again!

For this specific feature, trying to change the text buffer instead of the tab will is better because of two things:

  1. There will be no animation while creating or destroying a tab
  2. Make sure that the tab will be kept in the same position.
    I’ll try to develop the switch text buffer soon, but right now the destroy window works very well.

On vim using only tabs it is not an option because many commands works on buffer only (like the vim command grep, for example). But there are at least some organizational advantages - a tab has many buffers. Would be the same than having a new tab of tabs. I use both tabs and buffers on my day-to-day development with vim.

Other advantage of having support to buffers is that you can have two tabs showing the same files on diferent points. This is the advantage that atom have by using text buffer, since one editor can only have one text buffer, but the text buffer can have more than one editor. But I understand that you are more interested in many text buffers for the same viewport feature.

The truth is that I don’t know how to use other text editors. I have been working on vim for so long that I can’t tell. Since I decided to use atom for a while, I’m be forced to evaluate wheter buffers are need or not. So maybe we can talk about this after a while? :slight_smile:


#4

Sure thing :slight_smile: The topic is something I’ve been thinking about for a long time and your topic just brought it to the surface now. It’ll keep :laughing:


#5

@fotanus Did you get anywhere with this? In particular, did you figure out how to create a new TextBuffer? The constructor doesn’t seem to be exposed, and I’m not sure if they are designed to be created by package code.

I wanted to create something like emacs’ narrow-to-selection, for which the ‘obvious’ approach might be to assign a new TextBuffer, setting aside the original one temporarily. But I’m not sure that the obvious approach is either possible or optimal.


#6

@crispinb Sorry, I end up opening a new tab and closing the previous one.


#7

OK, cheers. Assuming I can’t create a TextBuffer, that leaves the Very Ugly route (mutating the TextBuffer’s contents), which might be a bridge too far.


#8

I know I’m a bit late to the party, but I just ran into this issue and this works for me:

buffer = atom.workspace.getActiveEditor().buffer
buffer.setPath('/path/to/file')
buffer.load()