[pycharm] PY-73444 Jupyter (refactor): Simplified work with AnActionEvent and DataContext, a number of shortcuts made.

For example:
event.editor
event.contextComponent
and many more.

GitOrigin-RevId: c8ff675531b7cf25308c6da981e020e80efed867
This commit is contained in:
Nikita Pavlenko
2024-06-21 17:26:45 +02:00
committed by intellij-monorepo-bot
parent 65bc1209a6
commit eb1ab8c490

View File

@@ -13,7 +13,7 @@ abstract class CellLinesActionBase : DumbAwareAction() {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
override fun update(event: AnActionEvent) {
event.presentation.isEnabled = event.dataContext.getAnyEditor()
event.presentation.isEnabled = event.getAnyEditor()
?.takeIf { NotebookCellLines.hasSupport(it) } != null
}
@@ -25,18 +25,20 @@ abstract class CellLinesActionBase : DumbAwareAction() {
final override fun actionPerformed(event: AnActionEvent) {
if (isModifyingSourceCode() && !isEditingAllowed(event)) return
val editor = event.dataContext.getAnyEditor() ?: return
val editor = event.getAnyEditor() ?: return
val cellLines = NotebookCellLines.get(editor)
actionPerformed(event, editor, cellLines)
}
}
fun DataContext.getAnyEditor(): EditorImpl? =
fun DataContext.getAnyEditor() =
getData(EDITORS_WITH_OFFSETS_DATA_KEY)
?.firstOrNull()
?.first
?.asSafely<EditorImpl>()
fun AnActionEvent.getAnyEditor(): EditorImpl? = dataContext.getAnyEditor()
fun isEditingAllowed(e: AnActionEvent): Boolean {
val virtualFile = e.dataContext.getData(PlatformCoreDataKeys.FILE_EDITOR)?.file ?: return false
val project = e.dataContext.getData(PlatformCoreDataKeys.PROJECT) ?: return false