@Nullable public static String newDocumentText(@NotNull final IpnbFilePanel ipnbPanel) { final IpnbFile ipnbFile = ipnbPanel.getIpnbFile(); if (ipnbFile == null) return null; for (IpnbEditablePanel panel : ipnbPanel.getIpnbPanels()) { if (panel.isModified()) { panel.updateCellSource(); } } final IpnbFileRaw fileRaw = new IpnbFileRaw(); fileRaw.metadata = ipnbFile.getMetadata(); if (ipnbFile.getNbformat() == 4) { for (IpnbCell cell : ipnbFile.getCells()) { fileRaw.cells.add(IpnbCellRaw.fromCell(cell, ipnbFile.getNbformat())); } } else { final IpnbWorksheet worksheet = new IpnbWorksheet(); worksheet.cells.clear(); for (IpnbCell cell : ipnbFile.getCells()) { worksheet.cells.add(IpnbCellRaw.fromCell(cell, ipnbFile.getNbformat())); } fileRaw.worksheets = new IpnbWorksheet[] {worksheet}; } final StringWriter stringWriter = new StringWriter(); final JsonWriter writer = new JsonWriter(stringWriter); writer.setIndent(" "); gson.toJson(fileRaw, fileRaw.getClass(), writer); return stringWriter.toString(); }
@NotNull @Override public PsiFile getContext() { final PsiFile psiFile = PsiDocumentManager.getInstance(getProject()).getPsiFile(myFilePanel.getDocument()); return psiFile != null ? psiFile : super.getOriginalFile(); }
@Nullable @Override public PsiDirectory getContainingDirectory() { final VirtualFile file = myFilePanel.getVirtualFile(); final VirtualFile parentFile = file.getParent(); if (parentFile == null) { return super.getContainingDirectory(); } if (!parentFile.isValid()) { return super.getContainingDirectory(); } return getManager().findDirectory(parentFile); }
public static void saveIpnbFile(@NotNull final IpnbFilePanel ipnbPanel) { final String json = newDocumentText(ipnbPanel); if (json == null) return; writeToFile(ipnbPanel.getIpnbFile().getPath(), json); }