Search in sources :

Example 1 with IpnbCell

use of org.jetbrains.plugins.ipnb.format.cells.IpnbCell in project intellij-community by JetBrains.

the class JsonParserTest method testOutputs.

public void testOutputs() throws IOException {
    final String fileName = "testData/outputs.ipynb";
    final String fileText = IpnbTestCase.getFileText(fileName);
    final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
    assertNotNull(ipnbFile);
    final List<IpnbCell> cells = ipnbFile.getCells();
    assertEquals(1, cells.size());
    final IpnbCell cell = cells.get(0);
    assertTrue(cell instanceof IpnbCodeCell);
    final List<IpnbOutputCell> outputs = ((IpnbCodeCell) cell).getCellOutputs();
    assertEquals(1, outputs.size());
    final IpnbOutputCell output = outputs.get(0);
    final List<String> text = output.getText();
    assertNotNull(text);
    final String joined = StringUtil.join(text, "");
    assertEquals("\"Add(Symbol('x'), Mul(Integer(2), Symbol('y')))\"", joined);
}
Also used : IpnbOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell) IpnbCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCell) IpnbCodeCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) IpnbFile(org.jetbrains.plugins.ipnb.format.IpnbFile)

Example 2 with IpnbCell

use of org.jetbrains.plugins.ipnb.format.cells.IpnbCell in project intellij-community by JetBrains.

the class JsonParserTest method testCodeCell.

public void testCodeCell() throws IOException {
    final String fileName = "testData/code.ipynb";
    final String fileText = IpnbTestCase.getFileText(fileName);
    final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
    assertNotNull(ipnbFile);
    final List<IpnbCell> cells = ipnbFile.getCells();
    assertEquals(1, cells.size());
    final IpnbCell cell = cells.get(0);
    assertTrue(cell instanceof IpnbCodeCell);
    final List<IpnbOutputCell> outputs = ((IpnbCodeCell) cell).getCellOutputs();
    assertEquals(0, outputs.size());
    final List<String> source = ((IpnbCodeCell) cell).getSource();
    final String joined = StringUtil.join(source, "");
    assertEquals("e = x + 2*y", joined);
    final String language = ((IpnbCodeCell) cell).getLanguage();
    assertEquals("python", language);
    final Integer number = ((IpnbCodeCell) cell).getPromptNumber();
    assertEquals(new Integer(4), number);
}
Also used : IpnbOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell) IpnbCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCell) IpnbCodeCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) IpnbFile(org.jetbrains.plugins.ipnb.format.IpnbFile)

Example 3 with IpnbCell

use of org.jetbrains.plugins.ipnb.format.cells.IpnbCell in project intellij-community by JetBrains.

the class JsonParserTest method testMarkdownCell.

public void testMarkdownCell() throws IOException {
    final String fileName = "testData/markdown.ipynb";
    final String fileText = IpnbTestCase.getFileText(fileName);
    final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
    assertNotNull(ipnbFile);
    final List<IpnbCell> cells = ipnbFile.getCells();
    assertEquals(1, cells.size());
    final IpnbCell cell = cells.get(0);
    assertTrue(cell instanceof IpnbMarkdownCell);
    final List<String> source = ((IpnbMarkdownCell) cell).getSource();
    final String joined = StringUtil.join(source, "");
    assertEquals("<img src=\"images/ipython_logo.png\">", joined);
}
Also used : IpnbCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCell) IpnbMarkdownCell(org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) IpnbFile(org.jetbrains.plugins.ipnb.format.IpnbFile)

Example 4 with IpnbCell

use of org.jetbrains.plugins.ipnb.format.cells.IpnbCell in project intellij-community by JetBrains.

the class IpnbFileEditor method updateCellType.

private void updateCellType(@NotNull final String selectedItem, @NotNull final IpnbEditablePanel selectedCell) {
    selectedCell.updateCellSource();
    if (selectedCell instanceof IpnbHeadingPanel) {
        final IpnbHeadingCell cell = ((IpnbHeadingPanel) selectedCell).getCell();
        if (selectedItem.startsWith(headingCellType)) {
            final char c = selectedItem.charAt(selectedItem.length() - 1);
            final int level = Character.getNumericValue(c);
            if (level != cell.getLevel()) {
                cell.setLevel(level);
                selectedCell.updateCellView();
            }
        } else if (selectedItem.equals(markdownCellType)) {
            final List<IpnbCell> cells = myIpnbFilePanel.getIpnbFile().getCells();
            final int index = cells.indexOf(((IpnbHeadingPanel) selectedCell).getCell());
            final IpnbMarkdownCell markdownCell = new IpnbMarkdownCell(cell.getSource(), cell.getMetadata());
            if (index >= 0) {
                cells.set(index, markdownCell);
            }
            myIpnbFilePanel.replaceComponent(selectedCell, markdownCell);
        } else if (selectedItem.equals(codeCellType)) {
            final List<IpnbCell> cells = myIpnbFilePanel.getIpnbFile().getCells();
            final int index = cells.indexOf(((IpnbHeadingPanel) selectedCell).getCell());
            final IpnbCodeCell codeCell = new IpnbCodeCell("python", cell.getSource(), null, Lists.newArrayList(), cell.getMetadata());
            if (index >= 0) {
                cells.set(index, codeCell);
            }
            myIpnbFilePanel.replaceComponent(selectedCell, codeCell);
        }
    } else if (selectedCell instanceof IpnbMarkdownPanel) {
        final IpnbMarkdownCell cell = ((IpnbMarkdownPanel) selectedCell).getCell();
        if (selectedItem.startsWith(headingCellType)) {
            final char c = selectedItem.charAt(selectedItem.length() - 1);
            final int level = Character.getNumericValue(c);
            final List<IpnbCell> cells = myIpnbFilePanel.getIpnbFile().getCells();
            final int index = cells.indexOf(((IpnbMarkdownPanel) selectedCell).getCell());
            final IpnbHeadingCell headingCell = new IpnbHeadingCell(cell.getSource(), level, cell.getMetadata());
            if (index >= 0) {
                cells.set(index, headingCell);
            }
            myIpnbFilePanel.replaceComponent(selectedCell, headingCell);
        } else if (selectedItem.equals(codeCellType)) {
            final List<IpnbCell> cells = myIpnbFilePanel.getIpnbFile().getCells();
            final int index = cells.indexOf(((IpnbMarkdownPanel) selectedCell).getCell());
            final IpnbCodeCell codeCell = new IpnbCodeCell("python", cell.getSource(), null, Lists.newArrayList(), cell.getMetadata());
            if (index >= 0) {
                cells.set(index, codeCell);
            }
            myIpnbFilePanel.replaceComponent(selectedCell, codeCell);
        }
    } else if (selectedCell instanceof IpnbCodePanel) {
        final IpnbCodeCell cell = ((IpnbCodePanel) selectedCell).getCell();
        if (selectedItem.startsWith(headingCellType)) {
            final char c = selectedItem.charAt(selectedItem.length() - 1);
            final int level = Character.getNumericValue(c);
            final List<IpnbCell> cells = myIpnbFilePanel.getIpnbFile().getCells();
            final int index = cells.indexOf(((IpnbCodePanel) selectedCell).getCell());
            final IpnbHeadingCell headingCell = new IpnbHeadingCell(cell.getSource(), level, cell.getMetadata());
            if (index >= 0) {
                cells.set(index, headingCell);
            }
            myIpnbFilePanel.replaceComponent(selectedCell, headingCell);
        } else if (selectedItem.equals(markdownCellType)) {
            final List<IpnbCell> cells = myIpnbFilePanel.getIpnbFile().getCells();
            final int index = cells.indexOf(((IpnbCodePanel) selectedCell).getCell());
            final IpnbMarkdownCell markdownCell = new IpnbMarkdownCell(cell.getSource(), cell.getMetadata());
            if (index >= 0) {
                cells.set(index, markdownCell);
            }
            myIpnbFilePanel.replaceComponent(selectedCell, markdownCell);
        }
    }
}
Also used : IpnbCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCell) IpnbMarkdownCell(org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell) IpnbCodeCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell) List(java.util.List) IpnbHeadingCell(org.jetbrains.plugins.ipnb.format.cells.IpnbHeadingCell) IpnbCodePanel(org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)

Example 5 with IpnbCell

use of org.jetbrains.plugins.ipnb.format.cells.IpnbCell in project intellij-community by JetBrains.

the class IpnbMarkdownCellAction method changeTypeToMarkdown.

public void changeTypeToMarkdown(@NotNull final IpnbFileEditor editor) {
    final IpnbFilePanel filePanel = editor.getIpnbFilePanel();
    final IpnbEditablePanel selectedCellPanel = filePanel.getSelectedCellPanel();
    if (selectedCellPanel == null)
        return;
    final IpnbEditableCell cell = selectedCellPanel.getCell();
    final List<IpnbCell> cells = filePanel.getIpnbFile().getCells();
    final int index = cells.indexOf(selectedCellPanel.getCell());
    final IpnbMarkdownCell markdownCell = new IpnbMarkdownCell(cell.getSource(), cell.getMetadata());
    if (index >= 0) {
        cells.set(index, markdownCell);
    }
    filePanel.replaceComponent(selectedCellPanel, markdownCell);
}
Also used : IpnbCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCell) IpnbFilePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel) IpnbEditableCell(org.jetbrains.plugins.ipnb.format.cells.IpnbEditableCell) IpnbMarkdownCell(org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)

Aggregations

IpnbCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCell)8 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)4 IpnbFile (org.jetbrains.plugins.ipnb.format.IpnbFile)4 IpnbCodeCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell)4 IpnbMarkdownCell (org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell)4 IpnbEditablePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)3 IpnbFilePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel)3 IpnbEditableCell (org.jetbrains.plugins.ipnb.format.cells.IpnbEditableCell)3 IpnbHeadingCell (org.jetbrains.plugins.ipnb.format.cells.IpnbHeadingCell)2 IpnbOutputCell (org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell)2 List (java.util.List)1 IpnbCodePanel (org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)1