Example usage for java.lang StringBuilder insert

List of usage examples for java.lang StringBuilder insert

Introduction

In this page you can find the example usage for java.lang StringBuilder insert.

Prototype

@Override
public StringBuilder insert(int offset, double d) 

Source Link

Usage

From source file:de.walware.docmlet.tex.internal.ui.editors.LtxCommandCompletionProposal.java

@Override
protected void doApply(final char trigger, final int stateMask, final int caretOffset,
        final int replacementOffset, final int replacementLength) throws BadLocationException {
    final ApplyData data = getApplyData();
    final IDocument document = data.getDocument();

    final StringBuilder replacement = new StringBuilder(fCommand.getControlWord());
    if ((stateMask & 0x1) == 0x1) {
        replacement.insert(0, '\\');
    }//from ww w  .  ja va2  s .  com
    int cursor = replacement.length();
    int mode = 0;
    IntList positions = null;
    if (fCommand == IEnvDefinitions.VERBATIM_verb_COMMAND) {
        mode = 201;
    } else if ((fCommand.getType() & TexCommand.MASK_MAIN) != TexCommand.ENV) {
        final List<Argument> args = fCommand.getArguments();
        if (args != null && !args.isEmpty()) {
            final boolean isFirstOptional = args.get(0).isOptional();
            int idxFirstRequired = -1;
            for (int i = (isFirstOptional) ? 1 : 0; i < args.size(); i++) {
                final Argument arg = args.get(i);
                if (arg.isRequired()) {
                    idxFirstRequired = i;
                    break;
                }
            }
            if (idxFirstRequired >= 0) {
                if (replacementOffset + replacementLength < document.getLength() - 1
                        && (document.getChar(replacementOffset + replacementLength) == '{' || (isFirstOptional
                                && document.getChar(replacementOffset + replacementLength) == '['))) {
                    cursor++;
                    mode = 10;
                } else if (!isFollowedByOpeningBracket(data, replacementOffset + replacementLength,
                        isFirstOptional)) {
                    replacement.append('{');
                    cursor++;
                    mode = 11;
                }
                if (mode >= 10) {
                    if (mode == 11 && !isClosedBracket(data, replacementOffset,
                            replacementOffset + replacementLength)) {
                        replacement.append('}');

                        positions = new ArrayIntList();
                        mode = 0;
                        if (isFirstOptional) {
                            positions.add(mode);
                        }
                        mode++;
                        positions.add(mode++);
                        for (int i = idxFirstRequired + 1; i < args.size(); i++) {
                            if (args.get(i).isRequired()) {
                                replacement.append("{}");
                                mode++;
                                positions.add(mode++);
                            } else if (positions.get(positions.size() - 1) != mode) {
                                positions.add(mode);
                            }
                        }
                        if (positions.get(positions.size() - 1) != mode) {
                            positions.add(mode);
                        }
                        mode = 110 + 1;
                        // add multiple arguments
                    }
                }
            }
        }
    }
    document.replace(replacementOffset, replacementLength, replacement.toString());
    setCursorPosition(replacementOffset + cursor);
    if (mode > 100 && mode < 200) {
        createLinkedMode(data, replacementOffset + cursor - (mode - 110), positions).enter();
    } else if (mode > 200 && mode < 300) {
        createLinkedVerbMode(data, replacementOffset + cursor);
    }
    if ((fCommand.getType() & TexCommand.MASK_MAIN) == TexCommand.GENERICENV) {
        reinvokeAssist(data.getViewer());
    }
}

From source file:ninja.text.TextImpl.java

@Override
public Text prepend(Text... parts) {
    StringBuilder newData = new StringBuilder(data.toString());
    for (Text part : parts) {
        newData.insert(0, part);
    }/* w  w  w.ja v  a 2s  .c  o  m*/
    return Text.of(newData);
}

From source file:StringUtils.java

/**
 * Reformats a string where lines that are longer than <tt>width</tt>
 * are split apart at the earliest wordbreak or at maxLength, whichever is
 * sooner. If the width specified is less than 5 or greater than the input
 * Strings length the string will be returned as is.
 * <p/>/*from  w  w  w . j  a v  a  2  s. com*/
 * Please note that this method can be lossy - trailing spaces on wrapped
 * lines may be trimmed.
 *
 * @param input the String to reformat.
 * @param width the maximum length of any one line.
 * @return a new String with reformatted as needed.
 */
public static String wordWrap(String input, int width, Locale locale) {
    // protect ourselves
    if (input == null) {
        return "";
    } else if (width < 5) {
        return input;
    } else if (width >= input.length()) {
        return input;
    }

    StringBuilder buf = new StringBuilder(input);
    boolean endOfLine = false;
    int lineStart = 0;

    for (int i = 0; i < buf.length(); i++) {
        if (buf.charAt(i) == '\n') {
            lineStart = i + 1;
            endOfLine = true;
        }

        // handle splitting at width character
        if (i > lineStart + width - 1) {
            if (!endOfLine) {
                int limit = i - lineStart - 1;
                BreakIterator breaks = BreakIterator.getLineInstance(locale);
                breaks.setText(buf.substring(lineStart, i));
                int end = breaks.last();

                // if the last character in the search string isn't a space,
                // we can't split on it (looks bad). Search for a previous
                // break character
                if (end == limit + 1) {
                    if (!Character.isWhitespace(buf.charAt(lineStart + end))) {
                        end = breaks.preceding(end - 1);
                    }
                }

                // if the last character is a space, replace it with a \n
                if (end != BreakIterator.DONE && end == limit + 1) {
                    buf.replace(lineStart + end, lineStart + end + 1, "\n");
                    lineStart = lineStart + end;
                }
                // otherwise, just insert a \n
                else if (end != BreakIterator.DONE && end != 0) {
                    buf.insert(lineStart + end, '\n');
                    lineStart = lineStart + end + 1;
                } else {
                    buf.insert(i, '\n');
                    lineStart = i + 1;
                }
            } else {
                buf.insert(i, '\n');
                lineStart = i + 1;
                endOfLine = false;
            }
        }
    }

    return buf.toString();
}

From source file:ninja.text.TextImpl.java

@Override
public Text prepend(Object... parts) {
    StringBuilder newData = new StringBuilder(this.data.toString());
    for (Object part : parts) {
        newData.insert(0, part);
    }/*from ww w  .j  a va2  s .  c  om*/
    return Text.of(newData);
}

From source file:ninja.text.TextImpl.java

@Override
public Text prepend(CharSequence... parts) {
    StringBuilder newData = new StringBuilder(data.toString());
    for (CharSequence part : parts) {
        newData.insert(0, part);
    }/*from   w w w  .j av  a2s  .com*/
    return Text.of(newData);
}

From source file:ninja.text.TextImpl.java

@Override
public Text center(int width, String padStr) {

    StringBuilder newData = new StringBuilder(data.toString());
    for (int i = 0; i < width; i++) {
        newData.insert(0, padStr);
        newData.append(padStr);/*from  ww  w  .jav a 2  s  .  c  om*/
    }
    return Text.of(newData);
}

From source file:com.scaleunlimited.cascading.FlowMonitor.java

private void insert(StringBuilder template, String key, String value) {
    int offset = template.indexOf(key);
    if (offset == -1) {
        throw new RuntimeException("Key doesn't exist in template: " + key);
    }//from  w  w  w.j av a 2  s  .c om

    template.insert(offset, value);
}

From source file:com.ewcms.core.site.model.Channel.java

private void constructAbsUrl() {
    if (StringUtils.isNotBlank(url)) {
        this.absUrl = url;
    } else {//from   w w  w .  j  a  va2  s. c om
        StringBuilder builder = new StringBuilder();
        for (Channel channel = this; channel != null; channel = channel.parent) {
            if (StringUtils.isNotBlank(channel.getUrl()) || StringUtils.isBlank(channel.getDir())) {
                break;
            }
            String dir = removeStartAndEndPathSeparator(channel.getDir());
            builder.insert(0, dir);
            builder.insert(0, PATH_SEPARATOR);
        }
        this.absUrl = builder.toString();
    }
}

From source file:com.google.testing.pogen.generator.template.TemplateUpdaterWithClassAttribute.java

protected StringBuilder buildModifiedTag(String template, HtmlTagInfo tagInfo) {
    StringBuilder tag = new StringBuilder(template.substring(tagInfo.getStartIndex(), tagInfo.getEndIndex()));
    tagInfo.setAttributeValue(generateUniqueValue());
    if (tagInfo.hasAttributeValue()) {
        int space = StringUtils.indexOfAny(tag, ' ', '\t', '\r', '\n');
        int equal;
        while (space > 0 && (equal = StringUtils.indexOf(tag, '=', space + 1)) > 0) {
            String name = tag.substring(space + 1, equal).trim();
            int leftQuote = StringUtils.indexOfAny(tag.substring(space + 1), '"', '\'') + space + 1;
            if (name.equals("class")) {
                tag.insert(leftQuote + 1, tagInfo.getAttributeValue() + " ");
                return tag;
            }//from  w w  w  .j  a  va 2 s.  c o m
            space = StringUtils.indexOf(tag, tag.charAt(leftQuote), leftQuote + 1) + 1;
        }
    }
    // Deal with closed tag such as <br />
    int insertIndex = tag.length() - 2;
    String tail = ">";
    if (tag.charAt(insertIndex) == '/') {
        --insertIndex;
        tail = " />";
    }
    // Remove redundant space
    while (tag.charAt(insertIndex) == ' ') {
        insertIndex--;
    }
    // Insert the generated id attribute
    tag.setLength(insertIndex + 1);
    tag.append(" class=\"" + tagInfo.getAttributeValue() + "\"" + tail);
    return tag;
}

From source file:com.joliciel.talismane.parser.ParseConfigurationImpl.java

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Iterator<PosTaggedToken> stackIterator = this.stack.iterator();
    if (stackIterator.hasNext())
        sb.insert(0, stackIterator.next().toString());
    if (stackIterator.hasNext())
        sb.insert(0, stackIterator.next().toString() + ",");
    if (stackIterator.hasNext())
        sb.insert(0, stackIterator.next().toString() + ",");
    if (stackIterator.hasNext())
        sb.insert(0, "...,");
    sb.insert(0, "Stack[");
    sb.append("]. Buffer[");
    Iterator<PosTaggedToken> bufferIterator = this.buffer.iterator();
    if (bufferIterator.hasNext())
        sb.append(bufferIterator.next().toString());
    if (bufferIterator.hasNext())
        sb.append("," + bufferIterator.next().toString());
    if (bufferIterator.hasNext())
        sb.append("," + bufferIterator.next().toString());
    if (bufferIterator.hasNext())
        sb.append(",...");
    sb.append("]");
    sb.append(" Deps[");
    for (DependencyArc arc : this.dependencies) {
        sb.append(arc.toString() + ",");
    }//from w ww . j  a  v  a 2 s . c  o m
    sb.append("]");
    return sb.toString();
}