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();
}
};
}
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);
}
}
}
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());
}
}
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();
}
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);
}
}
Aggregations