@Override
    public void run() {
      CaretModel caretModel = myEditor.getCaretModel();
      try {
        final CharSequence chars = myDocument.getCharsSequence();
        int i = CharArrayUtil.shiftBackwardUntil(chars, myOffset - 1, LINE_SEPARATOR) - 1;
        i = CharArrayUtil.shiftBackwardUntil(chars, i, LINE_SEPARATOR) + 1;
        if (i < 0) i = 0;
        int lineStart = CharArrayUtil.shiftForward(chars, i, " \t");
        Language language = myDataContext instanceof UserDataHolder ? CONTEXT_LANGUAGE.get((UserDataHolder)myDataContext):null;
        Commenter langCommenter = language != null ? LanguageCommenters.INSTANCE.forLanguage(language) : null;
        CodeDocumentationUtil.CommentContext commentContext
          = CodeDocumentationUtil.tryParseCommentContext(langCommenter, chars, lineStart);
        boolean isBeforeEof = myOffset > 0 && myOffset == chars.length() && chars.charAt(myOffset - 1) == '\n';

        PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(getProject());
        CodeInsightSettings codeInsightSettings = CodeInsightSettings.getInstance();
        if (commentContext.docStart) {
          psiDocumentManager.commitDocument(myDocument);
          PsiElement element = myFile.findElementAt(commentContext.lineStart);
          final String text = element.getText();
          final PsiElement parent = element.getParent();

          if (text.equals(commentContext.commenter.getDocumentationCommentPrefix()) && isDocComment(parent, commentContext.commenter) ||
              text.startsWith(commentContext.commenter.getDocumentationCommentPrefix()) && element instanceof PsiComment)
          {
            PsiComment comment = isDocComment(parent, commentContext.commenter) ? (PsiComment)parent:(PsiComment)element;
            int commentEnd = comment.getTextRange().getEndOffset();

            if (myOffset > commentEnd || myOffset == commentEnd && !isBeforeEof) {
              commentContext.docStart = false;
            }
            else {
              if (isCommentComplete(comment, commentContext.commenter, myEditor)) {
                if (myOffset >= commentEnd) {
                  commentContext.docAsterisk = false;
                }
                else {
                  commentContext.docAsterisk =
                    CodeStyleManager.getInstance(getProject()).getDocCommentSettings(myFile).isLeadingAsteriskEnabled();
                }
                commentContext.docStart = false;
              }
              else if (codeInsightSettings.CLOSE_COMMENT_ON_ENTER) {
                generateJavadoc(commentContext.commenter);
              }
              else {
                commentContext.docStart = false;
                commentContext.docAsterisk = false;
              }
            }
          }
          else {
            commentContext.docStart = false;
          }
        }
        else if (commentContext.docAsterisk) {
          psiDocumentManager.commitDocument(myDocument);
          PsiElement element = myFile.findElementAt(commentContext.lineStart);
          PsiComment comment = PsiTreeUtil.getParentOfType(element, PsiComment.class, false);
          if (comment == null || !isDocComment(comment, commentContext.commenter)) {
            commentContext.docAsterisk = false; // don't process block comments
          }
        }

        String indentInsideJavadoc = null;
        if (myOffset < myDocument.getTextLength()) {
          final int line = myDocument.getLineNumber(myOffset);
          if (line > 0 && (commentContext.docAsterisk || commentContext.docStart)) {
            indentInsideJavadoc = CodeDocumentationUtil.getIndentInsideJavadoc(myDocument, myDocument.getLineStartOffset(line - 1));
          }
        }

        if (commentContext.docAsterisk) {
          commentContext.docAsterisk = insertDocAsterisk(commentContext.lineStart, true,
                                                         !StringUtil.isEmpty(indentInsideJavadoc), commentContext.commenter);
        }

        boolean docIndentApplied = false;
        if (codeInsightSettings.SMART_INDENT_ON_ENTER || myForceIndent || commentContext.docStart || commentContext.docAsterisk) {
          final int offset = adjustLineIndentNoCommit(getLanguage(myDataContext), myDocument, myEditor, myOffset);
          if (offset >= 0) {
            myOffset = offset;
            myIsIndentAdjustmentNeeded = false;
          }

          if (commentContext.docAsterisk && !StringUtil.isEmpty(indentInsideJavadoc) && myOffset < myDocument.getTextLength()) {
            myDocument.insertString(myOffset + 1, indentInsideJavadoc);
            myOffset += indentInsideJavadoc.length();
            docIndentApplied = true;
          }

          if (myForceIndent && indentInsideJavadoc != null) {
            int indentSize = CodeStyle.getSettings(myFile).getIndentSize(myFile.getFileType());
            myDocument.insertString(myOffset + 1, StringUtil.repeatSymbol(' ', indentSize));
            myCaretAdvance += indentSize;
          }
        }

        if ((commentContext.docAsterisk || commentContext.docStart) && !docIndentApplied) {
          if (myInsertSpace && !(commentContext.docStart && isBeforeEof)) {
            if (myOffset == myDocument.getTextLength()) {
              myDocument.insertString(myOffset, " ");
            }
            myDocument.insertString(myOffset + 1, " ");
          }

          final char c = myDocument.getCharsSequence().charAt(myOffset);
          if (c != '\n') {
            myOffset += 1;
          }
        }

        if (commentContext.docAsterisk && !commentContext.docStart) {
          myCaretAdvance += 1;
        }
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
      }

      myOffset = Math.min(myOffset, myDocument.getTextLength());
      caretModel.moveToOffset(myOffset);
      EditorModificationUtil.scrollToCaret(myEditor);
      myEditor.getSelectionModel().removeSelection();
      if (myCaretAdvance != 0) {
        LogicalPosition caretPosition = caretModel.getLogicalPosition();
        LogicalPosition pos = new LogicalPosition(caretPosition.line, caretPosition.column + myCaretAdvance);
        caretModel.moveToLogicalPosition(pos);
      }
    }
@Override
    public void run()