<commit_before>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.command.undo;

import com.intellij.ide.DataManager;
import com.intellij.ide.IdeBundle;
import com.intellij.idea.ActionsBundle;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.UndoConfirmationPolicy;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.VisualPosition;
import com.intellij.openapi.editor.actions.IndentSelectionAction;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TestDialog;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.EditorTestUtil;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.io.IOException;

public class EditorTypingAndNavigationUndoTest extends EditorUndoTestCase {
  public void testUndoTypeIn() {
    typeInText("test");
    checkEditorState("test", 4, 0, 0);
    undoFirstEditor();
    checkEditorState("", 0, 0, 0);
    redoFirstEditor();
    checkEditorState("test", 4, 0, 0);
  }

  public void testMoveCaretUndo() {
    typeInText("test");
    checkEditorState("test", 4, 0, 0);
    moveCaret(4, LEFT, false);
    checkEditorState("test", 0, 0, 0);
    undoFirstEditor();
    checkEditorState("test", 4, 0, 0);
    undoFirstEditor();

    checkEditorState("", 0, 0, 0);

    redoFirstEditor();
    checkEditorState("test", 4, 0, 0);
  }

  public void testMoveCaretUndoWithSelection() {
    typeInText("test");
    checkEditorState("test", 4, 0, 0);
    moveCaret(4, LEFT, true);
    checkEditorState("test", 0, 0, 4);
    undoFirstEditor();
    checkEditorState("test", 4, 0, 0);

    undoFirstEditor();

    checkEditorState("", 0, 0, 0);

    redoFirstEditor();
    checkEditorState("test", 4, 0, 0);
  }

  public void testMoveOnly() {
    final Document document = getFirstEditor().getDocument();
    ApplicationManager.getApplication().runWriteAction(() -> document.setText("test"));

    moveCaret(4, RIGHT, false);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
      moveCaret(2, LEFT, false);
      checkEditorState("test", 2, 0, 0);
      typeInText("test");
    });
    checkEditorState("tetestst", 6, 0, 0);
    undoFirstEditor();
    checkEditorState("test", 4, 0, 0);
    assertUndoInFirstEditorNotAvailable();
  }

  public void testEnter() {
    typeInText("abc");
    checkEditorState("abc", 3, 0, 0);
    enter();
    checkEditorState("abc\n", 4, 0, 0);
    typeInText("def");
    checkEditorState("abc\ndef", 7, 0, 0);
    enter();
    checkEditorState("abc\ndef\n", 8, 0, 0);
    typeInText("eee");
    checkEditorState("abc\ndef\neee", 11, 0, 0);

    undoFirstEditor();
    checkEditorState("abc\ndef\n", 8, 0, 0);<commit_msg>Add test for range markers recovering on undoing move operations<commit_after>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.command.undo;

import com.intellij.ide.DataManager;
import com.intellij.ide.IdeBundle;
import com.intellij.idea.ActionsBundle;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.UndoConfirmationPolicy;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actions.IndentSelectionAction;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TestDialog;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.EditorTestUtil;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.io.IOException;

public class EditorTypingAndNavigationUndoTest extends EditorUndoTestCase {
  public void testUndoTypeIn() {
    typeInText("test");
    checkEditorState("test", 4, 0, 0);
    undoFirstEditor();
    checkEditorState("", 0, 0, 0);
    redoFirstEditor();
    checkEditorState("test", 4, 0, 0);
  }

  public void testMoveCaretUndo() {
    typeInText("test");
    checkEditorState("test", 4, 0, 0);
    moveCaret(4, LEFT, false);
    checkEditorState("test", 0, 0, 0);
    undoFirstEditor();
    checkEditorState("test", 4, 0, 0);
    undoFirstEditor();

    checkEditorState("", 0, 0, 0);

    redoFirstEditor();
    checkEditorState("test", 4, 0, 0);
  }

  public void testMoveCaretUndoWithSelection() {
    typeInText("test");
    checkEditorState("test", 4, 0, 0);
    moveCaret(4, LEFT, true);
    checkEditorState("test", 0, 0, 4);
    undoFirstEditor();
    checkEditorState("test", 4, 0, 0);

    undoFirstEditor();

    checkEditorState("", 0, 0, 0);

    redoFirstEditor();
    checkEditorState("test", 4, 0, 0);
  }

  public void testMoveOnly() {
    final Document document = getFirstEditor().getDocument();
    ApplicationManager.getApplication().runWriteAction(() -> document.setText("test"));

    moveCaret(4, RIGHT, false);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
      moveCaret(2, LEFT, false);
      checkEditorState("test", 2, 0, 0);
      typeInText("test");
    });
    checkEditorState("tetestst", 6, 0, 0);
    undoFirstEditor();
    checkEditorState("test", 4, 0, 0);
    assertUndoInFirstEditorNotAvailable();
  }

  public void testRangeMarkerOnMoveUndo() {
    final DocumentEx document = (DocumentEx) getFirstEditor().getDocument();
    ApplicationManager.getApplication().runWriteAction(() -> document.setText("line1\nline2"));
    int start = document.getLineStartOffset(1);
    int end = document.getLineEndOffset(1);
    RangeMarker rangeMarker = document.createRangeMarker(start, end);
    moveCaret(getFirstEditor(), DOWN, false);
    executeEditorAction(getFirstEditor(), IdeActions.ACTION_MOVE_LINE_UP_ACTION);
    undoFirstEditor();
    assertEquals(new TextRange(start, end), new TextRange(rangeMarker.getStartOffset(), rangeMarker.getEndOffset()));
  }

  public void testEnter() {
    typeInText("abc");
    checkEditorState("abc", 3, 0, 0);
    enter();
    checkEditorState("abc\n", 4, 0, 0);
    typeInText("def");
    checkEditorState("abc\ndef", 7, 0, 0);
    enter();
    checkEditorState("abc\ndef\n", 8, 0, 0);
    typeInText("eee");
    checkEditorState("abc\ndef\neee", 11, 0, 0);

    undoFirstEditor();
    checkEditorState("abc\ndef\n", 8, 0, 0);
{'fraction_non_alphanumeric': 0.13852224792488774, 'fraction_numerical': 0.022588107225472852, 'mean_word_length': 4.675675675675675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4675015', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2344, 'n_words': 486}