import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
+import org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell;
public class IpnbAddCellAboveAction extends AnAction {
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
- ipnbFilePanel.createAndAddCell(false);
+ ipnbFilePanel.createAndAddCell(false, IpnbCodeCell.createEmptyCodeCell());
ipnbFilePanel.saveToFile();
}
});
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
+import org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell;
public class IpnbAddCellBelowAction extends AnAction {
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
- ipnbFilePanel.createAndAddCell(true);
+ ipnbFilePanel.createAndAddCell(true, IpnbCodeCell.createEmptyCodeCell());
ipnbFilePanel.saveToFile();
}
});
import org.jetbrains.plugins.ipnb.editor.IpnbFileEditor;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel;
import org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel;
+import org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell;
public abstract class IpnbRunCellBaseAction extends AnAction {
public IpnbRunCellBaseAction() {
if (selectNext) {
final int index = ipnbFilePanel.getSelectedIndex();
if (ipnbFilePanel.getIpnbPanels().size() - 1 == index) {
- ipnbFilePanel.createAndAddCell(true);
+ ipnbFilePanel.createAndAddCell(true, IpnbCodeCell.createEmptyCodeCell());
CommandProcessor.getInstance().executeCommand(ipnbFilePanel.getProject(), new Runnable() {
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
import org.jetbrains.plugins.ipnb.format.IpnbFile;
import org.jetbrains.plugins.ipnb.format.IpnbParser;
import org.jetbrains.plugins.ipnb.format.cells.*;
-import org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
public class IpnbFilePanel extends JPanel implements Scrollable, DataProvider, Disposable {
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
- createAndAddCell(true);
+ createAndAddCell(true, IpnbCodeCell.createEmptyCodeCell());
saveToFile();
}
});
}
}
- public void createAndAddCell(final boolean below) {
- final IpnbCodeCell cell = new IpnbCodeCell("python", Collections.emptyList(), null, new ArrayList<>(),
- null);
+ public void createAndAddCell(final boolean below, IpnbCodeCell cell) {
final IpnbCodePanel codePanel = new IpnbCodePanel(myProject, myParent, cell);
addCell(codePanel, below);
import org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
return new IpnbCodeCell(myLanguage, new ArrayList<>(getSource()), myPromptNumber, new ArrayList<>(myCellOutputs),
myMetadata);
}
+
+ @NotNull
+ public static IpnbCodeCell createEmptyCodeCell() {
+ return new IpnbCodeCell("python", Collections.emptyList(), null, new ArrayList<>(),
+ Collections.emptyMap());
+ }
}
--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ ""
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2.0
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.6"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
\ No newline at end of file
--- /dev/null
+import com.intellij.testFramework.LightVirtualFile;
+import junit.framework.TestCase;
+import org.jetbrains.plugins.ipnb.format.IpnbFile;
+import org.jetbrains.plugins.ipnb.format.IpnbParser;
+import org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell;
+
+import java.io.IOException;
+
+
+public class CellOperationTest extends TestCase {
+
+ public void testAddCell() throws IOException {
+ final String fileName = "testData/emptyFile.ipynb";
+ final String fileText = IpnbTestUtil.getFileText(fileName);
+
+ final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
+ ipnbFile.addCell(IpnbCodeCell.createEmptyCodeCell(), ipnbFile.getCells().size());
+ final IpnbCodeCell cell = (IpnbCodeCell)ipnbFile.getCells().get(ipnbFile.getCells().size() - 1);
+ assertTrue(cell.getCellOutputs().isEmpty());
+ assertNull(cell.getPromptNumber());
+ assertTrue(cell.getMetadata().isEmpty());
+ }
+
+ public void testRemoveCell() throws IOException {
+ final String fileName = "testData/emptyFile.ipynb";
+ final String fileText = IpnbTestUtil.getFileText(fileName);
+
+ final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
+ ipnbFile.addCell(IpnbCodeCell.createEmptyCodeCell(), ipnbFile.getCells().size());
+ ipnbFile.removeCell(ipnbFile.getCells().size() - 1);
+
+ assertEquals(fileText, IpnbTestUtil.getFileText(fileName));
+ }
+}
--- /dev/null
+import com.intellij.openapi.application.PathManager;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+
+public class IpnbTestUtil {
+ static String getFileText(@NotNull final String fileName) throws IOException {
+ String fullPath = PathManager.getHomePath() + "/community/python/ipnb/" + fileName;
+ final BufferedReader br = new BufferedReader(new FileReader(fullPath));
+ try {
+ final StringBuilder sb = new StringBuilder();
+ String line = br.readLine();
+
+ while (line != null) {
+ sb.append(line);
+ sb.append("\n");
+ line = br.readLine();
+ }
+ return sb.toString();
+ }
+ finally {
+ br.close();
+ }
+ }
+}
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
-import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.LightVirtualFile;
import junit.framework.TestCase;
-import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.ipnb.format.IpnbFile;
import org.jetbrains.plugins.ipnb.format.IpnbParser;
import org.jetbrains.plugins.ipnb.format.cells.IpnbCell;
import org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell;
import org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell;
-import java.io.BufferedReader;
-import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class JsonParserTest extends TestCase {
public void testFile() throws IOException {
final String fileName = "testData/SymPy.ipynb";
- final String fileText = getFileText(fileName);
+ final String fileText = IpnbTestUtil.getFileText(fileName);
final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
assertNotNull(ipnbFile);
public void testMarkdownCells() throws IOException {
final String fileName = "testData/SymPy.ipynb";
- final String fileText = getFileText(fileName);
+ final String fileText = IpnbTestUtil.getFileText(fileName);
final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
assertNotNull(ipnbFile);
final List<IpnbCell> cells = ipnbFile.getCells();
public void testMarkdownCell() throws IOException {
final String fileName = "testData/markdown.ipynb";
- final String fileText = getFileText(fileName);
+ final String fileText = IpnbTestUtil.getFileText(fileName);
final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
assertNotNull(ipnbFile);
final List<IpnbCell> cells = ipnbFile.getCells();
public void testCodeCell() throws IOException {
final String fileName = "testData/code.ipynb";
- final String fileText = getFileText(fileName);
+ final String fileText = IpnbTestUtil.getFileText(fileName);
final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
assertNotNull(ipnbFile);
final List<IpnbCell> cells = ipnbFile.getCells();
public void testOutputs() throws IOException {
final String fileName = "testData/outputs.ipynb";
- final String fileText = getFileText(fileName);
+ final String fileText = IpnbTestUtil.getFileText(fileName);
final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
assertNotNull(ipnbFile);
final List<IpnbCell> cells = ipnbFile.getCells();
final String joined = StringUtil.join(text, "");
assertEquals("\"Add(Symbol('x'), Mul(Integer(2), Symbol('y')))\"", joined);
}
-
- private static String getFileText(@NotNull final String fileName) throws IOException {
- String fullPath = PathManager.getHomePath() + "/community/python/ipnb/" + fileName;
- final BufferedReader br = new BufferedReader(new FileReader(fullPath));
- try {
- final StringBuilder sb = new StringBuilder();
- String line = br.readLine();
-
- while (line != null) {
- sb.append(line);
- sb.append("\n");
- line = br.readLine();
- }
- return sb.toString();
- }
- finally {
- br.close();
- }
- }
}