Search in sources :

Example 6 with IpnbEditablePanel

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

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

the class IpnbCodeCellAction method changeTypeToCode.

public void changeTypeToCode(@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 IpnbCodeCell codeCell = new IpnbCodeCell("python", cell.getSource(), null, Lists.newArrayList(), cell.getMetadata());
    if (index >= 0) {
        cells.set(index, codeCell);
    }
    filePanel.replaceComponent(selectedCellPanel, codeCell);
}
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) IpnbCodeCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)

Example 8 with IpnbEditablePanel

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

the class IpnbHeadingCellActionBase method changeTypeToHeading.

public void changeTypeToHeading(@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 IpnbHeadingCell heading = new IpnbHeadingCell(cell.getSource(), myLevel, cell.getMetadata());
    if (index >= 0) {
        cells.set(index, heading);
    }
    filePanel.replaceComponent(selectedCellPanel, heading);
}
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) IpnbHeadingCell(org.jetbrains.plugins.ipnb.format.cells.IpnbHeadingCell) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)

Example 9 with IpnbEditablePanel

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

the class IpnbParser method newDocumentText.

@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();
}
Also used : JsonWriter(com.google.gson.stream.JsonWriter) IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with IpnbEditablePanel

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

the class IpnbPyReference method multiResolve.

@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
    ResolveResult[] results = super.multiResolve(incompleteCode);
    if (results.length == 0) {
        PsiFile file = myElement.getContainingFile();
        if (file instanceof IpnbPyFragment) {
            final IpnbFilePanel panel = ((IpnbPyFragment) file).getFilePanel();
            final List<IpnbEditablePanel> panels = panel.getIpnbPanels();
            final String referencedName = myElement.getReferencedName();
            if (referencedName == null)
                return ResolveResult.EMPTY_ARRAY;
            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 PyResolveProcessor processor = new PyResolveProcessor(referencedName);
                PyResolveUtil.scopeCrawlUp(processor, psiFile, referencedName, psiFile);
                final List<RatedResolveResult> resultList = getResultsFromProcessor(referencedName, processor, psiFile, psiFile);
                if (resultList.size() > 0) {
                    List<RatedResolveResult> ret = RatedResolveResult.sorted(resultList);
                    return ret.toArray(new RatedResolveResult[ret.size()]);
                }
            }
        }
    }
    return results;
}
Also used : IpnbEditablePanel(org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel) 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) ResolveResult(com.intellij.psi.ResolveResult) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IpnbEditablePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbEditablePanel)12 IpnbFilePanel (org.jetbrains.plugins.ipnb.editor.panels.IpnbFilePanel)9 DataContext (com.intellij.openapi.actionSystem.DataContext)3 Editor (com.intellij.openapi.editor.Editor)3 IpnbFileEditor (org.jetbrains.plugins.ipnb.editor.IpnbFileEditor)3 IpnbCodePanel (org.jetbrains.plugins.ipnb.editor.panels.code.IpnbCodePanel)3 IpnbCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCell)3 IpnbEditableCell (org.jetbrains.plugins.ipnb.format.cells.IpnbEditableCell)3 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 JsonWriter (com.google.gson.stream.JsonWriter)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ResolveResult (com.intellij.psi.ResolveResult)1 Nullable (org.jetbrains.annotations.Nullable)1 IpnbConnectionManager (org.jetbrains.plugins.ipnb.configuration.IpnbConnectionManager)1 IpnbCodeCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell)1 IpnbHeadingCell (org.jetbrains.plugins.ipnb.format.cells.IpnbHeadingCell)1 IpnbMarkdownCell (org.jetbrains.plugins.ipnb.format.cells.IpnbMarkdownCell)1