Program: 34370
Project: 34370
Buggy source:
@Override public void insert(MutableContext context) throws BadLocationException {
  BaseDocument doc=(BaseDocument)context.getDocument();
  TokenHierarchy<BaseDocument> tokenHierarchy=TokenHierarchy.get(doc);
  int offset=context.getCaretOffset();
  int lineBegin=Utilities.getRowStart(doc,offset);
  int lineEnd=Utilities.getRowEnd(doc,offset);
  if (lineBegin == offset && lineEnd == offset) {
    return;
  }
  TokenSequence<? extends JsTokenId> ts=LexUtilities.getTokenSequence(tokenHierarchy,offset,language);
  if (ts == null) {
    return;
  }
  ts.move(offset);
  if (!ts.moveNext() && !ts.movePrevious()) {
    return;
  }
  Token<? extends JsTokenId> token=ts.token();
  JsTokenId id=token.id();
  if (!id.isError() && isInsertMatchingEnabled() && !isDocToken(id)&& isAddRightBrace(doc,offset)) {
    int indent=GsfUtilities.getLineIndent(doc,offset);
    int afterLastNonWhite=Utilities.getRowLastNonWhite(doc,offset);
    StringBuilder sb=new StringBuilder();
    int carretOffset=0;
    int curlyOffset=getUnbalancedCurlyOffset(doc,offset);
    if (offset > afterLastNonWhite) {
      sb.append("String_Node_Str");
      sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
      carretOffset=sb.length();
      sb.append("String_Node_Str");
      if (curlyOffset >= 0) {
        sb.append(IndentUtils.createIndentString(doc,getCurlyIndent(doc,curlyOffset)));
      }
 else {
        sb.append(IndentUtils.createIndentString(doc,indent));
      }
      sb.append("String_Node_Str");
    }
 else {
      boolean insert[]={true};
      int end=getRowOrBlockEnd(doc,offset,insert);
      if (insert[0]) {
        String restOfLine=doc.getText(offset,Math.min(end,Utilities.getRowEnd(doc,afterLastNonWhite)) - offset);
        sb.append("String_Node_Str");
        sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
        carretOffset=sb.length();
        sb.append(restOfLine);
        sb.append("String_Node_Str");
        if (curlyOffset >= 0) {
          sb.append(IndentUtils.createIndentString(doc,getCurlyIndent(doc,curlyOffset)));
        }
 else {
          sb.append(IndentUtils.createIndentString(doc,indent));
        }
        sb.append("String_Node_Str");
        doc.remove(offset,restOfLine.length());
      }
    }
    if (sb.length() > 0) {
      context.setText(sb.toString(),0,carretOffset);
    }
    return;
  }
  if (id.isError()) {
    String text=token.text().toString();
    if (comments.test(doc) && text.startsWith("String_Node_Str") && ts.offset() == Utilities.getRowFirstNonWhite(doc,offset)) {
      int indent=GsfUtilities.getLineIndent(doc,offset);
      StringBuilder sb=new StringBuilder();
      sb.append("String_Node_Str");
      sb.append(IndentUtils.createIndentString(doc,indent));
      sb.append("String_Node_Str");
      int carretOffset=sb.length();
      sb.append("String_Node_Str");
      sb.append(IndentUtils.createIndentString(doc,indent));
      sb.append("String_Node_Str");
      if (text.startsWith("String_Node_Str")) {
        commentGenerator=new CommentGenerator(offset + carretOffset,indent + 1);
      }
      context.setText(sb.toString(),0,carretOffset);
      return;
    }
  }
  if (multiLineLiterals) {
    if (id == JsTokenId.STRING || (id == JsTokenId.STRING_END) && offset < ts.offset() + ts.token().length()) {
      String str="String_Node_Str";
      if (id != JsTokenId.STRING || offset > ts.offset()) {
        str="String_Node_Str";
        if (offset - ts.offset() < ts.token().length()) {
          String text=ts.token().text().toString();
          text=text.substring(0,offset - ts.offset());
          if (text.endsWith("String_Node_Str")) {
            str="String_Node_Str";
          }
        }
      }
      context.setText(str,-1,str.length());
      return;
    }
    if (id == JsTokenId.TEMPLATE || (id == JsTokenId.TEMPLATE_END) && offset < ts.offset() + ts.token().length()) {
      String str="String_Node_Str";
      context.setText(str,-1,str.length());
      return;
    }
    if (id == JsTokenId.REGEXP || (id == JsTokenId.REGEXP_END) && offset < ts.offset() + ts.token().length()) {
      String str=(id != JsTokenId.REGEXP || offset > ts.offset()) ? "String_Node_Str" : "String_Node_Str";
      context.setText(str,-1,str.length());
      return;
    }
  }
 else {
    final int indent=GsfUtilities.getLineIndent(doc,offset);
    final StringBuilder sb=new StringBuilder();
    sb.append("String_Node_Str");
    sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
    final int carretOffset=sb.length();
    context.setText(sb.toString(),0,carretOffset);
    return;
  }
  if ((id == JsTokenId.BRACKET_RIGHT_CURLY || id == JsTokenId.BRACKET_RIGHT_BRACKET) && offset > 0) {
    Token<? extends JsTokenId> prevToken=LexUtilities.getToken(doc,offset - 1,language);
    if (prevToken != null) {
      JsTokenId prevTokenId=prevToken.id();
      if (id == JsTokenId.BRACKET_RIGHT_CURLY && prevTokenId == JsTokenId.BRACKET_LEFT_CURLY || id == JsTokenId.BRACKET_RIGHT_BRACKET && prevTokenId == JsTokenId.BRACKET_LEFT_BRACKET) {
        int indent=GsfUtilities.getLineIndent(doc,offset);
        StringBuilder sb=new StringBuilder();
        sb.append("String_Node_Str");
        sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
        int carretOffset=sb.length();
        sb.append("String_Node_Str");
        sb.append(IndentUtils.createIndentString(doc,indent));
        context.setText(sb.toString(),0,carretOffset);
        return;
      }
    }
  }
  if (!comments.test(doc)) {
    return;
  }
  if (id == JsTokenId.WHITESPACE) {
    int begin=Utilities.getRowFirstNonWhite(doc,offset);
    if (begin != -1 && offset < begin) {
      ts.move(begin);
      if (ts.moveNext()) {
        id=ts.token().id();
        if (id == JsTokenId.LINE_COMMENT) {
          offset=begin;
        }
      }
    }
  }
  if ((id == JsTokenId.BLOCK_COMMENT || id == JsTokenId.DOC_COMMENT) && offset > ts.offset() && offset < ts.offset() + ts.token().length()) {
    int begin=Utilities.getRowFirstNonWhite(doc,offset);
    int end=Utilities.getRowEnd(doc,offset) + 1;
    if (begin == -1) {
      begin=end;
    }
    String line=doc.getText(begin,end - begin);
    boolean isBlockStart=line.startsWith("String_Node_Str") || (begin != -1 && begin < ts.offset());
    if (isBlockStart || line.startsWith("String_Node_Str")) {
      int indent=GsfUtilities.getLineIndent(doc,offset);
      StringBuilder sb=new StringBuilder("String_Node_Str");
      if (isBlockStart) {
        indent++;
      }
      int carretPosition=0;
      sb.append(IndentUtils.createIndentString(doc,indent));
      if (isBlockStart) {
        sb.append("String_Node_Str");
        carretPosition=sb.length();
        TokenSequence<? extends JsDocumentationTokenId> jsDocTS=LexUtilities.getJsDocumentationTokenSequence(tokenHierarchy,offset);
        if (jsDocTS != null) {
          if (!hasCommentEnd(jsDocTS)) {
            commentGenerator=new CommentGenerator(offset + carretPosition,indent);
            sb.append("String_Node_Str").append(IndentUtils.createIndentString(doc,indent)).append("String_Node_Str");
          }
        }
      }
 else {
        sb.append("String_Node_Str");
        int afterStar=isBlockStart ? begin + 2 : begin + 1;
        line=doc.getText(afterStar,Utilities.getRowEnd(doc,afterStar) - afterStar);
        for (int i=0; i < line.length(); i++) {
          char c=line.charAt(i);
          if (c == ' ' || c == '\t') {
            sb.append(c);
          }
 else {
            break;
          }
        }
        carretPosition=sb.length();
      }
      if (offset == begin && offset > 0) {
        context.setText(sb.toString(),-1,sb.length());
        return;
      }
      context.setText(sb.toString(),-1,carretPosition);
      return;
    }
  }
  boolean isComment=id == JsTokenId.LINE_COMMENT;
  if (id == JsTokenId.EOL) {
    if (ts.movePrevious() && ts.token().id() == JsTokenId.LINE_COMMENT) {
      isComment=true;
    }
  }
  if (isComment) {
    boolean continueComment=false;
    int begin=Utilities.getRowFirstNonWhite(doc,offset);
    boolean previousLineWasComment=false;
    boolean nextLineIsComment=false;
    int rowStart=Utilities.getRowStart(doc,offset);
    if (rowStart > 0) {
      int prevBegin=Utilities.getRowFirstNonWhite(doc,rowStart - 1);
      if (prevBegin != -1) {
        Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,prevBegin,language);
        if (firstToken != null && firstToken.id() == JsTokenId.LINE_COMMENT) {
          previousLineWasComment=true;
        }
      }
    }
    int rowEnd=Utilities.getRowEnd(doc,offset);
    if (rowEnd < doc.getLength()) {
      int nextBegin=Utilities.getRowFirstNonWhite(doc,rowEnd + 1);
      if (nextBegin != -1) {
        Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,nextBegin,language);
        if (firstToken != null && firstToken.id() == JsTokenId.LINE_COMMENT) {
          nextLineIsComment=true;
        }
      }
    }
    if (previousLineWasComment || nextLineIsComment || (offset > ts.offset() && offset < ts.offset() + ts.token().length())) {
      if (ts.offset() + token.length() > offset + 1) {
        String trailing=doc.getText(offset,Utilities.getRowEnd(doc,offset) - offset);
        if (trailing.trim().length() != 0) {
          continueComment=true;
        }
      }
 else       if (CONTINUE_COMMENTS) {
        Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,begin,language);
        if (firstToken.id() == JsTokenId.LINE_COMMENT) {
          continueComment=true;
        }
      }
      if (!continueComment) {
        int nextLine=Utilities.getRowEnd(doc,offset) + 1;
        if (nextLine < doc.getLength()) {
          int nextLineFirst=Utilities.getRowFirstNonWhite(doc,nextLine);
          if (nextLineFirst != -1) {
            Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,nextLineFirst,language);
            if (firstToken != null && firstToken.id() == JsTokenId.LINE_COMMENT) {
              continueComment=true;
            }
          }
        }
      }
    }
    if (continueComment) {
      int indent=GsfUtilities.getLineIndent(doc,offset);
      StringBuilder sb=new StringBuilder();
      if (offset != begin || offset <= 0) {
        sb.append("String_Node_Str");
      }
      sb.append(IndentUtils.createIndentString(doc,indent));
      sb.append("String_Node_Str");
      int afterSlash=begin + 2;
      String line=doc.getText(afterSlash,Utilities.getRowEnd(doc,afterSlash) - afterSlash);
      for (int i=0; i < line.length(); i++) {
        char c=line.charAt(i);
        if (c == ' ' || c == '\t') {
          sb.append(c);
        }
 else {
          break;
        }
      }
      if (offset == begin && offset > 0) {
        int caretPosition=sb.length();
        sb.append("String_Node_Str");
        context.setText(sb.toString(),-1,caretPosition);
        return;
      }
      context.setText(sb.toString(),-1,sb.length());
      return;
    }
  }
  int indentSize=getNextLineIndentation(doc,offset);
  if (indentSize > 0) {
    StringBuilder sb=new StringBuilder("String_Node_Str");
    sb.append(IndentUtils.createIndentString(doc,indentSize));
    context.setText(sb.toString(),-1,sb.length());
  }
}

Fixed version (ground truth):
@Override public void insert(MutableContext context) throws BadLocationException {
  BaseDocument doc=(BaseDocument)context.getDocument();
  TokenHierarchy<BaseDocument> tokenHierarchy=TokenHierarchy.get(doc);
  int offset=context.getCaretOffset();
  int lineBegin=Utilities.getRowStart(doc,offset);
  int lineEnd=Utilities.getRowEnd(doc,offset);
  if (lineBegin == offset && lineEnd == offset) {
    return;
  }
  TokenSequence<? extends JsTokenId> ts=LexUtilities.getTokenSequence(tokenHierarchy,offset,language);
  if (ts == null) {
    return;
  }
  ts.move(offset);
  if (!ts.moveNext() && !ts.movePrevious()) {
    return;
  }
  Token<? extends JsTokenId> token=ts.token();
  JsTokenId id=token.id();
  if (!id.isError() && isInsertMatchingEnabled() && !isDocToken(id)&& isAddRightBrace(doc,offset)) {
    int indent=GsfUtilities.getLineIndent(doc,offset);
    int afterLastNonWhite=Utilities.getRowLastNonWhite(doc,offset);
    StringBuilder sb=new StringBuilder();
    int carretOffset=0;
    int curlyOffset=getUnbalancedCurlyOffset(doc,offset);
    if (offset > afterLastNonWhite) {
      sb.append("String_Node_Str");
      sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
      carretOffset=sb.length();
      sb.append("String_Node_Str");
      if (curlyOffset >= 0) {
        sb.append(IndentUtils.createIndentString(doc,getCurlyIndent(doc,curlyOffset)));
      }
 else {
        sb.append(IndentUtils.createIndentString(doc,indent));
      }
      sb.append("String_Node_Str");
    }
 else {
      boolean insert[]={true};
      int end=getRowOrBlockEnd(doc,offset,insert);
      if (insert[0]) {
        String restOfLine=doc.getText(offset,Math.min(end,Utilities.getRowEnd(doc,afterLastNonWhite)) - offset);
        sb.append("String_Node_Str");
        sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
        carretOffset=sb.length();
        sb.append(restOfLine);
        sb.append("String_Node_Str");
        if (curlyOffset >= 0) {
          sb.append(IndentUtils.createIndentString(doc,getCurlyIndent(doc,curlyOffset)));
        }
 else {
          sb.append(IndentUtils.createIndentString(doc,indent));
        }
        sb.append("String_Node_Str");
        doc.remove(offset,restOfLine.length());
      }
    }
    if (sb.length() > 0) {
      context.setText(sb.toString(),0,carretOffset);
    }
    return;
  }
  if (id.isError()) {
    String text=token.text().toString();
    if (comments.test(doc) && text.startsWith("String_Node_Str")) {
      int indent=GsfUtilities.getLineIndent(doc,ts.offset());
      StringBuilder sb=new StringBuilder();
      sb.append("String_Node_Str");
      sb.append(IndentUtils.createIndentString(doc,indent));
      sb.append("String_Node_Str");
      int carretOffset=sb.length();
      sb.append("String_Node_Str");
      sb.append(IndentUtils.createIndentString(doc,indent));
      sb.append("String_Node_Str");
      if (text.startsWith("String_Node_Str")) {
        commentGenerator=new CommentGenerator(offset + carretOffset,indent + 1);
      }
      context.setText(sb.toString(),0,carretOffset);
      return;
    }
  }
  if (multiLineLiterals) {
    if (id == JsTokenId.STRING || (id == JsTokenId.STRING_END) && offset < ts.offset() + ts.token().length()) {
      String str="String_Node_Str";
      if (id != JsTokenId.STRING || offset > ts.offset()) {
        str="String_Node_Str";
        if (offset - ts.offset() < ts.token().length()) {
          String text=ts.token().text().toString();
          text=text.substring(0,offset - ts.offset());
          if (text.endsWith("String_Node_Str")) {
            str="String_Node_Str";
          }
        }
      }
      context.setText(str,-1,str.length());
      return;
    }
    if (id == JsTokenId.TEMPLATE || (id == JsTokenId.TEMPLATE_END) && offset < ts.offset() + ts.token().length()) {
      String str="String_Node_Str";
      context.setText(str,-1,str.length());
      return;
    }
    if (id == JsTokenId.REGEXP || (id == JsTokenId.REGEXP_END) && offset < ts.offset() + ts.token().length()) {
      String str=(id != JsTokenId.REGEXP || offset > ts.offset()) ? "String_Node_Str" : "String_Node_Str";
      context.setText(str,-1,str.length());
      return;
    }
  }
 else {
    final int indent=GsfUtilities.getLineIndent(doc,offset);
    final StringBuilder sb=new StringBuilder();
    sb.append("String_Node_Str");
    sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
    final int carretOffset=sb.length();
    context.setText(sb.toString(),0,carretOffset);
    return;
  }
  if ((id == JsTokenId.BRACKET_RIGHT_CURLY || id == JsTokenId.BRACKET_RIGHT_BRACKET) && offset > 0) {
    Token<? extends JsTokenId> prevToken=LexUtilities.getToken(doc,offset - 1,language);
    if (prevToken != null) {
      JsTokenId prevTokenId=prevToken.id();
      if (id == JsTokenId.BRACKET_RIGHT_CURLY && prevTokenId == JsTokenId.BRACKET_LEFT_CURLY || id == JsTokenId.BRACKET_RIGHT_BRACKET && prevTokenId == JsTokenId.BRACKET_LEFT_BRACKET) {
        int indent=GsfUtilities.getLineIndent(doc,offset);
        StringBuilder sb=new StringBuilder();
        sb.append("String_Node_Str");
        sb.append(IndentUtils.createIndentString(doc,indent + IndentUtils.indentLevelSize(doc)));
        int carretOffset=sb.length();
        sb.append("String_Node_Str");
        sb.append(IndentUtils.createIndentString(doc,indent));
        context.setText(sb.toString(),0,carretOffset);
        return;
      }
    }
  }
  if (!comments.test(doc)) {
    return;
  }
  if (id == JsTokenId.WHITESPACE) {
    int begin=Utilities.getRowFirstNonWhite(doc,offset);
    if (begin != -1 && offset < begin) {
      ts.move(begin);
      if (ts.moveNext()) {
        id=ts.token().id();
        if (id == JsTokenId.LINE_COMMENT) {
          offset=begin;
        }
      }
    }
  }
  if ((id == JsTokenId.BLOCK_COMMENT || id == JsTokenId.DOC_COMMENT) && offset > ts.offset() && offset < ts.offset() + ts.token().length()) {
    int begin=Utilities.getRowFirstNonWhite(doc,offset);
    int end=Utilities.getRowEnd(doc,offset) + 1;
    if (begin == -1) {
      begin=end;
    }
    String line=doc.getText(begin,end - begin);
    boolean isBlockStart=line.startsWith("String_Node_Str") || (begin != -1 && begin < ts.offset());
    if (isBlockStart || line.startsWith("String_Node_Str")) {
      int indent=GsfUtilities.getLineIndent(doc,offset);
      StringBuilder sb=new StringBuilder("String_Node_Str");
      if (isBlockStart) {
        indent++;
      }
      int carretPosition=0;
      sb.append(IndentUtils.createIndentString(doc,indent));
      if (isBlockStart) {
        sb.append("String_Node_Str");
        carretPosition=sb.length();
        TokenSequence<? extends JsDocumentationTokenId> jsDocTS=LexUtilities.getJsDocumentationTokenSequence(tokenHierarchy,offset);
        if (jsDocTS != null) {
          if (!hasCommentEnd(jsDocTS)) {
            commentGenerator=new CommentGenerator(offset + carretPosition,indent);
            sb.append("String_Node_Str").append(IndentUtils.createIndentString(doc,indent)).append("String_Node_Str");
          }
        }
      }
 else {
        sb.append("String_Node_Str");
        int afterStar=isBlockStart ? begin + 2 : begin + 1;
        line=doc.getText(afterStar,Utilities.getRowEnd(doc,afterStar) - afterStar);
        for (int i=0; i < line.length(); i++) {
          char c=line.charAt(i);
          if (c == ' ' || c == '\t') {
            sb.append(c);
          }
 else {
            break;
          }
        }
        carretPosition=sb.length();
      }
      if (offset == begin && offset > 0) {
        context.setText(sb.toString(),-1,sb.length());
        return;
      }
      context.setText(sb.toString(),-1,carretPosition);
      return;
    }
  }
  boolean isComment=id == JsTokenId.LINE_COMMENT;
  if (id == JsTokenId.EOL) {
    if (ts.movePrevious() && ts.token().id() == JsTokenId.LINE_COMMENT) {
      isComment=true;
    }
  }
  if (isComment) {
    boolean continueComment=false;
    int begin=Utilities.getRowFirstNonWhite(doc,offset);
    boolean previousLineWasComment=false;
    boolean nextLineIsComment=false;
    int rowStart=Utilities.getRowStart(doc,offset);
    if (rowStart > 0) {
      int prevBegin=Utilities.getRowFirstNonWhite(doc,rowStart - 1);
      if (prevBegin != -1) {
        Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,prevBegin,language);
        if (firstToken != null && firstToken.id() == JsTokenId.LINE_COMMENT) {
          previousLineWasComment=true;
        }
      }
    }
    int rowEnd=Utilities.getRowEnd(doc,offset);
    if (rowEnd < doc.getLength()) {
      int nextBegin=Utilities.getRowFirstNonWhite(doc,rowEnd + 1);
      if (nextBegin != -1) {
        Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,nextBegin,language);
        if (firstToken != null && firstToken.id() == JsTokenId.LINE_COMMENT) {
          nextLineIsComment=true;
        }
      }
    }
    if (previousLineWasComment || nextLineIsComment || (offset > ts.offset() && offset < ts.offset() + ts.token().length())) {
      if (ts.offset() + token.length() > offset + 1) {
        String trailing=doc.getText(offset,Utilities.getRowEnd(doc,offset) - offset);
        if (trailing.trim().length() != 0) {
          continueComment=true;
        }
      }
 else       if (CONTINUE_COMMENTS) {
        Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,begin,language);
        if (firstToken.id() == JsTokenId.LINE_COMMENT) {
          continueComment=true;
        }
      }
      if (!continueComment) {
        int nextLine=Utilities.getRowEnd(doc,offset) + 1;
        if (nextLine < doc.getLength()) {
          int nextLineFirst=Utilities.getRowFirstNonWhite(doc,nextLine);
          if (nextLineFirst != -1) {
            Token<? extends JsTokenId> firstToken=LexUtilities.getToken(doc,nextLineFirst,language);
            if (firstToken != null && firstToken.id() == JsTokenId.LINE_COMMENT) {
              continueComment=true;
            }
          }
        }
      }
    }
    if (continueComment) {
      int indent=GsfUtilities.getLineIndent(doc,offset);
      StringBuilder sb=new StringBuilder();
      if (offset != begin || offset <= 0) {
        sb.append("String_Node_Str");
      }
      sb.append(IndentUtils.createIndentString(doc,indent));
      sb.append("String_Node_Str");
      int afterSlash=begin + 2;
      String line=doc.getText(afterSlash,Utilities.getRowEnd(doc,afterSlash) - afterSlash);
      for (int i=0; i < line.length(); i++) {
        char c=line.charAt(i);
        if (c == ' ' || c == '\t') {
          sb.append(c);
        }
 else {
          break;
        }
      }
      if (offset == begin && offset > 0) {
        int caretPosition=sb.length();
        sb.append("String_Node_Str");
        context.setText(sb.toString(),-1,caretPosition);
        return;
      }
      context.setText(sb.toString(),-1,sb.length());
      return;
    }
  }
  int indentSize=getNextLineIndentation(doc,offset);
  if (indentSize > 0) {
    StringBuilder sb=new StringBuilder("String_Node_Str");
    sb.append(IndentUtils.createIndentString(doc,indentSize));
    context.setText(sb.toString(),-1,sb.length());
  }
}