go-back-view.js 979 B

1234567891011121314151617181920212223242526272829
  1. const SymbolsView = require('./symbols-view');
  2. // TODO: Does this really need to extend SymbolsView?
  3. module.exports = class GoBackView extends SymbolsView {
  4. toggle() {
  5. let previous = this.stack.pop();
  6. if (!previous) return;
  7. let restorePosition = () => {
  8. if (!previous.position) return;
  9. this.moveToPosition(previous.position, { beginningOfLine: false });
  10. };
  11. let allEditors = atom.workspace.getTextEditors();
  12. let previousEditor = allEditors.find(e => e.id === previous.editorId);
  13. if (previousEditor) {
  14. let pane = atom.workspace.paneForItem(previousEditor);
  15. pane.setActiveItem(previousEditor);
  16. restorePosition();
  17. } else if (previous.file) {
  18. // The editor is not there anymore; e.g., a package like `zentabs` might
  19. // have automatically closed it when a new editor view was opened. So we
  20. // should restore it if we can.
  21. atom.workspace.open(previous.file).then(restorePosition);
  22. }
  23. }
  24. }