openInIde function

void openInIde(
  1. IdeType type,
  2. Directory location, {
  3. File? file,
  4. ProcessManager processManager = const LocalProcessManager(),
  5. Platform platform = const LocalPlatform(),
  6. int startLine = 0,
})

Implementation

void openInIde(
  IdeType type,
  Directory location, {
  File? file,
  ProcessManager processManager = const LocalProcessManager(),
  Platform platform = const LocalPlatform(),
  int startLine = 0,
}) {
  switch (platform.operatingSystem) {
    case 'linux':
      switch (type) {
        case IdeType.idea:
          final List<String> cmd = <String>[
            'idea',
            location.absolute.path,
            if (file != null) '${file.absolute.path}:$startLine',
          ];
          processManager.run(cmd, runInShell: true);
          print('Launched ${cmd.join(' ')}');
          break;
        case IdeType.vscode:
          final List<String> cmd = <String>[
            'code',
            '-n',
            location.absolute.path,
            if (file != null) '--goto',
            if (file != null) '${file.absolute.path}:$startLine',
          ];
          processManager.run(cmd, runInShell: true);
          print('Launched ${cmd.join(' ')}');
          break;
      }
      break;
    case 'macos':
      switch (type) {
        case IdeType.idea:
          final ProcessResult result = processManager.runSync(<String>[
            'mdfind',
            'kMDItemCFBundleIdentifier=com.jetbrains.intellij* kind:application',
          ], stdoutEncoding: utf8);
          final Iterable<String> candidates =
              (result.stdout as String).split('\n').where((String candidate) {
            return !candidate.contains('/Application Support/');
          });
          final String appName =
              candidates.isNotEmpty ? candidates.first : 'IntelliJ IDEA CE';
          final List<String> command = <String>[
            'open',
            '-na',
            appName,
            '--args',
            file?.absolute.path ?? location.absolute.path,
            if (startLine != 0) '--line',
            if (startLine != 0) '$startLine',
          ];
          processManager
              .run(command, stdoutEncoding: utf8, stderrEncoding: utf8)
              .then((ProcessResult result) {
            if (result.exitCode != 0) {
              throw SnippetException(
                  'Unable to launch app $appName (${result.exitCode}): ${result.stderr}');
            }
          }).onError((Exception exception, StackTrace stackTrace) {
            throw SnippetException('Unable to launch app $appName: $exception');
          });
          break;
        case IdeType.vscode:
          final ProcessResult result = processManager.runSync(<String>[
            'mdfind',
            'kMDItemCFBundleIdentifier=com.microsoft.VSCode* kind:application',
          ], stdoutEncoding: utf8);
          final Iterable<String> candidates =
              (result.stdout as String).split('\n');
          final String appName =
              candidates.isNotEmpty ? candidates.first : 'Visual Studio Code';
          processManager.run(<String>[
            'open',
            '-na',
            appName,
            '--args',
            '-n',
            location.absolute.path,
            if (file != null) '--goto',
            if (file != null) '${file.absolute.path}:$startLine',
          ], stdoutEncoding: utf8, stderrEncoding: utf8).then(
              (ProcessResult result) {
            if (result.exitCode != 0) {
              throw SnippetException(
                  'Unable to launch app $appName (${result.exitCode}): ${result.stderr}');
            }
          }).onError((Exception exception, StackTrace stackTrace) {
            throw SnippetException('Unable to launch app $appName: $exception');
          });
          break;
      }
      break;
    case 'windows':
      switch (type) {
        case IdeType.idea:
          processManager.run(<String>[
            'idea64.exe',
            location.absolute.path,
            if (file != null) '${file.absolute.path}:$startLine',
          ]);
          break;
        case IdeType.vscode:
          processManager.run(<String>[
            'code',
            '-n',
            location.absolute.path,
            if (file != null) '--goto',
            if (file != null) '${file.absolute.path}:$startLine',
          ]);
          break;
      }
      break;
  }
}