From a5cd6ac6102731ea9b557dcc1c684340f7d8432a Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Tue, 18 Aug 2015 02:55:58 +0300 Subject: [PATCH] IDEA-143597 New behavior of creating Scratch Files is distracting --- .../intellij/ide/scratch/LRUPopupBuilder.java | 8 +- ...ileAction.java => ScratchFileActions.java} | 112 +++++++++++------- .../intellij/ide/scratch/ScratchRootType.java | 13 +- .../src/messages/ActionsBundle.properties | 2 + .../src/idea/LangActions.xml | 5 +- .../util/resources/misc/registry.properties | 5 +- 6 files changed, 92 insertions(+), 53 deletions(-) rename platform/lang-impl/src/com/intellij/ide/scratch/{NewScratchFileAction.java => ScratchFileActions.java} (64%) diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/LRUPopupBuilder.java b/platform/lang-impl/src/com/intellij/ide/scratch/LRUPopupBuilder.java index 8c799e7a3315..2676a3f9a1f4 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/LRUPopupBuilder.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/LRUPopupBuilder.java @@ -63,18 +63,14 @@ public abstract class LRUPopupBuilder { @NotNull public static ListPopup forFileLanguages(@NotNull Project project, @NotNull final Iterable files, @NotNull final PerFileMappings mappings) { - Consumer onChosen = new Consumer() { + return forFileLanguages(project, null, new Consumer() { @Override public void consume(Language t) { for (VirtualFile file : files) { mappings.setMapping(file, t); } } - }; - return languagePopupBuilder(project, "Languages"). - forValues(mappings.getAvailableValues(null)). - onChosen(onChosen). - buildPopup(); + }); } @NotNull diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/NewScratchFileAction.java b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileActions.java similarity index 64% rename from platform/lang-impl/src/com/intellij/ide/scratch/NewScratchFileAction.java rename to platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileActions.java index ea4a120cb16c..ec98e32a11ee 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/NewScratchFileAction.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchFileActions.java @@ -22,12 +22,9 @@ import com.intellij.lang.StdLanguages; import com.intellij.lang.injection.InjectedLanguageManager; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Caret; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.fileEditor.FileEditorManager; -import com.intellij.openapi.fileEditor.TextEditor; import com.intellij.openapi.fileTypes.LanguageFileType; import com.intellij.openapi.project.DumbAwareAction; import com.intellij.openapi.project.Project; @@ -38,6 +35,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.LanguageSubstitutors; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; +import com.intellij.util.Consumer; import com.intellij.util.Function; import com.intellij.util.ObjectUtils; import com.intellij.util.containers.ContainerUtil; @@ -52,38 +50,89 @@ import static com.intellij.openapi.util.Conditions.*; /** * @author ignatov */ -public class NewScratchFileAction extends DumbAwareAction { +public class ScratchFileActions { - @Override - public void update(@NotNull AnActionEvent e) { - e.getPresentation().setEnabledAndVisible(isEnabled(e)); + public static class NewFileAction extends DumbAwareAction { + + @Override + public void update(@NotNull AnActionEvent e) { + boolean enabled = e.getProject() != null && Registry.is("ide.scratch.enabled"); + e.getPresentation().setEnabledAndVisible(enabled); + } + + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + final Project project = e.getProject(); + if (project == null) return; + + PsiFile file = e.getData(CommonDataKeys.PSI_FILE); + Editor editor = e.getData(CommonDataKeys.EDITOR); + + final String text = StringUtil.notNullize(getSelectionText(editor)); + Language language = text.isEmpty() ? null : getLanguageFromCaret(project, editor, file); + Consumer consumer = new Consumer() { + @Override + public void consume(Language language) { + doCreateNewScratch(project, false, language, text); + } + }; + if (language != null) { + consumer.consume(language); + } + else { + LRUPopupBuilder.forFileLanguages(project, null, consumer).showCenteredInCurrentWindow(project); + } + } } - public static boolean isEnabled(@NotNull AnActionEvent e) { - return e.getProject() != null && Registry.is("ide.scratch.enabled"); + public static class NewBufferAction extends DumbAwareAction { + + @Override + public void update(@NotNull AnActionEvent e) { + boolean enabled = e.getProject() != null && Registry.is("ide.scratch.enabled") && Registry.intValue("ide.scratch.buffers") > 0; + e.getPresentation().setEnabledAndVisible(enabled); + } + + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + Project project = e.getProject(); + if (project == null) return; + + PsiFile file = e.getData(CommonDataKeys.PSI_FILE); + Editor editor = e.getData(CommonDataKeys.EDITOR); + + String text = StringUtil.notNullize(getSelectionText(editor)); + Language language = text.isEmpty() ? null : getLanguageFromCaret(project, editor, file); + + doCreateNewScratch(project, true, ObjectUtils.notNull(language, StdLanguages.TEXT), text); + } } - @Override - public void actionPerformed(@NotNull AnActionEvent e) { - final Project project = e.getProject(); - if (project == null) return; - - PsiFile file = e.getData(CommonDataKeys.PSI_FILE); - Editor editor = e.getData(CommonDataKeys.EDITOR); + static void doCreateNewScratch(@NotNull Project project, boolean buffer, @NotNull Language language, @NotNull String text) { + FeatureUsageTracker.getInstance().triggerFeatureUsed("scratch"); - String text = StringUtil.notNullize(getSelectionText(editor)); - Language language = ObjectUtils.notNull(text.isEmpty() ? null : getLanguageFromCaret(project, editor, file), Language.ANY); - openNewFile(project, language, text); + String fileName = buffer ? "buffer" + nextBufferIndex() : "scratch"; + ScratchFileService.Option option = buffer ? ScratchFileService.Option.create_if_missing : ScratchFileService.Option.create_new_always; + VirtualFile f = ScratchRootType.getInstance().createScratchFile(project, fileName, language, text, option); + if (f != null) { + FileEditorManager.getInstance(project).openFile(f, true); + } + } + + private static int ourCurrentBuffer = 0; + private static int nextBufferIndex() { + ourCurrentBuffer = (ourCurrentBuffer % Registry.intValue("ide.scratch.buffers")) + 1; + return ourCurrentBuffer; } @Nullable - public String getSelectionText(@Nullable Editor editor) { + static String getSelectionText(@Nullable Editor editor) { if (editor == null) return null; return editor.getSelectionModel().getSelectedText(); } @Nullable - public Language getLanguageFromCaret(@NotNull Project project, + static Language getLanguageFromCaret(@NotNull Project project, @Nullable Editor editor, @Nullable PsiFile psiFile) { if (editor == null || psiFile == null) return null; @@ -94,27 +143,6 @@ public class NewScratchFileAction extends DumbAwareAction { return file.getLanguage(); } - public static VirtualFile openNewFile(@NotNull final Project project, @NotNull Language language, @NotNull String text) { - FeatureUsageTracker.getInstance().triggerFeatureUsed("scratch"); - Language initialLanguage = language == Language.ANY ? StdLanguages.TEXT : language; - final VirtualFile file = ScratchRootType.getInstance().createScratchFile(project, "scratch", initialLanguage, text); - if (file != null) { - FileEditor[] editors = FileEditorManager.getInstance(project).openFile(file, true); - if (language == Language.ANY && editors.length != 0 && editors[0] instanceof TextEditor) { - final TextEditor textEditor = (TextEditor)editors[0]; - ApplicationManager.getApplication().invokeLater(new Runnable() { - @Override - public void run() { - if (!textEditor.isValid()) return; - PerFileMappings mappings = ScratchFileService.getInstance().getScratchesMapping(); - LRUPopupBuilder.forFileLanguages(project, JBIterable.of(file), mappings).showInBestPositionFor(textEditor.getEditor()); - } - }); - } - } - return file; - } - @NotNull private static Function ROOT_TYPE(final ScratchFileService service) { return new Function() { diff --git a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchRootType.java b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchRootType.java index 7cb0c0c8df87..634858a6c6da 100644 --- a/platform/lang-impl/src/com/intellij/ide/scratch/ScratchRootType.java +++ b/platform/lang-impl/src/com/intellij/ide/scratch/ScratchRootType.java @@ -60,7 +60,17 @@ public final class ScratchRootType extends RootType { return LayeredIcon.create(icon, AllIcons.Actions.Scratch); } + @Nullable public VirtualFile createScratchFile(Project project, final String fileName, final Language language, final String text) { + return createScratchFile(project, fileName, language, text, ScratchFileService.Option.create_new_always); + } + + @Nullable + public VirtualFile createScratchFile(Project project, + final String fileName, + final Language language, + final String text, + final ScratchFileService.Option option) { RunResult result = new WriteCommandAction(project, UIBundle.message("file.chooser.create.new.file.command.name")) { @Override @@ -76,11 +86,10 @@ public final class ScratchRootType extends RootType { @Override protected void run(@NotNull Result result) throws Throwable { ScratchFileService fileService = ScratchFileService.getInstance(); - VirtualFile file = fileService.findFile(ScratchRootType.this, "scratch", ScratchFileService.Option.create_new_always); + VirtualFile file = fileService.findFile(ScratchRootType.this, fileName, option); fileService.getScratchesMapping().setMapping(file, language); VfsUtil.saveText(file, text); result.setResult(file); - } }.execute(); if (result.hasException()) { diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index 707582f42f96..258e096321be 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -581,6 +581,8 @@ action.IdeScriptingConsole.text=IDE Scripting Console action.IdeScriptingConsole.description=Open IDE scripting console action.NewScratchFile.text=New Scratch File... action.NewScratchFile.description=Creates new scratch file with chosen language +action.NewScratchBuffer.text=New Scratch Buffer +action.NewScratchBuffer.description=Creates new or reuses existing scratch buffer action.Scratch.ChangeLanguage.text=Change File Language action.Scratch.ChangeLanguage.description=Change File Language group.RefactoringMenu.text=_Refactor diff --git a/platform/platform-resources/src/idea/LangActions.xml b/platform/platform-resources/src/idea/LangActions.xml index 53bb8185026c..69c0d2925124 100644 --- a/platform/platform-resources/src/idea/LangActions.xml +++ b/platform/platform-resources/src/idea/LangActions.xml @@ -346,10 +346,11 @@ - + - + + diff --git a/platform/util/resources/misc/registry.properties b/platform/util/resources/misc/registry.properties index 206ad19cf99f..69b3883c8861 100644 --- a/platform/util/resources/misc/registry.properties +++ b/platform/util/resources/misc/registry.properties @@ -454,7 +454,10 @@ new.retina.detection=false new.retina.detection.description=New implementation to detect retina devices ide.scratch.enabled=true -ide.scratch.enabled.description=Disables Tools > Scratchpad: temporary editor without persistence +ide.scratch.enabled.description=Disables scratch files and buffers +ide.scratch.buffers=5 +ide.scratch.buffers.description=Number of scratch buffer files + ide.show.progress.without.status.bar=true ide.show.progress.without.status.bar.description=Disables transparent progress indicator when status bar is switched off