<commit_before>/*
 * Copyright 2017 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.run;

import com.intellij.execution.*;
import com.intellij.execution.configurations.CommandLineState;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.GenericProgramRunner;
import com.intellij.execution.runners.ProgramRunner;
import com.intellij.execution.runners.RunContentBuilder;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.Separator;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.xdebugger.XDebugProcess;
import com.intellij.xdebugger.XDebugProcessStarter;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XDebuggerManager;
import com.jetbrains.lang.dart.ide.runner.DartExecutionHelper;
import com.jetbrains.lang.dart.util.DartUrlResolver;
import io.flutter.dart.DartPlugin;
import io.flutter.logging.FlutterLog;
import io.flutter.logging.FlutterLogView;
import io.flutter.run.daemon.*;
import io.flutter.view.OpenFlutterViewAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Launches a flutter app, showing it in the console.
 * <p>
 * Normally creates a debugging session, which is needed for hot reload.
 */
public class LaunchState extends CommandLineState {
  // We use the profile launch type, contributed by the Android IntelliJ plugins
  // in 2017.3 and Android Studio 3.0, if it's available. This allows us to support
  // their 'profile' launch button, next to the regular run and debug ones.
  public static final String ANDROID_PROFILER_EXECUTOR_ID = "Android Profiler";

  private final @NotNull VirtualFile workDir;

  /**
   * The file or directory holding the Flutter app's source code.
   * This determines how the analysis server resolves URI's (for breakpoints, etc).
   * <p>
   * If a file, this should be the file containing the main() method.
   */
  private final @NotNull VirtualFile sourceLocation;

  private final @NotNull RunConfig runConfig;
  private final @NotNull Callback callback;

  public LaunchState(@NotNull ExecutionEnvironment env,
                     @NotNull VirtualFile workDir,
                     @NotNull VirtualFile sourceLocation,
                     @NotNull RunConfig runConfig,
                     @NotNull Callback callback) {
    super(env);
    this.workDir = workDir;
    this.sourceLocation = sourceLocation;
    this.runConfig = runConfig;
    this.callback = callback;
    DaemonConsoleView.install(this, env, workDir);
  }

  @Nullable
  protected ConsoleView createConsole(@NotNull final Executor executor) throws ExecutionException {
    if (FlutterLog.isLoggingEnabled()) {
      final FlutterApp app = FlutterApp.fromEnv(getEnvironment());
      assert app != null;
      return new FlutterLogView(app);
    } else {
      return super.createConsole(executor);
    }
  }

  private RunContentDescriptor launch(@NotNull ExecutionEnvironment env) throws ExecutionException {
    FileDocumentManager.getInstance().saveAllDocuments();

    // Set our FlutterLaunchMode up in the ExecutionEnvironment.
    if (RunMode.fromEnv(env).isProfiling()) {
      FlutterLaunchMode.addToEnvironment(env, FlutterLaunchMode.PROFILE);
    }

    final Project project = getEnvironment().getProject();
    final FlutterDevice device = DeviceService.getInstance(project).getSelectedDevice();
    final FlutterApp app = callback.createApp(device);

    if (device == null) {
      Messages.showDialog(
        project,
        "No connected devices found; please connect a device, or see flutter.io/setup for getting started instructions.",
        "No Connected Devices Found",
        new String[]{Messages.OK_BUTTON}, 0, AllIcons.General.InformationDialog);

      return null;
    }

    // Cache for use in console configuration.
    FlutterApp.addToEnvironment(env, app);

    // Remember the run configuration that started this process.
    app.getProcessHandler().putUserData(FLUTTER_RUN_CONFIG_KEY, runConfig);

    final ExecutionResult result = setUpConsoleAndActions(app);

    device.bringToFront();

    // Check for and display any analysis errors when we launch an app.
    if (env.getRunProfile() instanceof SdkRunConfig) {
      final Class dartExecutionHelper = classForName("com.jetbrains.lang.dart.ide.runner.DartExecutionHelper");
      if (dartExecutionHelper != null) {
        final String message = ("<a href='open.dart.analysis'>Analysis issues</a> may affect " +
                                "the execution of '" + env.getRunProfile().getName() + "'.");
        final SdkRunConfig config = (SdkRunConfig)env.getRunProfile();
        final SdkFields sdkFields = config.getFields();
        final MainFile mainFile = MainFile.verify(sdkFields.getFilePath(), env.getProject()).get();

        DartExecutionHelper.displayIssues(project, mainFile.getFile(), message, env.getRunProfile().getIcon());
      }
    }

    final FlutterLaunchMode launchMode = FlutterLaunchMode.fromEnv(env);
    if (launchMode.supportsDebugConnection()) {
      return createDebugSession(env, app, result).getRunContentDescriptor();<commit_msg>chore: For BazelRunConfig launches, print the command-line to the console (#2368)<commit_after>/*
 * Copyright 2017 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.run;

import com.intellij.execution.*;
import com.intellij.execution.configurations.CommandLineState;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.GenericProgramRunner;
import com.intellij.execution.runners.ProgramRunner;
import com.intellij.execution.runners.RunContentBuilder;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.Separator;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.xdebugger.XDebugProcess;
import com.intellij.xdebugger.XDebugProcessStarter;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XDebuggerManager;
import com.jetbrains.lang.dart.ide.runner.DartExecutionHelper;
import com.jetbrains.lang.dart.util.DartUrlResolver;
import io.flutter.dart.DartPlugin;
import io.flutter.logging.FlutterLog;
import io.flutter.logging.FlutterLogView;
import io.flutter.run.bazel.BazelRunConfig;
import io.flutter.run.daemon.*;
import io.flutter.view.OpenFlutterViewAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Launches a flutter app, showing it in the console.
 * <p>
 * Normally creates a debugging session, which is needed for hot reload.
 */
public class LaunchState extends CommandLineState {
  // We use the profile launch type, contributed by the Android IntelliJ plugins
  // in 2017.3 and Android Studio 3.0, if it's available. This allows us to support
  // their 'profile' launch button, next to the regular run and debug ones.
  public static final String ANDROID_PROFILER_EXECUTOR_ID = "Android Profiler";

  private final @NotNull VirtualFile workDir;

  /**
   * The file or directory holding the Flutter app's source code.
   * This determines how the analysis server resolves URI's (for breakpoints, etc).
   * <p>
   * If a file, this should be the file containing the main() method.
   */
  private final @NotNull VirtualFile sourceLocation;

  private final @NotNull RunConfig runConfig;
  private final @NotNull Callback callback;

  public LaunchState(@NotNull ExecutionEnvironment env,
                     @NotNull VirtualFile workDir,
                     @NotNull VirtualFile sourceLocation,
                     @NotNull RunConfig runConfig,
                     @NotNull Callback callback) {
    super(env);
    this.workDir = workDir;
    this.sourceLocation = sourceLocation;
    this.runConfig = runConfig;
    this.callback = callback;
    DaemonConsoleView.install(this, env, workDir);
  }

  @Nullable
  protected ConsoleView createConsole(@NotNull final Executor executor) throws ExecutionException {
    if (FlutterLog.isLoggingEnabled()) {
      final FlutterApp app = FlutterApp.fromEnv(getEnvironment());
      assert app != null;
      return new FlutterLogView(app);
    }
    else {
      return super.createConsole(executor);
    }
  }

  private RunContentDescriptor launch(@NotNull ExecutionEnvironment env) throws ExecutionException {
    FileDocumentManager.getInstance().saveAllDocuments();

    // Set our FlutterLaunchMode up in the ExecutionEnvironment.
    if (RunMode.fromEnv(env).isProfiling()) {
      FlutterLaunchMode.addToEnvironment(env, FlutterLaunchMode.PROFILE);
    }

    final Project project = getEnvironment().getProject();
    final FlutterDevice device = DeviceService.getInstance(project).getSelectedDevice();
    final FlutterApp app = callback.createApp(device);

    if (device == null) {
      Messages.showDialog(
        project,
        "No connected devices found; please connect a device, or see flutter.io/setup for getting started instructions.",
        "No Connected Devices Found",
        new String[]{Messages.OK_BUTTON}, 0, AllIcons.General.InformationDialog);

      return null;
    }

    // Cache for use in console configuration.
    FlutterApp.addToEnvironment(env, app);

    // Remember the run configuration that started this process.
    app.getProcessHandler().putUserData(FLUTTER_RUN_CONFIG_KEY, runConfig);

    final ExecutionResult result = setUpConsoleAndActions(app);

    // For Bazel run configurations,
    // where the console is not null,
    // and we find the expected process handler type,
    // print the command line command to the console.
    if (runConfig instanceof BazelRunConfig &&
        app.getConsole() != null &&
        app.getProcessHandler() instanceof OSProcessHandler) {
      final String commandLineString = ((OSProcessHandler)app.getProcessHandler()).getCommandLine().trim();
      if (StringUtil.isNotEmpty(commandLineString)) {
        app.getConsole().print(commandLineString + "\n", ConsoleViewContentType.NORMAL_OUTPUT);
      }
    }

    device.bringToFront();

    // Check for and display any analysis errors when we launch an app.
    if (env.getRunProfile() instanceof SdkRunConfig) {
      final Class dartExecutionHelper = classForName("com.jetbrains.lang.dart.ide.runner.DartExecutionHelper");
      if (dartExecutionHelper != null) {
        final String message = ("<a href='open.dart.analysis'>Analysis issues</a> may affect " +
                                "the execution of '" + env.getRunProfile().getName() + "'.");
        final SdkRunConfig config = (SdkRunConfig)env.getRunProfile();
        final SdkFields sdkFields = config.getFields();
        final MainFile mainFile = MainFile.verify(sdkFields.getFilePath(), env.getProject()).get();

        DartExecutionHelper.displayIssues(project, mainFile.getFile(), message, env.getRunProfile().getIcon());
      }
    }

    final FlutterLaunchMode launchMode = FlutterLaunchMode.fromEnv(env);
    if (launchMode.supportsDebugConnection()) {
      return createDebugSession(env, app, result).getRunContentDescriptor();
{'fraction_non_alphanumeric': 0.09282636631642603, 'fraction_numerical': 0.0021165620984201377, 'mean_word_length': 4.186201489611917, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4023275', 'n_tokens_mistral': 3789, 'n_tokens_neox': 3660, 'n_words': 929}