Search in sources :

Example 1 with IntList

use of org.eclipse.jgit.util.IntList in project egit by eclipse.

the class SpellcheckableMessageArea method calculateWrapOffsets.

/**
 * Calculates wrap offsets for the given line, so that resulting lines are
 * no longer than <code>maxLineLength</code> if possible.
 *
 * @param line
 *            the line to wrap (can contain '\n', but no other line delimiters)
 * @param maxLineLength
 *            the maximum line length
 * @return an array of offsets where hard-wraps should be inserted, or
 *         <code>null</code> if the line does not need to be wrapped
 */
public static int[] calculateWrapOffsets(final String line, final int maxLineLength) {
    if (line.length() == 0)
        return null;
    IntList wrapOffsets = new IntList();
    int wordStart = 0;
    int lineStart = 0;
    int length = line.length();
    int nofPreviousWordChars = 0;
    int nofCurrentWordChars = 0;
    for (int i = 0; i < length; i++) {
        char ch = line.charAt(i);
        if (ch == ' ') {
            nofPreviousWordChars += nofCurrentWordChars;
            nofCurrentWordChars = 0;
        } else if (ch == '\n') {
            lineStart = i + 1;
            wordStart = i + 1;
            nofPreviousWordChars = 0;
            nofCurrentWordChars = 0;
        } else {
            // a word character
            if (nofCurrentWordChars == 0) {
                // such as " * <very_long_word>".
                if (nofPreviousWordChars > maxLineLength / 10 || nofPreviousWordChars > 0 && (i - lineStart) > maxLineLength / 2) {
                    wordStart = i;
                }
            }
            nofCurrentWordChars++;
            if (i >= lineStart + maxLineLength) {
                if (wordStart != lineStart) {
                    // don't break before a single long word
                    wrapOffsets.add(wordStart);
                    lineStart = wordStart;
                    nofPreviousWordChars = 0;
                    nofCurrentWordChars = 0;
                }
            }
        }
    }
    int size = wrapOffsets.size();
    if (size == 0) {
        return null;
    } else {
        int[] result = new int[size];
        for (int i = 0; i < size; i++) {
            result[i] = wrapOffsets.get(i);
        }
        return result;
    }
}
Also used : Point(org.eclipse.swt.graphics.Point) IntList(org.eclipse.jgit.util.IntList)

Example 2 with IntList

use of org.eclipse.jgit.util.IntList in project gerrit by GerritCodeReview.

the class FileHeaderUtil method getHeaderLines.

static ImmutableList<String> getHeaderLines(byte[] header) {
    final IntList lineStartOffsets = RawParseUtils.lineMap(header, 0, header.length);
    final ImmutableList.Builder<String> headerLines = ImmutableList.builderWithExpectedSize(lineStartOffsets.size() - 1);
    for (int i = 1; i < lineStartOffsets.size() - 1; i++) {
        final int b = lineStartOffsets.get(i);
        int e = lineStartOffsets.get(i + 1);
        if (header[e - 1] == '\n') {
            e--;
        }
        headerLines.add(RawParseUtils.decode(UTF_8, header, b, e));
    }
    return headerLines.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) IntList(org.eclipse.jgit.util.IntList)

Example 3 with IntList

use of org.eclipse.jgit.util.IntList in project gerrit by GerritCodeReview.

the class PatchListEntry method getHeaderLines.

public ImmutableList<String> getHeaderLines() {
    final IntList m = RawParseUtils.lineMap(header, 0, header.length);
    final ImmutableList.Builder<String> headerLines = ImmutableList.builderWithExpectedSize(m.size() - 1);
    for (int i = 1; i < m.size() - 1; i++) {
        final int b = m.get(i);
        int e = m.get(i + 1);
        if (header[e - 1] == '\n') {
            e--;
        }
        headerLines.add(RawParseUtils.decode(UTF_8, header, b, e));
    }
    return headerLines.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) BasicSerialization.writeString(com.google.gerrit.server.ioutil.BasicSerialization.writeString) BasicSerialization.readString(com.google.gerrit.server.ioutil.BasicSerialization.readString) IntList(org.eclipse.jgit.util.IntList)

Aggregations

IntList (org.eclipse.jgit.util.IntList)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 BasicSerialization.readString (com.google.gerrit.server.ioutil.BasicSerialization.readString)1 BasicSerialization.writeString (com.google.gerrit.server.ioutil.BasicSerialization.writeString)1 Point (org.eclipse.swt.graphics.Point)1