From bce05603faedde631c974224331f342eab33ea80 Mon Sep 17 00:00:00 2001 From: "Dmitry.Krasilschikov" Date: Thu, 19 Apr 2018 14:13:00 +0300 Subject: [PATCH] IDEA-188810 use stream `foldLeft` and docs change --- .../actions/runAnything/RunAnythingUtil.java | 2 - .../RunAnythingCommandCustomizer.java | 37 +++++++------------ .../items/RunAnythingCommandItem.java | 5 ++- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/ide/actions/runAnything/RunAnythingUtil.java b/platform/lang-impl/src/com/intellij/ide/actions/runAnything/RunAnythingUtil.java index 9bee000fe10a..2ab1e515236a 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/runAnything/RunAnythingUtil.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/runAnything/RunAnythingUtil.java @@ -19,7 +19,6 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.keymap.KeymapUtil; import com.intellij.openapi.keymap.MacKeymapUtil; -import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopup; import com.intellij.openapi.util.*; @@ -343,7 +342,6 @@ public class RunAnythingUtil { @NotNull String pattern, @NotNull VirtualFile workDirectory) { Project project = CommonDataKeys.PROJECT.getData(dataContext); - Module module = LangDataKeys.MODULE.getData(dataContext); if (pattern.isEmpty()) return; diff --git a/platform/lang-impl/src/com/intellij/ide/actions/runAnything/commands/RunAnythingCommandCustomizer.java b/platform/lang-impl/src/com/intellij/ide/actions/runAnything/commands/RunAnythingCommandCustomizer.java index c02676fb2d94..d6929aeb1843 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/runAnything/commands/RunAnythingCommandCustomizer.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/runAnything/commands/RunAnythingCommandCustomizer.java @@ -5,6 +5,7 @@ import com.intellij.execution.configurations.GeneralCommandLine; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.vfs.VirtualFile; +import one.util.streamex.StreamEx; import org.jetbrains.annotations.NotNull; /** @@ -16,51 +17,41 @@ public abstract class RunAnythingCommandCustomizer { ExtensionPointName.create("com.intellij.runAnything.commandCustomizer"); /** - * Customizes command line and pass it to others customizers + * Customizes command line to be executed * * @param workDirectory the working directory the command will be executed in * @param dataContext {@link DataContext} to fetch module, project etc. - * @param commandLine command line to be executed - * @return customized command line + * @param commandLine command line to be customized + * @return patched command line */ @NotNull - public GeneralCommandLine customizeCommandLine(@NotNull VirtualFile workDirectory, - @NotNull DataContext dataContext, - @NotNull GeneralCommandLine commandLine) { + protected GeneralCommandLine customizeCommandLine(@NotNull VirtualFile workDirectory, + @NotNull DataContext dataContext, + @NotNull GeneralCommandLine commandLine) { return commandLine; } /** - * Customizes data context and pass it to others providers + * Customizes data context command line to be executed on * - * @param dataContext {@link DataContext} to fetch module, project etc. + * @param dataContext original {@link DataContext} * @return customized {@link DataContext} */ @NotNull - public DataContext customizeDataContext(@NotNull DataContext dataContext) { + protected DataContext customizeDataContext(@NotNull DataContext dataContext) { return dataContext; } @NotNull public static GeneralCommandLine customizeCommandLine(@NotNull DataContext dataContext, @NotNull VirtualFile workDirectory, - @NotNull String command) { - GeneralCommandLine commandLine = new GeneralCommandLine(command) - .withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE); - - for (RunAnythingCommandCustomizer customizer : EP_NAME.getExtensions()) { - commandLine = customizer.customizeCommandLine(workDirectory, dataContext, commandLine); - } - - return commandLine; + @NotNull GeneralCommandLine commandLine) { + return StreamEx.of(EP_NAME.getExtensions()) + .foldLeft(commandLine, (cmdLine, customizer) -> customizer.customizeCommandLine(workDirectory, dataContext, cmdLine)); } @NotNull public static DataContext customizeContext(@NotNull DataContext dataContext) { - for (RunAnythingCommandCustomizer customizer : EP_NAME.getExtensions()) { - dataContext = customizer.customizeDataContext(dataContext); - } - - return dataContext; + return StreamEx.of(EP_NAME.getExtensions()).foldLeft(dataContext, (context, customizer) -> customizer.customizeDataContext(context)); } } \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/ide/actions/runAnything/items/RunAnythingCommandItem.java b/platform/lang-impl/src/com/intellij/ide/actions/runAnything/items/RunAnythingCommandItem.java index 56d2db25f4e3..42ba9b85a313 100644 --- a/platform/lang-impl/src/com/intellij/ide/actions/runAnything/items/RunAnythingCommandItem.java +++ b/platform/lang-impl/src/com/intellij/ide/actions/runAnything/items/RunAnythingCommandItem.java @@ -70,7 +70,10 @@ public class RunAnythingCommandItem extends RunAnythingItem { commands.add(commandString); dataContext = RunAnythingCommandCustomizer.customizeContext(dataContext); - GeneralCommandLine commandLine = RunAnythingCommandCustomizer.customizeCommandLine(dataContext, workDirectory, commandString); + + GeneralCommandLine initialCommandLine = + new GeneralCommandLine(commandString).withParentEnvironmentType(GeneralCommandLine.ParentEnvironmentType.CONSOLE); + GeneralCommandLine commandLine = RunAnythingCommandCustomizer.customizeCommandLine(dataContext, workDirectory, initialCommandLine); try { ExecutionEnvironmentBuilder.create(project, executor, new RunAnythingRunProfile(commandLine, commandString)) .dataContext(dataContext)