<commit_before>'use babel';

export default class Workspace {
  open (workspace) {
    if (!workspace) {
      return;
    }

    atom.project.setPaths(workspace.projectPaths);

    {
      // TODO Refactor out
      const rootContainer = atom.workspace.paneContainer.getRoot();
      if (rootContainer.children) {
        for (const subPane of rootContainer.children) {
          subPane.destroy();
        }
      } else {
        rootContainer.destroy();
      }
    }

    for (const i in workspace.panes) {
      const subPane = workspace.panes[i];
      if (subPane.type === 'pane') {
        this.__createPane(subPane, i);
      } else if (subPane.type === 'paneAxis') {
        this.__createPaneAxis(subPane, i);
      }
    }
  }

  save (workspaceName) {
    if (!workspaceName) {
      return;
    }

    const projectPaths    = atom.project.getPaths();
    const rootContainer   = atom.workspace.paneContainer.getRoot();
    const panes           = this.__buildPaneMap(rootContainer);
    const workspaceToSave = { panes, projectPaths };

    let savedWorkspaces = atom.config.get('save-workspace-Workspaces') || {};
    savedWorkspaces[workspaceName] = workspaceToSave;

    atom.config.set('save-workspace-Workspaces', savedWorkspaces);
  }

  remove (workspaceName) {
    let model = atom.config.get('save-workspace-Workspaces');
    if( workspaceName in model){
      delete model[workspaceName];
      atom.config.set('save-workspace-Workspaces', model);
    }
  }

  __buildPaneMap (pane) {
    if (!pane.children) {
      return [this.__buildPaneItem(pane)];
    }

    // TODO ES6
    const panes = [];

    for (const child of pane.children) {
      panes.push(this.__buildPaneItem(child));
    }

    return panes;
  }

  __buildPaneItem (pane) {
    if (pane.children) {
      return {
        type: 'paneAxis',
        data: this.__buildPaneMap(pane),
        orientation: pane.orientation
      };
    }

    return {
      type: 'pane',
      data: this.__savePaneTabs(pane)
    };
  }

  __openTabs (tabs) {
    for (const uri of tabs) {
      atom.workspace.open(uri, {});
    }
  }

  __createPane (pane, paneIndex) {
    let newPane;

    if (paneIndex != '0' && pane.data.orientation) {
      const activePane = atom.workspace.paneContainer.getActivePane();
      if (pane.data.orientation === 'horizontal') {
        newPane = activePane.splitRight();
      } else {
        newPane = activePane.splitDown();
      }
    }

    if (newPane) {
      newPane.activate();
    }

    atom.workspace.paneForURI(pane.data.tabs[0]);
    this.__openTabs(pane.data.tabs);
  }

  __createPaneAxis (paneAxis, paneIndex) {
    if (paneIndex !== '0') {
      this.__splitPane(paneAxis)
    }

    for (const i in paneAxis.data) {
      const subPane = paneAxis.data[i];
      if (subPane.type === 'pane') {
        this.__createPane(subPane, i);
      } else if (subPane.type === 'paneAxis') {
        this.__createPaneAxis(subPane, i)
      }
    }
  }

  __splitPane (paneAxis) {
    let newPane;
    const activePane = atom.workspace.paneContainer.getActivePane();

    if (paneAxis.orientation === 'horizontal') {
      newPane = activePane.splitDown();
    } else {
      newPane = activePane.splitRight();
    }

    if (newPane) {
      newPane.activate();
    }
  }

  __savePaneTabs (pane) {
    const tabs = [];

    for (const tab of pane.items) {
      tabs.push(tab.getURI());
    }

    return {
       tabs: tabs,
       orientation: pane.parent.orientation
     };
  }
}
<commit_msg>SPIKE Cleaned up some pane splitting code<commit_after>'use babel';

export default class Workspace {
  open (workspace) {
    if (!workspace) {
      return;
    }

    atom.project.setPaths(workspace.projectPaths);

    {
      // TODO Refactor out
      const rootContainer = atom.workspace.paneContainer.getRoot();
      if (rootContainer.children) {
        for (const subPane of rootContainer.children) {
          subPane.destroy();
        }
      } else {
        rootContainer.destroy();
      }
    }

    for (const i in workspace.panes) {
      const subPane = workspace.panes[i];
      if (subPane.type === 'pane') {
        this.__createPane(subPane, i);
      } else if (subPane.type === 'paneAxis') {
        this.__createPaneAxis(subPane, i);
      }
    }
  }

  save (workspaceName) {
    if (!workspaceName) {
      return;
    }

    const projectPaths    = atom.project.getPaths();
    const rootContainer   = atom.workspace.paneContainer.getRoot();
    const panes           = this.__buildPaneMap(rootContainer);
    const workspaceToSave = { panes, projectPaths };

    let savedWorkspaces = atom.config.get('save-workspace-Workspaces') || {};
    savedWorkspaces[workspaceName] = workspaceToSave;

    atom.config.set('save-workspace-Workspaces', savedWorkspaces);
  }

  remove (workspaceName) {
    let model = atom.config.get('save-workspace-Workspaces');
    if( workspaceName in model){
      delete model[workspaceName];
      atom.config.set('save-workspace-Workspaces', model);
    }
  }

  __buildPaneMap (pane) {
    if (!pane.children) {
      return [this.__buildPaneItem(pane)];
    }

    return pane.children.map(this.__buildPaneItem);
  }

  __buildPaneItem (pane) {
    if (pane.children) {
      return {
        type: 'paneAxis',
        data: this.__buildPaneMap(pane),
        orientation: pane.orientation
      };
    }

    return {
      type: 'pane',
      data: this.__savePaneTabs(pane)
    };
  }

  __openTabs (tabs) {
    for (const uri of tabs) {
      atom.workspace.open(uri, {});
    }
  }

  __createPane (pane, paneIndex) {
    if (paneIndex != '0' && pane.data.orientation) {
      const activePane = atom.workspace.paneContainer.getActivePane();
      this.__splitAndActivate(activePane, pane.data.orientation, 'vertical');
    }

    atom.workspace.paneForURI(pane.data.tabs[0]);
    this.__openTabs(pane.data.tabs);
  }

  __createPaneAxis (paneAxis, paneIndex) {
    if (paneIndex !== '0') {
      this.__splitPane(paneAxis)
    }

    for (const i in paneAxis.data) {
      const subPane = paneAxis.data[i];
      if (subPane.type === 'pane') {
        this.__createPane(subPane, i);
      } else if (subPane.type === 'paneAxis') {
        this.__createPaneAxis(subPane, i)
      }
    }
  }

  __splitPane (paneAxis) {
    const activePane = atom.workspace.paneContainer.getActivePane();
    this.__splitAndActivate(activePane, paneAxis.orientation, 'horizontal');
  }

  __splitAndActivate (activePane, orientation, downSplitDirection) {
    const isSplitDown = orientation === downSplitDirection;
    const newPane = isSplitDown ? activePane.splitDown() : activePane.splitRight();
    newPane.activate();
  }

  __savePaneTabs (pane) {
    const tabs = [];

    for (const tab of pane.items) {
      tabs.push(tab.getURI());
    }

    return {
       tabs: tabs,
       orientation: pane.parent.orientation
     };
  }
}

{'fraction_non_alphanumeric': 0.1310763888888889, 'fraction_numerical': 0.0010127314814814814, 'mean_word_length': 2.521650534895568, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6308652', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2212, 'n_words': 412}