public Result applyFilter()

in flutter-idea/src/io/flutter/console/FlutterConsoleFilter.java [122:237]


  public Result applyFilter(final String line, final int entireLength) {
    if (line.startsWith("Run \"flutter doctor\" for information about installing additional components.")) {
      return getFlutterDoctorResult(line, entireLength - line.length());
    }

    if (line.startsWith("Lost connection to device")) {
      TextAttributes attr = new TextAttributes(UIUtil.getErrorForeground(), null, null, EffectType.BOXED, Font.PLAIN);
      return new Result(entireLength - line.length(), entireLength, null, attr);
    }
    int lineNumber = 0;
    String pathPart = line.trim();
    VirtualFile file = null;
    int lineStart = -1;
    int highlightLength = 0;

    // Check for, e.g.,
    //   * "Launching lib/main.dart"
    //   * "open ios/Runner.xcworkspace"
    if (pathPart.startsWith("Launching ") || pathPart.startsWith("open ")) {
      final String[] parts = pathPart.split(" ");
      if (parts.length > 1) {
        pathPart = parts[1];
        file = fileAtPath(pathPart);
        if (file != null) {
          lineStart = entireLength - line.length() + line.indexOf(pathPart);
          highlightLength = pathPart.length();
        }
      }
    }

    // Check for embedded paths, e.g.,
    //    * "  • MyApp.xzzzz (lib/main.dart:6)"
    //    * "  • _MyHomePageState._incrementCounter (lib/main.dart:49)"
    final String[] parts = pathPart.split(" ");
    for (String part : parts) {
      // "(lib/main.dart:49)"
      if (part.startsWith("(") && part.endsWith(")")) {
        part = part.substring(1, part.length() - 1);
        final String[] split = part.split(":");
        if (split.length == 2) {
          try {
            // Reconcile line number indexing.
            lineNumber = Math.max(0, Integer.parseInt(split[1]) - 1);
          }
          catch (NumberFormatException e) {
            // Ignored.
          }
          pathPart = part;
          lineStart = entireLength - line.length() + line.indexOf(pathPart);
          highlightLength = pathPart.length();
          break;
        }
        else if (split.length == 4 && split[0].equals("file")) {
          // part = file:///Users/user/AndroidStudioProjects/flutter_app/test/widget_test.dart:23:18
          try {
            // Reconcile line number indexing.
            lineNumber = Math.max(0, Integer.parseInt(split[2]) - 1);
          }
          catch (NumberFormatException e) {
            // Ignored.
          }
          pathPart = findRelativePath(split[1]);
          if (pathPart == null) {
            return null;
          }
          lineStart = entireLength - line.length() + line.indexOf(part);
          highlightLength = part.length();
          break;
        }
      }
    }

    if (lineStart < 0) {
      // lib/registerC.dart:104:73: Error: Expected ';' after this.
      final String filePathAndLineNumberExpr = "(^.*?):(\\d+?):\\d+?:\\s*?Error";
      final Pattern pattern = Pattern.compile(filePathAndLineNumberExpr);
      final Matcher matcher = pattern.matcher(line);
      final boolean found = matcher.find();
      if (found) {
        final String filePathExpr = "((?:[^/]*/)*)(.*)";
        final Pattern pathPattern = Pattern.compile(filePathExpr);
        final Matcher pathMatcher = pathPattern.matcher(matcher.group(1));
        if (pathMatcher.find()) {
          final String path = pathMatcher.group(1) + pathMatcher.group(2);
          file = fileAtPath(path);
          if (file == null) {
            return null;
          }
          lineNumber = Integer.parseInt(matcher.group(2));
          lineStart = entireLength - line.length();
          highlightLength = path.length();
        }
        else {
          return null;
        }
      }
      else {
        return null;
      }
    }

    if (file == null) {
      file = fileAtPath(pathPart);
    }

    if (file != null) {
      // "open ios/Runner.xcworkspace"
      final boolean openAsExternalFile = FlutterUtils.isXcodeFileName(pathPart);

      final HyperlinkInfo hyperlinkInfo =
        openAsExternalFile ? new OpenExternalFileHyperlink(file) : new OpenFileHyperlinkInfo(module.getProject(), file, lineNumber, 0);
      return new Result(lineStart, lineStart + highlightLength, hyperlinkInfo);
    }

    return null;
  }