/*
 * IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
 * Copyright (C) 2003-2019 The IdeaVim authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.maddyhome.idea.vim.helper;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

/**
 * @author oleg
 */
public class UndoRedoHelper {

  public static boolean undo(@NotNull final DataContext context) {
    final Project project = PlatformDataKeys.PROJECT.getData(context);
    final FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(context);
    final com.intellij.openapi.command.undo.UndoManager undoManager = com.intellij.openapi.command.undo.UndoManager.getInstance(project);
    if (fileEditor != null && undoManager.isUndoAvailable(fileEditor)) {
      undoManager.undo(fileEditor);
      return true;
    }
    return false;
  }

  public static boolean redo(@NotNull final DataContext context) {
    final Project project = PlatformDataKeys.PROJECT.getData(context);
    final FileEditor fileEditor = PlatformDataKeys.FILE_EDITOR.getData(context);
    final com.intellij.openapi.command.undo.UndoManager undoManager = com.intellij.openapi.command.undo.UndoManager.getInstance(project);
    if (fileEditor != null && undoManager.isRedoAvailable(fileEditor)) {
      undoManager.redo(fileEditor);
      return true;
    }
    return false;
  }
}

@@ -946,74 +946,239 @@
 api.
-fileEditor.FileEditor;%0Aimport com.intellij.openapi.project.Project
+command.undo.UndoManager;%0Aimport com.intellij.openapi.editor.Editor;%0Aimport com.intellij.openapi.fileEditor.FileEditor;%0Aimport com.intellij.openapi.project.Project;%0Aimport com.maddyhome.idea.vim.group.SelectionVimListenerSuppressor
 ;%0Aim
@@ -1506,102 +1506,141 @@
 nal 
-com.intellij.openapi.command.undo.UndoManager undoManager = com.intellij.openapi.command.undo.
+Editor editor = PlatformDataKeys.EDITOR.getData(context);%0A    if (project == null) return false;%0A    final UndoManager undoManager = 
 Undo
@@ -1669,32 +1669,50 @@
 oject);%0A    if (
+editor != null && 
 fileEditor != nu
@@ -1770,35 +1770,309 @@
     
-undoManager.undo(fileEditor
+SelectionVimListenerSuppressor.INSTANCE.lock();%0A      undoManager.undo(fileEditor);%0A      // Visual mode should not be entered after undo. v_u will be called on next undo if visual mode stays%0A      editor.getSelectionModel().removeSelection(true);%0A      SelectionVimListenerSuppressor.INSTANCE.unlock(
 );%0A 
@@ -2248,32 +2248,71 @@
 tData(context);%0A
+    if (project == null) return false;%0A
     final FileEd
@@ -2390,102 +2390,102 @@
 nal 
-com.intellij.openapi.command.undo.UndoManager undoManager = com.intellij.openapi.command.undo.
+Editor editor = PlatformDataKeys.EDITOR.getData(context);%0A    final UndoManager undoManager = 
 Undo
@@ -2522,16 +2522,34 @@
     if (
+editor != null && 
 fileEdit
@@ -2615,35 +2615,309 @@
     
-undoManager.redo(fileEditor
+SelectionVimListenerSuppressor.INSTANCE.lock();%0A      undoManager.redo(fileEditor);%0A      // Visual mode should not be entered after redo. v_u will be called on next undo if visual mode stays%0A      editor.getSelectionModel().removeSelection(true);%0A      SelectionVimListenerSuppressor.INSTANCE.unlock(
 );%0A 

Fix selection with undo