private String getLineElementLabel(LineElement lineElement) {
    int lineNumber = lineElement.getLine();
    String lineNumberString =
        Messages.format(SearchMessages.FileLabelProvider_line_number, new Integer(lineNumber));

    String str = new String(lineNumberString);

    Match[] matches = lineElement.getMatches(fPage.getInput());
    Arrays.sort(matches, fMatchComparator);

    String content = lineElement.getContents();

    int pos = evaluateLineStart(matches, content, lineElement.getOffset());

    int length = content.length();

    int charsToCut =
        getCharsToCut(
            length, matches); // number of characters to leave away if the line is too long
    for (int i = 0; i < matches.length; i++) {
      FileMatch match = (FileMatch) matches[i];
      int start = Math.max(match.getOriginalOffset() - lineElement.getOffset(), 0);
      // append gap between last match and the new one
      if (pos < start) {
        if (charsToCut > 0) {
          charsToCut = appendShortenedGap(content, pos, start, charsToCut, i == 0, str);
        } else {
          str += content.substring(pos, start);
        }
      }
      // append match
      int end =
          Math.min(
              match.getOriginalOffset() + match.getOriginalLength() - lineElement.getOffset(),
              lineElement.getLength());
      str += content.substring(start, end);
      pos = end;
    }
    // append rest of the line
    if (charsToCut > 0) {
      appendShortenedGap(content, pos, length, charsToCut, false, str);
    } else {
      str += content.substring(pos);
    }
    return str;
  }
 private int evaluateLineStart(Match[] matches, String lineContent, int lineOffset) {
   int max = lineContent.length();
   if (matches.length > 0) {
     FileMatch match = (FileMatch) matches[0];
     max = match.getOriginalOffset() - lineOffset;
     if (max < 0) {
       return 0;
     }
   }
   for (int i = 0; i < max; i++) {
     char ch = lineContent.charAt(i);
     if (!Character.isWhitespace(ch) || ch == '\n' || ch == '\r') {
       return i;
     }
   }
   return max;
 }