@NotNull
  @Override
  public Result beforeSelectionRemoved(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    SelectionModel selectionModel = editor.getSelectionModel();
    if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE && selectionModel.hasSelection() && isQuote(c)) {
      String selectedText = selectionModel.getSelectedText();
      if (selectedText != null && selectedText.length() == 1) {
        char selectedChar = selectedText.charAt(0);
        if (isQuote(selectedChar) &&
            selectedChar != c &&
            !shouldSkipReplacementOfQuotesOrBraces(file, editor, selectedText, c) &&
            replaceQuotesBySelected(c, editor, file, selectionModel, selectedChar)) {
          return Result.STOP;
        }
      }
    }
    if (CodeInsightSettings.getInstance().SURROUND_SELECTION_ON_QUOTE_TYPED && selectionModel.hasSelection() && isDelimiter(c)) {
      String selectedText = selectionModel.getSelectedText();
      if (!StringUtil.isEmpty(selectedText)) {
        final int selectionStart = selectionModel.getSelectionStart();
        final int selectionEnd = selectionModel.getSelectionEnd();

        if (isReplaceInComparisonOperation(file, selectionStart, selectedText, c)) {
          return super.beforeSelectionRemoved(c, project, editor, file);
        }

        if (selectedText.length() > 1) {
          final char firstChar = selectedText.charAt(0);
          final char lastChar = selectedText.charAt(selectedText.length() - 1);
          if (isSimilarDelimiters(firstChar, c) && lastChar == getMatchingDelimiter(firstChar) &&
              (isQuote(firstChar) || firstChar != c) && !shouldSkipReplacementOfQuotesOrBraces(file, editor, selectedText, c) &&
              selectedText.indexOf(lastChar, 1) == selectedText.length() - 1) {
            selectedText = selectedText.substring(1, selectedText.length() - 1);
          }
        }
        final int caretOffset = selectionModel.getSelectionStart();
        final char c2 = getMatchingDelimiter(c);
        final String newText = c + selectedText + c2;
        boolean ltrSelection = selectionModel.getLeadSelectionOffset() != selectionModel.getSelectionEnd();
        boolean restoreStickySelection = editor instanceof EditorEx && ((EditorEx)editor).isStickySelection();
        selectionModel.removeSelection();
        editor.getDocument().replaceString(selectionStart, selectionEnd, newText);

        int startOffset = caretOffset + 1;
        int endOffset = caretOffset + newText.length() - 1;
        int length = editor.getDocument().getTextLength();

        // selection is removed here
        if (endOffset <= length) {
          if (restoreStickySelection) {
            EditorEx editorEx = (EditorEx)editor;
            CaretModel caretModel = editorEx.getCaretModel();
            caretModel.moveToOffset(ltrSelection ? startOffset : endOffset);
            editorEx.setStickySelection(true);
            caretModel.moveToOffset(ltrSelection ? endOffset : startOffset);
          }
          else {
            if (ltrSelection || editor instanceof EditorWindow) {
              editor.getSelectionModel().setSelection(startOffset, endOffset);
            }
            else {
              editor.getSelectionModel().setSelection(endOffset, startOffset);
            }
            editor.getCaretModel().moveToOffset(ltrSelection ? endOffset : startOffset);
          }
          if (c == '{') {
            int startOffsetToReformat = startOffset - 1;
            int endOffsetToReformat = length > endOffset ? endOffset + 1 : endOffset;
            CodeStyleManager.getInstance(project).reformatText(file, startOffsetToReformat, endOffsetToReformat);
          }
        }
        return Result.STOP;
      }
    }
    return super.beforeSelectionRemoved(c, project, editor, file);
  }
@NotNull
  @Override
  public Result beforeSelectionRemoved(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file)