예제 #1
0
  /**
   * Removes import statements from tabSource, replaces each with white spaces and adds the import
   * to the list of program imports
   *
   * @param tabProgram - Code in a tab
   * @param tabNumber - index of the tab
   * @return String - Tab code with imports replaced with white spaces
   */
  private String scrapImportStatements(String tabProgram, int tabNumber) {

    String tabSource = new String(tabProgram);
    do {
      // System.out.println("-->\n" + sourceAlt + "\n<--");
      String[] pieces = PApplet.match(tabSource, importRegexp);

      // Stop the loop if we've removed all the import lines
      if (pieces == null) break;

      String piece = pieces[1] + pieces[2] + pieces[3];
      int len = piece.length(); // how much to trim out

      // programImports.add(piece); // the package name

      // find index of this import in the program
      int idx = tabSource.indexOf(piece);
      // System.out.print("Import -> " + piece);
      // System.out.println(" - "
      // + Base.countLines(tabSource.substring(0, idx)) + " tab "
      // + tabNumber);
      programImports.add(
          new ImportStatement(piece, tabNumber, Base.countLines(tabSource.substring(0, idx))));
      // Remove the import from the main program
      // Substitue with white spaces
      String whiteSpace = "";
      for (int j = 0; j < piece.length(); j++) {
        whiteSpace += " ";
      }
      tabSource = tabSource.substring(0, idx) + whiteSpace + tabSource.substring(idx + len);

    } while (true);
    // System.out.println(tabSource);
    return tabSource;
  }
예제 #2
0
  /**
   * Calculates the tab number and line number of the error in that particular tab. Provides mapping
   * between pure java and pde code.
   *
   * @param problem - IProblem
   * @return int[0] - tab number, int[1] - line number
   */
  public int[] calculateTabIndexAndLineNumber(IProblem problem) {
    // String[] lines = {};// = PApplet.split(sourceString, '\n');
    int codeIndex = 0;
    int bigCount = 0;

    int x = problem.getSourceLineNumber() - mainClassOffset;
    if (x < 0) {
      // System.out.println("Negative line number "
      // + problem.getSourceLineNumber() + " , offset "
      // + mainClassOffset);
      x = problem.getSourceLineNumber() - 2; // Another -1 for 0 index
      if (x < programImports.size() && x >= 0) {
        ImportStatement is = programImports.get(x);
        // System.out.println(is.importName + ", " + is.tab + ", "
        // + is.lineNumber);
        return new int[] {is.tab, is.lineNumber};
      } else {

        // Some seriously ugly stray error, just can't find the source
        // line! Simply return first line for first tab.
        return new int[] {0, 1};
      }
    }

    try {
      for (SketchCode sc : editor.getSketch().getCode()) {
        if (sc.isExtension("pde")) {
          sc.setPreprocOffset(bigCount);
          int len = 0;
          if (editor.getSketch().getCurrentCode().equals(sc)) {
            len = Base.countLines(sc.getDocument().getText(0, sc.getDocument().getLength())) + 1;
          } else {
            len = Base.countLines(sc.getProgram()) + 1;
          }

          // System.out.println("x,len, CI: " + x + "," + len + ","
          // + codeIndex);

          if (x >= len) {

            // We're in the last tab and the line count is greater
            // than the no.
            // of lines in the tab,
            if (codeIndex >= editor.getSketch().getCodeCount() - 1) {
              // System.out.println("Exceeds lc " + x + "," + len
              // + problem.toString());
              // x = len
              x = editor.getSketch().getCode(codeIndex).getLineCount();
              // TODO: Obtain line having last non-white space
              // character in the code.
              break;
            } else {
              x -= len;
              codeIndex++;
            }
          } else {

            if (codeIndex >= editor.getSketch().getCodeCount())
              codeIndex = editor.getSketch().getCodeCount() - 1;
            break;
          }
        }
        bigCount += sc.getLineCount();
      }
    } catch (Exception e) {
      System.err.println(
          "Things got messed up in ErrorCheckerService.calculateTabIndexAndLineNumber()");
    }

    return new int[] {codeIndex, x};
  }