Search in sources :

Example 1 with IpnbCodePanel

use of org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel in project intellij-community by JetBrains.

the class IpnbConnectionManager method createConnectionListener.

@NotNull
private IpnbConnectionListenerBase createConnectionListener(@Nullable IpnbCodePanel codePanel, boolean[] connectionOpened) {
    return new IpnbConnectionListenerBase() {

        @Override
        public void onOpen(@NotNull IpnbConnection connection) {
            connectionOpened[0] = true;
            if (codePanel == null)
                return;
            final String messageId = connection.execute(codePanel.getCell().getSourceAsString());
            myUpdateMap.put(messageId, codePanel);
        }

        @Override
        public void onOutput(@NotNull IpnbConnection connection, @NotNull String parentMessageId) {
            if (!myUpdateMap.containsKey(parentMessageId))
                return;
            final IpnbCodePanel cell = myUpdateMap.get(parentMessageId);
            cell.getCell().setPromptNumber(connection.getExecCount());
            cell.updatePanel(null, connection.getOutput());
        }

        @Override
        public void onPayload(@Nullable String payload, @NotNull String parentMessageId) {
            if (!myUpdateMap.containsKey(parentMessageId))
                return;
            final IpnbCodePanel cell = myUpdateMap.remove(parentMessageId);
            if (payload != null) {
                cell.updatePanel(payload, null);
            }
        }

        @Override
        public void onFinished(@NotNull IpnbConnection connection, @NotNull String parentMessageId) {
            if (!myUpdateMap.containsKey(parentMessageId))
                return;
            final IpnbCodePanel cell = myUpdateMap.remove(parentMessageId);
            cell.getCell().setPromptNumber(connection.getExecCount());
            cell.finishExecution();
        }
    };
}
Also used : IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IpnbCodePanel(org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel) NotNull(org.jetbrains.annotations.NotNull) IpnbConnectionListenerBase(org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with IpnbCodePanel

use of org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel 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 3 with IpnbCodePanel

use of org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel in project intellij-community by JetBrains.

the class IpnbFilePanel method addCellToPanel.

private void addCellToPanel(IpnbCell cell) {
    IpnbEditablePanel panel;
    if (cell instanceof IpnbCodeCell) {
        panel = new IpnbCodePanel(myProject, myParent, (IpnbCodeCell) cell);
        add(panel);
        myIpnbPanels.add(panel);
    } else if (cell instanceof IpnbMarkdownCell) {
        panel = new IpnbMarkdownPanel((IpnbMarkdownCell) cell, this);
        addComponent(panel);
    } else if (cell instanceof IpnbHeadingCell) {
        panel = new IpnbHeadingPanel((IpnbHeadingCell) cell);
        addComponent(panel);
    } else if (cell instanceof IpnbRawCell) {
    // A raw cell is defined as content that should be included unmodified in nbconvert output.
    // It's not visible for user
    } else {
        throw new UnsupportedOperationException(cell.getClass().toString());
    }
}
Also used : IpnbCodePanel(org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)

Example 4 with IpnbCodePanel

use of org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel in project intellij-community by JetBrains.

the class IpnbPyReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    final List<Object> variants = Lists.newArrayList();
    Collections.addAll(variants, super.getVariants());
    PsiFile file = myElement.getContainingFile();
    if (file instanceof IpnbPyFragment) {
        final IpnbFilePanel panel = ((IpnbPyFragment) file).getFilePanel();
        final List<IpnbEditablePanel> panels = panel.getIpnbPanels();
        for (IpnbEditablePanel editablePanel : panels) {
            if (!(editablePanel instanceof IpnbCodePanel))
                continue;
            final Editor editor = ((IpnbCodePanel) editablePanel).getEditor();
            final IpnbPyFragment psiFile = (IpnbPyFragment) PsiDocumentManager.getInstance(myElement.getProject()).getPsiFile(editor.getDocument());
            if (psiFile == null)
                continue;
            final CompletionVariantsProcessor processor = new CompletionVariantsProcessor(myElement);
            PyResolveUtil.scopeCrawlUp(processor, psiFile, null, null);
            variants.addAll(getOriginalElements(processor));
        }
    }
    return variants.toArray();
}
Also used : IpnbFilePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel) PsiFile(com.intellij.psi.PsiFile) IpnbCodePanel(org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel) Editor(com.intellij.openapi.editor.Editor) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with IpnbCodePanel

use of org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel in project intellij-community by JetBrains.

the class IpnbFileEditor method updateCellTypeCombo.

private void updateCellTypeCombo(@NotNull final IpnbPanel ipnbPanel) {
    if (ipnbPanel instanceof IpnbHeadingPanel) {
        final IpnbHeadingCell cell = ((IpnbHeadingPanel) ipnbPanel).getCell();
        final int level = cell.getLevel();
        myCellTypeCombo.setSelectedItem(headingCellType + level);
    } else if (ipnbPanel instanceof IpnbMarkdownPanel) {
        myCellTypeCombo.setSelectedItem(markdownCellType);
    } else if (ipnbPanel instanceof IpnbCodePanel) {
        myCellTypeCombo.setSelectedItem(codeCellType);
    }
}
Also used : IpnbHeadingCell(org.jetbrains.plugins.ipnb.format.cells.IpnbHeadingCell) IpnbCodePanel(org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)

Aggregations

IpnbCodePanel (org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)10 Editor (com.intellij.openapi.editor.Editor)5 IpnbFilePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel)5 NotNull (org.jetbrains.annotations.NotNull)3 IpnbEditablePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)3 PsiFile (com.intellij.psi.PsiFile)2 IpnbFileEditor (org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)2 IpnbCodeSourcePanel (org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodeSourcePanel)2 IpnbHeadingCell (org.jetbrains.plugins.ipnb.format.cells.IpnbHeadingCell)2 ResolveResult (com.intellij.psi.ResolveResult)1 List (java.util.List)1 Nullable (org.jetbrains.annotations.Nullable)1 IpnbCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCell)1 IpnbCodeCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell)1 IpnbMarkdownCell (org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell)1 IpnbConnection (org.jetbrains.plugins.ipnb.protocol.IpnbConnection)1 IpnbConnectionListenerBase (org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)1