/**
   * Find start and length of the element that <code>issue</code> represents in an array. The
   * resulting <code>bounds</code> will include preceeding or trailing comma (not both) if present
   * so that the syntax of the array is intact if the range is removed.
   *
   * @param txt The text containing the array
   * @param issue The issue that represents the array element
   * @param bounds A two element array where the start and lenght will be returned
   * @return <code>true</code> if the range could be deteremined or <code>false</code> to indicate
   *     that the tokens expected to precede and succeed the element could not be found.
   */
  public static boolean findArrayElementBounds(String txt, Issue issue, int[] bounds) {
    int start = issue.getOffset();
    int end = issue.getOffset() + issue.getLength();

    // Find preceding comma or start of array
    char c = 0;
    int cma = start;
    while (--cma >= 0) {
      c = txt.charAt(cma);
      if (c == ',' || c == '[') break;
    }

    if (c != ',') {
      if (c != '[')
        // Element must be preceeded by comma or start of array
        return false;

      // No preceding comma. Find trailing comma or end of array
      int eot = txt.length();
      cma = end;
      c = 0;
      while (cma < eot) {
        c = txt.charAt(cma);
        if (c == ',' || c == ']') break;
        ++cma;
      }
      if (c == ',') {
        end = cma + 1;
        // Also swallow whitespace after comma
        while (end < eot && Character.isWhitespace(txt.charAt(end))) end++;
      } else if (c == ']') end = cma;
      else
        // Neither comma nor array end follow after element.
        return false;

    } else start = cma;
    bounds[0] = start;
    bounds[1] = end - start;
    return true;
  }
Esempio n. 2
0
 protected String getIssueLocationText(Issue issue) {
   String text = resource.getParseResult().getRootNode().getText();
   String markertext = text.substring(issue.getOffset(), issue.getOffset() + issue.getLength());
   return markertext.replace('\n', ' ').replace('\r', ' ');
 }