void (@NotNull AnActionEvent e) { Project project = e.getProject(); if (project == null) return; Component component = e.getData(PlatformCoreDataKeys.CONTEXT_COMPONENT); // selection from the current editor ScratchFileCreationHelper.Context context = createContext(e); LanguageItem selectionItem = context.language != null ? LanguageItem.fromLanguage(context.language) : context.fileExtension != null ? new LanguageItem( null, FileTypeManager.getInstance().getFileTypeByExtension(context.fileExtension), context.fileExtension) : StringUtil.isNotEmpty(context.text) ? new LanguageItem( null, PlainTextFileType.INSTANCE, PlainTextFileType.INSTANCE.getDefaultExtension()) : null; // extract text from the focused component, e.g. a tree or a list ScratchImplUtil.TextExtractor textExtractor = selectionItem == null ? ScratchImplUtil.getTextExtractor(component) : null; LanguageItem extractItem = textExtractor != null && StringUtil.isEmpty(context.text) && !EditorUtil.isRealFileEditor(e.getData(CommonDataKeys.EDITOR)) ? new LanguageItem(null, PlainTextFileType.INSTANCE, PlainTextFileType.INSTANCE.getDefaultExtension()) : null; Consumer<LanguageItem> consumer = o -> { context.language = o.language(); context.fileExtension = o.fileExtension(); if (o == extractItem) { context.text = StringUtil.notNullize(textExtractor.extractText()); context.caretOffset = 0; } else if (o != selectionItem) { context.text = ""; context.caretOffset = 0; } doCreateNewScratch(project, context); }; if (selectionItem != null && ApplicationManager.getApplication().isUnitTestMode()) { consumer.consume(selectionItem); return; } LRUPopupBuilder<LanguageItem> builder = ScratchImplUtil.buildLanguagesPopup( project, ActionsBundle.message("action.NewScratchFile.text.with.new")); if (selectionItem != null) { String displayName = LangBundle.message("scratch.file.action.new.from.selection", selectionItem.fileType().getDisplayName()); builder.withExtraTopValue(selectionItem, displayName, EmptyIcon.ICON_16); } else if (extractItem != null) { String displayName = LangBundle.message("scratch.file.action.new.from.ui"); if (textExtractor.hasSelection()) { builder.withExtraTopValue(extractItem, displayName, EmptyIcon.ICON_16); } else { builder.withExtraMiddleValue(extractItem, displayName, EmptyIcon.ICON_16); } } builder .onChosen(consumer) .buildPopup() .showCenteredInCurrentWindow(project); }