package org.jetbrains.plugins.ipnb.editor.actions;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.fileEditor.FileEditor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;

public class IpnbRunCellAction extends AnAction {
  public IpnbRunCellAction() {
    super(AllIcons.General.Run);
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent event) {
    final DataContext context = event.getDataContext();
    final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
    if (editor instanceof IpnbFileEditor) {
      final IpnbFilePanel component = ((IpnbFileEditor)editor).getIpnbFilePanel();
      runCell(component, false);
    }
  }

  public void runCell(@NotNull final IpnbFilePanel ipnbFilePanel, boolean selectNext) {
    final IpnbEditablePanel cell = ipnbFilePanel.getSelectedCell();
    cell.runCell();
    if (selectNext)
      ipnbFilePanel.selectNext(cell);
    ipnbFilePanel.revalidate();
    ipnbFilePanel.repaint();
    ipnbFilePanel.requestFocus();
  }
}

@@ -1274,16 +1274,187 @@
 ectNext)
+ %7B%0A      final int index = ipnbFilePanel.getSelectedIndex();%0A      if (ipnbFilePanel.getIpnbPanels().size()-1 == index) %7B%0A        ipnbFilePanel.createAndAddCell();%0A      %7D
 %0A      i
@@ -1484,16 +1484,22 @@
 (cell);%0A
+    %7D%0A
     ipnb

add new cell on run if it was the last cell in file