Пример #1
0
    // Used in compiling to mark out "dirty" areas of the code; ie, comments and string literals.
    // These dirty areas don't get their syntax highlighted or any special treatment.
    // @returns: pairs of integers that represent dirty boundaries.
    private ArrayList<Integer> getDirty(String code) {
      ArrayList<Integer> dirtyBounds = new ArrayList<>();

      // Handles string literals
      int j = -1;
      while (true) {
        j = code.indexOf("\"", j + 1);
        if (j < 0) break;
        // Ignore escaped characters
        if (!code.substring(j - 1, j).equals("\\")) dirtyBounds.add(j);
      }

      // End of line comments
      j = -1;
      while (true) {
        j = code.indexOf("//", j + 1);
        if (j < 0) break;
        dirtyBounds.add(j);
        // If there's no newline, then the comment lasts for the length of the code
        dirtyBounds.add(
            code.indexOf("\n", j + 1) == -1 ? code.length() : code.indexOf("\n", j + 1));
      }

      // Block comments (and javadoc comments)
      j = -1;
      while (true) {
        j = code.indexOf("/*", j + 1);
        if (j < 0) break;
        dirtyBounds.add(j);
        dirtyBounds.add(code.indexOf("*/", j + 1));
      }

      return dirtyBounds;
    }