private static String formatParagraph(String paragraph, int[] offset, String prefix, String lineDelimiter,
                                          int margin)
    {

        LineBreakIterator iterator = new LineBreakIterator(paragraph);

        StringBuilder paragraphBuffer = new StringBuilder();
        StringBuilder lineBuffer = new StringBuilder();
        StringBuilder whiteSpaceBuffer = new StringBuilder();

        int index = offset[0];
        int indexBuffer = -1;

        // line delimiter could be null
        if (lineDelimiter == null) {
            lineDelimiter = ""; //$NON-NLS-1$
        }

        for (int start = iterator.first(), end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
            .next()) {

            String word = paragraph.substring(start, end);

            // word is whitespace
            if (word.trim().length() == 0) {
                whiteSpaceBuffer.append(word);

                // first word of line is always appended
            } else if (lineBuffer.length() == 0) {
                lineBuffer.append(prefix);
                lineBuffer.append(whiteSpaceBuffer.toString());
                lineBuffer.append(word);

            } else {
                String line = lineBuffer.toString() + whiteSpaceBuffer.toString() + word;

                // margin exceeded
                if (calculateDisplayedWidth(line) > margin) {
                    // flush line buffer and wrap paragraph
                    paragraphBuffer.append(lineBuffer.toString());
                    paragraphBuffer.append(lineDelimiter);
                    lineBuffer.setLength(0);
                    lineBuffer.append(prefix);
                    lineBuffer.append(word);

                    // flush index buffer
                    if (indexBuffer != -1) {
                        offset[0] = indexBuffer;
                        // correct for caret in whitespace at the end of line
                        if (whiteSpaceBuffer.length() != 0 && index < start
                            && index >= start - whiteSpaceBuffer.length()) {
                            offset[0] -= (index - (start - whiteSpaceBuffer.length()));
                        }
                        indexBuffer = -1;
                    }

                    whiteSpaceBuffer.setLength(0);

                    // margin not exceeded
                } else {
                    lineBuffer.append(whiteSpaceBuffer.toString());
                    lineBuffer.append(word);
                    whiteSpaceBuffer.setLength(0);
                }
            }

            if (index >= start && index < end) {
                indexBuffer = paragraphBuffer.length() + lineBuffer.length() + (index - start);
                if (word.trim().length() != 0) {
                    indexBuffer -= word.length();
                }
            }
        }

        // flush line buffer
        paragraphBuffer.append(lineBuffer.toString());
        paragraphBuffer.append(lineDelimiter);

        // flush index buffer
        if (indexBuffer != -1) {
            offset[0] = indexBuffer;
        }

        // last position is not returned by break iterator
        else if (offset[0] == paragraph.length()) {
            offset[0] = paragraphBuffer.length() - lineDelimiter.length();
        }

        return paragraphBuffer.toString();
    }
Formats a paragraph, using break iterator.

@param offset an offset within the paragraph, which will be updated with respect to formatting.
plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/indent/SQLCommentAutoIndentStrategy.java