private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) return false;

    final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
    assert vfile != null;
    PsiFile file = PsiManager.getInstance(project).findFile(vfile);

    Document document = editor.getDocument();
    String fileText = document.getText();
    if (fileText.length() == caretOffset) return false;

    if (!checkStringApplicable(editor, caretOffset)) return false;
    if (file == null) return false;

    PsiDocumentManager.getInstance(project).commitDocument(document);

    final PsiElement stringElement = inferStringPair(file, caretOffset);
    if (stringElement == null) return false;

    ASTNode node = stringElement.getNode();
    final IElementType nodeElementType = node.getElementType();

    boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);

    // For simple String literals like 'abc'
    CaretModel caretModel = editor.getCaretModel();
    if (nodeElementType == STRING_SQ || nodeElementType == STRING_TSQ) {
      if (isSingleQuoteString(stringElement)) {

        //the case of print '\<caret>'
        if (isSlashBeforeCaret(caretOffset, fileText)) {
          EditorModificationUtilEx.insertStringAtCaret(editor, "\n");
        }
        else if(stringElement.getParent() instanceof GrReferenceExpression) {
          TextRange range = stringElement.getTextRange();
          convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
          document.insertString(range.getStartOffset(), "''");
          caretModel.moveToOffset(caretOffset + 2);
          EditorModificationUtilEx.insertStringAtCaret(editor, "\n");
        }
        else {
          EditorModificationUtilEx.insertStringAtCaret(editor, "'+");
          originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext);
          EditorModificationUtilEx.insertStringAtCaret(editor, "'");
          PsiDocumentManager.getInstance(project).commitDocument(document);
          CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
        }
      }
      else {
        insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
      }
      return true;
    }

    if (GSTRING_TOKENS.contains(nodeElementType) ||
        nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) ||
        nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
      PsiElement parent = stringElement.getParent();
      if (nodeElementType == STRING_DQ || nodeElementType == STRING_TDQ) {
        parent = stringElement;
      }
      else {
        while (parent != null && !(parent instanceof GrLiteral)) {
          parent = parent.getParent();
        }
      }
      if (parent == null) return false;
      if (isDoubleQuotedString(parent)) {
        PsiElement exprSibling = stringElement.getNextSibling();
        boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
        if (rightFromDollar) caretOffset--;
        TextRange parentRange = parent.getTextRange();
        if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
          convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
          document.insertString(parentRange.getStartOffset(), "\"\"");
          caretModel.moveToOffset(caretOffset + 2);
          EditorModificationUtilEx.insertStringAtCaret(editor, "\n");
          if (rightFromDollar) {
            caretModel.moveCaretRelatively(1, 0, false, false, true);
          }
        }
        else if (isSlashBeforeCaret(caretOffset, fileText)) {
          EditorModificationUtilEx.insertStringAtCaret(editor, "\n");
        }
        else {
          EditorModificationUtilEx.insertStringAtCaret(editor, "\"+");
          originalHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), dataContext);
          EditorModificationUtilEx.insertStringAtCaret(editor, "\"");
          PsiDocumentManager.getInstance(project).commitDocument(document);
          CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
        }
      }
      else {
        insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
      }
      return true;
    }

    if (REGEX_TOKENS.contains(nodeElementType) ||
        nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) ||
        nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
      PsiElement parent = stringElement.getParent();
      if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        parent = stringElement;
      }
      else {
        while (parent != null && !(parent instanceof GrLiteral)) {
          parent = parent.getParent();
        }
      }
      if (parent == null || parent.getLastChild() instanceof PsiErrorElement) return false;
      PsiElement exprSibling = stringElement.getNextSibling();
      boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
      if (rightFromDollar) {
        caretModel.moveToOffset(caretOffset - 1);
      }
      insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
      if (rightFromDollar) {
        caretModel.moveCaretRelatively(1, 0, false, false, true);
      }
      return true;
    }

    return false;
  }
private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler)