Skip to content

Instantly share code, notes, and snippets.

@ileasile
Created September 5, 2025 22:32
Show Gist options
  • Select an option

  • Save ileasile/7b1b2976732f41bba598b5883fbaa6f1 to your computer and use it in GitHub Desktop.

Select an option

Save ileasile/7b1b2976732f41bba598b5883fbaa6f1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells" : [ {
"cell_type" : "code",
"metadata" : {
"collapsed" : true,
"ExecuteTime" : {
"end_time" : "2025-09-05T22:16:00.417621Z",
"start_time" : "2025-09-05T22:15:49.748411Z"
}
},
"source" : "%use intellij-platform",
"id" : "f64a99d0005dc30b",
"outputs" : [ {
"data" : {
"text/plain" : [ "IntelliJ Platform integration is loaded" ]
},
"metadata" : { },
"output_type" : "display_data",
"jetTransient" : {
"display_id" : null
}
} ],
"execution_count" : 1
}, {
"metadata" : {
"ExecuteTime" : {
"end_time" : "2025-09-05T22:16:04.141297Z",
"start_time" : "2025-09-05T22:16:00.419080Z"
}
},
"cell_type" : "code",
"source" : "loadBundledPlugins(\"com.intellij.notebooks.core\", \"intellij.jupyter\")",
"id" : "48f5b431b683123",
"outputs" : [ ],
"execution_count" : 2
}, {
"metadata" : {
"ExecuteTime" : {
"end_time" : "2025-09-05T22:17:09.659240Z",
"start_time" : "2025-09-05T22:17:09.520654Z"
}
},
"cell_type" : "code",
"source" : [ "import com.intellij.openapi.vfs.VirtualFileManager\n", "\n", "val notebookVirtualFile = VirtualFileManager.getInstance().findFileByNioPath(notebook.workingDir.resolve(\"amend-motebook.ipynb\"))" ],
"id" : "16e48200c2546a25",
"outputs" : [ ],
"execution_count" : 3
}, {
"metadata" : {
"ExecuteTime" : {
"end_time" : "2025-09-05T22:17:13.912608Z",
"start_time" : "2025-09-05T22:17:13.869676Z"
}
},
"cell_type" : "code",
"source" : [ "import com.intellij.jupyter.core.jupyter.helper.jupyterNotebookOrNull\n", "\n", "val currentNotebook = notebookVirtualFile.jupyterNotebookOrNull!!" ],
"id" : "9896b11e6990870f",
"outputs" : [ ],
"execution_count" : 4
}, {
"metadata" : {
"ExecuteTime" : {
"end_time" : "2025-09-05T22:25:13.047051Z",
"start_time" : "2025-09-05T22:25:12.823232Z"
}
},
"cell_type" : "code",
"source" : [ "import com.intellij.openapi.editor.impl.EditorImpl\n", "import com.intellij.openapi.fileEditor.TextEditor\n", "\n", "val editor = (currentEditor() as? TextEditor)?.editor as EditorImpl" ],
"id" : "6e70620af73cc12d",
"outputs" : [ ],
"execution_count" : 5
}, {
"metadata" : {
"ExecuteTime" : {
"end_time" : "2025-09-05T22:26:15.891693Z",
"start_time" : "2025-09-05T22:26:15.599174Z"
}
},
"cell_type" : "code",
"source" : [ "import com.intellij.jupyter.core.core.impl.actions.NotebookCellLinesDocumentUtils.insertCells\n", "import com.intellij.jupyter.core.core.impl.actions.NotebookCellLinesEditorUtils.withIgnoreStandardEditorPositionKeeper\n", "import com.intellij.notebooks.visualization.NotebookCellLines\n", "import com.intellij.openapi.command.WriteCommandAction\n", "\n", "fun addNewCell(\n", " cellText: String,\n", " targetOrdinal: Int,\n", " onInsert: (NotebookCellLines.Interval) -> Unit,\n", ") {\n", " val project = currentProject()\n", " val cellLines = NotebookCellLines.get(editor)\n", " WriteCommandAction.writeCommandAction(project).withName(\"Inserting cell\").run<RuntimeException> {\n", " val insertedCells = editor.withIgnoreStandardEditorPositionKeeper {\n", " editor.document.insertCells(cellLines, cellText, targetOrdinal)\n", " }\n", " insertedCells.firstOrNull()?.let { cellInterval ->\n", " onInsert(cellInterval)\n", " }\n", " }\n", "}" ],
"id" : "941f16ff42436180",
"outputs" : [ ],
"execution_count" : 6
}, {
"metadata" : {
"ExecuteTime" : {
"end_time" : "2025-09-05T22:30:07.239510Z",
"start_time" : "2025-09-05T22:30:07.185541Z"
}
},
"cell_type" : "code",
"source" : [ "addNewCell(\"\"\"\n", " #%%\n", " 22 + 23 - 1\n", "\"\"\".trimIndent(), currentNotebook.cellsCount() - 1, {})" ],
"id" : "eb91dc2b78a2db31",
"outputs" : [ ],
"execution_count" : 9
}, {
"metadata" : { },
"cell_type" : "code",
"outputs" : [ ],
"execution_count" : null,
"source" : "22 + 23 - 1",
"id" : "bd151f47ca71a4b1"
}, {
"metadata" : {
"ExecuteTime" : {
"end_time" : "2025-09-05T22:30:42.574501Z",
"start_time" : "2025-09-05T22:30:42.519266Z"
}
},
"cell_type" : "code",
"source" : [ "addNewCell(\"\"\"\n", " #%% md\n", " # HEADER\n", "\"\"\".trimIndent(), currentNotebook.cellsCount() - 1, {})" ],
"id" : "b34df6f58396f307",
"outputs" : [ ],
"execution_count" : 10
}, {
"metadata" : { },
"cell_type" : "markdown",
"source" : "# HEADER",
"id" : "387c1ac943218774"
}, {
"metadata" : { },
"cell_type" : "code",
"outputs" : [ ],
"execution_count" : null,
"source" : "",
"id" : "8c87af203117df26"
} ],
"metadata" : {
"kernelspec" : {
"display_name" : "Kotlin",
"language" : "kotlin",
"name" : "kotlin"
},
"language_info" : {
"name" : "kotlin",
"version" : "2.2.20-Beta2",
"mimetype" : "text/x-kotlin",
"file_extension" : ".kt",
"pygments_lexer" : "kotlin",
"codemirror_mode" : "text/x-kotlin",
"nbconvert_exporter" : ""
},
"ktnbPluginMetadata" : {
"sessionRunMode" : "IDE_PROCESS"
}
},
"nbformat" : 4,
"nbformat_minor" : 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment